blob: 3b8cca9f6123cc9f787f75b04991317aecabb7ab [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 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>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369#endif
370
Willy Tarreau42250582007-04-01 01:30:43 +0200371static void http_sess_log(struct session *s);
372
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100373/*
374 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
375 * CRLF. Text length is measured first, so it cannot be NULL.
376 * The header is also automatically added to the index <hdr_idx>, and the end
377 * of headers is automatically adjusted. The number of bytes added is returned
378 * on success, otherwise <0 is returned indicating an error.
379 */
380int http_header_add_tail(struct buffer *b, struct http_msg *msg,
381 struct hdr_idx *hdr_idx, const char *text)
382{
383 int bytes, len;
384
385 len = strlen(text);
386 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
387 if (!bytes)
388 return -1;
389 msg->eoh += bytes;
390 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
391}
392
393/*
394 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
395 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
396 * the buffer is only opened and the space reserved, but nothing is copied.
397 * The header is also automatically added to the index <hdr_idx>, and the end
398 * of headers is automatically adjusted. The number of bytes added is returned
399 * on success, otherwise <0 is returned indicating an error.
400 */
401int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
402 struct hdr_idx *hdr_idx, const char *text, int len)
403{
404 int bytes;
405
406 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
407 if (!bytes)
408 return -1;
409 msg->eoh += bytes;
410 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
411}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412
413/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100414 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
415 * If so, returns the position of the first non-space character relative to
416 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
417 * to return a pointer to the place after the first space. Returns 0 if the
418 * header name does not match. Checks are case-insensitive.
419 */
420int http_header_match2(const char *hdr, const char *end,
421 const char *name, int len)
422{
423 const char *val;
424
425 if (hdr + len >= end)
426 return 0;
427 if (hdr[len] != ':')
428 return 0;
429 if (strncasecmp(hdr, name, len) != 0)
430 return 0;
431 val = hdr + len + 1;
432 while (val < end && HTTP_IS_SPHT(*val))
433 val++;
434 if ((val >= end) && (len + 2 <= end - hdr))
435 return len + 2; /* we may replace starting from second space */
436 return val - hdr;
437}
438
Willy Tarreau33a7e692007-06-10 19:45:56 +0200439/* Find the end of the header value contained between <s> and <e>.
440 * See RFC2616, par 2.2 for more information. Note that it requires
441 * a valid header to return a valid result.
442 */
443const char *find_hdr_value_end(const char *s, const char *e)
444{
445 int quoted, qdpair;
446
447 quoted = qdpair = 0;
448 for (; s < e; s++) {
449 if (qdpair) qdpair = 0;
450 else if (quoted && *s == '\\') qdpair = 1;
451 else if (quoted && *s == '"') quoted = 0;
452 else if (*s == '"') quoted = 1;
453 else if (*s == ',') return s;
454 }
455 return s;
456}
457
458/* Find the first or next occurrence of header <name> in message buffer <sol>
459 * using headers index <idx>, and return it in the <ctx> structure. This
460 * structure holds everything necessary to use the header and find next
461 * occurrence. If its <idx> member is 0, the header is searched from the
462 * beginning. Otherwise, the next occurrence is returned. The function returns
463 * 1 when it finds a value, and 0 when there is no more.
464 */
465int http_find_header2(const char *name, int len,
466 const char *sol, struct hdr_idx *idx,
467 struct hdr_ctx *ctx)
468{
469 __label__ return_hdr, next_hdr;
470 const char *eol, *sov;
471 int cur_idx;
472
473 if (ctx->idx) {
474 /* We have previously returned a value, let's search
475 * another one on the same line.
476 */
477 cur_idx = ctx->idx;
478 sol = ctx->line;
479 sov = sol + ctx->val + ctx->vlen;
480 eol = sol + idx->v[cur_idx].len;
481
482 if (sov >= eol)
483 /* no more values in this header */
484 goto next_hdr;
485
486 /* values remaining for this header, skip the comma */
487 sov++;
488 while (sov < eol && http_is_lws[(unsigned char)*sov])
489 sov++;
490
491 goto return_hdr;
492 }
493
494 /* first request for this header */
495 sol += hdr_idx_first_pos(idx);
496 cur_idx = hdr_idx_first_idx(idx);
497
498 while (cur_idx) {
499 eol = sol + idx->v[cur_idx].len;
500
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200501 if (len == 0) {
502 /* No argument was passed, we want any header.
503 * To achieve this, we simply build a fake request. */
504 while (sol + len < eol && sol[len] != ':')
505 len++;
506 name = sol;
507 }
508
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 if ((len < eol - sol) &&
510 (sol[len] == ':') &&
511 (strncasecmp(sol, name, len) == 0)) {
512
513 sov = sol + len + 1;
514 while (sov < eol && http_is_lws[(unsigned char)*sov])
515 sov++;
516 return_hdr:
517 ctx->line = sol;
518 ctx->idx = cur_idx;
519 ctx->val = sov - sol;
520
521 eol = find_hdr_value_end(sov, eol);
522 ctx->vlen = eol - sov;
523 return 1;
524 }
525 next_hdr:
526 sol = eol + idx->v[cur_idx].cr + 1;
527 cur_idx = idx->v[cur_idx].next;
528 }
529 return 0;
530}
531
532int http_find_header(const char *name,
533 const char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 return http_find_header2(name, strlen(name), sol, idx, ctx);
537}
538
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200539/* This function shuts down the buffers on the server side, and sets indicators
540 * accordingly. The server's fd is supposed to already be closed. Note that if
541 * <status> is 0, or if the message pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 */
543void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100544 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545{
Willy Tarreauadfb8562008-08-11 15:24:42 +0200546 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200547 buffer_shutw(t->req);
548 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100550 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100551 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100552 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553 }
554 if (!(t->flags & SN_ERR_MASK))
555 t->flags |= err;
556 if (!(t->flags & SN_FINST_MASK))
557 t->flags |= finst;
558}
559
Willy Tarreau80587432006-12-24 17:47:20 +0100560/* This function returns the appropriate error location for the given session
561 * and message.
562 */
563
564struct chunk *error_message(struct session *s, int msgnum)
565{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200566 if (s->be->errmsg[msgnum].str)
567 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100568 else if (s->fe->errmsg[msgnum].str)
569 return &s->fe->errmsg[msgnum];
570 else
571 return &http_err_chunks[msgnum];
572}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573
Willy Tarreau53b6c742006-12-17 13:37:46 +0100574/*
575 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
576 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
577 */
578static http_meth_t find_http_meth(const char *str, const int len)
579{
580 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100581 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100582
583 m = ((unsigned)*str - 'A');
584
585 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 for (h = http_methods[m]; h->len > 0; h++) {
587 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100589 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 };
592 return HTTP_METH_OTHER;
593 }
594 return HTTP_METH_NONE;
595
596}
597
Willy Tarreau21d2af32008-02-14 20:25:24 +0100598/* Parse the URI from the given transaction (which is assumed to be in request
599 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
600 * It is returned otherwise.
601 */
602static char *
603http_get_path(struct http_txn *txn)
604{
605 char *ptr, *end;
606
607 ptr = txn->req.sol + txn->req.sl.rq.u;
608 end = ptr + txn->req.sl.rq.u_l;
609
610 if (ptr >= end)
611 return NULL;
612
613 /* RFC2616, par. 5.1.2 :
614 * Request-URI = "*" | absuri | abspath | authority
615 */
616
617 if (*ptr == '*')
618 return NULL;
619
620 if (isalpha((unsigned char)*ptr)) {
621 /* this is a scheme as described by RFC3986, par. 3.1 */
622 ptr++;
623 while (ptr < end &&
624 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
625 ptr++;
626 /* skip '://' */
627 if (ptr == end || *ptr++ != ':')
628 return NULL;
629 if (ptr == end || *ptr++ != '/')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 }
634 /* skip [user[:passwd]@]host[:[port]] */
635
636 while (ptr < end && *ptr != '/')
637 ptr++;
638
639 if (ptr == end)
640 return NULL;
641
642 /* OK, we got the '/' ! */
643 return ptr;
644}
645
Willy Tarreaudafde432008-08-17 01:00:46 +0200646/* Processes the client, server, request and response jobs of a session task,
647 * then puts it back to the wait queue in a clean state, or cleans up its
648 * resources if it must be deleted. Returns in <next> the date the task wants
649 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
650 * nothing too many times, the request and response buffers flags are monitored
651 * and each function is called only if at least another function has changed at
652 * least one flag. If one of the functions called returns non-zero, then it
653 * will be called once again after all other functions. This permits explicit
654 * external loops which may be useful for complex state machines.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 */
Willy Tarreaudafde432008-08-17 01:00:46 +0200656#define PROCESS_CLI 0x1
657#define PROCESS_SRV 0x2
658#define PROCESS_REQ 0x4
659#define PROCESS_RTR 0x8
660#define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR)
661
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200662void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663{
664 struct session *s = t->context;
Willy Tarreaudafde432008-08-17 01:00:46 +0200665 unsigned resync = PROCESS_ALL;
666 unsigned int rqf;
667 unsigned int rpf;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668
Willy Tarreau507385d2008-08-17 13:04:25 +0200669 /* check timeout expiration only once and adjust buffer flags
670 * accordingly.
671 */
672 if (unlikely(tick_is_expired(t->expire, now_ms))) {
673 if (tick_is_expired(s->req->rex, now_ms))
674 s->req->flags |= BF_READ_TIMEOUT;
675
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200676 //if (tick_is_expired(s->req->wex, now_ms))
677 // s->req->flags |= BF_WRITE_TIMEOUT;
678 //
679 //if (tick_is_expired(s->rep->rex, now_ms))
680 // s->rep->flags |= BF_READ_TIMEOUT;
Willy Tarreau507385d2008-08-17 13:04:25 +0200681
682 if (tick_is_expired(s->rep->wex, now_ms))
683 s->rep->flags |= BF_WRITE_TIMEOUT;
684 }
685
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200686 //if (fdtab[s->cli_fd].state == FD_STERROR) {
687 // fprintf(stderr, "s=%p fd=%d req=%p rep=%p cs=%d ss=%d, term=%08x\n",
688 // s, s->cli_fd, s->req, s->rep, s->cli_state,
689 // s->si[1].state, s->term_trace);
690 // sleep(1);
691 //}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 do {
Willy Tarreaudafde432008-08-17 01:00:46 +0200693 if (resync & PROCESS_REQ) {
694 resync &= ~PROCESS_REQ;
Willy Tarreau41f40ed2008-08-21 10:05:00 +0200695 rqf = s->req->flags;
696 rpf = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +0200697
Willy Tarreau41f40ed2008-08-21 10:05:00 +0200698 /* the analysers must block it themselves */
699 s->req->flags |= BF_MAY_FORWARD;
700
701 if (s->req->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200702 if (process_request(s))
703 resync |= PROCESS_REQ;
704
Willy Tarreaudafde432008-08-17 01:00:46 +0200705 if (rqf != s->req->flags || rpf != s->rep->flags)
706 resync |= PROCESS_ALL & ~PROCESS_REQ;
707 }
708 }
709
710 if (resync & PROCESS_RTR) {
711 resync &= ~PROCESS_RTR;
Willy Tarreau41f40ed2008-08-21 10:05:00 +0200712 rqf = s->req->flags;
713 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200714
Willy Tarreau41f40ed2008-08-21 10:05:00 +0200715 /* the analysers must block it themselves */
716 s->rep->flags |= BF_MAY_FORWARD;
717
718 if (s->rep->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200719 if (process_response(s))
720 resync |= PROCESS_RTR;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200721
Willy Tarreaudafde432008-08-17 01:00:46 +0200722 if (rqf != s->req->flags || rpf != s->rep->flags)
723 resync |= PROCESS_ALL & ~PROCESS_RTR;
724 }
725 }
726
727 if (resync & PROCESS_CLI) {
728 rqf = s->req->flags;
729 rpf = s->rep->flags;
730
731 resync &= ~PROCESS_CLI;
732 if (process_cli(s))
733 resync |= PROCESS_CLI;
734
735 if (rqf != s->req->flags || rpf != s->rep->flags)
736 resync |= PROCESS_ALL & ~PROCESS_CLI;
737 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200738
Willy Tarreaudafde432008-08-17 01:00:46 +0200739 if (resync & PROCESS_SRV) {
740 rqf = s->req->flags;
741 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200742
Willy Tarreaudafde432008-08-17 01:00:46 +0200743 resync &= ~PROCESS_SRV;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200744 if (s->req->cons->state != SI_ST_CLO) {
745 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_MAY_FORWARD)
746 process_srv_conn(s);
747
748 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200749 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD) &&
750 s->be->options & PR_O_FORCE_CLO &&
751 s->rep->flags & BF_READ_STATUS) {
752 /* We want to force the connection to the server to close,
753 * and the server has begun to respond. That's the right
754 * time.
755 */
756 buffer_shutw_now(s->req);
757 }
758
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200759 if (process_srv_data(s))
760 resync |= PROCESS_SRV;
Willy Tarreau376580a2008-08-27 18:52:22 +0200761
762 /* Count server-side errors (but not timeouts). */
763 if (s->req->flags & BF_WRITE_ERROR) {
764 s->be->failed_resp++;
765 if (s->srv)
766 s->srv->failed_resp++;
767 }
768
769 /* When a server-side connection is released, we have to
770 * count it and check for pending connections on this server.
771 */
772 if (s->req->cons->state == SI_ST_CLO) {
773 if (s->srv) {
774 s->srv->cur_sess--;
775 sess_change_server(s, NULL);
776 if (may_dequeue_tasks(s->srv, s->be))
777 process_srv_queue(s->srv);
778 }
779 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200780 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200781
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200782 if (unlikely((s->req->cons->state == SI_ST_CLO) &&
783 (global.mode & MODE_DEBUG) &&
784 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
785 int len;
786 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
787 s->uniq_id, s->be->id, (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd);
788 write(1, trash, len);
789 }
790 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200791 if (rqf != s->req->flags || rpf != s->rep->flags)
792 resync |= PROCESS_ALL & ~PROCESS_SRV;
793 }
794 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200796 if (likely(s->cli_state != CL_STCLOSE ||
797 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100798
799 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
800 session_process_counters(s);
801
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200802 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
803 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804
Willy Tarreau7f875f62008-08-11 17:35:01 +0200805 /* Trick: if a request is being waiting for the server to respond,
806 * and if we know the server can timeout, we don't want the timeout
807 * to expire on the client side first, but we're still interested
808 * in passing data from the client to the server (eg: POST). Thus,
809 * we can cancel the client's request timeout if the server's
810 * request timeout is set and the server has not yet sent a response.
811 */
812
Willy Tarreauba392ce2008-08-16 21:13:23 +0200813 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200814 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200815 s->req->rex = TICK_ETERNITY;
816
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200817 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
818 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200819 if (s->req->analysers)
820 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821
822 /* restore t to its place in the task list */
823 task_queue(t);
824
Willy Tarreaua7c52762008-08-16 18:40:18 +0200825#ifdef DEBUG_DEV
826 /* this may only happen when no timeout is set or in case of an FSM bug */
827 if (!t->expire)
828 ABORT_NOW();
829#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200830 *next = t->expire;
831 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832 }
833
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100834 s->fe->feconn--;
835 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200836 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200837 actconn--;
838
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200839 if (unlikely((global.mode & MODE_DEBUG) &&
840 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200842 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200843 s->uniq_id, s->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200844 (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200845 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 write(1, trash, len);
847 }
848
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200849 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100850 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851
852 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200853 if (s->logs.logwait &&
854 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200855 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
856 if (s->fe->to_log & LW_REQ)
857 http_sess_log(s);
858 else
859 tcp_sess_log(s);
860 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861
862 /* the task MUST not be in the run queue anymore */
863 task_delete(t);
864 session_free(s);
865 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200866 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867}
868
869
Willy Tarreau42250582007-04-01 01:30:43 +0200870extern const char sess_term_cond[8];
871extern const char sess_fin_state[8];
872extern const char *monthname[12];
873const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
874const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
875 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
876 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200877struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200878struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100879
Willy Tarreau42250582007-04-01 01:30:43 +0200880/*
881 * send a log for the session when we have enough info about it.
882 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100883 */
Willy Tarreau42250582007-04-01 01:30:43 +0200884static void http_sess_log(struct session *s)
885{
886 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
887 struct proxy *fe = s->fe;
888 struct proxy *be = s->be;
889 struct proxy *prx_log;
890 struct http_txn *txn = &s->txn;
891 int tolog;
892 char *uri, *h;
893 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200894 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200895 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200896 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200897 int hdr;
898
899 if (fe->logfac1 < 0 && fe->logfac2 < 0)
900 return;
901 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100902
Willy Tarreau42250582007-04-01 01:30:43 +0200903 if (s->cli_addr.ss_family == AF_INET)
904 inet_ntop(AF_INET,
905 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
906 pn, sizeof(pn));
907 else
908 inet_ntop(AF_INET6,
909 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
910 pn, sizeof(pn));
911
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200912 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200913
914 /* FIXME: let's limit ourselves to frontend logging for now. */
915 tolog = fe->to_log;
916
917 h = tmpline;
918 if (fe->to_log & LW_REQHDR &&
919 txn->req.cap &&
920 (h < tmpline + sizeof(tmpline) - 10)) {
921 *(h++) = ' ';
922 *(h++) = '{';
923 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
924 if (hdr)
925 *(h++) = '|';
926 if (txn->req.cap[hdr] != NULL)
927 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
928 '#', hdr_encode_map, txn->req.cap[hdr]);
929 }
930 *(h++) = '}';
931 }
932
933 if (fe->to_log & LW_RSPHDR &&
934 txn->rsp.cap &&
935 (h < tmpline + sizeof(tmpline) - 7)) {
936 *(h++) = ' ';
937 *(h++) = '{';
938 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
939 if (hdr)
940 *(h++) = '|';
941 if (txn->rsp.cap[hdr] != NULL)
942 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
943 '#', hdr_encode_map, txn->rsp.cap[hdr]);
944 }
945 *(h++) = '}';
946 }
947
948 if (h < tmpline + sizeof(tmpline) - 4) {
949 *(h++) = ' ';
950 *(h++) = '"';
951 uri = txn->uri ? txn->uri : "<BADREQ>";
952 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
953 '#', url_encode_map, uri);
954 *(h++) = '"';
955 }
956 *h = '\0';
957
958 svid = (tolog & LW_SVID) ?
959 (s->data_source != DATA_SRC_STATS) ?
960 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
961
Willy Tarreau70089872008-06-13 21:12:51 +0200962 t_request = -1;
963 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
964 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
965
Willy Tarreau42250582007-04-01 01:30:43 +0200966 send_log(prx_log, LOG_INFO,
967 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
968 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100969 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200970 pn,
971 (s->cli_addr.ss_family == AF_INET) ?
972 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
973 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200974 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200975 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200976 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200977 t_request,
978 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200979 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
980 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
981 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
982 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100983 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200984 txn->cli_cookie ? txn->cli_cookie : "-",
985 txn->srv_cookie ? txn->srv_cookie : "-",
986 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
987 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
988 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
989 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
990 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100991 (s->flags & SN_REDISP)?"+":"",
992 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200993 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
994
995 s->logs.logwait = 0;
996}
997
Willy Tarreau117f59e2007-03-04 18:17:17 +0100998
999/*
1000 * Capture headers from message starting at <som> according to header list
1001 * <cap_hdr>, and fill the <idx> structure appropriately.
1002 */
1003void capture_headers(char *som, struct hdr_idx *idx,
1004 char **cap, struct cap_hdr *cap_hdr)
1005{
1006 char *eol, *sol, *col, *sov;
1007 int cur_idx;
1008 struct cap_hdr *h;
1009 int len;
1010
1011 sol = som + hdr_idx_first_pos(idx);
1012 cur_idx = hdr_idx_first_idx(idx);
1013
1014 while (cur_idx) {
1015 eol = sol + idx->v[cur_idx].len;
1016
1017 col = sol;
1018 while (col < eol && *col != ':')
1019 col++;
1020
1021 sov = col + 1;
1022 while (sov < eol && http_is_lws[(unsigned char)*sov])
1023 sov++;
1024
1025 for (h = cap_hdr; h; h = h->next) {
1026 if ((h->namelen == col - sol) &&
1027 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1028 if (cap[h->index] == NULL)
1029 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001030 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001031
1032 if (cap[h->index] == NULL) {
1033 Alert("HTTP capture : out of memory.\n");
1034 continue;
1035 }
1036
1037 len = eol - sov;
1038 if (len > h->len)
1039 len = h->len;
1040
1041 memcpy(cap[h->index], sov, len);
1042 cap[h->index][len]=0;
1043 }
1044 }
1045 sol = eol + idx->v[cur_idx].cr + 1;
1046 cur_idx = idx->v[cur_idx].next;
1047 }
1048}
1049
1050
Willy Tarreau42250582007-04-01 01:30:43 +02001051/* either we find an LF at <ptr> or we jump to <bad>.
1052 */
1053#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1054
1055/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1056 * otherwise to <http_msg_ood> with <state> set to <st>.
1057 */
1058#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1059 ptr++; \
1060 if (likely(ptr < end)) \
1061 goto good; \
1062 else { \
1063 state = (st); \
1064 goto http_msg_ood; \
1065 } \
1066 } while (0)
1067
1068
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001070 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001071 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1072 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1073 * will give undefined results.
1074 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1075 * and that msg->sol points to the beginning of the response.
1076 * If a complete line is found (which implies that at least one CR or LF is
1077 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1078 * returned indicating an incomplete line (which does not mean that parts have
1079 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1080 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1081 * upon next call.
1082 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001083 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001084 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1085 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001086 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001087 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001088const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1089 unsigned int state, const char *ptr, const char *end,
1090 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001091{
1092 __label__
1093 http_msg_rpver,
1094 http_msg_rpver_sp,
1095 http_msg_rpcode,
1096 http_msg_rpcode_sp,
1097 http_msg_rpreason,
1098 http_msg_rpline_eol,
1099 http_msg_ood, /* out of data */
1100 http_msg_invalid;
1101
1102 switch (state) {
1103 http_msg_rpver:
1104 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001105 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001106 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1107
1108 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001109 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001110 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1111 }
1112 goto http_msg_invalid;
1113
1114 http_msg_rpver_sp:
1115 case HTTP_MSG_RPVER_SP:
1116 if (likely(!HTTP_IS_LWS(*ptr))) {
1117 msg->sl.st.c = ptr - msg_buf;
1118 goto http_msg_rpcode;
1119 }
1120 if (likely(HTTP_IS_SPHT(*ptr)))
1121 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1122 /* so it's a CR/LF, this is invalid */
1123 goto http_msg_invalid;
1124
1125 http_msg_rpcode:
1126 case HTTP_MSG_RPCODE:
1127 if (likely(!HTTP_IS_LWS(*ptr)))
1128 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1129
1130 if (likely(HTTP_IS_SPHT(*ptr))) {
1131 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1132 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1133 }
1134
1135 /* so it's a CR/LF, so there is no reason phrase */
1136 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1137 http_msg_rsp_reason:
1138 /* FIXME: should we support HTTP responses without any reason phrase ? */
1139 msg->sl.st.r = ptr - msg_buf;
1140 msg->sl.st.r_l = 0;
1141 goto http_msg_rpline_eol;
1142
1143 http_msg_rpcode_sp:
1144 case HTTP_MSG_RPCODE_SP:
1145 if (likely(!HTTP_IS_LWS(*ptr))) {
1146 msg->sl.st.r = ptr - msg_buf;
1147 goto http_msg_rpreason;
1148 }
1149 if (likely(HTTP_IS_SPHT(*ptr)))
1150 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1151 /* so it's a CR/LF, so there is no reason phrase */
1152 goto http_msg_rsp_reason;
1153
1154 http_msg_rpreason:
1155 case HTTP_MSG_RPREASON:
1156 if (likely(!HTTP_IS_CRLF(*ptr)))
1157 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1158 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1159 http_msg_rpline_eol:
1160 /* We have seen the end of line. Note that we do not
1161 * necessarily have the \n yet, but at least we know that we
1162 * have EITHER \r OR \n, otherwise the response would not be
1163 * complete. We can then record the response length and return
1164 * to the caller which will be able to register it.
1165 */
1166 msg->sl.st.l = ptr - msg->sol;
1167 return ptr;
1168
1169#ifdef DEBUG_FULL
1170 default:
1171 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1172 exit(1);
1173#endif
1174 }
1175
1176 http_msg_ood:
1177 /* out of data */
1178 if (ret_state)
1179 *ret_state = state;
1180 if (ret_ptr)
1181 *ret_ptr = (char *)ptr;
1182 return NULL;
1183
1184 http_msg_invalid:
1185 /* invalid message */
1186 if (ret_state)
1187 *ret_state = HTTP_MSG_ERROR;
1188 return NULL;
1189}
1190
1191
1192/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 * This function parses a request line between <ptr> and <end>, starting with
1194 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1195 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1196 * will give undefined results.
1197 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1198 * and that msg->sol points to the beginning of the request.
1199 * If a complete line is found (which implies that at least one CR or LF is
1200 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1201 * returned indicating an incomplete line (which does not mean that parts have
1202 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1203 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1204 * upon next call.
1205 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001206 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1208 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001209 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001210 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001211const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1212 unsigned int state, const char *ptr, const char *end,
1213 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001214{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 __label__
1216 http_msg_rqmeth,
1217 http_msg_rqmeth_sp,
1218 http_msg_rquri,
1219 http_msg_rquri_sp,
1220 http_msg_rqver,
1221 http_msg_rqline_eol,
1222 http_msg_ood, /* out of data */
1223 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001224
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 switch (state) {
1226 http_msg_rqmeth:
1227 case HTTP_MSG_RQMETH:
1228 if (likely(HTTP_IS_TOKEN(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001232 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 if (likely(HTTP_IS_CRLF(*ptr))) {
1237 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001238 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 http_msg_req09_uri:
1240 msg->sl.rq.u = ptr - msg_buf;
1241 http_msg_req09_uri_e:
1242 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1243 http_msg_req09_ver:
1244 msg->sl.rq.v = ptr - msg_buf;
1245 msg->sl.rq.v_l = 0;
1246 goto http_msg_rqline_eol;
1247 }
1248 goto http_msg_invalid;
1249
1250 http_msg_rqmeth_sp:
1251 case HTTP_MSG_RQMETH_SP:
1252 if (likely(!HTTP_IS_LWS(*ptr))) {
1253 msg->sl.rq.u = ptr - msg_buf;
1254 goto http_msg_rquri;
1255 }
1256 if (likely(HTTP_IS_SPHT(*ptr)))
1257 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1258 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1259 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 http_msg_rquri:
1262 case HTTP_MSG_RQURI:
1263 if (likely(!HTTP_IS_LWS(*ptr)))
1264 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 if (likely(HTTP_IS_SPHT(*ptr))) {
1267 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1268 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1272 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 http_msg_rquri_sp:
1275 case HTTP_MSG_RQURI_SP:
1276 if (likely(!HTTP_IS_LWS(*ptr))) {
1277 msg->sl.rq.v = ptr - msg_buf;
1278 goto http_msg_rqver;
1279 }
1280 if (likely(HTTP_IS_SPHT(*ptr)))
1281 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1282 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1283 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 http_msg_rqver:
1286 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001287 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001289
1290 if (likely(HTTP_IS_CRLF(*ptr))) {
1291 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1292 http_msg_rqline_eol:
1293 /* We have seen the end of line. Note that we do not
1294 * necessarily have the \n yet, but at least we know that we
1295 * have EITHER \r OR \n, otherwise the request would not be
1296 * complete. We can then record the request length and return
1297 * to the caller which will be able to register it.
1298 */
1299 msg->sl.rq.l = ptr - msg->sol;
1300 return ptr;
1301 }
1302
1303 /* neither an HTTP_VER token nor a CRLF */
1304 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306#ifdef DEBUG_FULL
1307 default:
1308 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1309 exit(1);
1310#endif
1311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 http_msg_ood:
1314 /* out of data */
1315 if (ret_state)
1316 *ret_state = state;
1317 if (ret_ptr)
1318 *ret_ptr = (char *)ptr;
1319 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 http_msg_invalid:
1322 /* invalid message */
1323 if (ret_state)
1324 *ret_state = HTTP_MSG_ERROR;
1325 return NULL;
1326}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
1328
Willy Tarreau8973c702007-01-21 23:58:29 +01001329/*
1330 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001331 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 * when data are missing and recalled at the exact same location with no
1333 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001334 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1335 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1338{
1339 __label__
1340 http_msg_rqbefore,
1341 http_msg_rqbefore_cr,
1342 http_msg_rqmeth,
1343 http_msg_rqline_end,
1344 http_msg_hdr_first,
1345 http_msg_hdr_name,
1346 http_msg_hdr_l1_sp,
1347 http_msg_hdr_l1_lf,
1348 http_msg_hdr_l1_lws,
1349 http_msg_hdr_val,
1350 http_msg_hdr_l2_lf,
1351 http_msg_hdr_l2_lws,
1352 http_msg_complete_header,
1353 http_msg_last_lf,
1354 http_msg_ood, /* out of data */
1355 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreaue69eada2008-01-27 00:34:10 +01001357 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001360 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 ptr = buf->lr;
1362 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (unlikely(ptr >= end))
1365 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 /*
1369 * First, states that are specific to the response only.
1370 * We check them first so that request and headers are
1371 * closer to each other (accessed more often).
1372 */
1373 http_msg_rpbefore:
1374 case HTTP_MSG_RPBEFORE:
1375 if (likely(HTTP_IS_TOKEN(*ptr))) {
1376 if (likely(ptr == buf->data)) {
1377 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001378 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 } else {
1380#if PARSE_PRESERVE_EMPTY_LINES
1381 /* only skip empty leading lines, don't remove them */
1382 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001383 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001384#else
1385 /* Remove empty leading lines, as recommended by
1386 * RFC2616. This takes a lot of time because we
1387 * must move all the buffer backwards, but this
1388 * is rarely needed. The method above will be
1389 * cleaner when we'll be able to start sending
1390 * the request from any place in the buffer.
1391 */
1392 buf->lr = ptr;
1393 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 msg->sol = buf->data;
1396 ptr = buf->data;
1397 end = buf->r;
1398#endif
1399 }
1400 hdr_idx_init(idx);
1401 state = HTTP_MSG_RPVER;
1402 goto http_msg_rpver;
1403 }
1404
1405 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1406 goto http_msg_invalid;
1407
1408 if (unlikely(*ptr == '\n'))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1410 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1411 /* stop here */
1412
1413 http_msg_rpbefore_cr:
1414 case HTTP_MSG_RPBEFORE_CR:
1415 EXPECT_LF_HERE(ptr, http_msg_invalid);
1416 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1417 /* stop here */
1418
1419 http_msg_rpver:
1420 case HTTP_MSG_RPVER:
1421 case HTTP_MSG_RPVER_SP:
1422 case HTTP_MSG_RPCODE:
1423 case HTTP_MSG_RPCODE_SP:
1424 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001425 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001426 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 if (unlikely(!ptr))
1428 return;
1429
1430 /* we have a full response and we know that we have either a CR
1431 * or an LF at <ptr>.
1432 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001433 //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 +01001434 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1435
1436 msg->sol = ptr;
1437 if (likely(*ptr == '\r'))
1438 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1439 goto http_msg_rpline_end;
1440
1441 http_msg_rpline_end:
1442 case HTTP_MSG_RPLINE_END:
1443 /* msg->sol must point to the first of CR or LF. */
1444 EXPECT_LF_HERE(ptr, http_msg_invalid);
1445 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1446 /* stop here */
1447
1448 /*
1449 * Second, states that are specific to the request only
1450 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 http_msg_rqbefore:
1452 case HTTP_MSG_RQBEFORE:
1453 if (likely(HTTP_IS_TOKEN(*ptr))) {
1454 if (likely(ptr == buf->data)) {
1455 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001456 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 } else {
1458#if PARSE_PRESERVE_EMPTY_LINES
1459 /* only skip empty leading lines, don't remove them */
1460 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001461 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001462#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 /* Remove empty leading lines, as recommended by
1464 * RFC2616. This takes a lot of time because we
1465 * must move all the buffer backwards, but this
1466 * is rarely needed. The method above will be
1467 * cleaner when we'll be able to start sending
1468 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001469 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 buf->lr = ptr;
1471 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001472 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 msg->sol = buf->data;
1474 ptr = buf->data;
1475 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001478 /* we will need this when keep-alive will be supported
1479 hdr_idx_init(idx);
1480 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 state = HTTP_MSG_RQMETH;
1482 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1486 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 if (unlikely(*ptr == '\n'))
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1490 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001491 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 http_msg_rqbefore_cr:
1494 case HTTP_MSG_RQBEFORE_CR:
1495 EXPECT_LF_HERE(ptr, http_msg_invalid);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 http_msg_rqmeth:
1500 case HTTP_MSG_RQMETH:
1501 case HTTP_MSG_RQMETH_SP:
1502 case HTTP_MSG_RQURI:
1503 case HTTP_MSG_RQURI_SP:
1504 case HTTP_MSG_RQVER:
1505 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001506 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (unlikely(!ptr))
1508 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 /* we have a full request and we know that we have either a CR
1511 * or an LF at <ptr>.
1512 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001513 //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 +01001514 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 msg->sol = ptr;
1517 if (likely(*ptr == '\r'))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_rqline_end:
1522 case HTTP_MSG_RQLINE_END:
1523 /* check for HTTP/0.9 request : no version information available.
1524 * msg->sol must point to the first of CR or LF.
1525 */
1526 if (unlikely(msg->sl.rq.v_l == 0))
1527 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 EXPECT_LF_HERE(ptr, http_msg_invalid);
1530 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001532
Willy Tarreau8973c702007-01-21 23:58:29 +01001533 /*
1534 * Common states below
1535 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 http_msg_hdr_first:
1537 case HTTP_MSG_HDR_FIRST:
1538 msg->sol = ptr;
1539 if (likely(!HTTP_IS_CRLF(*ptr))) {
1540 goto http_msg_hdr_name;
1541 }
1542
1543 if (likely(*ptr == '\r'))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1545 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 http_msg_hdr_name:
1548 case HTTP_MSG_HDR_NAME:
1549 /* assumes msg->sol points to the first char */
1550 if (likely(HTTP_IS_TOKEN(*ptr)))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 if (likely(*ptr == ':')) {
1554 msg->col = ptr - buf->data;
1555 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1556 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 http_msg_hdr_l1_sp:
1561 case HTTP_MSG_HDR_L1_SP:
1562 /* assumes msg->sol points to the first char and msg->col to the colon */
1563 if (likely(HTTP_IS_SPHT(*ptr)))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 /* header value can be basically anything except CR/LF */
1567 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 if (likely(!HTTP_IS_CRLF(*ptr))) {
1570 goto http_msg_hdr_val;
1571 }
1572
1573 if (likely(*ptr == '\r'))
1574 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1575 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 http_msg_hdr_l1_lf:
1578 case HTTP_MSG_HDR_L1_LF:
1579 EXPECT_LF_HERE(ptr, http_msg_invalid);
1580 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 http_msg_hdr_l1_lws:
1583 case HTTP_MSG_HDR_L1_LWS:
1584 if (likely(HTTP_IS_SPHT(*ptr))) {
1585 /* replace HT,CR,LF with spaces */
1586 for (; buf->data+msg->sov < ptr; msg->sov++)
1587 buf->data[msg->sov] = ' ';
1588 goto http_msg_hdr_l1_sp;
1589 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001590 /* we had a header consisting only in spaces ! */
1591 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 goto http_msg_complete_header;
1593
1594 http_msg_hdr_val:
1595 case HTTP_MSG_HDR_VAL:
1596 /* assumes msg->sol points to the first char, msg->col to the
1597 * colon, and msg->sov points to the first character of the
1598 * value.
1599 */
1600 if (likely(!HTTP_IS_CRLF(*ptr)))
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 msg->eol = ptr;
1604 /* Note: we could also copy eol into ->eoh so that we have the
1605 * real header end in case it ends with lots of LWS, but is this
1606 * really needed ?
1607 */
1608 if (likely(*ptr == '\r'))
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1610 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 http_msg_hdr_l2_lf:
1613 case HTTP_MSG_HDR_L2_LF:
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 http_msg_hdr_l2_lws:
1618 case HTTP_MSG_HDR_L2_LWS:
1619 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1620 /* LWS: replace HT,CR,LF with spaces */
1621 for (; msg->eol < ptr; msg->eol++)
1622 *msg->eol = ' ';
1623 goto http_msg_hdr_val;
1624 }
1625 http_msg_complete_header:
1626 /*
1627 * It was a new header, so the last one is finished.
1628 * Assumes msg->sol points to the first char, msg->col to the
1629 * colon, msg->sov points to the first character of the value
1630 * and msg->eol to the first CR or LF so we know how the line
1631 * ends. We insert last header into the index.
1632 */
1633 /*
1634 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1635 write(2, msg->sol, msg->eol-msg->sol);
1636 fprintf(stderr,"\n");
1637 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1640 idx, idx->tail) < 0))
1641 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 msg->sol = ptr;
1644 if (likely(!HTTP_IS_CRLF(*ptr))) {
1645 goto http_msg_hdr_name;
1646 }
1647
1648 if (likely(*ptr == '\r'))
1649 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1650 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 http_msg_last_lf:
1653 case HTTP_MSG_LAST_LF:
1654 /* Assumes msg->sol points to the first of either CR or LF */
1655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 ptr++;
1657 buf->lr = ptr;
1658 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001659 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 return;
1661#ifdef DEBUG_FULL
1662 default:
1663 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1664 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001665#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 }
1667 http_msg_ood:
1668 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001669 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 buf->lr = ptr;
1671 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001672
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 http_msg_invalid:
1674 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001675 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 return;
1677}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001678
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001679/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001680 * It normally returns zero, but may return 1 if it absolutely needs to be
1681 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001682 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001683 * functions. Its behaviour is rather simple :
1684 * - all enabled analysers are called in turn from the lower to the higher
1685 * bit.
1686 * - if an analyser does not have enough data, it must return without calling
1687 * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure
1688 * that unprocessed data will not be forwarded. But that probably depends on
1689 * the protocol. Generally it is not reset in case of errors.
1690 * - if an analyser has enough data, it just has to pass on to the next
1691 * analyser without touching BF_MAY_FORWARD (it is enabled prior to
1692 * analysis).
1693 * - if an analyser thinks it has no added value anymore staying here, it must
1694 * reset its bit from the analysers flags in order not to be called anymore.
1695 *
1696 * In the future, analysers should be able to indicate that they want to be
1697 * called after XXX bytes have been received (or transfered), and the min of
1698 * all's wishes will be used to ring back (unless a special condition occurs).
1699 *
1700 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001702int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 struct buffer *req = t->req;
1705 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001706
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001707 DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
1708 now_ms, __FUNCTION__,
1709 cli_stnames[t->cli_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001710 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1711 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02001712 req->rex, rep->wex, req->flags, rep->flags, req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001714 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001715 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001716 struct tcp_rule *rule;
1717 int partial;
1718
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001719 /* We will abort if we encounter a read error. In theory, we
1720 * should not abort if we get a close, it might be valid,
1721 * although very unlikely. FIXME: we'll abort for now, this
1722 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001723 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001724 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001725 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001726 t->fe->failed_req++;
1727 if (!(t->flags & SN_ERR_MASK))
1728 t->flags |= SN_ERR_CLICL;
1729 if (!(t->flags & SN_FINST_MASK))
1730 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001731 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001732 }
1733
1734 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001735 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001736 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 t->fe->failed_req++;
1738 if (!(t->flags & SN_ERR_MASK))
1739 t->flags |= SN_ERR_CLITO;
1740 if (!(t->flags & SN_FINST_MASK))
1741 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001742 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001743 }
1744
1745 /* We don't know whether we have enough data, so must proceed
1746 * this way :
1747 * - iterate through all rules in their declaration order
1748 * - if one rule returns MISS, it means the inspect delay is
1749 * not over yet, then return immediately, otherwise consider
1750 * it as a non-match.
1751 * - if one rule returns OK, then return OK
1752 * - if one rule returns KO, then return KO
1753 */
1754
Willy Tarreauffab5b42008-08-17 18:03:28 +02001755 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001756 partial = 0;
1757 else
1758 partial = ACL_PARTIAL;
1759
1760 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1761 int ret = ACL_PAT_PASS;
1762
1763 if (rule->cond) {
1764 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1765 if (ret == ACL_PAT_MISS) {
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001766 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001767 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001768 if (!tick_isset(req->analyse_exp))
1769 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001770 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001771 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001772
Willy Tarreaub6866442008-07-14 23:54:42 +02001773 ret = acl_pass(ret);
1774 if (rule->cond->pol == ACL_COND_UNLESS)
1775 ret = !ret;
1776 }
1777
1778 if (ret) {
1779 /* we have a matching rule. */
1780 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001781 buffer_abort(req);
1782 buffer_abort(rep);
1783 //FIXME: this delete this
1784 //fd_delete(t->cli_fd);
1785 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001786 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001787 t->fe->failed_req++;
1788 if (!(t->flags & SN_ERR_MASK))
1789 t->flags |= SN_ERR_PRXCOND;
1790 if (!(t->flags & SN_FINST_MASK))
1791 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001792 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001793 }
1794 /* otherwise accept */
1795 break;
1796 }
1797 }
1798
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001799 /* if we get there, it means we have no rule which matches, or
1800 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001801 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001802 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001803 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001804 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001805
Willy Tarreau2df28e82008-08-17 15:20:19 +02001806 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 /*
1808 * Now parse the partial (or complete) lines.
1809 * We will check the request syntax, and also join multi-line
1810 * headers. An index of all the lines will be elaborated while
1811 * parsing.
1812 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001813 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001816 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 * req->data + req->eoh = end of processed headers / start of current one
1818 * req->data + req->eol = end of current header or line (LF or CRLF)
1819 * req->lr = first non-visited byte
1820 * req->r = end of data
1821 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001824 struct http_txn *txn = &t->txn;
1825 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001829 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 /* 1: we might have to print this header in debug mode */
1832 if (unlikely((global.mode & MODE_DEBUG) &&
1833 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001834 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001837 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 eol = sol + msg->sl.rq.l;
1839 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001840
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001841 sol += hdr_idx_first_pos(&txn->hdr_idx);
1842 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001845 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001847 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1848 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 }
1850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001851
Willy Tarreau58f10d72006-12-04 02:26:12 +01001852
1853 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001855 * If not so, we check the FD and buffer states before leaving.
1856 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1858 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001859 *
1860 */
1861
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001862 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 /*
1864 * First, let's catch bad requests.
1865 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001866 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001868
1869 /* 1: Since we are in header mode, if there's no space
1870 * left for headers, we won't be able to free more
1871 * later, so the session will never terminate. We
1872 * must terminate it now.
1873 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001874 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 /* FIXME: check if URI is set and return Status
1876 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001877 */
Willy Tarreau06619262006-12-17 08:37:22 +01001878 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001879 }
1880
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001881 /* 2: have we encountered a close ? */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001882 else if (req->flags & (BF_READ_NULL | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001883 txn->status = 400;
1884 client_retnclose(t, error_message(t, HTTP_ERR_400));
1885 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001886 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001887 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001888
Willy Tarreau58f10d72006-12-04 02:26:12 +01001889 if (!(t->flags & SN_ERR_MASK))
1890 t->flags |= SN_ERR_CLICL;
1891 if (!(t->flags & SN_FINST_MASK))
1892 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001893 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001894 }
1895
1896 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001897 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001898 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001899 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001900 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001901 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001902 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001903 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001904 if (!(t->flags & SN_ERR_MASK))
1905 t->flags |= SN_ERR_CLITO;
1906 if (!(t->flags & SN_FINST_MASK))
1907 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001908 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001909 }
1910
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001911 /* 4: have we encountered a read error ? */
1912 else if (req->flags & BF_READ_ERROR) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001913 /* we cannot return any message on error */
1914 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001915 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001916 t->fe->failed_req++;
1917 if (!(t->flags & SN_ERR_MASK))
1918 t->flags |= SN_ERR_CLICL;
1919 if (!(t->flags & SN_FINST_MASK))
1920 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001921 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001923
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001924 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001925 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001926 if (!tick_isset(req->analyse_exp))
1927 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001928
1929 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001930 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001931 }
1932
1933
1934 /****************************************************************
1935 * More interesting part now : we know that we have a complete *
1936 * request which at least looks like HTTP. We have an indicator *
1937 * of each header's length, so we can parse them quickly. *
1938 ****************************************************************/
1939
Willy Tarreau2df28e82008-08-17 15:20:19 +02001940 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001941 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001942
Willy Tarreau9cdde232007-05-02 20:58:19 +02001943 /* ensure we keep this pointer to the beginning of the message */
1944 msg->sol = req->data + msg->som;
1945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 /*
1947 * 1: identify the method
1948 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001949 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001950
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001951 /* we can make use of server redirect on GET and HEAD */
1952 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1953 t->flags |= SN_REDIRECTABLE;
1954
Willy Tarreau58f10d72006-12-04 02:26:12 +01001955 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001957 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001958 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001959 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 if (unlikely((t->fe->monitor_uri_len != 0) &&
1961 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1962 !memcmp(&req->data[msg->sl.rq.u],
1963 t->fe->monitor_uri,
1964 t->fe->monitor_uri_len))) {
1965 /*
1966 * We have found the monitor URI
1967 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001968 struct acl_cond *cond;
1969 cur_proxy = t->fe;
1970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001971 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001972
1973 /* Check if we want to fail this monitor request or not */
1974 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1975 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001976
1977 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001978 if (cond->pol == ACL_COND_UNLESS)
1979 ret = !ret;
1980
1981 if (ret) {
1982 /* we fail this request, let's return 503 service unavail */
1983 txn->status = 503;
1984 client_retnclose(t, error_message(t, HTTP_ERR_503));
1985 goto return_prx_cond;
1986 }
1987 }
1988
1989 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001990 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 client_retnclose(t, &http_200_chunk);
1992 goto return_prx_cond;
1993 }
1994
1995 /*
1996 * 3: Maybe we have to copy the original REQURI for the logs ?
1997 * Note: we cannot log anymore if the request has been
1998 * classified as invalid.
1999 */
2000 if (unlikely(t->logs.logwait & LW_REQ)) {
2001 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002002 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 int urilen = msg->sl.rq.l;
2004
2005 if (urilen >= REQURI_LEN)
2006 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002007 memcpy(txn->uri, &req->data[msg->som], urilen);
2008 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002009
2010 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002011 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002012 } else {
2013 Alert("HTTP logging : out of memory.\n");
2014 }
2015 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002016
Willy Tarreau06619262006-12-17 08:37:22 +01002017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002018 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2019 if (unlikely(msg->sl.rq.v_l == 0)) {
2020 int delta;
2021 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002022 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002023 cur_end = msg->sol + msg->sl.rq.l;
2024 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002025
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002026 if (msg->sl.rq.u_l == 0) {
2027 /* if no URI was set, add "/" */
2028 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2029 cur_end += delta;
2030 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002031 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 /* add HTTP version */
2033 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2034 msg->eoh += delta;
2035 cur_end += delta;
2036 cur_end = (char *)http_parse_reqline(msg, req->data,
2037 HTTP_MSG_RQMETH,
2038 msg->sol, cur_end + 1,
2039 NULL, NULL);
2040 if (unlikely(!cur_end))
2041 goto return_bad_req;
2042
2043 /* we have a full HTTP/1.0 request now and we know that
2044 * we have either a CR or an LF at <ptr>.
2045 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002046 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002047 }
2048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002049
2050 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002051 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002052 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002053 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002054
2055 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002057 * As opposed to version 1.2, now they will be evaluated in the
2058 * filters order and not in the header order. This means that
2059 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002060 *
2061 * We can now check whether we want to switch to another
2062 * backend, in which case we will re-check the backend's
2063 * filters and various options. In order to support 3-level
2064 * switching, here's how we should proceed :
2065 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002066 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002067 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002068 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002069 * There cannot be any switch from there, so ->be cannot be
2070 * changed anymore.
2071 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002072 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002073 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002074 * The response path will be able to apply either ->be, or
2075 * ->be then ->fe filters in order to match the reverse of
2076 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002077 */
2078
Willy Tarreau06619262006-12-17 08:37:22 +01002079 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002080 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002081 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002082 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002083 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002084
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002085 /* first check whether we have some ACLs set to redirect this request */
2086 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2087 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002088
2089 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002090 if (rule->cond->pol == ACL_COND_UNLESS)
2091 ret = !ret;
2092
2093 if (ret) {
2094 struct chunk rdr = { trash, 0 };
2095 const char *msg_fmt;
2096
2097 /* build redirect message */
2098 switch(rule->code) {
2099 case 303:
2100 rdr.len = strlen(HTTP_303);
2101 msg_fmt = HTTP_303;
2102 break;
2103 case 301:
2104 rdr.len = strlen(HTTP_301);
2105 msg_fmt = HTTP_301;
2106 break;
2107 case 302:
2108 default:
2109 rdr.len = strlen(HTTP_302);
2110 msg_fmt = HTTP_302;
2111 break;
2112 }
2113
2114 if (unlikely(rdr.len > sizeof(trash)))
2115 goto return_bad_req;
2116 memcpy(rdr.str, msg_fmt, rdr.len);
2117
2118 switch(rule->type) {
2119 case REDIRECT_TYPE_PREFIX: {
2120 const char *path;
2121 int pathlen;
2122
2123 path = http_get_path(txn);
2124 /* build message using path */
2125 if (path) {
2126 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2127 } else {
2128 path = "/";
2129 pathlen = 1;
2130 }
2131
2132 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2133 goto return_bad_req;
2134
2135 /* add prefix */
2136 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2137 rdr.len += rule->rdr_len;
2138
2139 /* add path */
2140 memcpy(rdr.str + rdr.len, path, pathlen);
2141 rdr.len += pathlen;
2142 break;
2143 }
2144 case REDIRECT_TYPE_LOCATION:
2145 default:
2146 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2147 goto return_bad_req;
2148
2149 /* add location */
2150 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2151 rdr.len += rule->rdr_len;
2152 break;
2153 }
2154
2155 /* add end of headers */
2156 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2157 rdr.len += 4;
2158
2159 txn->status = rule->code;
2160 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002161 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002162 client_retnclose(t, &rdr);
2163 goto return_prx_cond;
2164 }
2165 }
2166
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002167 /* first check whether we have some ACLs set to block this request */
2168 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002169 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002170
2171 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002172 if (cond->pol == ACL_COND_UNLESS)
2173 ret = !ret;
2174
2175 if (ret) {
2176 txn->status = 403;
2177 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002178 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002179 client_retnclose(t, error_message(t, HTTP_ERR_403));
2180 goto return_prx_cond;
2181 }
2182 }
2183
Willy Tarreau06619262006-12-17 08:37:22 +01002184 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002185 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002186 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2187 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002188 }
2189
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002190 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2191 /* to ensure correct connection accounting on
2192 * the backend, we count the connection for the
2193 * one managing the queue.
2194 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002195 t->be->beconn++;
2196 if (t->be->beconn > t->be->beconn_max)
2197 t->be->beconn_max = t->be->beconn;
2198 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002199 t->flags |= SN_BE_ASSIGNED;
2200 }
2201
Willy Tarreau06619262006-12-17 08:37:22 +01002202 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002203 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002204 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002205 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002206 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002207 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002208 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002209 goto return_prx_cond;
2210 }
2211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002212 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002213 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002214 !(t->flags & SN_CONN_CLOSED)) {
2215 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002216 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002217 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002218
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002219 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002220 old_idx = 0;
2221
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2223 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002224 cur_ptr = cur_next;
2225 cur_end = cur_ptr + cur_hdr->len;
2226 cur_next = cur_end + cur_hdr->cr + 1;
2227
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002228 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2229 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002230 /* 3 possibilities :
2231 * - we have already set Connection: close,
2232 * so we remove this line.
2233 * - we have not yet set Connection: close,
2234 * but this line indicates close. We leave
2235 * it untouched and set the flag.
2236 * - we have not yet set Connection: close,
2237 * and this line indicates non-close. We
2238 * replace it.
2239 */
2240 if (t->flags & SN_CONN_CLOSED) {
2241 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002242 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002243 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002244 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2245 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002246 cur_hdr->len = 0;
2247 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002248 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2249 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2250 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002251 cur_next += delta;
2252 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002253 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002254 }
2255 t->flags |= SN_CONN_CLOSED;
2256 }
2257 }
2258 old_idx = cur_idx;
2259 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002260 }
2261 /* add request headers from the rule sets in the same order */
2262 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2263 if (unlikely(http_header_add_tail(req,
2264 &txn->req,
2265 &txn->hdr_idx,
2266 rule_set->req_add[cur_idx])) < 0)
2267 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002268 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002270 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002271 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002272 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002273 /* we have to check the URI and auth for this request.
2274 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002275 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002276 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002277 if (stats_check_uri_auth(t, rule_set))
2278 return 1;
2279 }
2280
Willy Tarreau55ea7572007-06-17 19:56:27 +02002281 /* now check whether we have some switching rules for this request */
2282 if (!(t->flags & SN_BE_ASSIGNED)) {
2283 struct switching_rule *rule;
2284
2285 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2286 int ret;
2287
2288 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002289
2290 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002291 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002292 ret = !ret;
2293
2294 if (ret) {
2295 t->be = rule->be.backend;
2296 t->be->beconn++;
2297 if (t->be->beconn > t->be->beconn_max)
2298 t->be->beconn_max = t->be->beconn;
2299 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002300
2301 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002302 t->rep->rto = t->req->wto = t->be->timeout.server;
2303 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002304 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002305 t->flags |= SN_BE_ASSIGNED;
2306 break;
2307 }
2308 }
2309 }
2310
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002311 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2312 /* No backend was set, but there was a default
2313 * backend set in the frontend, so we use it and
2314 * loop again.
2315 */
2316 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002317 t->be->beconn++;
2318 if (t->be->beconn > t->be->beconn_max)
2319 t->be->beconn_max = t->be->beconn;
2320 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002321
2322 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002323 t->rep->rto = t->req->wto = t->be->timeout.server;
2324 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002325 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002326 t->flags |= SN_BE_ASSIGNED;
2327 }
2328 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002329
Willy Tarreau58f10d72006-12-04 02:26:12 +01002330
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002331 if (!(t->flags & SN_BE_ASSIGNED)) {
2332 /* To ensure correct connection accounting on
2333 * the backend, we count the connection for the
2334 * one managing the queue.
2335 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002336 t->be->beconn++;
2337 if (t->be->beconn > t->be->beconn_max)
2338 t->be->beconn_max = t->be->beconn;
2339 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002340 t->flags |= SN_BE_ASSIGNED;
2341 }
2342
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002343 /*
2344 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002345 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002346 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002347 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002348 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002349
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002350 /*
2351 * If HTTP PROXY is set we simply get remote server address
2352 * parsing incoming request.
2353 */
2354 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2355 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau2a324282006-12-05 00:05:46 +01002358 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002360 * so let's do the same now.
2361 */
2362
2363 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002364 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002365 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002366 }
2367
2368
2369 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002370 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002371 * Note that doing so might move headers in the request, but
2372 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002373 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002374 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002375 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2376 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002377 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau58f10d72006-12-04 02:26:12 +01002379
Willy Tarreau2a324282006-12-05 00:05:46 +01002380 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002381 * 9: add X-Forwarded-For if either the frontend or the backend
2382 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002383 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002384 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002385 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002386 /* Add an X-Forwarded-For header unless the source IP is
2387 * in the 'except' network range.
2388 */
2389 if ((!t->fe->except_mask.s_addr ||
2390 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2391 != t->fe->except_net.s_addr) &&
2392 (!t->be->except_mask.s_addr ||
2393 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2394 != t->be->except_net.s_addr)) {
2395 int len;
2396 unsigned char *pn;
2397 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002398
Ross Westaf72a1d2008-08-03 10:51:45 +02002399 /* Note: we rely on the backend to get the header name to be used for
2400 * x-forwarded-for, because the header is really meant for the backends.
2401 * However, if the backend did not specify any option, we have to rely
2402 * on the frontend's header name.
2403 */
2404 if (t->be->fwdfor_hdr_len) {
2405 len = t->be->fwdfor_hdr_len;
2406 memcpy(trash, t->be->fwdfor_hdr_name, len);
2407 } else {
2408 len = t->fe->fwdfor_hdr_len;
2409 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2410 }
2411 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002412
Ross Westaf72a1d2008-08-03 10:51:45 +02002413 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002414 &txn->hdr_idx, trash, len)) < 0)
2415 goto return_bad_req;
2416 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002417 }
2418 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002419 /* FIXME: for the sake of completeness, we should also support
2420 * 'except' here, although it is mostly useless in this case.
2421 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002422 int len;
2423 char pn[INET6_ADDRSTRLEN];
2424 inet_ntop(AF_INET6,
2425 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2426 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002427
2428 /* Note: we rely on the backend to get the header name to be used for
2429 * x-forwarded-for, because the header is really meant for the backends.
2430 * However, if the backend did not specify any option, we have to rely
2431 * on the frontend's header name.
2432 */
2433 if (t->be->fwdfor_hdr_len) {
2434 len = t->be->fwdfor_hdr_len;
2435 memcpy(trash, t->be->fwdfor_hdr_name, len);
2436 } else {
2437 len = t->fe->fwdfor_hdr_len;
2438 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2439 }
2440 len += sprintf(trash + len, ": %s", pn);
2441
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002442 if (unlikely(http_header_add_tail2(req, &txn->req,
2443 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002444 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002445 }
2446 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002447
Willy Tarreau2a324282006-12-05 00:05:46 +01002448 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002449 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002450 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002451 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002452 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002453 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002454 if ((unlikely(msg->sl.rq.v_l != 8) ||
2455 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2456 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002457 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002458 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002459 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002460 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002461 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2462 * If not assigned, perhaps we are balancing on url_param, but this is a
2463 * POST; and the parameters are in the body, maybe scan there to find our server.
2464 * (unless headers overflowed the buffer?)
2465 */
2466 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2467 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002468 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002469 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2470 /* are there enough bytes here? total == l || r || rlim ?
2471 * len is unsigned, but eoh is int,
2472 * how many bytes of body have we received?
2473 * eoh is the first empty line of the header
2474 */
2475 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002476 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002477
2478 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2479 * We can't assume responsibility for the server's decision,
2480 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2481 * We also can't change our mind later, about which server to choose, so round robin.
2482 */
2483 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2484 struct hdr_ctx ctx;
2485 ctx.idx = 0;
2486 /* Expect is allowed in 1.1, look for it */
2487 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2488 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002489 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002490 /* We can't reliablly stall and wait for data, because of
2491 * .NET clients that don't conform to rfc2616; so, no need for
2492 * the next block to check length expectations.
2493 * We could send 100 status back to the client, but then we need to
2494 * re-write headers, and send the message. And this isn't the right
2495 * place for that action.
2496 * TODO: support Expect elsewhere and delete this block.
2497 */
2498 goto end_check_maybe_wait_for_body;
2499 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002500
2501 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002502 /* nothing to do, we got enough */
2503 } else {
2504 /* limit implies we are supposed to need this many bytes
2505 * to find the parameter. Let's see how many bytes we can wait for.
2506 */
2507 long long hint = len;
2508 struct hdr_ctx ctx;
2509 ctx.idx = 0;
2510 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002511 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2512 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002513 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002514 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002515 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002516 ctx.idx = 0;
2517 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2518 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002519 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002520 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002521 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002522 hint = 0; /* parse failure, untrusted client */
2523 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002524 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002525 msg->hdr_content_len = hint;
2526 else
2527 hint = 0; /* bad client, sent negative length */
2528 }
2529 }
2530 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002531 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002532 hint = t->be->url_param_post_limit;
2533 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002534 if (len < hint) {
2535 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002536 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002537 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002538 /* else... There are no body bytes to wait for */
2539 }
2540 }
2541 }
2542 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002543
Willy Tarreau2a324282006-12-05 00:05:46 +01002544 /*************************************************************
2545 * OK, that's finished for the headers. We have done what we *
2546 * could. Let's switch to the DATA state. *
2547 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002548
Willy Tarreaue393fe22008-08-16 22:18:07 +02002549 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002550 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002551
Willy Tarreau1fa31262007-12-03 00:36:16 +01002552 /* When a connection is tarpitted, we use the tarpit timeout,
2553 * which may be the same as the connect timeout if unspecified.
2554 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002555 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002556 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002557 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002558 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002559 /* flush the request so that we can drop the connection early
2560 * if the client closes first.
2561 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002562 req->flags &= ~BF_MAY_FORWARD;
2563 req->analysers |= AN_REQ_HTTP_TARPIT;
2564 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2565 if (!req->analyse_exp)
2566 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002569 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002570 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002571
2572 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002573 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002574 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002575 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002576 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002577 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002578 return_prx_cond:
2579 if (!(t->flags & SN_ERR_MASK))
2580 t->flags |= SN_ERR_PRXCOND;
2581 if (!(t->flags & SN_FINST_MASK))
2582 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002583 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002584 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002585 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002586 }
2587
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002588 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2589 struct http_txn *txn = &t->txn;
2590
2591 /* This connection is being tarpitted. The CLIENT side has
2592 * already set the connect expiration date to the right
2593 * timeout. We just have to check that the client is still
2594 * there and that the timeout has not expired.
2595 */
2596 if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 &&
2597 !tick_is_expired(req->analyse_exp, now_ms))
2598 return 0;
2599
2600 /* We will set the queue timer to the time spent, just for
2601 * logging purposes. We fake a 500 server error, so that the
2602 * attacker will not suspect his connection has been tarpitted.
2603 * It will not cause trouble to the logs because we can exclude
2604 * the tarpitted connections by filtering on the 'PT' status flags.
2605 */
2606 trace_term(t, TT_HTTP_SRV_2);
2607 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2608
2609 txn->status = 500;
2610 if (req->flags != BF_READ_ERROR)
2611 client_retnclose(t, error_message(t, HTTP_ERR_500));
2612
2613 req->analysers = 0;
2614 req->analyse_exp = TICK_ETERNITY;
2615
2616 t->fe->failed_req++;
2617 if (!(t->flags & SN_ERR_MASK))
2618 t->flags |= SN_ERR_PRXCOND;
2619 if (!(t->flags & SN_FINST_MASK))
2620 t->flags |= SN_FINST_T;
2621 return 0;
2622 }
2623
Willy Tarreau2df28e82008-08-17 15:20:19 +02002624 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002625 /* We have to parse the HTTP request body to find any required data.
2626 * "balance url_param check_post" should have been the only way to get
2627 * into this. We were brought here after HTTP header analysis, so all
2628 * related structures are ready.
2629 */
2630 struct http_msg *msg = &t->txn.req;
2631 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2632 long long limit = t->be->url_param_post_limit;
2633 struct hdr_ctx ctx;
2634
2635 ctx.idx = 0;
2636
2637 /* now if we have a length, we'll take the hint */
2638 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2639 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2640 unsigned int chunk = 0;
2641 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2642 char c = msg->sol[body];
2643 if (ishex(c)) {
2644 unsigned int hex = toupper(c) - '0';
2645 if (hex > 9)
2646 hex -= 'A' - '9' - 1;
2647 chunk = (chunk << 4) | hex;
2648 } else
2649 break;
2650 body++;
2651 }
2652 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002653 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002654
2655 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002656 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002657
2658 body += 2; // skip CRLF
2659
2660 /* if we support more then one chunk here, we have to do it again when assigning server
2661 * 1. how much entity data do we have? new var
2662 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2663 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2664 */
2665
2666 if (chunk < limit)
2667 limit = chunk; /* only reading one chunk */
2668 } else {
2669 if (msg->hdr_content_len < limit)
2670 limit = msg->hdr_content_len;
2671 }
2672
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002673 http_body_end:
2674 /* we leave once we know we have nothing left to do. This means that we have
2675 * enough bytes, or that we know we'll not get any more (buffer full, read
2676 * buffer closed).
2677 */
2678 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002679 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002680 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002681 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002682 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002683 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002684 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002685 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002686 else {
2687 /* Not enough data. We'll re-use the http-request
2688 * timeout here. Ideally, we should set the timeout
2689 * relative to the accept() date. We just set the
2690 * request timeout once at the beginning of the
2691 * request.
2692 */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002693 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc52164a2008-08-17 19:17:57 +02002694 if (!tick_isset(req->analyse_exp))
2695 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2696 return 0;
2697 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002698 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002699
Willy Tarreau2df28e82008-08-17 15:20:19 +02002700 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2701 * probably reduce one day's debugging session.
2702 */
2703#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002704 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002705 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2706 __FILE__, __LINE__, req->analysers);
2707 ABORT_NOW();
2708 }
2709#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002710 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002711 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002712}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002714/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002715 * It normally returns zero, but may return 1 if it absolutely needs to be
2716 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002717 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002718 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002719 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002720int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002721{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002722 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002723 struct buffer *req = t->req;
2724 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002725
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002726 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
2727 now_ms, __FUNCTION__,
2728 cli_stnames[t->cli_state],
Willy Tarreau2df28e82008-08-17 15:20:19 +02002729 req->rex, rep->wex, req->flags, rep->flags, rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002730
Willy Tarreau2df28e82008-08-17 15:20:19 +02002731 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002732 /*
2733 * Now parse the partial (or complete) lines.
2734 * We will check the response syntax, and also join multi-line
2735 * headers. An index of all the lines will be elaborated while
2736 * parsing.
2737 *
2738 * For the parsing, we use a 28 states FSM.
2739 *
2740 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002741 * rep->data + rep->som = beginning of response
2742 * rep->data + rep->eoh = end of processed headers / start of current one
2743 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002744 * rep->lr = first non-visited byte
2745 * rep->r = end of data
2746 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002747
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002748 int cur_idx;
2749 struct http_msg *msg = &txn->rsp;
2750 struct proxy *cur_proxy;
2751
2752 if (likely(rep->lr < rep->r))
2753 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2754
2755 /* 1: we might have to print this header in debug mode */
2756 if (unlikely((global.mode & MODE_DEBUG) &&
2757 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2758 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2759 char *eol, *sol;
2760
2761 sol = rep->data + msg->som;
2762 eol = sol + msg->sl.rq.l;
2763 debug_hdr("srvrep", t, sol, eol);
2764
2765 sol += hdr_idx_first_pos(&txn->hdr_idx);
2766 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2767
2768 while (cur_idx) {
2769 eol = sol + txn->hdr_idx.v[cur_idx].len;
2770 debug_hdr("srvhdr", t, sol, eol);
2771 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2772 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002776 /*
2777 * Now we quickly check if we have found a full valid response.
2778 * If not so, we check the FD and buffer states before leaving.
2779 * A full response is indicated by the fact that we have seen
2780 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2781 * responses are checked first.
2782 *
2783 * Depending on whether the client is still there or not, we
2784 * may send an error response back or not. Note that normally
2785 * we should only check for HTTP status there, and check I/O
2786 * errors somewhere else.
2787 */
2788
2789 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002790 /* Invalid response */
2791 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2792 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002793 //buffer_shutr(rep);
2794 //buffer_shutw(req);
2795 //fd_delete(req->cons->fd);
2796 //req->cons->state = SI_ST_CLO;
2797 buffer_shutr_now(rep);
2798 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002799 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002800 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002801 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002802 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002803 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002804 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002805 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002806 txn->status = 502;
2807 client_return(t, error_message(t, HTTP_ERR_502));
2808 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002809 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002810 if (!(t->flags & SN_FINST_MASK))
2811 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002812
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002813 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2814 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002815
Willy Tarreaudafde432008-08-17 01:00:46 +02002816 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002818 /* write error to client, read error or close from server */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002819 if (rep->flags & (BF_WRITE_ERROR|BF_SHUTW|BF_READ_ERROR|BF_SHUTR|BF_READ_NULL)) {
2820 buffer_shutr_now(rep);
2821 buffer_shutw_now(req);
2822 //fd_delete(req->cons->fd);
2823 //req->cons->state = SI_ST_CLO;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002824 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002825 //t->srv->cur_sess--;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002826 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002827 //sess_change_server(t, NULL);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002828 }
2829 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002830 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002831 txn->status = 502;
2832 client_return(t, error_message(t, HTTP_ERR_502));
2833 if (!(t->flags & SN_ERR_MASK))
2834 t->flags |= SN_ERR_SRVCL;
2835 if (!(t->flags & SN_FINST_MASK))
2836 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002837
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002838 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2839 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002840
Willy Tarreaudafde432008-08-17 01:00:46 +02002841 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002842 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002843 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002844 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002845 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002846 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002847 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002848 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002849 buffer_shutr_now(rep);
2850 buffer_shutw_now(req);
2851 //fd_delete(req->cons->fd);
2852 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002853 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002854 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002855 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002856 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002857 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002858 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002859 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002860 txn->status = 504;
2861 client_return(t, error_message(t, HTTP_ERR_504));
2862 if (!(t->flags & SN_ERR_MASK))
2863 t->flags |= SN_ERR_SRVTO;
2864 if (!(t->flags & SN_FINST_MASK))
2865 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002866
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002867 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2868 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002869 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870 }
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002871
2872 rep->flags &= ~BF_MAY_FORWARD;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002873 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002874 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreau21d2af32008-02-14 20:25:24 +01002876
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002877 /*****************************************************************
2878 * More interesting part now : we know that we have a complete *
2879 * response which at least looks like HTTP. We have an indicator *
2880 * of each header's length, so we can parse them quickly. *
2881 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002882
Willy Tarreau2df28e82008-08-17 15:20:19 +02002883 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 /* ensure we keep this pointer to the beginning of the message */
2886 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002887
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002888 /*
2889 * 1: get the status code and check for cacheability.
2890 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002891
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002892 t->logs.logwait &= ~LW_RESP;
2893 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002894
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002895 switch (txn->status) {
2896 case 200:
2897 case 203:
2898 case 206:
2899 case 300:
2900 case 301:
2901 case 410:
2902 /* RFC2616 @13.4:
2903 * "A response received with a status code of
2904 * 200, 203, 206, 300, 301 or 410 MAY be stored
2905 * by a cache (...) unless a cache-control
2906 * directive prohibits caching."
2907 *
2908 * RFC2616 @9.5: POST method :
2909 * "Responses to this method are not cacheable,
2910 * unless the response includes appropriate
2911 * Cache-Control or Expires header fields."
2912 */
2913 if (likely(txn->meth != HTTP_METH_POST) &&
2914 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2915 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2916 break;
2917 default:
2918 break;
2919 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002921 /*
2922 * 2: we may need to capture headers
2923 */
2924 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2925 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2926 txn->rsp.cap, t->fe->rsp_cap);
2927
2928 /*
2929 * 3: we will have to evaluate the filters.
2930 * As opposed to version 1.2, now they will be evaluated in the
2931 * filters order and not in the header order. This means that
2932 * each filter has to be validated among all headers.
2933 *
2934 * Filters are tried with ->be first, then with ->fe if it is
2935 * different from ->be.
2936 */
2937
2938 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2939
2940 cur_proxy = t->be;
2941 while (1) {
2942 struct proxy *rule_set = cur_proxy;
2943
2944 /* try headers filters */
2945 if (rule_set->rsp_exp != NULL) {
2946 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2947 return_bad_resp:
2948 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002949 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002950 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002951 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002952 }
2953 cur_proxy->failed_resp++;
2954 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002955 buffer_shutr_now(rep);
2956 buffer_shutw_now(req);
2957 //fd_delete(req->cons->fd);
2958 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002959 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002960 txn->status = 502;
2961 client_return(t, error_message(t, HTTP_ERR_502));
2962 if (!(t->flags & SN_ERR_MASK))
2963 t->flags |= SN_ERR_PRXCOND;
2964 if (!(t->flags & SN_FINST_MASK))
2965 t->flags |= SN_FINST_H;
2966 /* We used to have a free connection slot. Since we'll never use it,
2967 * we have to inform the server that it may be used by another session.
2968 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002969 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2970 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002971 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002972 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002973 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002975 /* has the response been denied ? */
2976 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002977 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002978 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002979 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002980 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002981 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002982 cur_proxy->denied_resp++;
2983 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002986 /* We might have to check for "Connection:" */
2987 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2988 !(t->flags & SN_CONN_CLOSED)) {
2989 char *cur_ptr, *cur_end, *cur_next;
2990 int cur_idx, old_idx, delta, val;
2991 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002992
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002993 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2994 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002995
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002996 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2997 cur_hdr = &txn->hdr_idx.v[cur_idx];
2998 cur_ptr = cur_next;
2999 cur_end = cur_ptr + cur_hdr->len;
3000 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003002 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3003 if (val) {
3004 /* 3 possibilities :
3005 * - we have already set Connection: close,
3006 * so we remove this line.
3007 * - we have not yet set Connection: close,
3008 * but this line indicates close. We leave
3009 * it untouched and set the flag.
3010 * - we have not yet set Connection: close,
3011 * and this line indicates non-close. We
3012 * replace it.
3013 */
3014 if (t->flags & SN_CONN_CLOSED) {
3015 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3016 txn->rsp.eoh += delta;
3017 cur_next += delta;
3018 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3019 txn->hdr_idx.used--;
3020 cur_hdr->len = 0;
3021 } else {
3022 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3023 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3024 "close", 5);
3025 cur_next += delta;
3026 cur_hdr->len += delta;
3027 txn->rsp.eoh += delta;
3028 }
3029 t->flags |= SN_CONN_CLOSED;
3030 }
3031 }
3032 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003033 }
3034 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003036 /* add response headers from the rule sets in the same order */
3037 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3038 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3039 rule_set->rsp_add[cur_idx])) < 0)
3040 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003041 }
3042
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003043 /* check whether we're already working on the frontend */
3044 if (cur_proxy == t->fe)
3045 break;
3046 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003049 /*
3050 * 4: check for server cookie.
3051 */
3052 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3053 || (t->be->options & PR_O_CHK_CACHE))
3054 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003055
Willy Tarreaubaaee002006-06-26 02:48:02 +02003056
Willy Tarreaua15645d2007-03-18 16:22:39 +01003057 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003058 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003059 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003060 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3061 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003062
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003063 /*
3064 * 6: add server cookie in the response if needed
3065 */
3066 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3067 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3068 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003069
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003070 /* the server is known, it's not the one the client requested, we have to
3071 * insert a set-cookie here, except if we want to insert only on POST
3072 * requests and this one isn't. Note that servers which don't have cookies
3073 * (eg: some backup servers) will return a full cookie removal request.
3074 */
3075 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3076 t->be->cookie_name,
3077 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003078
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003079 if (t->be->cookie_domain)
3080 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003081
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003082 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3083 trash, len)) < 0)
3084 goto return_bad_resp;
3085 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 /* Here, we will tell an eventual cache on the client side that we don't
3088 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3089 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3090 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3091 */
3092 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003094 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3095
3096 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3097 "Cache-control: private", 22)) < 0)
3098 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003099 }
3100 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101
Willy Tarreaubaaee002006-06-26 02:48:02 +02003102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003103 /*
3104 * 7: check if result will be cacheable with a cookie.
3105 * We'll block the response if security checks have caught
3106 * nasty things such as a cacheable cookie.
3107 */
3108 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3109 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3110 (t->be->options & PR_O_CHK_CACHE)) {
3111
3112 /* we're in presence of a cacheable response containing
3113 * a set-cookie header. We'll block it as requested by
3114 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003115 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003116 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003117 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003118 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003119 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003120 }
3121 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003122
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003123 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3124 t->be->id, t->srv?t->srv->id:"<dispatch>");
3125 send_log(t->be, LOG_ALERT,
3126 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3127 t->be->id, t->srv?t->srv->id:"<dispatch>");
3128 goto return_srv_prx_502;
3129 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003130
3131 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003132 * 8: add "Connection: close" if needed and not yet set.
3133 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003134 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 if (!(t->flags & SN_CONN_CLOSED) &&
3136 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3137 if ((unlikely(msg->sl.st.v_l != 8) ||
3138 unlikely(req->data[msg->som + 7] != '0')) &&
3139 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3140 "Connection: close", 17)) < 0)
3141 goto return_bad_resp;
3142 t->flags |= SN_CONN_CLOSED;
3143 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003144
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003145 /*************************************************************
3146 * OK, that's finished for the headers. We have done what we *
3147 * could. Let's switch to the DATA state. *
3148 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003149
Willy Tarreaue393fe22008-08-16 22:18:07 +02003150 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003151 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003152
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003153#ifdef CONFIG_HAP_TCPSPLICE
3154 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3155 /* TCP splicing supported by both FE and BE */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003156 tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003157 }
3158#endif
3159 /* if the user wants to log as soon as possible, without counting
3160 * bytes from the server, then this is the right moment. We have
3161 * to temporarily assign bytes_out to log what we currently have.
3162 */
3163 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3164 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3165 t->logs.bytes_out = txn->rsp.eoh;
3166 if (t->fe->to_log & LW_REQ)
3167 http_sess_log(t);
3168 else
3169 tcp_sess_log(t);
3170 t->logs.bytes_out = 0;
3171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003173 /* Note: we must not try to cheat by jumping directly to DATA,
3174 * otherwise we would not let the client side wake up.
3175 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003176
Willy Tarreaudafde432008-08-17 01:00:46 +02003177 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003178 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003179
Willy Tarreau2df28e82008-08-17 15:20:19 +02003180 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3181 * probably reduce one day's debugging session.
3182 */
3183#ifdef DEBUG_DEV
3184 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3185 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3186 __FILE__, __LINE__, rep->analysers);
3187 ABORT_NOW();
3188 }
3189#endif
3190 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003191 return 0;
3192}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003193
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003194/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003195 * Manages the client FSM and its socket. It normally returns zero, but may
3196 * return 1 if it absolutely wants to be called again.
3197 *
3198 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003199 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003200 */
3201int process_cli(struct session *t)
3202{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003203 struct buffer *req = t->req;
3204 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003205
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003206 DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3207 now_ms, __FUNCTION__,
3208 t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3209 cli_stnames[t->cli_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003210 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3211 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003212 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003213 req->flags, rep->flags,
3214 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003215
Willy Tarreaudafde432008-08-17 01:00:46 +02003216 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003217 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3218 * still set this state (and will do until unix sockets are converted).
3219 */
3220 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003221 /* we can skip most of the tests at once if some conditions are not met */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003222 if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3223 (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3224 (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003225 (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3226 (!(rep->flags & BF_SHUTW) &&
3227 (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3228 goto update_timeouts;
3229
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003230 /* read or write error */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003231 if (fdtab[t->cli_fd].state == FD_STERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003232 buffer_shutr(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003233 req->flags |= BF_READ_ERROR;
Willy Tarreauba392ce2008-08-16 21:13:23 +02003234 buffer_shutw(rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003235 rep->flags |= BF_WRITE_ERROR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003236 fd_delete(t->cli_fd);
3237 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003238 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003239 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003240 if (!(t->flags & SN_ERR_MASK))
3241 t->flags |= SN_ERR_CLICL;
3242 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003243 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003244 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003245 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003246 t->flags |= SN_FINST_C;
3247 else
3248 t->flags |= SN_FINST_D;
3249 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003250 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003251 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003252 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003253 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003254 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003255 req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003256 buffer_shutr(req);
3257 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003258 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003259 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003260 } else {
3261 /* output was already closed */
3262 fd_delete(t->cli_fd);
3263 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003264 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003265 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003266 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003267 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003268 /* last server read and buffer empty : we only check them when we're
3269 * allowed to forward the data.
3270 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003271 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003272 ((rep->flags & BF_SHUTW_NOW) ||
3273 (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3274 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003275 buffer_shutw(rep);
3276 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003277 EV_FD_CLR(t->cli_fd, DIR_WR);
3278 shutdown(t->cli_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003279 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003280 } else {
3281 fd_delete(t->cli_fd);
3282 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003283 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003284 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003285 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003286 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003287 /* read timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003288 else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003289 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003290 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003291 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003292 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003293 } else {
3294 /* output was already closed */
3295 fd_delete(t->cli_fd);
3296 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003297 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003298 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003299 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003300 if (!(t->flags & SN_ERR_MASK))
3301 t->flags |= SN_ERR_CLITO;
3302 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003303 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003304 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003305 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003306 t->flags |= SN_FINST_C;
3307 else
3308 t->flags |= SN_FINST_D;
3309 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003310 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003311 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003312 }
3313 /* write timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003314 else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003315 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003316 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003317 EV_FD_CLR(t->cli_fd, DIR_WR);
3318 shutdown(t->cli_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003319 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003320 } else {
3321 fd_delete(t->cli_fd);
3322 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003323 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003324 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003325 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003326 if (!(t->flags & SN_ERR_MASK))
3327 t->flags |= SN_ERR_CLITO;
3328 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003329 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003330 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003331 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003332 t->flags |= SN_FINST_C;
3333 else
3334 t->flags |= SN_FINST_D;
3335 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003336 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003337 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003338 }
3339
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003340 update_timeouts:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003341 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003342 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003343 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003344 /* no room to read more data */
3345 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3346 /* stop reading until we get some space */
3347 req->rex = TICK_ETERNITY;
3348 }
3349 } else {
3350 EV_FD_COND_S(t->cli_fd, DIR_RD);
3351 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3352 }
3353 }
3354
3355 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003356 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003357 /* first, we may have to produce data (eg: stats).
3358 * right now, this is limited to the SHUTR state.
3359 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003360 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003361 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003362 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003363 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003364 fd_delete(t->cli_fd);
3365 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003366 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003367 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003368 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003369 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003370
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003371 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003372 if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3374 /* stop writing */
3375 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003376 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003377 } else {
3378 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003379 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003380 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003381 /* restart writing */
3382 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003383 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003384 /* FIXME: to prevent the client from expiring read timeouts during writes,
3385 * we refresh it, except if it was already infinite. */
3386 req->rex = rep->wex;
3387 }
3388 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003389 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003390 }
3391 return 0; /* other cases change nothing */
3392 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003393 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003394 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3395 int len;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003396 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003397 write(1, trash, len);
3398 }
3399 return 0;
3400 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003401#ifdef DEBUG_DEV
3402 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3403 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003404#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003405 return 0;
3406}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003407
Willy Tarreaubaaee002006-06-26 02:48:02 +02003408
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003409/* Return 1 if the pending connection has failed and should be retried,
3410 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3411 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003412 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003413int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003414{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003415 struct buffer *req = t->req;
3416 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003417 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003418
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003419 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3420 now_ms, __FUNCTION__,
3421 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003422 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003423 req->flags, rep->flags,
3424 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003425
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003426 if ((req->flags & BF_SHUTW_NOW) ||
3427 (rep->flags & BF_SHUTW) ||
3428 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3429 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
3430 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3431
3432 trace_term(t, TT_HTTP_SRV_5);
3433 req->wex = TICK_ETERNITY;
3434 fd_delete(req->cons->fd);
3435 if (t->srv) {
3436 t->srv->cur_sess--;
3437 sess_change_server(t, NULL);
3438 }
3439 /* note that this must not return any error because it would be able to
3440 * overwrite the client_retnclose() output.
3441 */
3442 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3443
3444 // FIXME: should we set rep->MAY_FORWARD ?
3445 buffer_shutw(req);
3446 buffer_shutr(rep);
3447 req->cons->state = SI_ST_CLO;
3448 if (!req->cons->err_type)
3449 req->cons->err_type = SI_ET_CONN_ABRT;
3450 req->cons->err_loc = t->srv;
3451 return 0;
3452 }
3453
3454 /* check for timeouts and asynchronous connect errors */
3455 if (fdtab[req->cons->fd].state == FD_STERROR) {
3456 conn_err = SI_ET_CONN_ERR;
3457 if (!req->cons->err_type)
3458 req->cons->err_type = SI_ET_CONN_ERR;
3459 }
3460 else if (!(req->flags & BF_WRITE_STATUS)) {
3461 /* nothing happened, maybe we timed out */
3462 if (tick_is_expired(req->wex, now_ms)) {
3463 conn_err = SI_ET_CONN_TO;
3464 if (!req->cons->err_type)
3465 req->cons->err_type = SI_ET_CONN_TO;
3466 }
3467 else
3468 return 0; /* let's wait a bit more */
3469 }
3470
3471 if (conn_err) {
3472 fd_delete(req->cons->fd);
3473 req->cons->state = SI_ST_CLO;
3474
3475 if (t->srv) {
3476 t->srv->cur_sess--;
3477 sess_change_server(t, NULL);
3478 req->cons->err_loc = t->srv;
3479 }
3480
3481 /* ensure that we have enough retries left */
3482 if (srv_count_retry_down(t, conn_err))
3483 return 0;
3484
3485 if (conn_err == SI_ET_CONN_ERR) {
3486 /* we encountered an immediate connection error, and we
3487 * will have to retry connecting to the same server, most
3488 * likely leading to the same result. To avoid this, we
3489 * fake a connection timeout to retry after a turn-around
3490 * time of 1 second. We will wait in the previous if block.
3491 */
3492 req->cons->state = SI_ST_TAR;
3493 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3494 return 0;
3495 }
3496
3497 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3498 /* We're on our last chance, and the REDISP option was specified.
3499 * We will ignore cookie and force to balance or use the dispatcher.
3500 */
3501 /* let's try to offer this slot to anybody */
3502 if (may_dequeue_tasks(t->srv, t->be))
3503 process_srv_queue(t->srv);
3504
3505 /* it's left to the dispatcher to choose a server */
3506 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3507 t->prev_srv = t->srv;
3508 } else {
3509 /* we just want to retry */
3510 if (t->srv)
3511 t->srv->retries++;
3512 t->be->retries++;
3513
3514 /* Now we will try to either reconnect to the same server or
3515 * connect to another server. If the connection gets queued
3516 * because all servers are saturated, then we will go back to
3517 * the idle state where the buffer's consumer is marked as
3518 * unknown.
3519 */
3520 if (srv_retryable_connect(t)) {
3521 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003522 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003523 return 0;
3524 }
3525 }
3526
3527 /* We'll rely on the caller to try to get a connection again */
3528 return 1;
3529 }
3530 else {
3531 /* no error and write OK : connection succeeded */
3532 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3533 req->cons->state = SI_ST_EST;
3534 req->cons->err_type = SI_ET_NONE;
3535 req->cons->err_loc = NULL;
3536
3537 if (req->flags & BF_EMPTY) {
3538 EV_FD_CLR(req->cons->fd, DIR_WR);
3539 req->wex = TICK_ETERNITY;
3540 } else {
3541 EV_FD_SET(req->cons->fd, DIR_WR);
3542 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3543 if (tick_isset(req->wex)) {
3544 /* FIXME: to prevent the server from expiring read timeouts during writes,
3545 * we refresh it. */
3546 rep->rex = req->wex;
3547 }
3548 }
3549
3550 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3551 if (!(rep->flags & BF_HIJACK)) {
3552 EV_FD_SET(req->cons->fd, DIR_RD);
3553 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3554 }
3555 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3556
3557 /* if the user wants to log as soon as possible, without counting
3558 bytes from the server, then this is the right moment. */
3559 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3560 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3561 tcp_sess_log(t);
3562 }
3563#ifdef CONFIG_HAP_TCPSPLICE
3564 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3565 /* TCP splicing supported by both FE and BE */
3566 tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
3567 }
3568#endif
3569 }
3570 else {
3571 rep->analysers |= AN_RTR_HTTP_HDR;
3572 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3573 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3574 /* reset hdr_idx which was already initialized by the request.
3575 * right now, the http parser does it.
3576 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003577 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003578 }
3579
3580 if (!rep->analysers)
3581 t->rep->flags |= BF_MAY_FORWARD;
3582 req->wex = TICK_ETERNITY;
3583 return 0;
3584 }
3585}
3586
3587
3588/*
3589 * This function tries to assign a server to a stream_sock interface.
3590 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3591 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3592 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3593 * it returns SI_ST_ASS, otherwise zero.
3594 */
3595int stream_sock_assign_server(struct session *t)
3596{
3597 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3598 now_ms, __FUNCTION__,
3599 cli_stnames[t->cli_state],
3600 t->rep->rex, t->req->wex,
3601 t->req->flags, t->rep->flags,
3602 t->req->l, t->rep->l);
3603
3604 if (t->req->cons->state == SI_ST_TAR) {
3605 /* connection might be aborted */
3606 if ((t->req->flags & BF_SHUTW_NOW) ||
3607 (t->rep->flags & BF_SHUTW) ||
3608 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3609 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003610
Willy Tarreauf8533202008-08-16 14:55:08 +02003611 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003612 t->req->wex = TICK_ETERNITY;
3613
3614 // FIXME: should we set rep->MAY_FORWARD ?
3615 buffer_shutr(t->rep);
3616 buffer_shutw(t->req);
3617 if (!t->req->cons->err_type)
3618 t->req->cons->err_type = SI_ET_CONN_ABRT;
3619 t->req->cons->state = SI_ST_CLO;
3620 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003621 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003622
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003623 if (!tick_is_expired(t->req->wex, now_ms))
3624 return 0; /* still in turn-around */
3625
3626 t->req->cons->state = SI_ST_INI;
3627 }
3628 else if (t->req->cons->state == SI_ST_QUE) {
3629 if (t->pend_pos) {
3630 /* request still in queue... */
3631 if (tick_is_expired(t->req->wex, now_ms)) {
3632 /* ... and timeout expired */
3633 trace_term(t, TT_HTTP_SRV_3);
3634 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003635 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003636 if (t->srv)
3637 t->srv->failed_conns++;
3638 t->be->failed_conns++;
3639
3640 // FIXME: should we set rep->MAY_FORWARD ?
3641 buffer_shutr(t->rep);
3642 buffer_shutw(t->req);
3643 t->req->flags |= BF_WRITE_TIMEOUT;
3644 if (!t->req->cons->err_type)
3645 t->req->cons->err_type = SI_ET_QUEUE_TO;
3646 t->req->cons->state = SI_ST_CLO;
3647 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003648 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003649 /* connection remains in queue, check if we have to abort it */
3650 if ((t->req->flags & BF_SHUTW_NOW) ||
3651 (t->rep->flags & BF_SHUTW) ||
3652 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3653 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3654 /* give up */
3655 trace_term(t, TT_HTTP_SRV_1);
3656 t->req->wex = TICK_ETERNITY;
3657 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003658
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003659 // FIXME: should we set rep->MAY_FORWARD ?
3660 buffer_shutr(t->rep);
3661 buffer_shutw(t->req);
3662 if (!t->req->cons->err_type)
3663 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3664 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003665 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003666 return 0;
3667 }
3668 /* The connection is not in the queue anymore */
3669 t->req->cons->state = SI_ST_INI;
3670 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003671
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003672 /* we may get here from above */
3673 if (t->req->cons->state == SI_ST_INI) {
3674 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003675
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003676 /* first, check if the connection has been aborted */
3677 if ((t->req->flags & BF_SHUTW_NOW) ||
3678 (t->rep->flags & BF_SHUTW) ||
3679 ((t->req->flags & BF_SHUTR) &&
3680 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003681
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003682 trace_term(t, TT_HTTP_SRV_1);
3683 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003684
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003685 // FIXME: should we set rep->MAY_FORWARD ?
3686 buffer_shutr(t->rep);
3687 buffer_shutw(t->req);
3688 if (!t->req->cons->err_type)
3689 t->req->cons->err_type = SI_ET_CONN_ABRT;
3690 t->req->cons->state = SI_ST_CLO;
3691 return 0;
3692 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003693
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003694 /* try to get a server assigned */
3695 if (srv_redispatch_connect(t) != 0) {
3696 /* we did not get any server, let's check the cause */
3697 if (t->req->cons->state == SI_ST_QUE) {
3698 /* the connection was queued, that's OK */
3699 return 0;
3700 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003701
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003702 trace_term(t, TT_HTTP_SRV_2);
3703 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003704
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003705 // FIXME: should we set rep->MAY_FORWARD ?
3706 buffer_shutr(t->rep);
3707 buffer_shutw(t->req);
3708 t->req->flags |= BF_WRITE_ERROR;
3709 if (!t->req->cons->err_type)
3710 t->req->cons->err_type = SI_ET_CONN_OTHER;
3711 t->req->cons->state = SI_ST_CLO;
3712 return 0;
3713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003714
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003715 t->req->cons->state = SI_ST_ASS;
3716 /* Once the server is assigned, we have to return because
3717 * the caller might be interested in checking several
3718 * things before connecting.
3719 */
3720 return 1;
3721 }
3722 return 0;
3723}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003724
Willy Tarreauf8533202008-08-16 14:55:08 +02003725
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003726/*
3727 * This function tries to establish a connection to an assigned server. It also
3728 * performs connection retries. It may only be called with t->req->cons->state
3729 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3730 * SI_ST_EST, or SI_ST_CLO.
3731 */
3732int stream_sock_connect_server(struct session *t)
3733{
3734 if (t->req->cons->state == SI_ST_ASS) {
3735 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003737 if (!srv_retryable_connect(t)) {
3738 /* we need to redispatch */
3739 t->req->cons->state = SI_ST_INI;
3740 return 0;
3741 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003742
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003743 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3744 if (t->req->cons->state != SI_ST_CON) {
3745 /* it was an error */
3746 trace_term(t, TT_HTTP_SRV_4);
3747 t->req->wex = TICK_ETERNITY;
3748
3749 // FIXME: should we set rep->MAY_FORWARD ?
3750 buffer_shutr(t->rep);
3751 buffer_shutw(t->req);
3752 t->req->flags |= BF_WRITE_ERROR;
3753 if (!t->req->cons->err_type)
3754 t->req->cons->err_type = SI_ET_CONN_OTHER;
3755 t->req->cons->state = SI_ST_CLO;
3756 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003757 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003758 /* We have a socket and switched to SI_ST_CON */
3759 }
3760
3761 /* we may also get here from above */
3762 if (t->req->cons->state == SI_ST_CON) {
3763 /* connection in progress or just completed */
3764 if (!tcp_connection_status(t))
3765 return 0;
3766 }
3767 return 0;
3768}
3769
3770
3771/*
3772 * Tries to establish a connection to the server and associate it to the
3773 * request buffer's consumer side. It is assumed that this function will not be
3774 * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally
3775 * returns zero, but may return 1 if it absolutely wants to be called again.
3776 */
3777int process_srv_conn(struct session *t)
3778{
3779 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3780 now_ms, __FUNCTION__,
3781 cli_stnames[t->cli_state],
3782 t->rep->rex, t->req->wex,
3783 t->req->flags, t->rep->flags,
3784 t->req->l, t->rep->l);
3785
3786 do {
3787 if (t->req->cons->state == SI_ST_INI ||
3788 t->req->cons->state == SI_ST_TAR ||
3789 t->req->cons->state == SI_ST_QUE) {
3790 /* try to assign a server */
3791 if (!stream_sock_assign_server(t))
3792 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003793 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003794
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003795 if (t->req->cons->state == SI_ST_ASS &&
3796 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3797 /* Server supporting redirection and it is possible.
3798 * Invalid requests are reported as such. It concerns all
3799 * the largest ones.
3800 */
3801 struct http_txn *txn = &t->txn;
3802 struct chunk rdr;
3803 char *path;
3804 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003805
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003806 /* 1: create the response header */
3807 rdr.len = strlen(HTTP_302);
3808 rdr.str = trash;
3809 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003810
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003811 /* 2: add the server's prefix */
3812 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3813 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003814
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003815 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3816 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003817
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003818 /* 3: add the request URI */
3819 path = http_get_path(txn);
3820 if (!path)
3821 goto cancel_redir;
3822 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3823 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3824 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003825
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003826 memcpy(rdr.str + rdr.len, path, len);
3827 rdr.len += len;
3828 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3829 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003830
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003831 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3832 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003833
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003834 /* FIXME: we should increase a counter of redirects per server and per backend. */
3835 if (t->srv)
3836 t->srv->cum_sess++;
3837
3838 t->req->cons->state = SI_ST_CLO;
3839 return 0;
3840 cancel_redir:
3841 //txn->status = 400;
3842 //t->fe->failed_req++;
3843 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3844 // 400, error_message(t, HTTP_ERR_400));
3845 trace_term(t, TT_HTTP_SRV_4);
3846
3847 // FIXME: should we set rep->MAY_FORWARD ?
3848 buffer_shutw(t->req);
3849 buffer_shutr(t->rep);
3850 if (!t->req->cons->err_type)
3851 t->req->cons->err_type = SI_ET_CONN_OTHER;
3852 t->req->cons->state = SI_ST_CLO;
3853 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003854 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003855
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003856 if (t->req->cons->state == SI_ST_CON ||
3857 t->req->cons->state == SI_ST_ASS) {
3858 stream_sock_connect_server(t);
3859 }
3860 } while (t->req->cons->state != SI_ST_CLO &&
3861 t->req->cons->state != SI_ST_CON &&
3862 t->req->cons->state != SI_ST_EST);
3863 return 0;
3864}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003865
Willy Tarreaubaaee002006-06-26 02:48:02 +02003866
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003867/*
3868 * Manages the server FSM and its socket during the DATA phase. It must not be
3869 * called when a file descriptor is not attached to the buffer. It must only be
3870 * called during SI_ST_EST. It normally returns zero, but may return 1 if it
3871 * absolutely wants to be called again.
3872 */
3873int process_srv_data(struct session *t)
3874{
3875 struct buffer *req = t->req;
3876 struct buffer *rep = t->rep;
3877 int fd = req->cons->fd;
Willy Tarreaudafde432008-08-17 01:00:46 +02003878
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003879 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3880 now_ms, __FUNCTION__,
3881 cli_stnames[t->cli_state],
3882 rep->rex, req->wex,
3883 req->flags, rep->flags,
3884 req->l, rep->l);
3885
3886 if (req->flags & (BF_WRITE_ERROR | BF_WRITE_TIMEOUT) ||
3887 rep->flags & (BF_READ_ERROR | BF_READ_TIMEOUT)) {
3888 /* nothing more to be done here */
3889 fprintf(stderr, "Hey what are you doing there? t=%p fd=%d state=%d\n",
3890 t, t->req->cons->fd, t->req->cons->state);
3891 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003892 }
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003893
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003894 /* we can skip most of the tests at once if some conditions are not met */
3895 /* FIXME: place req->BF_SHUTW_NOW here */
3896 //if (!((fdtab[fd].state == FD_STERROR) ||
3897 // (!(req->flags & BF_SHUTW) &&
3898 // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
3899 // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3900 // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW))))
3901 // goto update_timeouts;
3902
3903 /* read or write error */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003904 if (fdtab[fd].state == FD_STERROR) {
3905 trace_term(t, TT_HTTP_SRV_6);
3906 buffer_shutw(req);
3907 req->flags |= BF_WRITE_ERROR;
3908 buffer_shutr(rep);
3909 rep->flags |= BF_READ_ERROR;
3910 fd_delete(fd);
3911 req->cons->state = SI_ST_CLO;
Willy Tarreau376580a2008-08-27 18:52:22 +02003912 if (!req->cons->err_type) {
3913 req->cons->err_loc = t->srv;
3914 req->cons->err_type = SI_ET_DATA_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003915 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003916 return 0;
3917 }
3918
3919 /* last read, or end of client write */
3920 if (!(rep->flags & BF_SHUTR) && /* not already done */
3921 rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3922 buffer_shutr(rep);
Willy Tarreau376580a2008-08-27 18:52:22 +02003923 if (req->flags & BF_SHUTW) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003924 /* output was already closed */
3925 trace_term(t, TT_HTTP_SRV_8);
3926 fd_delete(fd);
3927 req->cons->state = SI_ST_CLO;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003928 return 0;
Willy Tarreau376580a2008-08-27 18:52:22 +02003929 } else {
3930 EV_FD_CLR(fd, DIR_RD);
3931 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003932 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003933 }
3934 /* end of client read and no more data to send. We can forward
3935 * the close when we're allowed to forward data (anytime right
3936 * now). If we're using option forceclose, then we may also
3937 * shutdown the outgoing write channel once the response starts
3938 * coming from the server.
3939 */
Willy Tarreau461f6622008-08-15 23:43:19 +02003940
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003941 if (!(req->flags & BF_SHUTW) && /* not already done */
3942 (req->flags & BF_SHUTW_NOW ||
Willy Tarreau376580a2008-08-27 18:52:22 +02003943 (req->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003944 buffer_shutw(req);
Willy Tarreau376580a2008-08-27 18:52:22 +02003945 if (rep->flags & BF_SHUTR) {
3946 /* input was already closed */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003947 trace_term(t, TT_HTTP_SRV_10);
3948 fd_delete(fd);
3949 req->cons->state = SI_ST_CLO;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003950 return 0;
Willy Tarreau376580a2008-08-27 18:52:22 +02003951 } else {
3952 trace_term(t, TT_HTTP_SRV_9);
3953 EV_FD_CLR(fd, DIR_WR);
3954 shutdown(fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003955 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003956 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003957
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003958 /* read timeout */
3959 if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == 0 &&
3960 tick_is_expired(rep->rex, now_ms)) {
3961 rep->flags |= BF_READ_TIMEOUT;
Willy Tarreau376580a2008-08-27 18:52:22 +02003962 if (!req->cons->err_type) {
3963 req->cons->err_loc = t->srv;
3964 req->cons->err_type = SI_ET_DATA_TO;
3965 }
3966
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003967 buffer_shutr(rep);
Willy Tarreau376580a2008-08-27 18:52:22 +02003968 if (req->flags & BF_SHUTW) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003969 trace_term(t, TT_HTTP_SRV_12);
3970 fd_delete(fd);
3971 req->cons->state = SI_ST_CLO;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003972 return 0;
Willy Tarreau376580a2008-08-27 18:52:22 +02003973 } else {
3974 trace_term(t, TT_HTTP_SRV_11);
3975 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003976 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003977 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003978
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003979 /* write timeout */
3980 if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == 0 &&
3981 tick_is_expired(req->wex, now_ms)) {
3982 req->flags |= BF_WRITE_TIMEOUT;
Willy Tarreau376580a2008-08-27 18:52:22 +02003983 if (!req->cons->err_type) {
3984 req->cons->err_loc = t->srv;
3985 req->cons->err_type = SI_ET_DATA_TO;
3986 }
3987
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003988 buffer_shutw(req);
Willy Tarreau376580a2008-08-27 18:52:22 +02003989 if (rep->flags & BF_SHUTR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003990 trace_term(t, TT_HTTP_SRV_14);
3991 fd_delete(fd);
3992 req->cons->state = SI_ST_CLO;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003993 return 0;
Willy Tarreau376580a2008-08-27 18:52:22 +02003994 } else {
3995 trace_term(t, TT_HTTP_SRV_13);
3996 EV_FD_CLR(fd, DIR_WR);
3997 shutdown(fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003998 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003999 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004000
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004001 update_timeouts:
4002 /* manage read timeout */
4003 if (!(rep->flags & BF_SHUTR)) {
4004 if (rep->flags & (BF_FULL|BF_HIJACK)) {
4005 if (EV_FD_COND_C(fd, DIR_RD))
4006 rep->rex = TICK_ETERNITY;
4007 } else {
4008 EV_FD_COND_S(fd, DIR_RD);
4009 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004010 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004011 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004012
4013 /* manage write timeout */
4014 if (!(req->flags & BF_SHUTW)) {
4015 if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
4016 /* stop writing */
4017 if (EV_FD_COND_C(fd, DIR_WR))
4018 req->wex = TICK_ETERNITY;
4019 } else {
4020 /* buffer not empty, there are still data to be transferred */
4021 EV_FD_COND_S(fd, DIR_WR);
4022 if (!tick_isset(req->wex)) {
4023 /* restart writing */
4024 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4025 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4026 /* FIXME: to prevent the server from expiring read timeouts during writes,
4027 * we refresh it, except if it was already infinite.
4028 */
4029 rep->rex = req->wex;
4030 }
4031 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004032 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004033 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004034 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02004035}
4036
4037
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004038///*
4039// * Manages the client FSM and its socket. It normally returns zero, but may
4040// * return 1 if it absolutely wants to be called again.
4041// *
4042// * Note: process_cli is the ONLY function allowed to set cli_state to anything
4043// * but CL_STCLOSE.
4044// */
4045//int process_cli(struct session *t)
4046//{
4047// struct buffer *req = t->req;
4048// struct buffer *rep = t->rep;
4049//
4050// DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4051// now_ms, __FUNCTION__,
4052// cli_stnames[t->cli_state],
4053// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
4054// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
4055// req->rex, rep->wex,
4056// req->flags, rep->flags,
4057// req->l, rep->l);
4058//
4059// update_state:
4060// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
4061// * still set this state (and will do until unix sockets are converted).
4062// */
4063// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
4064// /* we can skip most of the tests at once if some conditions are not met */
4065// if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4066// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4067// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
4068// (!(rep->flags & BF_SHUTW) &&
4069// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
4070// goto update_timeouts;
4071//
4072// /* read or write error */
4073// if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
4074// buffer_shutr(req);
4075// buffer_shutw(rep);
4076// fd_delete(t->cli_fd);
4077// t->cli_state = CL_STCLOSE;
4078// trace_term(t, TT_HTTP_CLI_1);
4079// if (!req->analysers) {
4080// if (!(t->flags & SN_ERR_MASK))
4081// t->flags |= SN_ERR_CLICL;
4082// if (!(t->flags & SN_FINST_MASK)) {
4083// if (t->pend_pos)
4084// t->flags |= SN_FINST_Q;
4085// else if (!(req->flags & BF_CONNECTED))
4086// t->flags |= SN_FINST_C;
4087// else
4088// t->flags |= SN_FINST_D;
4089// }
4090// }
4091// goto update_state;
4092// }
4093// /* last read, or end of server write */
4094// else if (!(req->flags & BF_SHUTR) && /* not already done */
4095// req->flags & (BF_READ_NULL | BF_SHUTW)) {
4096// buffer_shutr(req);
4097// if (!(rep->flags & BF_SHUTW)) {
4098// EV_FD_CLR(t->cli_fd, DIR_RD);
4099// trace_term(t, TT_HTTP_CLI_2);
4100// } else {
4101// /* output was already closed */
4102// fd_delete(t->cli_fd);
4103// t->cli_state = CL_STCLOSE;
4104// trace_term(t, TT_HTTP_CLI_3);
4105// }
4106// goto update_state;
4107// }
4108// /* last server read and buffer empty : we only check them when we're
4109// * allowed to forward the data.
4110// */
4111// else if (!(rep->flags & BF_SHUTW) && /* not already done */
4112// rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
4113// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
4114// buffer_shutw(rep);
4115// if (!(req->flags & BF_SHUTR)) {
4116// EV_FD_CLR(t->cli_fd, DIR_WR);
4117// shutdown(t->cli_fd, SHUT_WR);
4118// /* We must ensure that the read part is still alive when switching to shutw */
4119// /* FIXME: is this still true ? */
4120// EV_FD_SET(t->cli_fd, DIR_RD);
4121// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4122// trace_term(t, TT_HTTP_CLI_4);
4123// } else {
4124// fd_delete(t->cli_fd);
4125// t->cli_state = CL_STCLOSE;
4126// trace_term(t, TT_HTTP_CLI_5);
4127// }
4128// goto update_state;
4129// }
4130// /* read timeout */
4131// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4132// buffer_shutr(req);
4133// if (!(rep->flags & BF_SHUTW)) {
4134// EV_FD_CLR(t->cli_fd, DIR_RD);
4135// trace_term(t, TT_HTTP_CLI_6);
4136// } else {
4137// /* output was already closed */
4138// fd_delete(t->cli_fd);
4139// t->cli_state = CL_STCLOSE;
4140// trace_term(t, TT_HTTP_CLI_7);
4141// }
4142// if (!req->analysers) {
4143// if (!(t->flags & SN_ERR_MASK))
4144// t->flags |= SN_ERR_CLITO;
4145// if (!(t->flags & SN_FINST_MASK)) {
4146// if (t->pend_pos)
4147// t->flags |= SN_FINST_Q;
4148// else if (!(req->flags & BF_CONNECTED))
4149// t->flags |= SN_FINST_C;
4150// else
4151// t->flags |= SN_FINST_D;
4152// }
4153// }
4154// goto update_state;
4155// }
4156// /* write timeout */
4157// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4158// buffer_shutw(rep);
4159// if (!(req->flags & BF_SHUTR)) {
4160// EV_FD_CLR(t->cli_fd, DIR_WR);
4161// shutdown(t->cli_fd, SHUT_WR);
4162// /* We must ensure that the read part is still alive when switching to shutw */
4163// /* FIXME: is this still true ? */
4164// EV_FD_SET(t->cli_fd, DIR_RD);
4165// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4166// trace_term(t, TT_HTTP_CLI_8);
4167// } else {
4168// fd_delete(t->cli_fd);
4169// t->cli_state = CL_STCLOSE;
4170// trace_term(t, TT_HTTP_CLI_9);
4171// }
4172// if (!req->analysers) {
4173// if (!(t->flags & SN_ERR_MASK))
4174// t->flags |= SN_ERR_CLITO;
4175// if (!(t->flags & SN_FINST_MASK)) {
4176// if (t->pend_pos)
4177// t->flags |= SN_FINST_Q;
4178// else if (!(req->flags & BF_CONNECTED))
4179// t->flags |= SN_FINST_C;
4180// else
4181// t->flags |= SN_FINST_D;
4182// }
4183// }
4184// goto update_state;
4185// }
4186//
4187// update_timeouts:
4188// /* manage read timeout */
4189// if (!(req->flags & BF_SHUTR)) {
4190// if (req->flags & BF_FULL) {
4191// /* no room to read more data */
4192// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
4193// /* stop reading until we get some space */
4194// req->rex = TICK_ETERNITY;
4195// }
4196// } else {
4197// EV_FD_COND_S(t->cli_fd, DIR_RD);
4198// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4199// }
4200// }
4201//
4202// /* manage write timeout */
4203// if (!(rep->flags & BF_SHUTW)) {
4204// /* first, we may have to produce data (eg: stats).
4205// * right now, this is limited to the SHUTR state.
4206// */
4207// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
4208// produce_content(t);
4209// if (rep->flags & BF_EMPTY) {
4210// buffer_shutw(rep);
4211// fd_delete(t->cli_fd);
4212// t->cli_state = CL_STCLOSE;
4213// trace_term(t, TT_HTTP_CLI_10);
4214// goto update_state;
4215// }
4216// }
4217//
4218// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
4219// if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
4220// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
4221// /* stop writing */
4222// rep->wex = TICK_ETERNITY;
4223// }
4224// } else {
4225// /* buffer not empty */
4226// EV_FD_COND_S(t->cli_fd, DIR_WR);
4227// if (!tick_isset(rep->wex)) {
4228// /* restart writing */
4229// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
4230// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
4231// /* FIXME: to prevent the client from expiring read timeouts during writes,
4232// * we refresh it, except if it was already infinite. */
4233// req->rex = rep->wex;
4234// }
4235// }
4236// }
4237// }
4238// return 0; /* other cases change nothing */
4239// }
4240// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
4241// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
4242// int len;
4243// len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd);
4244// write(1, trash, len);
4245// }
4246// return 0;
4247// }
4248//#ifdef DEBUG_DEV
4249// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
4250// ABORT_NOW();
4251//#endif
4252// return 0;
4253//}
4254//
4255//
4256///* Return 1 if we could get a new connection for session t, otherwise zero */
4257//int tcp_get_connection(struct session *t)
4258//{
4259// struct http_txn *txn = &t->txn;
4260// struct buffer *req = t->req;
4261// struct buffer *rep = t->rep;
4262//
4263// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4264// now_ms, __FUNCTION__,
4265// cli_stnames[t->cli_state],
4266// rep->rex, req->wex,
4267// req->flags, rep->flags,
4268// req->l, rep->l);
4269//
4270//
4271// if ((rep->flags & BF_SHUTW) ||
4272// ((req->flags & BF_SHUTR) &&
4273// (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4274// req->wex = TICK_ETERNITY;
4275// if (t->pend_pos)
4276// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4277// /* note that this must not return any error because it would be able to
4278// * overwrite the client_retnclose() output.
4279// */
4280// if (txn->flags & TX_CLTARPIT)
4281// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
4282// else
4283// srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
4284//
4285// trace_term(t, TT_HTTP_SRV_1);
4286// return 0;
4287// }
4288//
4289// /* stop here if we're not allowed to connect */
4290// if (!(req->flags & BF_MAY_FORWARD))
4291// return 0;
4292//
4293// /* the client allows the server to connect */
4294// if (txn->flags & TX_CLTARPIT) {
4295// /* This connection is being tarpitted. The CLIENT side has
4296// * already set the connect expiration date to the right
4297// * timeout. We just have to check that it has not expired.
4298// */
4299// if (!(req->flags & BF_WRITE_TIMEOUT))
4300// return 0;
4301//
4302// /* We will set the queue timer to the time spent, just for
4303// * logging purposes. We fake a 500 server error, so that the
4304// * attacker will not suspect his connection has been tarpitted.
4305// * It will not cause trouble to the logs because we can exclude
4306// * the tarpitted connections by filtering on the 'PT' status flags.
4307// */
4308// req->wex = TICK_ETERNITY;
4309// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4310// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
4311// 500, error_message(t, HTTP_ERR_500));
4312// trace_term(t, TT_HTTP_SRV_2);
4313// return 0;
4314// }
4315//
4316// /* Right now, we will need to create a connection to the server.
4317// * We might already have tried, and got a connection pending, in
4318// * which case we will not do anything till it's pending. It's up
4319// * to any other session to release it and wake us up again.
4320// */
4321// if (t->pend_pos) {
4322// if (!(req->flags & BF_WRITE_TIMEOUT)) {
4323// return 0;
4324// } else {
4325// /* we've been waiting too long here */
4326// req->wex = TICK_ETERNITY;
4327// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4328// srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
4329// 503, error_message(t, HTTP_ERR_503));
4330// trace_term(t, TT_HTTP_SRV_3);
4331// if (t->srv)
4332// t->srv->failed_conns++;
4333// t->be->failed_conns++;
4334// return 0;
4335// }
4336// }
4337//
4338// do {
4339// if (srv_redispatch_connect(t) != 0)
4340// return 0;
4341//
4342// if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
4343// /* Server supporting redirection and it is possible.
4344// * Invalid requests are reported as such. It concerns all
4345// * the largest ones.
4346// */
4347// struct chunk rdr;
4348// char *path;
4349// int len;
4350//
4351// /* 1: create the response header */
4352// rdr.len = strlen(HTTP_302);
4353// rdr.str = trash;
4354// memcpy(rdr.str, HTTP_302, rdr.len);
4355//
4356// /* 2: add the server's prefix */
4357// if (rdr.len + t->srv->rdr_len > sizeof(trash))
4358// goto cancel_redir;
4359//
4360// memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
4361// rdr.len += t->srv->rdr_len;
4362//
4363// /* 3: add the request URI */
4364// path = http_get_path(txn);
4365// if (!path)
4366// goto cancel_redir;
4367// len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
4368// if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
4369// goto cancel_redir;
4370//
4371// memcpy(rdr.str + rdr.len, path, len);
4372// rdr.len += len;
4373// memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
4374// rdr.len += 4;
4375//
4376// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
4377// trace_term(t, TT_HTTP_SRV_3);
4378//
4379// /* FIXME: we should increase a counter of redirects per server and per backend. */
4380// if (t->srv)
4381// t->srv->cum_sess++;
4382// return 0;
4383// cancel_redir:
4384// txn->status = 400;
4385// t->fe->failed_req++;
4386// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
4387// 400, error_message(t, HTTP_ERR_400));
4388// trace_term(t, TT_HTTP_SRV_4);
4389// return 0;
4390// }
4391//
4392// /* try to (re-)connect to the server, and fail if we expire the
4393// * number of retries.
4394// */
4395// if (srv_retryable_connect(t)) {
4396// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4397// if (!(req->cons.flags & BC_KNOWN))
4398// return 0;
4399// /* We got an FD */
4400// return 1;
4401// }
4402// } while (1);
4403//}
4404//
4405//
4406///* Return 1 if the pending connection has failed and should be retried,
4407// * otherwise zero.
4408// */
4409//int tcp_connection_failed(struct session *t)
4410//{
4411// struct buffer *req = t->req;
4412// struct buffer *rep = t->rep;
4413// int conn_err;
4414//
4415// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4416// now_ms, __FUNCTION__,
4417// cli_stnames[t->cli_state],
4418// rep->rex, req->wex,
4419// req->flags, rep->flags,
4420// req->l, rep->l);
4421//
4422// if ((rep->flags & BF_SHUTW) ||
4423// ((req->flags & BF_SHUTR) &&
4424// ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
4425// t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4426// req->wex = TICK_ETERNITY;
4427// if (!(t->flags & SN_CONN_TAR)) {
4428// /* if we are in turn-around, we have already closed the FD */
4429// fd_delete(req->cons->fd);
4430// req->cons->state = SI_ST_CLO;
4431// if (t->srv) {
4432// t->srv->cur_sess--;
4433// sess_change_server(t, NULL);
4434// }
4435// }
4436//
4437// /* note that this must not return any error because it would be able to
4438// * overwrite the client_retnclose() output.
4439// */
4440// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
4441// trace_term(t, TT_HTTP_SRV_5);
4442// return 0;
4443// }
4444//
4445// if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT)))
4446// return 0; /* nothing changed */
4447//
4448// if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
4449// /* timeout, asynchronous connect error or first write error */
4450// if (t->flags & SN_CONN_TAR) {
4451// /* We are doing a turn-around waiting for a new connection attempt. */
4452// if (!(req->flags & BF_WRITE_TIMEOUT))
4453// return 0;
4454// t->flags &= ~SN_CONN_TAR;
4455// }
4456// else {
4457// fd_delete(req->cons->fd);
4458// req->cons->state = SI_ST_CLO;
4459// if (t->srv) {
4460// t->srv->cur_sess--;
4461// sess_change_server(t, NULL);
4462// }
4463//
4464// if (!(req->flags & BF_WRITE_STATUS))
4465// conn_err = SN_ERR_SRVTO; // it was a connect timeout.
4466// else
4467// conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
4468//
4469// /* ensure that we have enough retries left */
4470// if (srv_count_retry_down(t, conn_err))
4471// return 0;
4472//
4473// if (req->flags & BF_WRITE_ERROR) {
4474// /* we encountered an immediate connection error, and we
4475// * will have to retry connecting to the same server, most
4476// * likely leading to the same result. To avoid this, we
4477// * fake a connection timeout to retry after a turn-around
4478// * time of 1 second. We will wait in the previous if block.
4479// */
4480// t->flags |= SN_CONN_TAR;
4481// req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
4482// return 0;
4483// }
4484// }
4485//
4486// if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
4487// /* We're on our last chance, and the REDISP option was specified.
4488// * We will ignore cookie and force to balance or use the dispatcher.
4489// */
4490// /* let's try to offer this slot to anybody */
4491// if (may_dequeue_tasks(t->srv, t->be))
4492// process_srv_queue(t->srv);
4493//
4494// /* it's left to the dispatcher to choose a server */
4495// t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
4496// t->prev_srv = t->srv;
4497//
4498// /* first, get a connection */
4499// if (srv_redispatch_connect(t)) {
4500// if (req->cons.flags & BC_KNOWN)
4501// return 0;
4502// /* we need to get a connection */
4503// return 1;
4504// }
4505// } else {
4506// if (t->srv)
4507// t->srv->retries++;
4508// t->be->retries++;
4509// }
4510//
4511// do {
4512// /* Now we will try to either reconnect to the same server or
4513// * connect to another server. If the connection gets queued
4514// * because all servers are saturated, then we will go back to
4515// * the idle state where the buffer's consumer is marked as
4516// * unknown.
4517// */
4518// if (srv_retryable_connect(t)) {
4519// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4520// if (req->cons.flags & BC_KNOWN)
4521// return 0;
4522// /* we did not get a connection */
4523// return 1;
4524// }
4525//
4526// /* we need to redispatch the connection to another server */
4527// if (srv_redispatch_connect(t)) {
4528// if (req->cons.flags & BC_KNOWN)
4529// return 0;
4530// /* we need to get a connection */
4531// return 1;
4532// }
4533// } while (1);
4534// }
4535// else { /* no error and write OK */
4536// t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
4537//
4538// if (req->flags & BF_EMPTY) {
4539// EV_FD_CLR(req->cons->fd, DIR_WR);
4540// req->wex = TICK_ETERNITY;
4541// } else {
4542// EV_FD_SET(req->cons->fd, DIR_WR);
4543// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4544// if (tick_isset(req->wex)) {
4545// /* FIXME: to prevent the server from expiring read timeouts during writes,
4546// * we refresh it. */
4547// rep->rex = req->wex;
4548// }
4549// }
4550//
4551// if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
4552// EV_FD_SET(req->cons->fd, DIR_RD);
4553// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4554// buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
4555//
4556// /* if the user wants to log as soon as possible, without counting
4557// bytes from the server, then this is the right moment. */
4558// if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4559// t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
4560// tcp_sess_log(t);
4561// }
4562//#ifdef CONFIG_HAP_TCPSPLICE
4563// if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
4564// /* TCP splicing supported by both FE and BE */
4565// tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
4566// }
4567//#endif
4568// }
4569// else {
4570// rep->analysers |= AN_RTR_HTTP_HDR;
4571// buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
4572// t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
4573// /* reset hdr_idx which was already initialized by the request.
4574// * right now, the http parser does it.
4575// * hdr_idx_init(&t->txn.hdr_idx);
4576// */
4577// }
4578//
4579// req->flags |= BF_CONNECTED;
4580// if (!rep->analysers)
4581// t->rep->flags |= BF_MAY_FORWARD;
4582// req->wex = TICK_ETERNITY;
4583// return 0;
4584// }
4585//}
4586//
4587//
4588///*
4589// * Tries to establish a connection to the server and associate it to the
4590// * request buffer's consumer side. It normally returns zero, but may return 1
4591// * if it absolutely wants to be called again.
4592// */
4593//int process_srv_conn(struct session *t)
4594//{
4595// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4596// now_ms, __FUNCTION__,
4597// cli_stnames[t->cli_state],
4598// t->rep->rex, t->req->wex,
4599// t->req->flags, t->rep->flags,
4600// t->req->l, t->rep->l);
4601//
4602// while (!(t->req->flags & BF_CONNECTED)) {
4603// if (!(t->req->cons.flags & BC_KNOWN)) {
4604// /* no connection in progress, get a new one */
4605// if (!tcp_get_connection(t))
4606// break;
4607// } else {
4608// /* connection in progress or just completed */
4609// if (!tcp_connection_failed(t))
4610// break;
4611// }
4612// }
4613// return 0;
4614//}
4615//
4616//
4617///*
4618// * Manages the server FSM and its socket during the DATA phase. It must not
4619// * be called when a file descriptor is not attached to the buffer. It normally
4620// * returns zero, but may return 1 if it absolutely wants to be called again.
4621// */
4622//int process_srv_data(struct session *t)
4623//{
4624// struct buffer *req = t->req;
4625// struct buffer *rep = t->rep;
4626//
4627// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4628// now_ms, __FUNCTION__,
4629// cli_stnames[t->cli_state],
4630// rep->rex, req->wex,
4631// req->flags, rep->flags,
4632// req->l, rep->l);
4633//
4634// /* we can skip most of the tests at once if some conditions are not met */
4635// if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4636// (!(req->flags & BF_SHUTW) &&
4637// (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
4638// (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4639// (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
4640// goto update_timeouts;
4641//
4642// /* read or write error */
4643// /* FIXME: what happens when we have to deal with HTTP ??? */
4644// if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
4645// buffer_shutr(rep);
4646// buffer_shutw(req);
4647// fd_delete(req->cons->fd);
4648// req->cons->state = SI_ST_CLO;
4649// if (t->srv) {
4650// t->srv->cur_sess--;
4651// t->srv->failed_resp++;
4652// sess_change_server(t, NULL);
4653// }
4654// t->be->failed_resp++;
4655// trace_term(t, TT_HTTP_SRV_6);
4656// if (!rep->analysers) {
4657// if (!(t->flags & SN_ERR_MASK))
4658// t->flags |= SN_ERR_SRVCL;
4659// if (!(t->flags & SN_FINST_MASK))
4660// t->flags |= SN_FINST_D;
4661// }
4662// if (may_dequeue_tasks(t->srv, t->be))
4663// process_srv_queue(t->srv);
4664//
4665// return 0;
4666// }
4667//
4668// /* last read, or end of client write */
4669// if (!(rep->flags & BF_SHUTR) && /* not already done */
4670// rep->flags & (BF_READ_NULL | BF_SHUTW)) {
4671// buffer_shutr(rep);
4672// if (!(req->flags & BF_SHUTW)) {
4673// EV_FD_CLR(req->cons->fd, DIR_RD);
4674// trace_term(t, TT_HTTP_SRV_7);
4675// } else {
4676// /* output was already closed */
4677// fd_delete(req->cons->fd);
4678// req->cons->state = SI_ST_CLO;
4679// if (t->srv) {
4680// t->srv->cur_sess--;
4681// sess_change_server(t, NULL);
4682// }
4683// trace_term(t, TT_HTTP_SRV_8);
4684//
4685// if (may_dequeue_tasks(t->srv, t->be))
4686// process_srv_queue(t->srv);
4687// return 0;
4688// }
4689// }
4690// /* end of client read and no more data to send. We can forward
4691// * the close when we're allowed to forward data (anytime right
4692// * now). If we're using option forceclose, then we may also
4693// * shutdown the outgoing write channel once the response starts
4694// * coming from the server.
4695// */
4696// if (!(req->flags & BF_SHUTW) && /* not already done */
4697// req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
4698// (req->flags & BF_SHUTR ||
4699// (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
4700// buffer_shutw(req);
4701// if (!(rep->flags & BF_SHUTR)) {
4702// EV_FD_CLR(req->cons->fd, DIR_WR);
4703// shutdown(req->cons->fd, SHUT_WR);
4704// trace_term(t, TT_HTTP_SRV_9);
4705// /* We must ensure that the read part is still alive when switching to shutw */
4706// /* FIXME: is this still true ? */
4707// EV_FD_SET(req->cons->fd, DIR_RD);
4708// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4709// } else {
4710// fd_delete(req->cons->fd);
4711// req->cons->state = SI_ST_CLO;
4712// if (t->srv) {
4713// t->srv->cur_sess--;
4714// sess_change_server(t, NULL);
4715// }
4716// trace_term(t, TT_HTTP_SRV_10);
4717//
4718// if (may_dequeue_tasks(t->srv, t->be))
4719// process_srv_queue(t->srv);
4720// return 0;
4721// }
4722// }
4723//
4724// /* read timeout */
4725// if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4726// if (!rep->analysers) {
4727// if (!(t->flags & SN_ERR_MASK))
4728// t->flags |= SN_ERR_SRVTO;
4729// if (!(t->flags & SN_FINST_MASK))
4730// t->flags |= SN_FINST_D;
4731// }
4732// buffer_shutr(rep);
4733// if (!(req->flags & BF_SHUTW)) {
4734// EV_FD_CLR(req->cons->fd, DIR_RD);
4735// trace_term(t, TT_HTTP_SRV_11);
4736// } else {
4737// fd_delete(req->cons->fd);
4738// req->cons->state = SI_ST_CLO;
4739// if (t->srv) {
4740// t->srv->cur_sess--;
4741// sess_change_server(t, NULL);
4742// }
4743// trace_term(t, TT_HTTP_SRV_12);
4744//
4745// if (may_dequeue_tasks(t->srv, t->be))
4746// process_srv_queue(t->srv);
4747// return 0;
4748// }
4749// }
4750//
4751// /* write timeout */
4752// if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4753// if (!rep->analysers) {
4754// if (!(t->flags & SN_ERR_MASK))
4755// t->flags |= SN_ERR_SRVTO;
4756// if (!(t->flags & SN_FINST_MASK))
4757// t->flags |= SN_FINST_D;
4758// }
4759// buffer_shutw(req);
4760// if (!(rep->flags & BF_SHUTR)) {
4761// EV_FD_CLR(req->cons->fd, DIR_WR);
4762// shutdown(req->cons->fd, SHUT_WR);
4763// /* We must ensure that the read part is still alive when switching to shutw */
4764// /* FIXME: is this still needed ? */
4765// EV_FD_SET(req->cons->fd, DIR_RD);
4766// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4767// trace_term(t, TT_HTTP_SRV_13);
4768// } else {
4769// fd_delete(req->cons->fd);
4770// req->cons->state = SI_ST_CLO;
4771// if (t->srv) {
4772// t->srv->cur_sess--;
4773// sess_change_server(t, NULL);
4774// }
4775// trace_term(t, TT_HTTP_SRV_14);
4776//
4777// if (may_dequeue_tasks(t->srv, t->be))
4778// process_srv_queue(t->srv);
4779// return 0;
4780// }
4781// }
4782//
4783// update_timeouts:
4784// /* manage read timeout */
4785// if (!(rep->flags & BF_SHUTR)) {
4786// if (rep->flags & BF_FULL) {
4787// if (EV_FD_COND_C(req->cons->fd, DIR_RD))
4788// rep->rex = TICK_ETERNITY;
4789// } else {
4790// EV_FD_COND_S(req->cons->fd, DIR_RD);
4791// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4792// }
4793// }
4794//
4795// /* manage write timeout */
4796// if (!(req->flags & BF_SHUTW)) {
4797// if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
4798// /* stop writing */
4799// if (EV_FD_COND_C(req->cons->fd, DIR_WR))
4800// req->wex = TICK_ETERNITY;
4801// } else {
4802// /* buffer not empty, there are still data to be transferred */
4803// EV_FD_COND_S(req->cons->fd, DIR_WR);
4804// if (!tick_isset(req->wex)) {
4805// /* restart writing */
4806// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4807// if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4808// /* FIXME: to prevent the server from expiring read timeouts during writes,
4809// * we refresh it, except if it was already infinite.
4810// */
4811// rep->rex = req->wex;
4812// }
4813// }
4814// }
4815// }
4816// return 0; /* other cases change nothing */
4817//}
4818//
4819
Willy Tarreaubaaee002006-06-26 02:48:02 +02004820/*
4821 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004822 * called with client socket shut down on input. Right now, only statistics can
4823 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02004824 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004825 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004826 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004827 */
4828int produce_content(struct session *s)
4829{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004830 if (s->data_source == DATA_SRC_NONE) {
4831 s->flags &= ~SN_SELF_GEN;
4832 return 1;
4833 }
4834 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004835 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004836 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004837 if (ret >= 0)
4838 return ret;
4839 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004840 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004841
Willy Tarreau91861262007-10-17 17:06:05 +02004842 /* unknown data source or internal error */
4843 s->txn.status = 500;
4844 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004845 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004846 if (!(s->flags & SN_ERR_MASK))
4847 s->flags |= SN_ERR_PRXCOND;
4848 if (!(s->flags & SN_FINST_MASK))
4849 s->flags |= SN_FINST_R;
4850 s->flags &= ~SN_SELF_GEN;
4851 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004852}
4853
4854
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004855/* Iterate the same filter through all request headers.
4856 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004857 * Since it can manage the switch to another backend, it updates the per-proxy
4858 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004859 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004860int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004861{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004862 char term;
4863 char *cur_ptr, *cur_end, *cur_next;
4864 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004865 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004866 struct hdr_idx_elem *cur_hdr;
4867 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004869 last_hdr = 0;
4870
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004871 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004872 old_idx = 0;
4873
4874 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004875 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004876 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004877 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004878 (exp->action == ACT_ALLOW ||
4879 exp->action == ACT_DENY ||
4880 exp->action == ACT_TARPIT))
4881 return 0;
4882
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004883 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004884 if (!cur_idx)
4885 break;
4886
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004887 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888 cur_ptr = cur_next;
4889 cur_end = cur_ptr + cur_hdr->len;
4890 cur_next = cur_end + cur_hdr->cr + 1;
4891
4892 /* Now we have one header between cur_ptr and cur_end,
4893 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004894 */
4895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004896 /* The annoying part is that pattern matching needs
4897 * that we modify the contents to null-terminate all
4898 * strings before testing them.
4899 */
4900
4901 term = *cur_end;
4902 *cur_end = '\0';
4903
4904 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4905 switch (exp->action) {
4906 case ACT_SETBE:
4907 /* It is not possible to jump a second time.
4908 * FIXME: should we return an HTTP/500 here so that
4909 * the admin knows there's a problem ?
4910 */
4911 if (t->be != t->fe)
4912 break;
4913
4914 /* Swithing Proxy */
4915 t->be = (struct proxy *) exp->replace;
4916
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004917 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004918 * because we have associated req_cap and rsp_cap to the
4919 * frontend, and the beconn will be updated later.
4920 */
4921
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004922 t->rep->rto = t->req->wto = t->be->timeout.server;
4923 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004924 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004925 last_hdr = 1;
4926 break;
4927
4928 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004929 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004930 last_hdr = 1;
4931 break;
4932
4933 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004934 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004935 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004936 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004937 break;
4938
4939 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004940 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004941 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004942 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004943 break;
4944
4945 case ACT_REPLACE:
4946 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4947 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4948 /* FIXME: if the user adds a newline in the replacement, the
4949 * index will not be recalculated for now, and the new line
4950 * will not be counted as a new header.
4951 */
4952
4953 cur_end += delta;
4954 cur_next += delta;
4955 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004956 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004957 break;
4958
4959 case ACT_REMOVE:
4960 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4961 cur_next += delta;
4962
4963 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004964 txn->req.eoh += delta;
4965 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4966 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004967 cur_hdr->len = 0;
4968 cur_end = NULL; /* null-term has been rewritten */
4969 break;
4970
4971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004972 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004973 if (cur_end)
4974 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004976 /* keep the link from this header to next one in case of later
4977 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004978 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004979 old_idx = cur_idx;
4980 }
4981 return 0;
4982}
4983
4984
4985/* Apply the filter to the request line.
4986 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4987 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004988 * Since it can manage the switch to another backend, it updates the per-proxy
4989 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 */
4991int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4992{
4993 char term;
4994 char *cur_ptr, *cur_end;
4995 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004996 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004997 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004998
Willy Tarreau58f10d72006-12-04 02:26:12 +01004999
Willy Tarreau3d300592007-03-18 18:34:41 +01005000 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005001 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005002 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005003 (exp->action == ACT_ALLOW ||
5004 exp->action == ACT_DENY ||
5005 exp->action == ACT_TARPIT))
5006 return 0;
5007 else if (exp->action == ACT_REMOVE)
5008 return 0;
5009
5010 done = 0;
5011
Willy Tarreau9cdde232007-05-02 20:58:19 +02005012 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005013 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014
5015 /* Now we have the request line between cur_ptr and cur_end */
5016
5017 /* The annoying part is that pattern matching needs
5018 * that we modify the contents to null-terminate all
5019 * strings before testing them.
5020 */
5021
5022 term = *cur_end;
5023 *cur_end = '\0';
5024
5025 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5026 switch (exp->action) {
5027 case ACT_SETBE:
5028 /* It is not possible to jump a second time.
5029 * FIXME: should we return an HTTP/500 here so that
5030 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005032 if (t->be != t->fe)
5033 break;
5034
5035 /* Swithing Proxy */
5036 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005038 /* right now, the backend switch is not too much complicated
5039 * because we have associated req_cap and rsp_cap to the
5040 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005041 */
5042
Willy Tarreaud7c30f92007-12-03 01:38:36 +01005043 t->rep->rto = t->req->wto = t->be->timeout.server;
5044 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02005045 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005046 done = 1;
5047 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005049 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005050 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005051 done = 1;
5052 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005054 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005055 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005056 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005057 done = 1;
5058 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005060 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005061 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005062 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 done = 1;
5064 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005066 case ACT_REPLACE:
5067 *cur_end = term; /* restore the string terminator */
5068 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5069 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5070 /* FIXME: if the user adds a newline in the replacement, the
5071 * index will not be recalculated for now, and the new line
5072 * will not be counted as a new header.
5073 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005074
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005075 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005076 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005077
Willy Tarreau9cdde232007-05-02 20:58:19 +02005078 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005079 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005080 HTTP_MSG_RQMETH,
5081 cur_ptr, cur_end + 1,
5082 NULL, NULL);
5083 if (unlikely(!cur_end))
5084 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 /* we have a full request and we know that we have either a CR
5087 * or an LF at <ptr>.
5088 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005089 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5090 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005091 /* there is no point trying this regex on headers */
5092 return 1;
5093 }
5094 }
5095 *cur_end = term; /* restore the string terminator */
5096 return done;
5097}
Willy Tarreau97de6242006-12-27 17:18:38 +01005098
Willy Tarreau58f10d72006-12-04 02:26:12 +01005099
Willy Tarreau58f10d72006-12-04 02:26:12 +01005100
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005101/*
5102 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5103 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005104 * unparsable request. Since it can manage the switch to another backend, it
5105 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005106 */
5107int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5108{
Willy Tarreau3d300592007-03-18 18:34:41 +01005109 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005110 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005111 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005112 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005114 /*
5115 * The interleaving of transformations and verdicts
5116 * makes it difficult to decide to continue or stop
5117 * the evaluation.
5118 */
5119
Willy Tarreau3d300592007-03-18 18:34:41 +01005120 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005121 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5122 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5123 exp = exp->next;
5124 continue;
5125 }
5126
5127 /* Apply the filter to the request line. */
5128 ret = apply_filter_to_req_line(t, req, exp);
5129 if (unlikely(ret < 0))
5130 return -1;
5131
5132 if (likely(ret == 0)) {
5133 /* The filter did not match the request, it can be
5134 * iterated through all headers.
5135 */
5136 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005137 }
5138 exp = exp->next;
5139 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005140 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005141}
5142
5143
Willy Tarreaua15645d2007-03-18 16:22:39 +01005144
Willy Tarreau58f10d72006-12-04 02:26:12 +01005145/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005146 * Manage client-side cookie. It can impact performance by about 2% so it is
5147 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005148 */
5149void manage_client_side_cookies(struct session *t, struct buffer *req)
5150{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005151 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005152 char *p1, *p2, *p3, *p4;
5153 char *del_colon, *del_cookie, *colon;
5154 int app_cookies;
5155
5156 appsess *asession_temp = NULL;
5157 appsess local_asession;
5158
5159 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005160 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005161
Willy Tarreau2a324282006-12-05 00:05:46 +01005162 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005163 * we start with the start line.
5164 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005165 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005166 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005167
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005168 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005169 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005170 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005171
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005172 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005173 cur_ptr = cur_next;
5174 cur_end = cur_ptr + cur_hdr->len;
5175 cur_next = cur_end + cur_hdr->cr + 1;
5176
5177 /* We have one full header between cur_ptr and cur_end, and the
5178 * next header starts at cur_next. We're only interested in
5179 * "Cookie:" headers.
5180 */
5181
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005182 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5183 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005184 old_idx = cur_idx;
5185 continue;
5186 }
5187
5188 /* Now look for cookies. Conforming to RFC2109, we have to support
5189 * attributes whose name begin with a '$', and associate them with
5190 * the right cookie, if we want to delete this cookie.
5191 * So there are 3 cases for each cookie read :
5192 * 1) it's a special attribute, beginning with a '$' : ignore it.
5193 * 2) it's a server id cookie that we *MAY* want to delete : save
5194 * some pointers on it (last semi-colon, beginning of cookie...)
5195 * 3) it's an application cookie : we *MAY* have to delete a previous
5196 * "special" cookie.
5197 * At the end of loop, if a "special" cookie remains, we may have to
5198 * remove it. If no application cookie persists in the header, we
5199 * *MUST* delete it
5200 */
5201
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005202 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005203
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204 /* del_cookie == NULL => nothing to be deleted */
5205 del_colon = del_cookie = NULL;
5206 app_cookies = 0;
5207
5208 while (p1 < cur_end) {
5209 /* skip spaces and colons, but keep an eye on these ones */
5210 while (p1 < cur_end) {
5211 if (*p1 == ';' || *p1 == ',')
5212 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005213 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 break;
5215 p1++;
5216 }
5217
5218 if (p1 == cur_end)
5219 break;
5220
5221 /* p1 is at the beginning of the cookie name */
5222 p2 = p1;
5223 while (p2 < cur_end && *p2 != '=')
5224 p2++;
5225
5226 if (p2 == cur_end)
5227 break;
5228
5229 p3 = p2 + 1; /* skips the '=' sign */
5230 if (p3 == cur_end)
5231 break;
5232
5233 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005234 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005235 p4++;
5236
5237 /* here, we have the cookie name between p1 and p2,
5238 * and its value between p3 and p4.
5239 * we can process it :
5240 *
5241 * Cookie: NAME=VALUE;
5242 * | || || |
5243 * | || || +--> p4
5244 * | || |+-------> p3
5245 * | || +--------> p2
5246 * | |+------------> p1
5247 * | +-------------> colon
5248 * +--------------------> cur_ptr
5249 */
5250
5251 if (*p1 == '$') {
5252 /* skip this one */
5253 }
5254 else {
5255 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005256 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005257 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005258 (p4 - p1 >= t->fe->capture_namelen) &&
5259 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005260 int log_len = p4 - p1;
5261
Willy Tarreau086b3b42007-05-13 21:45:51 +02005262 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005263 Alert("HTTP logging : out of memory.\n");
5264 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005265 if (log_len > t->fe->capture_len)
5266 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005267 memcpy(txn->cli_cookie, p1, log_len);
5268 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005269 }
5270 }
5271
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005272 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5273 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005274 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005275 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005276 char *delim;
5277
5278 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5279 * have the server ID betweek p3 and delim, and the original cookie between
5280 * delim+1 and p4. Otherwise, delim==p4 :
5281 *
5282 * Cookie: NAME=SRV~VALUE;
5283 * | || || | |
5284 * | || || | +--> p4
5285 * | || || +--------> delim
5286 * | || |+-----------> p3
5287 * | || +------------> p2
5288 * | |+----------------> p1
5289 * | +-----------------> colon
5290 * +------------------------> cur_ptr
5291 */
5292
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005293 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005294 for (delim = p3; delim < p4; delim++)
5295 if (*delim == COOKIE_DELIM)
5296 break;
5297 }
5298 else
5299 delim = p4;
5300
5301
5302 /* Here, we'll look for the first running server which supports the cookie.
5303 * This allows to share a same cookie between several servers, for example
5304 * to dedicate backup servers to specific servers only.
5305 * However, to prevent clients from sticking to cookie-less backup server
5306 * when they have incidentely learned an empty cookie, we simply ignore
5307 * empty cookies and mark them as invalid.
5308 */
5309 if (delim == p3)
5310 srv = NULL;
5311
5312 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005313 if (srv->cookie && (srv->cklen == delim - p3) &&
5314 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005315 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005316 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005317 txn->flags &= ~TX_CK_MASK;
5318 txn->flags |= TX_CK_VALID;
5319 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005320 t->srv = srv;
5321 break;
5322 } else {
5323 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005324 txn->flags &= ~TX_CK_MASK;
5325 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005326 }
5327 }
5328 srv = srv->next;
5329 }
5330
Willy Tarreau3d300592007-03-18 18:34:41 +01005331 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005332 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005333 txn->flags &= ~TX_CK_MASK;
5334 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005335 }
5336
5337 /* depending on the cookie mode, we may have to either :
5338 * - delete the complete cookie if we're in insert+indirect mode, so that
5339 * the server never sees it ;
5340 * - remove the server id from the cookie value, and tag the cookie as an
5341 * application cookie so that it does not get accidentely removed later,
5342 * if we're in cookie prefix mode
5343 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005344 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005345 int delta; /* negative */
5346
5347 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5348 p4 += delta;
5349 cur_end += delta;
5350 cur_next += delta;
5351 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005352 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005353
5354 del_cookie = del_colon = NULL;
5355 app_cookies++; /* protect the header from deletion */
5356 }
5357 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005358 (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 +01005359 del_cookie = p1;
5360 del_colon = colon;
5361 }
5362 } else {
5363 /* now we know that we must keep this cookie since it's
5364 * not ours. But if we wanted to delete our cookie
5365 * earlier, we cannot remove the complete header, but we
5366 * can remove the previous block itself.
5367 */
5368 app_cookies++;
5369
5370 if (del_cookie != NULL) {
5371 int delta; /* negative */
5372
5373 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5374 p4 += delta;
5375 cur_end += delta;
5376 cur_next += delta;
5377 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005378 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 del_cookie = del_colon = NULL;
5380 }
5381 }
5382
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005383 if ((t->be->appsession_name != NULL) &&
5384 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005386
Willy Tarreau58f10d72006-12-04 02:26:12 +01005387 /* Cool... it's the right one */
5388
5389 asession_temp = &local_asession;
5390
Willy Tarreau63963c62007-05-13 21:29:55 +02005391 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005392 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5393 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5394 return;
5395 }
5396
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005397 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5398 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005399 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02005400
Willy Tarreau58f10d72006-12-04 02:26:12 +01005401 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005402 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5403 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005404 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005405 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005406 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005407 Alert("Not enough memory process_cli():asession:calloc().\n");
5408 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5409 return;
5410 }
5411
5412 asession_temp->sessid = local_asession.sessid;
5413 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005414 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005415 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005416 } else {
5417 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005418 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005419 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005420 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005421 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005422 Alert("Found Application Session without matching server.\n");
5423 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005424 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005425 while (srv) {
5426 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005427 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005428 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005429 txn->flags &= ~TX_CK_MASK;
5430 txn->flags |= TX_CK_VALID;
5431 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005432 t->srv = srv;
5433 break;
5434 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005435 txn->flags &= ~TX_CK_MASK;
5436 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005437 }
5438 }
5439 srv = srv->next;
5440 }/* end while(srv) */
5441 }/* end else if server == NULL */
5442
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005443 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005444 asession_temp->request_count++;
5445#if defined(DEBUG_HASH)
5446 Alert("manage_client_side_cookies\n");
5447 appsession_hash_dump(&(t->be->htbl_proxy));
5448#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449 }/* end if ((t->proxy->appsession_name != NULL) ... */
5450 }
5451
5452 /* we'll have to look for another cookie ... */
5453 p1 = p4;
5454 } /* while (p1 < cur_end) */
5455
5456 /* There's no more cookie on this line.
5457 * We may have marked the last one(s) for deletion.
5458 * We must do this now in two ways :
5459 * - if there is no app cookie, we simply delete the header ;
5460 * - if there are app cookies, we must delete the end of the
5461 * string properly, including the colon/semi-colon before
5462 * the cookie name.
5463 */
5464 if (del_cookie != NULL) {
5465 int delta;
5466 if (app_cookies) {
5467 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5468 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005469 cur_hdr->len += delta;
5470 } else {
5471 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005472
5473 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005474 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5475 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005476 cur_hdr->len = 0;
5477 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005478 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005480 }
5481
5482 /* keep the link from this header to next one */
5483 old_idx = cur_idx;
5484 } /* end of cookie processing on this header */
5485}
5486
5487
Willy Tarreaua15645d2007-03-18 16:22:39 +01005488/* Iterate the same filter through all response headers contained in <rtr>.
5489 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5490 */
5491int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5492{
5493 char term;
5494 char *cur_ptr, *cur_end, *cur_next;
5495 int cur_idx, old_idx, last_hdr;
5496 struct http_txn *txn = &t->txn;
5497 struct hdr_idx_elem *cur_hdr;
5498 int len, delta;
5499
5500 last_hdr = 0;
5501
5502 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5503 old_idx = 0;
5504
5505 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005506 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005507 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005508 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005509 (exp->action == ACT_ALLOW ||
5510 exp->action == ACT_DENY))
5511 return 0;
5512
5513 cur_idx = txn->hdr_idx.v[old_idx].next;
5514 if (!cur_idx)
5515 break;
5516
5517 cur_hdr = &txn->hdr_idx.v[cur_idx];
5518 cur_ptr = cur_next;
5519 cur_end = cur_ptr + cur_hdr->len;
5520 cur_next = cur_end + cur_hdr->cr + 1;
5521
5522 /* Now we have one header between cur_ptr and cur_end,
5523 * and the next header starts at cur_next.
5524 */
5525
5526 /* The annoying part is that pattern matching needs
5527 * that we modify the contents to null-terminate all
5528 * strings before testing them.
5529 */
5530
5531 term = *cur_end;
5532 *cur_end = '\0';
5533
5534 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5535 switch (exp->action) {
5536 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005537 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005538 last_hdr = 1;
5539 break;
5540
5541 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005542 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005543 last_hdr = 1;
5544 break;
5545
5546 case ACT_REPLACE:
5547 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5548 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5549 /* FIXME: if the user adds a newline in the replacement, the
5550 * index will not be recalculated for now, and the new line
5551 * will not be counted as a new header.
5552 */
5553
5554 cur_end += delta;
5555 cur_next += delta;
5556 cur_hdr->len += delta;
5557 txn->rsp.eoh += delta;
5558 break;
5559
5560 case ACT_REMOVE:
5561 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5562 cur_next += delta;
5563
5564 /* FIXME: this should be a separate function */
5565 txn->rsp.eoh += delta;
5566 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5567 txn->hdr_idx.used--;
5568 cur_hdr->len = 0;
5569 cur_end = NULL; /* null-term has been rewritten */
5570 break;
5571
5572 }
5573 }
5574 if (cur_end)
5575 *cur_end = term; /* restore the string terminator */
5576
5577 /* keep the link from this header to next one in case of later
5578 * removal of next header.
5579 */
5580 old_idx = cur_idx;
5581 }
5582 return 0;
5583}
5584
5585
5586/* Apply the filter to the status line in the response buffer <rtr>.
5587 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5588 * or -1 if a replacement resulted in an invalid status line.
5589 */
5590int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5591{
5592 char term;
5593 char *cur_ptr, *cur_end;
5594 int done;
5595 struct http_txn *txn = &t->txn;
5596 int len, delta;
5597
5598
Willy Tarreau3d300592007-03-18 18:34:41 +01005599 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005600 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005601 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005602 (exp->action == ACT_ALLOW ||
5603 exp->action == ACT_DENY))
5604 return 0;
5605 else if (exp->action == ACT_REMOVE)
5606 return 0;
5607
5608 done = 0;
5609
Willy Tarreau9cdde232007-05-02 20:58:19 +02005610 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005611 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5612
5613 /* Now we have the status line between cur_ptr and cur_end */
5614
5615 /* The annoying part is that pattern matching needs
5616 * that we modify the contents to null-terminate all
5617 * strings before testing them.
5618 */
5619
5620 term = *cur_end;
5621 *cur_end = '\0';
5622
5623 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5624 switch (exp->action) {
5625 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005626 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005627 done = 1;
5628 break;
5629
5630 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005631 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005632 done = 1;
5633 break;
5634
5635 case ACT_REPLACE:
5636 *cur_end = term; /* restore the string terminator */
5637 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5638 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5639 /* FIXME: if the user adds a newline in the replacement, the
5640 * index will not be recalculated for now, and the new line
5641 * will not be counted as a new header.
5642 */
5643
5644 txn->rsp.eoh += delta;
5645 cur_end += delta;
5646
Willy Tarreau9cdde232007-05-02 20:58:19 +02005647 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005648 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005649 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005650 cur_ptr, cur_end + 1,
5651 NULL, NULL);
5652 if (unlikely(!cur_end))
5653 return -1;
5654
5655 /* we have a full respnse and we know that we have either a CR
5656 * or an LF at <ptr>.
5657 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005658 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005659 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5660 /* there is no point trying this regex on headers */
5661 return 1;
5662 }
5663 }
5664 *cur_end = term; /* restore the string terminator */
5665 return done;
5666}
5667
5668
5669
5670/*
5671 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5672 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5673 * unparsable response.
5674 */
5675int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5676{
Willy Tarreau3d300592007-03-18 18:34:41 +01005677 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005678 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005679 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005680 int ret;
5681
5682 /*
5683 * The interleaving of transformations and verdicts
5684 * makes it difficult to decide to continue or stop
5685 * the evaluation.
5686 */
5687
Willy Tarreau3d300592007-03-18 18:34:41 +01005688 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005689 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5690 exp->action == ACT_PASS)) {
5691 exp = exp->next;
5692 continue;
5693 }
5694
5695 /* Apply the filter to the status line. */
5696 ret = apply_filter_to_sts_line(t, rtr, exp);
5697 if (unlikely(ret < 0))
5698 return -1;
5699
5700 if (likely(ret == 0)) {
5701 /* The filter did not match the response, it can be
5702 * iterated through all headers.
5703 */
5704 apply_filter_to_resp_headers(t, rtr, exp);
5705 }
5706 exp = exp->next;
5707 }
5708 return 0;
5709}
5710
5711
5712
5713/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005714 * Manage server-side cookies. It can impact performance by about 2% so it is
5715 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005716 */
5717void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5718{
5719 struct http_txn *txn = &t->txn;
5720 char *p1, *p2, *p3, *p4;
5721
5722 appsess *asession_temp = NULL;
5723 appsess local_asession;
5724
5725 char *cur_ptr, *cur_end, *cur_next;
5726 int cur_idx, old_idx, delta;
5727
Willy Tarreaua15645d2007-03-18 16:22:39 +01005728 /* Iterate through the headers.
5729 * we start with the start line.
5730 */
5731 old_idx = 0;
5732 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5733
5734 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5735 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005736 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005737
5738 cur_hdr = &txn->hdr_idx.v[cur_idx];
5739 cur_ptr = cur_next;
5740 cur_end = cur_ptr + cur_hdr->len;
5741 cur_next = cur_end + cur_hdr->cr + 1;
5742
5743 /* We have one full header between cur_ptr and cur_end, and the
5744 * next header starts at cur_next. We're only interested in
5745 * "Cookie:" headers.
5746 */
5747
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005748 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5749 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005750 old_idx = cur_idx;
5751 continue;
5752 }
5753
5754 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005755 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756
5757
5758 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005759 if (t->be->cookie_name == NULL &&
5760 t->be->appsession_name == NULL &&
5761 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762 return;
5763
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005764 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765
5766 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005767 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5768 break;
5769
5770 /* p1 is at the beginning of the cookie name */
5771 p2 = p1;
5772
5773 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5774 p2++;
5775
5776 if (p2 == cur_end || *p2 == ';') /* next cookie */
5777 break;
5778
5779 p3 = p2 + 1; /* skip the '=' sign */
5780 if (p3 == cur_end)
5781 break;
5782
5783 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005784 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785 p4++;
5786
5787 /* here, we have the cookie name between p1 and p2,
5788 * and its value between p3 and p4.
5789 * we can process it.
5790 */
5791
5792 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005793 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005794 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005795 (p4 - p1 >= t->be->capture_namelen) &&
5796 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005797 int log_len = p4 - p1;
5798
Willy Tarreau086b3b42007-05-13 21:45:51 +02005799 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005800 Alert("HTTP logging : out of memory.\n");
5801 }
5802
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005803 if (log_len > t->be->capture_len)
5804 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005805 memcpy(txn->srv_cookie, p1, log_len);
5806 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005807 }
5808
5809 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005810 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5811 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005813 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005814
5815 /* If the cookie is in insert mode on a known server, we'll delete
5816 * this occurrence because we'll insert another one later.
5817 * We'll delete it too if the "indirect" option is set and we're in
5818 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005819 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5820 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005821 /* this header must be deleted */
5822 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5823 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5824 txn->hdr_idx.used--;
5825 cur_hdr->len = 0;
5826 cur_next += delta;
5827 txn->rsp.eoh += delta;
5828
Willy Tarreau3d300592007-03-18 18:34:41 +01005829 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830 }
5831 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005832 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005833 /* replace bytes p3->p4 with the cookie name associated
5834 * with this server since we know it.
5835 */
5836 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5837 cur_hdr->len += delta;
5838 cur_next += delta;
5839 txn->rsp.eoh += delta;
5840
Willy Tarreau3d300592007-03-18 18:34:41 +01005841 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005842 }
5843 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005844 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845 /* insert the cookie name associated with this server
5846 * before existing cookie, and insert a delimitor between them..
5847 */
5848 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5849 cur_hdr->len += delta;
5850 cur_next += delta;
5851 txn->rsp.eoh += delta;
5852
5853 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005854 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855 }
5856 }
5857 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005858 else if ((t->be->appsession_name != NULL) &&
5859 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005860
5861 /* Cool... it's the right one */
5862
5863 size_t server_id_len = strlen(t->srv->id) + 1;
5864 asession_temp = &local_asession;
5865
Willy Tarreau63963c62007-05-13 21:29:55 +02005866 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005867 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5868 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5869 return;
5870 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005871 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5872 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005873 asession_temp->serverid = NULL;
5874
5875 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005876 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5877 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005878 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879 Alert("Not enough Memory process_srv():asession:calloc().\n");
5880 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5881 return;
5882 }
5883 asession_temp->sessid = local_asession.sessid;
5884 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005885 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005886 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5887 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005889 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005890 }
5891
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005893 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005894 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5895 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5896 return;
5897 }
5898 asession_temp->serverid[0] = '\0';
5899 }
5900
5901 if (asession_temp->serverid[0] == '\0')
5902 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5903
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005904 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005905 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005907 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005908 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005909#endif
5910 }/* end if ((t->proxy->appsession_name != NULL) ... */
5911 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5912 } /* we're now at the end of the cookie value */
5913
5914 /* keep the link from this header to next one */
5915 old_idx = cur_idx;
5916 } /* end of cookie processing on this header */
5917}
5918
5919
5920
5921/*
5922 * Check if response is cacheable or not. Updates t->flags.
5923 */
5924void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5925{
5926 struct http_txn *txn = &t->txn;
5927 char *p1, *p2;
5928
5929 char *cur_ptr, *cur_end, *cur_next;
5930 int cur_idx;
5931
Willy Tarreau5df51872007-11-25 16:20:08 +01005932 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005933 return;
5934
5935 /* Iterate through the headers.
5936 * we start with the start line.
5937 */
5938 cur_idx = 0;
5939 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5940
5941 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5942 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005943 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944
5945 cur_hdr = &txn->hdr_idx.v[cur_idx];
5946 cur_ptr = cur_next;
5947 cur_end = cur_ptr + cur_hdr->len;
5948 cur_next = cur_end + cur_hdr->cr + 1;
5949
5950 /* We have one full header between cur_ptr and cur_end, and the
5951 * next header starts at cur_next. We're only interested in
5952 * "Cookie:" headers.
5953 */
5954
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005955 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5956 if (val) {
5957 if ((cur_end - (cur_ptr + val) >= 8) &&
5958 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5959 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5960 return;
5961 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005962 }
5963
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005964 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5965 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005966 continue;
5967
5968 /* OK, right now we know we have a cache-control header at cur_ptr */
5969
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005970 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971
5972 if (p1 >= cur_end) /* no more info */
5973 continue;
5974
5975 /* p1 is at the beginning of the value */
5976 p2 = p1;
5977
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005978 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979 p2++;
5980
5981 /* we have a complete value between p1 and p2 */
5982 if (p2 < cur_end && *p2 == '=') {
5983 /* we have something of the form no-cache="set-cookie" */
5984 if ((cur_end - p1 >= 21) &&
5985 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5986 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005987 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005988 continue;
5989 }
5990
5991 /* OK, so we know that either p2 points to the end of string or to a comma */
5992 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5993 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5994 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5995 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005996 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005997 return;
5998 }
5999
6000 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006001 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002 continue;
6003 }
6004 }
6005}
6006
6007
Willy Tarreau58f10d72006-12-04 02:26:12 +01006008/*
6009 * Try to retrieve a known appsession in the URI, then the associated server.
6010 * If the server is found, it's assigned to the session.
6011 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006013{
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006015 appsess *asession_temp = NULL;
6016 appsess local_asession;
6017 char *request_line;
6018
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006019 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01006020 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006021 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006022 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01006023 return;
6024
6025 /* skip ';' */
6026 request_line++;
6027
6028 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006029 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006030 return;
6031
6032 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006033 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034
6035 /* First try if we already have an appsession */
6036 asession_temp = &local_asession;
6037
Willy Tarreau63963c62007-05-13 21:29:55 +02006038 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
6040 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
6041 return;
6042 }
6043
6044 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006045 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
6046 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006047 asession_temp->serverid = NULL;
6048
6049 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01006050 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
6051 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02006052 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006053 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006054 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006055 Alert("Not enough memory process_cli():asession:calloc().\n");
6056 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
6057 return;
6058 }
6059 asession_temp->sessid = local_asession.sessid;
6060 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006061 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02006062 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063 }
6064 else {
6065 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006066 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006067 }
Willy Tarreau51041c72007-09-09 21:56:53 +02006068
Willy Tarreau0c303ee2008-07-07 00:09:58 +02006069 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02006071
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006073 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006074 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075#endif
6076 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006077 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078 Alert("Found Application Session without matching server.\n");
6079 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006080 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006081 while (srv) {
6082 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006083 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01006085 txn->flags &= ~TX_CK_MASK;
6086 txn->flags |= TX_CK_VALID;
6087 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 t->srv = srv;
6089 break;
6090 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01006091 txn->flags &= ~TX_CK_MASK;
6092 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093 }
6094 }
6095 srv = srv->next;
6096 }
6097 }
6098}
6099
6100
Willy Tarreaub2513902006-12-17 14:52:38 +01006101/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006102 * In a GET or HEAD request, check if the requested URI matches the stats uri
6103 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006104 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006105 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006106 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006107 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006108 *
6109 * Returns 1 if the session's state changes, otherwise 0.
6110 */
6111int stats_check_uri_auth(struct session *t, struct proxy *backend)
6112{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006113 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006114 struct uri_auth *uri_auth = backend->uri_auth;
6115 struct user_auth *user;
6116 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006117 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006118
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006119 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006121 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006122 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006123 return 0;
6124
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006125 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006127 /* the URI is in h */
6128 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006129 return 0;
6130
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006131 h += uri_auth->uri_len;
6132 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6133 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006134 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006135 break;
6136 }
6137 h++;
6138 }
6139
6140 if (uri_auth->refresh) {
6141 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6142 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6143 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006144 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006145 break;
6146 }
6147 h++;
6148 }
6149 }
6150
Willy Tarreau55bb8452007-10-17 18:44:57 +02006151 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6152 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6153 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006154 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006155 break;
6156 }
6157 h++;
6158 }
6159
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006160 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6161
Willy Tarreaub2513902006-12-17 14:52:38 +01006162 /* we are in front of a interceptable URI. Let's check
6163 * if there's an authentication and if it's valid.
6164 */
6165 user = uri_auth->users;
6166 if (!user) {
6167 /* no user auth required, it's OK */
6168 authenticated = 1;
6169 } else {
6170 authenticated = 0;
6171
6172 /* a user list is defined, we have to check.
6173 * skip 21 chars for "Authorization: Basic ".
6174 */
6175
6176 /* FIXME: this should move to an earlier place */
6177 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006178 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6179 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6180 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006181 if (len > 14 &&
6182 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006183 txn->auth_hdr.str = h;
6184 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006185 break;
6186 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006187 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006188 }
6189
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006190 if (txn->auth_hdr.len < 21 ||
6191 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006192 user = NULL;
6193
6194 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006195 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6196 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006197 user->user_pwd, user->user_len)) {
6198 authenticated = 1;
6199 break;
6200 }
6201 user = user->next;
6202 }
6203 }
6204
6205 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006206 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006207
6208 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01006209 msg.str = trash;
6210 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006211 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01006212 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02006213 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006214 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006215 if (!(t->flags & SN_ERR_MASK))
6216 t->flags |= SN_ERR_PRXCOND;
6217 if (!(t->flags & SN_FINST_MASK))
6218 t->flags |= SN_FINST_R;
6219 return 1;
6220 }
6221
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006222 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006223 * data.
6224 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02006225 EV_FD_CLR(t->cli_fd, DIR_RD);
6226 buffer_shutr(t->req);
6227 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02006228 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02006229 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006230 t->data_source = DATA_SRC_STATS;
6231 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006232 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01006233 produce_content(t);
6234 return 1;
6235}
6236
6237
Willy Tarreaubaaee002006-06-26 02:48:02 +02006238/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239 * Print a debug line with a header
6240 */
6241void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6242{
6243 int len, max;
6244 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +02006245 dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246 max = end - start;
6247 UBOUND(max, sizeof(trash) - len - 1);
6248 len += strlcpy2(trash + len, start, max + 1);
6249 trash[len++] = '\n';
6250 write(1, trash, len);
6251}
6252
6253
Willy Tarreau8797c062007-05-07 00:55:35 +02006254/************************************************************************/
6255/* The code below is dedicated to ACL parsing and matching */
6256/************************************************************************/
6257
6258
6259
6260
6261/* 1. Check on METHOD
6262 * We use the pre-parsed method if it is known, and store its number as an
6263 * integer. If it is unknown, we use the pointer and the length.
6264 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006265static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006266{
6267 int len, meth;
6268
Willy Tarreauae8b7962007-06-09 23:10:04 +02006269 len = strlen(*text);
6270 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006271
6272 pattern->val.i = meth;
6273 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006274 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006275 if (!pattern->ptr.str)
6276 return 0;
6277 pattern->len = len;
6278 }
6279 return 1;
6280}
6281
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006282static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006283acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6284 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006285{
6286 int meth;
6287 struct http_txn *txn = l7;
6288
Willy Tarreaub6866442008-07-14 23:54:42 +02006289 if (!txn)
6290 return 0;
6291
Willy Tarreauc11416f2007-06-17 16:58:38 +02006292 if (txn->req.msg_state != HTTP_MSG_BODY)
6293 return 0;
6294
Willy Tarreau8797c062007-05-07 00:55:35 +02006295 meth = txn->meth;
6296 test->i = meth;
6297 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006298 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6299 /* ensure the indexes are not affected */
6300 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006301 test->len = txn->req.sl.rq.m_l;
6302 test->ptr = txn->req.sol;
6303 }
6304 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6305 return 1;
6306}
6307
6308static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6309{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006310 int icase;
6311
Willy Tarreau8797c062007-05-07 00:55:35 +02006312 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006313 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006314
6315 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006316 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006317
6318 /* Other method, we must compare the strings */
6319 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006320 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006321
6322 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6323 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6324 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006325 return ACL_PAT_FAIL;
6326 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006327}
6328
6329/* 2. Check on Request/Status Version
6330 * We simply compare strings here.
6331 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006332static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006333{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006334 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006335 if (!pattern->ptr.str)
6336 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006337 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006338 return 1;
6339}
6340
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006341static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006342acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6343 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006344{
6345 struct http_txn *txn = l7;
6346 char *ptr;
6347 int len;
6348
Willy Tarreaub6866442008-07-14 23:54:42 +02006349 if (!txn)
6350 return 0;
6351
Willy Tarreauc11416f2007-06-17 16:58:38 +02006352 if (txn->req.msg_state != HTTP_MSG_BODY)
6353 return 0;
6354
Willy Tarreau8797c062007-05-07 00:55:35 +02006355 len = txn->req.sl.rq.v_l;
6356 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6357
6358 while ((len-- > 0) && (*ptr++ != '/'));
6359 if (len <= 0)
6360 return 0;
6361
6362 test->ptr = ptr;
6363 test->len = len;
6364
6365 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6366 return 1;
6367}
6368
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006369static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006370acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6371 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006372{
6373 struct http_txn *txn = l7;
6374 char *ptr;
6375 int len;
6376
Willy Tarreaub6866442008-07-14 23:54:42 +02006377 if (!txn)
6378 return 0;
6379
Willy Tarreauc11416f2007-06-17 16:58:38 +02006380 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6381 return 0;
6382
Willy Tarreau8797c062007-05-07 00:55:35 +02006383 len = txn->rsp.sl.st.v_l;
6384 ptr = txn->rsp.sol;
6385
6386 while ((len-- > 0) && (*ptr++ != '/'));
6387 if (len <= 0)
6388 return 0;
6389
6390 test->ptr = ptr;
6391 test->len = len;
6392
6393 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6394 return 1;
6395}
6396
6397/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006398static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006399acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6400 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006401{
6402 struct http_txn *txn = l7;
6403 char *ptr;
6404 int len;
6405
Willy Tarreaub6866442008-07-14 23:54:42 +02006406 if (!txn)
6407 return 0;
6408
Willy Tarreauc11416f2007-06-17 16:58:38 +02006409 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6410 return 0;
6411
Willy Tarreau8797c062007-05-07 00:55:35 +02006412 len = txn->rsp.sl.st.c_l;
6413 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6414
6415 test->i = __strl2ui(ptr, len);
6416 test->flags = ACL_TEST_F_VOL_1ST;
6417 return 1;
6418}
6419
6420/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006421static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006422acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6423 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006424{
6425 struct http_txn *txn = l7;
6426
Willy Tarreaub6866442008-07-14 23:54:42 +02006427 if (!txn)
6428 return 0;
6429
Willy Tarreauc11416f2007-06-17 16:58:38 +02006430 if (txn->req.msg_state != HTTP_MSG_BODY)
6431 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006432
Willy Tarreauc11416f2007-06-17 16:58:38 +02006433 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6434 /* ensure the indexes are not affected */
6435 return 0;
6436
Willy Tarreau8797c062007-05-07 00:55:35 +02006437 test->len = txn->req.sl.rq.u_l;
6438 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6439
Willy Tarreauf3d25982007-05-08 22:45:09 +02006440 /* we do not need to set READ_ONLY because the data is in a buffer */
6441 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006442 return 1;
6443}
6444
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006445static int
6446acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6447 struct acl_expr *expr, struct acl_test *test)
6448{
6449 struct http_txn *txn = l7;
6450
Willy Tarreaub6866442008-07-14 23:54:42 +02006451 if (!txn)
6452 return 0;
6453
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006454 if (txn->req.msg_state != HTTP_MSG_BODY)
6455 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006456
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006457 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6458 /* ensure the indexes are not affected */
6459 return 0;
6460
6461 /* Parse HTTP request */
6462 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6463 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6464 test->i = AF_INET;
6465
6466 /*
6467 * If we are parsing url in frontend space, we prepare backend stage
6468 * to not parse again the same url ! optimization lazyness...
6469 */
6470 if (px->options & PR_O_HTTP_PROXY)
6471 l4->flags |= SN_ADDR_SET;
6472
6473 test->flags = ACL_TEST_F_READ_ONLY;
6474 return 1;
6475}
6476
6477static int
6478acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6479 struct acl_expr *expr, struct acl_test *test)
6480{
6481 struct http_txn *txn = l7;
6482
Willy Tarreaub6866442008-07-14 23:54:42 +02006483 if (!txn)
6484 return 0;
6485
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006486 if (txn->req.msg_state != HTTP_MSG_BODY)
6487 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006488
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006489 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6490 /* ensure the indexes are not affected */
6491 return 0;
6492
6493 /* Same optimization as url_ip */
6494 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6495 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6496
6497 if (px->options & PR_O_HTTP_PROXY)
6498 l4->flags |= SN_ADDR_SET;
6499
6500 test->flags = ACL_TEST_F_READ_ONLY;
6501 return 1;
6502}
6503
Willy Tarreauc11416f2007-06-17 16:58:38 +02006504/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6505 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6506 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006507static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006508acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006509 struct acl_expr *expr, struct acl_test *test)
6510{
6511 struct http_txn *txn = l7;
6512 struct hdr_idx *idx = &txn->hdr_idx;
6513 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006514
Willy Tarreaub6866442008-07-14 23:54:42 +02006515 if (!txn)
6516 return 0;
6517
Willy Tarreau33a7e692007-06-10 19:45:56 +02006518 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6519 /* search for header from the beginning */
6520 ctx->idx = 0;
6521
Willy Tarreau33a7e692007-06-10 19:45:56 +02006522 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6523 test->flags |= ACL_TEST_F_FETCH_MORE;
6524 test->flags |= ACL_TEST_F_VOL_HDR;
6525 test->len = ctx->vlen;
6526 test->ptr = (char *)ctx->line + ctx->val;
6527 return 1;
6528 }
6529
6530 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6531 test->flags |= ACL_TEST_F_VOL_HDR;
6532 return 0;
6533}
6534
Willy Tarreau33a7e692007-06-10 19:45:56 +02006535static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006536acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6537 struct acl_expr *expr, struct acl_test *test)
6538{
6539 struct http_txn *txn = l7;
6540
Willy Tarreaub6866442008-07-14 23:54:42 +02006541 if (!txn)
6542 return 0;
6543
Willy Tarreauc11416f2007-06-17 16:58:38 +02006544 if (txn->req.msg_state != HTTP_MSG_BODY)
6545 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006546
Willy Tarreauc11416f2007-06-17 16:58:38 +02006547 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6548 /* ensure the indexes are not affected */
6549 return 0;
6550
6551 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6552}
6553
6554static int
6555acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6556 struct acl_expr *expr, struct acl_test *test)
6557{
6558 struct http_txn *txn = l7;
6559
Willy Tarreaub6866442008-07-14 23:54:42 +02006560 if (!txn)
6561 return 0;
6562
Willy Tarreauc11416f2007-06-17 16:58:38 +02006563 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6564 return 0;
6565
6566 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6567}
6568
6569/* 6. Check on HTTP header count. The number of occurrences is returned.
6570 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6571 */
6572static int
6573acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006574 struct acl_expr *expr, struct acl_test *test)
6575{
6576 struct http_txn *txn = l7;
6577 struct hdr_idx *idx = &txn->hdr_idx;
6578 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006579 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006580
Willy Tarreaub6866442008-07-14 23:54:42 +02006581 if (!txn)
6582 return 0;
6583
Willy Tarreau33a7e692007-06-10 19:45:56 +02006584 ctx.idx = 0;
6585 cnt = 0;
6586 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6587 cnt++;
6588
6589 test->i = cnt;
6590 test->flags = ACL_TEST_F_VOL_HDR;
6591 return 1;
6592}
6593
Willy Tarreauc11416f2007-06-17 16:58:38 +02006594static int
6595acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6596 struct acl_expr *expr, struct acl_test *test)
6597{
6598 struct http_txn *txn = l7;
6599
Willy Tarreaub6866442008-07-14 23:54:42 +02006600 if (!txn)
6601 return 0;
6602
Willy Tarreauc11416f2007-06-17 16:58:38 +02006603 if (txn->req.msg_state != HTTP_MSG_BODY)
6604 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006605
Willy Tarreauc11416f2007-06-17 16:58:38 +02006606 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6607 /* ensure the indexes are not affected */
6608 return 0;
6609
6610 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6611}
6612
6613static int
6614acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6615 struct acl_expr *expr, struct acl_test *test)
6616{
6617 struct http_txn *txn = l7;
6618
Willy Tarreaub6866442008-07-14 23:54:42 +02006619 if (!txn)
6620 return 0;
6621
Willy Tarreauc11416f2007-06-17 16:58:38 +02006622 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6623 return 0;
6624
6625 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6626}
6627
Willy Tarreau33a7e692007-06-10 19:45:56 +02006628/* 7. Check on HTTP header's integer value. The integer value is returned.
6629 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006630 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006631 */
6632static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006633acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006634 struct acl_expr *expr, struct acl_test *test)
6635{
6636 struct http_txn *txn = l7;
6637 struct hdr_idx *idx = &txn->hdr_idx;
6638 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006639
Willy Tarreaub6866442008-07-14 23:54:42 +02006640 if (!txn)
6641 return 0;
6642
Willy Tarreau33a7e692007-06-10 19:45:56 +02006643 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6644 /* search for header from the beginning */
6645 ctx->idx = 0;
6646
Willy Tarreau33a7e692007-06-10 19:45:56 +02006647 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6648 test->flags |= ACL_TEST_F_FETCH_MORE;
6649 test->flags |= ACL_TEST_F_VOL_HDR;
6650 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6651 return 1;
6652 }
6653
6654 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6655 test->flags |= ACL_TEST_F_VOL_HDR;
6656 return 0;
6657}
6658
Willy Tarreauc11416f2007-06-17 16:58:38 +02006659static int
6660acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6661 struct acl_expr *expr, struct acl_test *test)
6662{
6663 struct http_txn *txn = l7;
6664
Willy Tarreaub6866442008-07-14 23:54:42 +02006665 if (!txn)
6666 return 0;
6667
Willy Tarreauc11416f2007-06-17 16:58:38 +02006668 if (txn->req.msg_state != HTTP_MSG_BODY)
6669 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006670
Willy Tarreauc11416f2007-06-17 16:58:38 +02006671 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6672 /* ensure the indexes are not affected */
6673 return 0;
6674
6675 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6676}
6677
6678static int
6679acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6680 struct acl_expr *expr, struct acl_test *test)
6681{
6682 struct http_txn *txn = l7;
6683
Willy Tarreaub6866442008-07-14 23:54:42 +02006684 if (!txn)
6685 return 0;
6686
Willy Tarreauc11416f2007-06-17 16:58:38 +02006687 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6688 return 0;
6689
6690 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6691}
6692
Willy Tarreau737b0c12007-06-10 21:28:46 +02006693/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6694 * the first '/' after the possible hostname, and ends before the possible '?'.
6695 */
6696static int
6697acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6698 struct acl_expr *expr, struct acl_test *test)
6699{
6700 struct http_txn *txn = l7;
6701 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006702
Willy Tarreaub6866442008-07-14 23:54:42 +02006703 if (!txn)
6704 return 0;
6705
Willy Tarreauc11416f2007-06-17 16:58:38 +02006706 if (txn->req.msg_state != HTTP_MSG_BODY)
6707 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006708
Willy Tarreauc11416f2007-06-17 16:58:38 +02006709 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6710 /* ensure the indexes are not affected */
6711 return 0;
6712
Willy Tarreau21d2af32008-02-14 20:25:24 +01006713 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6714 ptr = http_get_path(txn);
6715 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006716 return 0;
6717
6718 /* OK, we got the '/' ! */
6719 test->ptr = ptr;
6720
6721 while (ptr < end && *ptr != '?')
6722 ptr++;
6723
6724 test->len = ptr - test->ptr;
6725
6726 /* we do not need to set READ_ONLY because the data is in a buffer */
6727 test->flags = ACL_TEST_F_VOL_1ST;
6728 return 1;
6729}
6730
6731
Willy Tarreau8797c062007-05-07 00:55:35 +02006732
6733/************************************************************************/
6734/* All supported keywords must be declared here. */
6735/************************************************************************/
6736
6737/* Note: must not be declared <const> as its list will be overwritten */
6738static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006739 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6740 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6741 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6742 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006743
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006744 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6745 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6746 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6747 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6748 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6749 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6750 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6751 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6752 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006753
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006754 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6755 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6756 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6757 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6758 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6759 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6760 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6761 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6762 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6763 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006764
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006765 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6766 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6767 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6768 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6769 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6770 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6771 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6772 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6773 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006774
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006775 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6776 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6777 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6778 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6779 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6780 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6781 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006782
Willy Tarreauf3d25982007-05-08 22:45:09 +02006783 { NULL, NULL, NULL, NULL },
6784
6785#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006786 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6787 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6788 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6789 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6790 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6791 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6792 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6793
Willy Tarreau8797c062007-05-07 00:55:35 +02006794 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6795 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6796 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6797 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6798 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6799 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6800 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6801 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6802
6803 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6804 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6805 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6806 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6807 { NULL, NULL, NULL, NULL },
6808#endif
6809}};
6810
6811
6812__attribute__((constructor))
6813static void __http_protocol_init(void)
6814{
6815 acl_register_keywords(&acl_kws);
6816}
6817
6818
Willy Tarreau58f10d72006-12-04 02:26:12 +01006819/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006820 * Local variables:
6821 * c-indent-level: 8
6822 * c-basic-offset: 8
6823 * End:
6824 */