blob: b53bb6c8185829a065e532f4bf58d0b2076b5c96 [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 Tarreaufa7e1022008-10-19 07:30:41 +02003866/*
3867 * Manages the server FSM and its socket during the DATA phase. It must not be
3868 * called when a file descriptor is not attached to the buffer. It must only be
3869 * called during SI_ST_EST. It normally returns zero, but may return 1 if it
3870 * absolutely wants to be called again.
3871 */
3872int process_srv_data(struct session *t)
3873{
3874 struct buffer *req = t->req;
3875 struct buffer *rep = t->rep;
3876 int fd = req->cons->fd;
Willy Tarreaudafde432008-08-17 01:00:46 +02003877
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003878 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3879 now_ms, __FUNCTION__,
3880 cli_stnames[t->cli_state],
3881 rep->rex, req->wex,
3882 req->flags, rep->flags,
3883 req->l, rep->l);
3884
Willy Tarreau8fbd3b42008-08-27 20:09:19 +02003885 /* Read or write error on the file descriptor */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003886 if (fdtab[fd].state == FD_STERROR) {
3887 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreau8fbd3b42008-08-27 20:09:19 +02003888 if (!req->cons->err_type) {
3889 req->cons->err_loc = t->srv;
3890 req->cons->err_type = SI_ET_DATA_ERR;
3891 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003892 buffer_shutw(req);
3893 req->flags |= BF_WRITE_ERROR;
3894 buffer_shutr(rep);
3895 rep->flags |= BF_READ_ERROR;
Willy Tarreau8fbd3b42008-08-27 20:09:19 +02003896
3897 do_close_and_return:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003898 fd_delete(fd);
3899 req->cons->state = SI_ST_CLO;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003900 return 0;
3901 }
3902
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003903 /* Check if we need to close the read side */
3904 if (!(rep->flags & BF_SHUTR)) {
3905 /* Last read, forced read-shutdown, or other end closed */
3906 if (rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3907 trace_term(t, TT_HTTP_SRV_10);
3908 do_close_read:
3909 buffer_shutr(rep);
3910 if (req->flags & BF_SHUTW)
3911 goto do_close_and_return;
Willy Tarreau461f6622008-08-15 23:43:19 +02003912
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003913 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau376580a2008-08-27 18:52:22 +02003914 }
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003915 /* Read timeout */
3916 else if (unlikely(!(rep->flags & BF_READ_TIMEOUT) && tick_is_expired(rep->rex, now_ms))) {
3917 trace_term(t, TT_HTTP_SRV_12);
3918 rep->flags |= BF_READ_TIMEOUT;
3919 if (!req->cons->err_type) {
3920 req->cons->err_loc = t->srv;
3921 req->cons->err_type = SI_ET_DATA_TO;
3922 }
3923 goto do_close_read;
Willy Tarreau376580a2008-08-27 18:52:22 +02003924 }
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003925 /* Read not closed, update FD status and timeout for reads */
3926 else if (rep->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau8a818832008-08-27 21:10:25 +02003927 /* stop reading */
3928 EV_FD_COND_C(fd, DIR_RD);
3929 rep->rex = TICK_ETERNITY;
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003930 }
3931 else {
Willy Tarreau8a818832008-08-27 21:10:25 +02003932 /* (re)start reading and update timeout. Note: we don't recompute the timeout
3933 * everytime we get here, otherwise it would risk never to expire. We only
3934 * update it if is was not yet set, or if we already got some read status.
3935 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003936 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau8a818832008-08-27 21:10:25 +02003937 if (!tick_isset(rep->rex) || rep->flags & BF_READ_STATUS)
3938 rep->rex = tick_add_ifset(now_ms, rep->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003939 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003940 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003941
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003942 /* Check if we need to close the write side */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003943 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003944 /* Forced write-shutdown or other end closed with empty buffer. */
3945 if ((req->flags & BF_SHUTW_NOW) ||
3946 (req->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) {
3947 trace_term(t, TT_HTTP_SRV_11);
3948 do_close_write:
3949 buffer_shutw(req);
3950 if (rep->flags & BF_SHUTR)
3951 goto do_close_and_return;
3952
3953 EV_FD_CLR(fd, DIR_WR);
3954 shutdown(fd, SHUT_WR);
3955 }
3956 /* Write timeout */
3957 else if (unlikely(!(req->flags & BF_WRITE_TIMEOUT) && tick_is_expired(req->wex, now_ms))) {
3958 trace_term(t, TT_HTTP_SRV_13);
3959 req->flags |= BF_WRITE_TIMEOUT;
3960 if (!req->cons->err_type) {
3961 req->cons->err_loc = t->srv;
3962 req->cons->err_type = SI_ET_DATA_TO;
3963 }
3964 goto do_close_write;
3965 }
3966 /* Write not closed, update FD status and timeout for writes */
3967 else if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003968 /* stop writing */
Willy Tarreau8a818832008-08-27 21:10:25 +02003969 EV_FD_COND_C(fd, DIR_WR);
3970 req->wex = TICK_ETERNITY;
Willy Tarreau2ac679d2008-08-27 20:35:41 +02003971 }
3972 else {
Willy Tarreau8a818832008-08-27 21:10:25 +02003973 /* (re)start writing and update timeout. Note: we don't recompute the timeout
3974 * everytime we get here, otherwise it would risk never to expire. We only
3975 * update it if is was not yet set, or if we already got some write status.
3976 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003977 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau8a818832008-08-27 21:10:25 +02003978 if (!tick_isset(req->wex) || req->flags & BF_WRITE_STATUS) {
Willy Tarreau8fbd3b42008-08-27 20:09:19 +02003979 req->wex = tick_add_ifset(now_ms, req->wto);
Willy Tarreau8a818832008-08-27 21:10:25 +02003980 if (tick_isset(req->wex) && !(rep->flags & BF_SHUTR) && tick_isset(rep->rex)) {
Willy Tarreau8fbd3b42008-08-27 20:09:19 +02003981 /* Note: depending on the protocol, we don't know if we're waiting
3982 * for incoming data or not. So in order to prevent the socket from
3983 * expiring read timeouts during writes, we refresh the read timeout,
3984 * except if it was already infinite.
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003985 */
3986 rep->rex = req->wex;
3987 }
3988 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003989 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003990 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003991 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003992}
3993
3994
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003995///*
3996// * Manages the client FSM and its socket. It normally returns zero, but may
3997// * return 1 if it absolutely wants to be called again.
3998// *
3999// * Note: process_cli is the ONLY function allowed to set cli_state to anything
4000// * but CL_STCLOSE.
4001// */
4002//int process_cli(struct session *t)
4003//{
4004// struct buffer *req = t->req;
4005// struct buffer *rep = t->rep;
4006//
4007// 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",
4008// now_ms, __FUNCTION__,
4009// cli_stnames[t->cli_state],
4010// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
4011// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
4012// req->rex, rep->wex,
4013// req->flags, rep->flags,
4014// req->l, rep->l);
4015//
4016// update_state:
4017// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
4018// * still set this state (and will do until unix sockets are converted).
4019// */
4020// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
4021// /* we can skip most of the tests at once if some conditions are not met */
4022// if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4023// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4024// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
4025// (!(rep->flags & BF_SHUTW) &&
4026// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
4027// goto update_timeouts;
4028//
4029// /* read or write error */
4030// if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
4031// buffer_shutr(req);
4032// buffer_shutw(rep);
4033// fd_delete(t->cli_fd);
4034// t->cli_state = CL_STCLOSE;
4035// trace_term(t, TT_HTTP_CLI_1);
4036// if (!req->analysers) {
4037// if (!(t->flags & SN_ERR_MASK))
4038// t->flags |= SN_ERR_CLICL;
4039// if (!(t->flags & SN_FINST_MASK)) {
4040// if (t->pend_pos)
4041// t->flags |= SN_FINST_Q;
4042// else if (!(req->flags & BF_CONNECTED))
4043// t->flags |= SN_FINST_C;
4044// else
4045// t->flags |= SN_FINST_D;
4046// }
4047// }
4048// goto update_state;
4049// }
4050// /* last read, or end of server write */
4051// else if (!(req->flags & BF_SHUTR) && /* not already done */
4052// req->flags & (BF_READ_NULL | BF_SHUTW)) {
4053// buffer_shutr(req);
4054// if (!(rep->flags & BF_SHUTW)) {
4055// EV_FD_CLR(t->cli_fd, DIR_RD);
4056// trace_term(t, TT_HTTP_CLI_2);
4057// } else {
4058// /* output was already closed */
4059// fd_delete(t->cli_fd);
4060// t->cli_state = CL_STCLOSE;
4061// trace_term(t, TT_HTTP_CLI_3);
4062// }
4063// goto update_state;
4064// }
4065// /* last server read and buffer empty : we only check them when we're
4066// * allowed to forward the data.
4067// */
4068// else if (!(rep->flags & BF_SHUTW) && /* not already done */
4069// rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
4070// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
4071// buffer_shutw(rep);
4072// if (!(req->flags & BF_SHUTR)) {
4073// EV_FD_CLR(t->cli_fd, DIR_WR);
4074// shutdown(t->cli_fd, SHUT_WR);
4075// /* We must ensure that the read part is still alive when switching to shutw */
4076// /* FIXME: is this still true ? */
4077// EV_FD_SET(t->cli_fd, DIR_RD);
4078// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4079// trace_term(t, TT_HTTP_CLI_4);
4080// } else {
4081// fd_delete(t->cli_fd);
4082// t->cli_state = CL_STCLOSE;
4083// trace_term(t, TT_HTTP_CLI_5);
4084// }
4085// goto update_state;
4086// }
4087// /* read timeout */
4088// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4089// buffer_shutr(req);
4090// if (!(rep->flags & BF_SHUTW)) {
4091// EV_FD_CLR(t->cli_fd, DIR_RD);
4092// trace_term(t, TT_HTTP_CLI_6);
4093// } else {
4094// /* output was already closed */
4095// fd_delete(t->cli_fd);
4096// t->cli_state = CL_STCLOSE;
4097// trace_term(t, TT_HTTP_CLI_7);
4098// }
4099// if (!req->analysers) {
4100// if (!(t->flags & SN_ERR_MASK))
4101// t->flags |= SN_ERR_CLITO;
4102// if (!(t->flags & SN_FINST_MASK)) {
4103// if (t->pend_pos)
4104// t->flags |= SN_FINST_Q;
4105// else if (!(req->flags & BF_CONNECTED))
4106// t->flags |= SN_FINST_C;
4107// else
4108// t->flags |= SN_FINST_D;
4109// }
4110// }
4111// goto update_state;
4112// }
4113// /* write timeout */
4114// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4115// buffer_shutw(rep);
4116// if (!(req->flags & BF_SHUTR)) {
4117// EV_FD_CLR(t->cli_fd, DIR_WR);
4118// shutdown(t->cli_fd, SHUT_WR);
4119// /* We must ensure that the read part is still alive when switching to shutw */
4120// /* FIXME: is this still true ? */
4121// EV_FD_SET(t->cli_fd, DIR_RD);
4122// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4123// trace_term(t, TT_HTTP_CLI_8);
4124// } else {
4125// fd_delete(t->cli_fd);
4126// t->cli_state = CL_STCLOSE;
4127// trace_term(t, TT_HTTP_CLI_9);
4128// }
4129// if (!req->analysers) {
4130// if (!(t->flags & SN_ERR_MASK))
4131// t->flags |= SN_ERR_CLITO;
4132// if (!(t->flags & SN_FINST_MASK)) {
4133// if (t->pend_pos)
4134// t->flags |= SN_FINST_Q;
4135// else if (!(req->flags & BF_CONNECTED))
4136// t->flags |= SN_FINST_C;
4137// else
4138// t->flags |= SN_FINST_D;
4139// }
4140// }
4141// goto update_state;
4142// }
4143//
4144// update_timeouts:
4145// /* manage read timeout */
4146// if (!(req->flags & BF_SHUTR)) {
4147// if (req->flags & BF_FULL) {
4148// /* no room to read more data */
4149// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
4150// /* stop reading until we get some space */
4151// req->rex = TICK_ETERNITY;
4152// }
4153// } else {
4154// EV_FD_COND_S(t->cli_fd, DIR_RD);
4155// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4156// }
4157// }
4158//
4159// /* manage write timeout */
4160// if (!(rep->flags & BF_SHUTW)) {
4161// /* first, we may have to produce data (eg: stats).
4162// * right now, this is limited to the SHUTR state.
4163// */
4164// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
4165// produce_content(t);
4166// if (rep->flags & BF_EMPTY) {
4167// buffer_shutw(rep);
4168// fd_delete(t->cli_fd);
4169// t->cli_state = CL_STCLOSE;
4170// trace_term(t, TT_HTTP_CLI_10);
4171// goto update_state;
4172// }
4173// }
4174//
4175// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
4176// if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
4177// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
4178// /* stop writing */
4179// rep->wex = TICK_ETERNITY;
4180// }
4181// } else {
4182// /* buffer not empty */
4183// EV_FD_COND_S(t->cli_fd, DIR_WR);
4184// if (!tick_isset(rep->wex)) {
4185// /* restart writing */
4186// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
4187// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
4188// /* FIXME: to prevent the client from expiring read timeouts during writes,
4189// * we refresh it, except if it was already infinite. */
4190// req->rex = rep->wex;
4191// }
4192// }
4193// }
4194// }
4195// return 0; /* other cases change nothing */
4196// }
4197// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
4198// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
4199// int len;
4200// 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);
4201// write(1, trash, len);
4202// }
4203// return 0;
4204// }
4205//#ifdef DEBUG_DEV
4206// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
4207// ABORT_NOW();
4208//#endif
4209// return 0;
4210//}
4211//
4212//
4213///* Return 1 if we could get a new connection for session t, otherwise zero */
4214//int tcp_get_connection(struct session *t)
4215//{
4216// struct http_txn *txn = &t->txn;
4217// struct buffer *req = t->req;
4218// struct buffer *rep = t->rep;
4219//
4220// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4221// now_ms, __FUNCTION__,
4222// cli_stnames[t->cli_state],
4223// rep->rex, req->wex,
4224// req->flags, rep->flags,
4225// req->l, rep->l);
4226//
4227//
4228// if ((rep->flags & BF_SHUTW) ||
4229// ((req->flags & BF_SHUTR) &&
4230// (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4231// req->wex = TICK_ETERNITY;
4232// if (t->pend_pos)
4233// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4234// /* note that this must not return any error because it would be able to
4235// * overwrite the client_retnclose() output.
4236// */
4237// if (txn->flags & TX_CLTARPIT)
4238// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
4239// else
4240// srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
4241//
4242// trace_term(t, TT_HTTP_SRV_1);
4243// return 0;
4244// }
4245//
4246// /* stop here if we're not allowed to connect */
4247// if (!(req->flags & BF_MAY_FORWARD))
4248// return 0;
4249//
4250// /* the client allows the server to connect */
4251// if (txn->flags & TX_CLTARPIT) {
4252// /* This connection is being tarpitted. The CLIENT side has
4253// * already set the connect expiration date to the right
4254// * timeout. We just have to check that it has not expired.
4255// */
4256// if (!(req->flags & BF_WRITE_TIMEOUT))
4257// return 0;
4258//
4259// /* We will set the queue timer to the time spent, just for
4260// * logging purposes. We fake a 500 server error, so that the
4261// * attacker will not suspect his connection has been tarpitted.
4262// * It will not cause trouble to the logs because we can exclude
4263// * the tarpitted connections by filtering on the 'PT' status flags.
4264// */
4265// req->wex = TICK_ETERNITY;
4266// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4267// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
4268// 500, error_message(t, HTTP_ERR_500));
4269// trace_term(t, TT_HTTP_SRV_2);
4270// return 0;
4271// }
4272//
4273// /* Right now, we will need to create a connection to the server.
4274// * We might already have tried, and got a connection pending, in
4275// * which case we will not do anything till it's pending. It's up
4276// * to any other session to release it and wake us up again.
4277// */
4278// if (t->pend_pos) {
4279// if (!(req->flags & BF_WRITE_TIMEOUT)) {
4280// return 0;
4281// } else {
4282// /* we've been waiting too long here */
4283// req->wex = TICK_ETERNITY;
4284// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4285// srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
4286// 503, error_message(t, HTTP_ERR_503));
4287// trace_term(t, TT_HTTP_SRV_3);
4288// if (t->srv)
4289// t->srv->failed_conns++;
4290// t->be->failed_conns++;
4291// return 0;
4292// }
4293// }
4294//
4295// do {
4296// if (srv_redispatch_connect(t) != 0)
4297// return 0;
4298//
4299// if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
4300// /* Server supporting redirection and it is possible.
4301// * Invalid requests are reported as such. It concerns all
4302// * the largest ones.
4303// */
4304// struct chunk rdr;
4305// char *path;
4306// int len;
4307//
4308// /* 1: create the response header */
4309// rdr.len = strlen(HTTP_302);
4310// rdr.str = trash;
4311// memcpy(rdr.str, HTTP_302, rdr.len);
4312//
4313// /* 2: add the server's prefix */
4314// if (rdr.len + t->srv->rdr_len > sizeof(trash))
4315// goto cancel_redir;
4316//
4317// memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
4318// rdr.len += t->srv->rdr_len;
4319//
4320// /* 3: add the request URI */
4321// path = http_get_path(txn);
4322// if (!path)
4323// goto cancel_redir;
4324// len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
4325// if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
4326// goto cancel_redir;
4327//
4328// memcpy(rdr.str + rdr.len, path, len);
4329// rdr.len += len;
4330// memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
4331// rdr.len += 4;
4332//
4333// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
4334// trace_term(t, TT_HTTP_SRV_3);
4335//
4336// /* FIXME: we should increase a counter of redirects per server and per backend. */
4337// if (t->srv)
4338// t->srv->cum_sess++;
4339// return 0;
4340// cancel_redir:
4341// txn->status = 400;
4342// t->fe->failed_req++;
4343// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
4344// 400, error_message(t, HTTP_ERR_400));
4345// trace_term(t, TT_HTTP_SRV_4);
4346// return 0;
4347// }
4348//
4349// /* try to (re-)connect to the server, and fail if we expire the
4350// * number of retries.
4351// */
4352// if (srv_retryable_connect(t)) {
4353// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4354// if (!(req->cons.flags & BC_KNOWN))
4355// return 0;
4356// /* We got an FD */
4357// return 1;
4358// }
4359// } while (1);
4360//}
4361//
4362//
4363///* Return 1 if the pending connection has failed and should be retried,
4364// * otherwise zero.
4365// */
4366//int tcp_connection_failed(struct session *t)
4367//{
4368// struct buffer *req = t->req;
4369// struct buffer *rep = t->rep;
4370// int conn_err;
4371//
4372// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4373// now_ms, __FUNCTION__,
4374// cli_stnames[t->cli_state],
4375// rep->rex, req->wex,
4376// req->flags, rep->flags,
4377// req->l, rep->l);
4378//
4379// if ((rep->flags & BF_SHUTW) ||
4380// ((req->flags & BF_SHUTR) &&
4381// ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
4382// t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4383// req->wex = TICK_ETERNITY;
4384// if (!(t->flags & SN_CONN_TAR)) {
4385// /* if we are in turn-around, we have already closed the FD */
4386// fd_delete(req->cons->fd);
4387// req->cons->state = SI_ST_CLO;
4388// if (t->srv) {
4389// t->srv->cur_sess--;
4390// sess_change_server(t, NULL);
4391// }
4392// }
4393//
4394// /* note that this must not return any error because it would be able to
4395// * overwrite the client_retnclose() output.
4396// */
4397// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
4398// trace_term(t, TT_HTTP_SRV_5);
4399// return 0;
4400// }
4401//
4402// if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT)))
4403// return 0; /* nothing changed */
4404//
4405// if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
4406// /* timeout, asynchronous connect error or first write error */
4407// if (t->flags & SN_CONN_TAR) {
4408// /* We are doing a turn-around waiting for a new connection attempt. */
4409// if (!(req->flags & BF_WRITE_TIMEOUT))
4410// return 0;
4411// t->flags &= ~SN_CONN_TAR;
4412// }
4413// else {
4414// fd_delete(req->cons->fd);
4415// req->cons->state = SI_ST_CLO;
4416// if (t->srv) {
4417// t->srv->cur_sess--;
4418// sess_change_server(t, NULL);
4419// }
4420//
4421// if (!(req->flags & BF_WRITE_STATUS))
4422// conn_err = SN_ERR_SRVTO; // it was a connect timeout.
4423// else
4424// conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
4425//
4426// /* ensure that we have enough retries left */
4427// if (srv_count_retry_down(t, conn_err))
4428// return 0;
4429//
4430// if (req->flags & BF_WRITE_ERROR) {
4431// /* we encountered an immediate connection error, and we
4432// * will have to retry connecting to the same server, most
4433// * likely leading to the same result. To avoid this, we
4434// * fake a connection timeout to retry after a turn-around
4435// * time of 1 second. We will wait in the previous if block.
4436// */
4437// t->flags |= SN_CONN_TAR;
4438// req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
4439// return 0;
4440// }
4441// }
4442//
4443// if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
4444// /* We're on our last chance, and the REDISP option was specified.
4445// * We will ignore cookie and force to balance or use the dispatcher.
4446// */
4447// /* let's try to offer this slot to anybody */
4448// if (may_dequeue_tasks(t->srv, t->be))
4449// process_srv_queue(t->srv);
4450//
4451// /* it's left to the dispatcher to choose a server */
4452// t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
4453// t->prev_srv = t->srv;
4454//
4455// /* first, get a connection */
4456// if (srv_redispatch_connect(t)) {
4457// if (req->cons.flags & BC_KNOWN)
4458// return 0;
4459// /* we need to get a connection */
4460// return 1;
4461// }
4462// } else {
4463// if (t->srv)
4464// t->srv->retries++;
4465// t->be->retries++;
4466// }
4467//
4468// do {
4469// /* Now we will try to either reconnect to the same server or
4470// * connect to another server. If the connection gets queued
4471// * because all servers are saturated, then we will go back to
4472// * the idle state where the buffer's consumer is marked as
4473// * unknown.
4474// */
4475// if (srv_retryable_connect(t)) {
4476// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4477// if (req->cons.flags & BC_KNOWN)
4478// return 0;
4479// /* we did not get a connection */
4480// return 1;
4481// }
4482//
4483// /* we need to redispatch the connection to another server */
4484// if (srv_redispatch_connect(t)) {
4485// if (req->cons.flags & BC_KNOWN)
4486// return 0;
4487// /* we need to get a connection */
4488// return 1;
4489// }
4490// } while (1);
4491// }
4492// else { /* no error and write OK */
4493// t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
4494//
4495// if (req->flags & BF_EMPTY) {
4496// EV_FD_CLR(req->cons->fd, DIR_WR);
4497// req->wex = TICK_ETERNITY;
4498// } else {
4499// EV_FD_SET(req->cons->fd, DIR_WR);
4500// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4501// if (tick_isset(req->wex)) {
4502// /* FIXME: to prevent the server from expiring read timeouts during writes,
4503// * we refresh it. */
4504// rep->rex = req->wex;
4505// }
4506// }
4507//
4508// if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
4509// EV_FD_SET(req->cons->fd, DIR_RD);
4510// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4511// buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
4512//
4513// /* if the user wants to log as soon as possible, without counting
4514// bytes from the server, then this is the right moment. */
4515// if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4516// t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
4517// tcp_sess_log(t);
4518// }
4519//#ifdef CONFIG_HAP_TCPSPLICE
4520// if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
4521// /* TCP splicing supported by both FE and BE */
4522// tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
4523// }
4524//#endif
4525// }
4526// else {
4527// rep->analysers |= AN_RTR_HTTP_HDR;
4528// buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
4529// t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
4530// /* reset hdr_idx which was already initialized by the request.
4531// * right now, the http parser does it.
4532// * hdr_idx_init(&t->txn.hdr_idx);
4533// */
4534// }
4535//
4536// req->flags |= BF_CONNECTED;
4537// if (!rep->analysers)
4538// t->rep->flags |= BF_MAY_FORWARD;
4539// req->wex = TICK_ETERNITY;
4540// return 0;
4541// }
4542//}
4543//
4544//
4545///*
4546// * Tries to establish a connection to the server and associate it to the
4547// * request buffer's consumer side. It normally returns zero, but may return 1
4548// * if it absolutely wants to be called again.
4549// */
4550//int process_srv_conn(struct session *t)
4551//{
4552// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4553// now_ms, __FUNCTION__,
4554// cli_stnames[t->cli_state],
4555// t->rep->rex, t->req->wex,
4556// t->req->flags, t->rep->flags,
4557// t->req->l, t->rep->l);
4558//
4559// while (!(t->req->flags & BF_CONNECTED)) {
4560// if (!(t->req->cons.flags & BC_KNOWN)) {
4561// /* no connection in progress, get a new one */
4562// if (!tcp_get_connection(t))
4563// break;
4564// } else {
4565// /* connection in progress or just completed */
4566// if (!tcp_connection_failed(t))
4567// break;
4568// }
4569// }
4570// return 0;
4571//}
4572//
4573//
4574///*
4575// * Manages the server FSM and its socket during the DATA phase. It must not
4576// * be called when a file descriptor is not attached to the buffer. It normally
4577// * returns zero, but may return 1 if it absolutely wants to be called again.
4578// */
4579//int process_srv_data(struct session *t)
4580//{
4581// struct buffer *req = t->req;
4582// struct buffer *rep = t->rep;
4583//
4584// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4585// now_ms, __FUNCTION__,
4586// cli_stnames[t->cli_state],
4587// rep->rex, req->wex,
4588// req->flags, rep->flags,
4589// req->l, rep->l);
4590//
4591// /* we can skip most of the tests at once if some conditions are not met */
4592// if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4593// (!(req->flags & BF_SHUTW) &&
4594// (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
4595// (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4596// (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
4597// goto update_timeouts;
4598//
4599// /* read or write error */
4600// /* FIXME: what happens when we have to deal with HTTP ??? */
4601// if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
4602// buffer_shutr(rep);
4603// buffer_shutw(req);
4604// fd_delete(req->cons->fd);
4605// req->cons->state = SI_ST_CLO;
4606// if (t->srv) {
4607// t->srv->cur_sess--;
4608// t->srv->failed_resp++;
4609// sess_change_server(t, NULL);
4610// }
4611// t->be->failed_resp++;
4612// trace_term(t, TT_HTTP_SRV_6);
4613// if (!rep->analysers) {
4614// if (!(t->flags & SN_ERR_MASK))
4615// t->flags |= SN_ERR_SRVCL;
4616// if (!(t->flags & SN_FINST_MASK))
4617// t->flags |= SN_FINST_D;
4618// }
4619// if (may_dequeue_tasks(t->srv, t->be))
4620// process_srv_queue(t->srv);
4621//
4622// return 0;
4623// }
4624//
4625// /* last read, or end of client write */
4626// if (!(rep->flags & BF_SHUTR) && /* not already done */
4627// rep->flags & (BF_READ_NULL | BF_SHUTW)) {
4628// buffer_shutr(rep);
4629// if (!(req->flags & BF_SHUTW)) {
4630// EV_FD_CLR(req->cons->fd, DIR_RD);
4631// trace_term(t, TT_HTTP_SRV_7);
4632// } else {
4633// /* output was already closed */
4634// fd_delete(req->cons->fd);
4635// req->cons->state = SI_ST_CLO;
4636// if (t->srv) {
4637// t->srv->cur_sess--;
4638// sess_change_server(t, NULL);
4639// }
4640// trace_term(t, TT_HTTP_SRV_8);
4641//
4642// if (may_dequeue_tasks(t->srv, t->be))
4643// process_srv_queue(t->srv);
4644// return 0;
4645// }
4646// }
4647// /* end of client read and no more data to send. We can forward
4648// * the close when we're allowed to forward data (anytime right
4649// * now). If we're using option forceclose, then we may also
4650// * shutdown the outgoing write channel once the response starts
4651// * coming from the server.
4652// */
4653// if (!(req->flags & BF_SHUTW) && /* not already done */
4654// req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
4655// (req->flags & BF_SHUTR ||
4656// (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
4657// buffer_shutw(req);
4658// if (!(rep->flags & BF_SHUTR)) {
4659// EV_FD_CLR(req->cons->fd, DIR_WR);
4660// shutdown(req->cons->fd, SHUT_WR);
4661// trace_term(t, TT_HTTP_SRV_9);
4662// /* We must ensure that the read part is still alive when switching to shutw */
4663// /* FIXME: is this still true ? */
4664// EV_FD_SET(req->cons->fd, DIR_RD);
4665// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4666// } else {
4667// fd_delete(req->cons->fd);
4668// req->cons->state = SI_ST_CLO;
4669// if (t->srv) {
4670// t->srv->cur_sess--;
4671// sess_change_server(t, NULL);
4672// }
4673// trace_term(t, TT_HTTP_SRV_10);
4674//
4675// if (may_dequeue_tasks(t->srv, t->be))
4676// process_srv_queue(t->srv);
4677// return 0;
4678// }
4679// }
4680//
4681// /* read timeout */
4682// if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4683// if (!rep->analysers) {
4684// if (!(t->flags & SN_ERR_MASK))
4685// t->flags |= SN_ERR_SRVTO;
4686// if (!(t->flags & SN_FINST_MASK))
4687// t->flags |= SN_FINST_D;
4688// }
4689// buffer_shutr(rep);
4690// if (!(req->flags & BF_SHUTW)) {
4691// EV_FD_CLR(req->cons->fd, DIR_RD);
4692// trace_term(t, TT_HTTP_SRV_11);
4693// } else {
4694// fd_delete(req->cons->fd);
4695// req->cons->state = SI_ST_CLO;
4696// if (t->srv) {
4697// t->srv->cur_sess--;
4698// sess_change_server(t, NULL);
4699// }
4700// trace_term(t, TT_HTTP_SRV_12);
4701//
4702// if (may_dequeue_tasks(t->srv, t->be))
4703// process_srv_queue(t->srv);
4704// return 0;
4705// }
4706// }
4707//
4708// /* write timeout */
4709// if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4710// if (!rep->analysers) {
4711// if (!(t->flags & SN_ERR_MASK))
4712// t->flags |= SN_ERR_SRVTO;
4713// if (!(t->flags & SN_FINST_MASK))
4714// t->flags |= SN_FINST_D;
4715// }
4716// buffer_shutw(req);
4717// if (!(rep->flags & BF_SHUTR)) {
4718// EV_FD_CLR(req->cons->fd, DIR_WR);
4719// shutdown(req->cons->fd, SHUT_WR);
4720// /* We must ensure that the read part is still alive when switching to shutw */
4721// /* FIXME: is this still needed ? */
4722// EV_FD_SET(req->cons->fd, DIR_RD);
4723// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4724// trace_term(t, TT_HTTP_SRV_13);
4725// } else {
4726// fd_delete(req->cons->fd);
4727// req->cons->state = SI_ST_CLO;
4728// if (t->srv) {
4729// t->srv->cur_sess--;
4730// sess_change_server(t, NULL);
4731// }
4732// trace_term(t, TT_HTTP_SRV_14);
4733//
4734// if (may_dequeue_tasks(t->srv, t->be))
4735// process_srv_queue(t->srv);
4736// return 0;
4737// }
4738// }
4739//
4740// update_timeouts:
4741// /* manage read timeout */
4742// if (!(rep->flags & BF_SHUTR)) {
4743// if (rep->flags & BF_FULL) {
4744// if (EV_FD_COND_C(req->cons->fd, DIR_RD))
4745// rep->rex = TICK_ETERNITY;
4746// } else {
4747// EV_FD_COND_S(req->cons->fd, DIR_RD);
4748// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4749// }
4750// }
4751//
4752// /* manage write timeout */
4753// if (!(req->flags & BF_SHUTW)) {
4754// if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
4755// /* stop writing */
4756// if (EV_FD_COND_C(req->cons->fd, DIR_WR))
4757// req->wex = TICK_ETERNITY;
4758// } else {
4759// /* buffer not empty, there are still data to be transferred */
4760// EV_FD_COND_S(req->cons->fd, DIR_WR);
4761// if (!tick_isset(req->wex)) {
4762// /* restart writing */
4763// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4764// if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4765// /* FIXME: to prevent the server from expiring read timeouts during writes,
4766// * we refresh it, except if it was already infinite.
4767// */
4768// rep->rex = req->wex;
4769// }
4770// }
4771// }
4772// }
4773// return 0; /* other cases change nothing */
4774//}
4775//
4776
Willy Tarreaubaaee002006-06-26 02:48:02 +02004777/*
4778 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004779 * called with client socket shut down on input. Right now, only statistics can
4780 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02004781 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004782 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004783 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004784 */
4785int produce_content(struct session *s)
4786{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004787 if (s->data_source == DATA_SRC_NONE) {
4788 s->flags &= ~SN_SELF_GEN;
4789 return 1;
4790 }
4791 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004792 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004793 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004794 if (ret >= 0)
4795 return ret;
4796 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004797 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004798
Willy Tarreau91861262007-10-17 17:06:05 +02004799 /* unknown data source or internal error */
4800 s->txn.status = 500;
4801 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004802 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004803 if (!(s->flags & SN_ERR_MASK))
4804 s->flags |= SN_ERR_PRXCOND;
4805 if (!(s->flags & SN_FINST_MASK))
4806 s->flags |= SN_FINST_R;
4807 s->flags &= ~SN_SELF_GEN;
4808 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004809}
4810
4811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004812/* Iterate the same filter through all request headers.
4813 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004814 * Since it can manage the switch to another backend, it updates the per-proxy
4815 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004816 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004817int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004818{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004819 char term;
4820 char *cur_ptr, *cur_end, *cur_next;
4821 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004822 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004823 struct hdr_idx_elem *cur_hdr;
4824 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004826 last_hdr = 0;
4827
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004828 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004829 old_idx = 0;
4830
4831 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004832 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004833 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004834 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004835 (exp->action == ACT_ALLOW ||
4836 exp->action == ACT_DENY ||
4837 exp->action == ACT_TARPIT))
4838 return 0;
4839
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004840 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004841 if (!cur_idx)
4842 break;
4843
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004844 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004845 cur_ptr = cur_next;
4846 cur_end = cur_ptr + cur_hdr->len;
4847 cur_next = cur_end + cur_hdr->cr + 1;
4848
4849 /* Now we have one header between cur_ptr and cur_end,
4850 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004851 */
4852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004853 /* The annoying part is that pattern matching needs
4854 * that we modify the contents to null-terminate all
4855 * strings before testing them.
4856 */
4857
4858 term = *cur_end;
4859 *cur_end = '\0';
4860
4861 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4862 switch (exp->action) {
4863 case ACT_SETBE:
4864 /* It is not possible to jump a second time.
4865 * FIXME: should we return an HTTP/500 here so that
4866 * the admin knows there's a problem ?
4867 */
4868 if (t->be != t->fe)
4869 break;
4870
4871 /* Swithing Proxy */
4872 t->be = (struct proxy *) exp->replace;
4873
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004874 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004875 * because we have associated req_cap and rsp_cap to the
4876 * frontend, and the beconn will be updated later.
4877 */
4878
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004879 t->rep->rto = t->req->wto = t->be->timeout.server;
4880 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004881 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004882 last_hdr = 1;
4883 break;
4884
4885 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004886 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004887 last_hdr = 1;
4888 break;
4889
4890 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004891 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004892 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004893 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004894 break;
4895
4896 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004897 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004898 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004899 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004900 break;
4901
4902 case ACT_REPLACE:
4903 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4904 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4905 /* FIXME: if the user adds a newline in the replacement, the
4906 * index will not be recalculated for now, and the new line
4907 * will not be counted as a new header.
4908 */
4909
4910 cur_end += delta;
4911 cur_next += delta;
4912 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004913 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004914 break;
4915
4916 case ACT_REMOVE:
4917 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4918 cur_next += delta;
4919
4920 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004921 txn->req.eoh += delta;
4922 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4923 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004924 cur_hdr->len = 0;
4925 cur_end = NULL; /* null-term has been rewritten */
4926 break;
4927
4928 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004929 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004930 if (cur_end)
4931 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004933 /* keep the link from this header to next one in case of later
4934 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004935 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004936 old_idx = cur_idx;
4937 }
4938 return 0;
4939}
4940
4941
4942/* Apply the filter to the request line.
4943 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4944 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945 * Since it can manage the switch to another backend, it updates the per-proxy
4946 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004947 */
4948int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4949{
4950 char term;
4951 char *cur_ptr, *cur_end;
4952 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004953 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004955
Willy Tarreau58f10d72006-12-04 02:26:12 +01004956
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004959 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004960 (exp->action == ACT_ALLOW ||
4961 exp->action == ACT_DENY ||
4962 exp->action == ACT_TARPIT))
4963 return 0;
4964 else if (exp->action == ACT_REMOVE)
4965 return 0;
4966
4967 done = 0;
4968
Willy Tarreau9cdde232007-05-02 20:58:19 +02004969 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004970 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004971
4972 /* Now we have the request line between cur_ptr and cur_end */
4973
4974 /* The annoying part is that pattern matching needs
4975 * that we modify the contents to null-terminate all
4976 * strings before testing them.
4977 */
4978
4979 term = *cur_end;
4980 *cur_end = '\0';
4981
4982 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4983 switch (exp->action) {
4984 case ACT_SETBE:
4985 /* It is not possible to jump a second time.
4986 * FIXME: should we return an HTTP/500 here so that
4987 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004988 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004989 if (t->be != t->fe)
4990 break;
4991
4992 /* Swithing Proxy */
4993 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004995 /* right now, the backend switch is not too much complicated
4996 * because we have associated req_cap and rsp_cap to the
4997 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004998 */
4999
Willy Tarreaud7c30f92007-12-03 01:38:36 +01005000 t->rep->rto = t->req->wto = t->be->timeout.server;
5001 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02005002 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005003 done = 1;
5004 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005005
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005006 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005007 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005008 done = 1;
5009 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005010
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005011 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005012 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005013 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 done = 1;
5015 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005019 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 done = 1;
5021 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005023 case ACT_REPLACE:
5024 *cur_end = term; /* restore the string terminator */
5025 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5026 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5027 /* FIXME: if the user adds a newline in the replacement, the
5028 * index will not be recalculated for now, and the new line
5029 * will not be counted as a new header.
5030 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005031
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005032 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005033 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005034
Willy Tarreau9cdde232007-05-02 20:58:19 +02005035 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005036 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005037 HTTP_MSG_RQMETH,
5038 cur_ptr, cur_end + 1,
5039 NULL, NULL);
5040 if (unlikely(!cur_end))
5041 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005043 /* we have a full request and we know that we have either a CR
5044 * or an LF at <ptr>.
5045 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005046 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5047 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005048 /* there is no point trying this regex on headers */
5049 return 1;
5050 }
5051 }
5052 *cur_end = term; /* restore the string terminator */
5053 return done;
5054}
Willy Tarreau97de6242006-12-27 17:18:38 +01005055
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056
Willy Tarreau58f10d72006-12-04 02:26:12 +01005057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005058/*
5059 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5060 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005061 * unparsable request. Since it can manage the switch to another backend, it
5062 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 */
5064int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5065{
Willy Tarreau3d300592007-03-18 18:34:41 +01005066 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005067 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005068 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005069 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 /*
5072 * The interleaving of transformations and verdicts
5073 * makes it difficult to decide to continue or stop
5074 * the evaluation.
5075 */
5076
Willy Tarreau3d300592007-03-18 18:34:41 +01005077 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5079 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5080 exp = exp->next;
5081 continue;
5082 }
5083
5084 /* Apply the filter to the request line. */
5085 ret = apply_filter_to_req_line(t, req, exp);
5086 if (unlikely(ret < 0))
5087 return -1;
5088
5089 if (likely(ret == 0)) {
5090 /* The filter did not match the request, it can be
5091 * iterated through all headers.
5092 */
5093 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005094 }
5095 exp = exp->next;
5096 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098}
5099
5100
Willy Tarreaua15645d2007-03-18 16:22:39 +01005101
Willy Tarreau58f10d72006-12-04 02:26:12 +01005102/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005103 * Manage client-side cookie. It can impact performance by about 2% so it is
5104 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005105 */
5106void manage_client_side_cookies(struct session *t, struct buffer *req)
5107{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005108 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005109 char *p1, *p2, *p3, *p4;
5110 char *del_colon, *del_cookie, *colon;
5111 int app_cookies;
5112
5113 appsess *asession_temp = NULL;
5114 appsess local_asession;
5115
5116 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005117 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005118
Willy Tarreau2a324282006-12-05 00:05:46 +01005119 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005120 * we start with the start line.
5121 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005122 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005123 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005125 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005126 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005127 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005129 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130 cur_ptr = cur_next;
5131 cur_end = cur_ptr + cur_hdr->len;
5132 cur_next = cur_end + cur_hdr->cr + 1;
5133
5134 /* We have one full header between cur_ptr and cur_end, and the
5135 * next header starts at cur_next. We're only interested in
5136 * "Cookie:" headers.
5137 */
5138
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005139 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5140 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005141 old_idx = cur_idx;
5142 continue;
5143 }
5144
5145 /* Now look for cookies. Conforming to RFC2109, we have to support
5146 * attributes whose name begin with a '$', and associate them with
5147 * the right cookie, if we want to delete this cookie.
5148 * So there are 3 cases for each cookie read :
5149 * 1) it's a special attribute, beginning with a '$' : ignore it.
5150 * 2) it's a server id cookie that we *MAY* want to delete : save
5151 * some pointers on it (last semi-colon, beginning of cookie...)
5152 * 3) it's an application cookie : we *MAY* have to delete a previous
5153 * "special" cookie.
5154 * At the end of loop, if a "special" cookie remains, we may have to
5155 * remove it. If no application cookie persists in the header, we
5156 * *MUST* delete it
5157 */
5158
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005159 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005160
Willy Tarreau58f10d72006-12-04 02:26:12 +01005161 /* del_cookie == NULL => nothing to be deleted */
5162 del_colon = del_cookie = NULL;
5163 app_cookies = 0;
5164
5165 while (p1 < cur_end) {
5166 /* skip spaces and colons, but keep an eye on these ones */
5167 while (p1 < cur_end) {
5168 if (*p1 == ';' || *p1 == ',')
5169 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005170 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005171 break;
5172 p1++;
5173 }
5174
5175 if (p1 == cur_end)
5176 break;
5177
5178 /* p1 is at the beginning of the cookie name */
5179 p2 = p1;
5180 while (p2 < cur_end && *p2 != '=')
5181 p2++;
5182
5183 if (p2 == cur_end)
5184 break;
5185
5186 p3 = p2 + 1; /* skips the '=' sign */
5187 if (p3 == cur_end)
5188 break;
5189
5190 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005191 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005192 p4++;
5193
5194 /* here, we have the cookie name between p1 and p2,
5195 * and its value between p3 and p4.
5196 * we can process it :
5197 *
5198 * Cookie: NAME=VALUE;
5199 * | || || |
5200 * | || || +--> p4
5201 * | || |+-------> p3
5202 * | || +--------> p2
5203 * | |+------------> p1
5204 * | +-------------> colon
5205 * +--------------------> cur_ptr
5206 */
5207
5208 if (*p1 == '$') {
5209 /* skip this one */
5210 }
5211 else {
5212 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005213 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005214 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005215 (p4 - p1 >= t->fe->capture_namelen) &&
5216 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217 int log_len = p4 - p1;
5218
Willy Tarreau086b3b42007-05-13 21:45:51 +02005219 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005220 Alert("HTTP logging : out of memory.\n");
5221 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005222 if (log_len > t->fe->capture_len)
5223 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005224 memcpy(txn->cli_cookie, p1, log_len);
5225 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005226 }
5227 }
5228
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005229 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5230 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005231 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005232 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 char *delim;
5234
5235 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5236 * have the server ID betweek p3 and delim, and the original cookie between
5237 * delim+1 and p4. Otherwise, delim==p4 :
5238 *
5239 * Cookie: NAME=SRV~VALUE;
5240 * | || || | |
5241 * | || || | +--> p4
5242 * | || || +--------> delim
5243 * | || |+-----------> p3
5244 * | || +------------> p2
5245 * | |+----------------> p1
5246 * | +-----------------> colon
5247 * +------------------------> cur_ptr
5248 */
5249
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005250 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005251 for (delim = p3; delim < p4; delim++)
5252 if (*delim == COOKIE_DELIM)
5253 break;
5254 }
5255 else
5256 delim = p4;
5257
5258
5259 /* Here, we'll look for the first running server which supports the cookie.
5260 * This allows to share a same cookie between several servers, for example
5261 * to dedicate backup servers to specific servers only.
5262 * However, to prevent clients from sticking to cookie-less backup server
5263 * when they have incidentely learned an empty cookie, we simply ignore
5264 * empty cookies and mark them as invalid.
5265 */
5266 if (delim == p3)
5267 srv = NULL;
5268
5269 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005270 if (srv->cookie && (srv->cklen == delim - p3) &&
5271 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005272 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005273 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005274 txn->flags &= ~TX_CK_MASK;
5275 txn->flags |= TX_CK_VALID;
5276 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005277 t->srv = srv;
5278 break;
5279 } else {
5280 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005281 txn->flags &= ~TX_CK_MASK;
5282 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005283 }
5284 }
5285 srv = srv->next;
5286 }
5287
Willy Tarreau3d300592007-03-18 18:34:41 +01005288 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005289 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005290 txn->flags &= ~TX_CK_MASK;
5291 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005292 }
5293
5294 /* depending on the cookie mode, we may have to either :
5295 * - delete the complete cookie if we're in insert+indirect mode, so that
5296 * the server never sees it ;
5297 * - remove the server id from the cookie value, and tag the cookie as an
5298 * application cookie so that it does not get accidentely removed later,
5299 * if we're in cookie prefix mode
5300 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005301 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005302 int delta; /* negative */
5303
5304 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5305 p4 += delta;
5306 cur_end += delta;
5307 cur_next += delta;
5308 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005309 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005310
5311 del_cookie = del_colon = NULL;
5312 app_cookies++; /* protect the header from deletion */
5313 }
5314 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005315 (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 +01005316 del_cookie = p1;
5317 del_colon = colon;
5318 }
5319 } else {
5320 /* now we know that we must keep this cookie since it's
5321 * not ours. But if we wanted to delete our cookie
5322 * earlier, we cannot remove the complete header, but we
5323 * can remove the previous block itself.
5324 */
5325 app_cookies++;
5326
5327 if (del_cookie != NULL) {
5328 int delta; /* negative */
5329
5330 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5331 p4 += delta;
5332 cur_end += delta;
5333 cur_next += delta;
5334 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005335 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005336 del_cookie = del_colon = NULL;
5337 }
5338 }
5339
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005340 if ((t->be->appsession_name != NULL) &&
5341 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005342 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005343
Willy Tarreau58f10d72006-12-04 02:26:12 +01005344 /* Cool... it's the right one */
5345
5346 asession_temp = &local_asession;
5347
Willy Tarreau63963c62007-05-13 21:29:55 +02005348 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5350 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5351 return;
5352 }
5353
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005354 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5355 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005356 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02005357
Willy Tarreau58f10d72006-12-04 02:26:12 +01005358 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005359 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5360 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005361 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005362 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005363 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005364 Alert("Not enough memory process_cli():asession:calloc().\n");
5365 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5366 return;
5367 }
5368
5369 asession_temp->sessid = local_asession.sessid;
5370 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005371 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005372 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005373 } else {
5374 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005375 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005376 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005377 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005378 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 Alert("Found Application Session without matching server.\n");
5380 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005381 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005382 while (srv) {
5383 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005384 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005386 txn->flags &= ~TX_CK_MASK;
5387 txn->flags |= TX_CK_VALID;
5388 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005389 t->srv = srv;
5390 break;
5391 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005392 txn->flags &= ~TX_CK_MASK;
5393 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005394 }
5395 }
5396 srv = srv->next;
5397 }/* end while(srv) */
5398 }/* end else if server == NULL */
5399
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005400 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005401 asession_temp->request_count++;
5402#if defined(DEBUG_HASH)
5403 Alert("manage_client_side_cookies\n");
5404 appsession_hash_dump(&(t->be->htbl_proxy));
5405#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005406 }/* end if ((t->proxy->appsession_name != NULL) ... */
5407 }
5408
5409 /* we'll have to look for another cookie ... */
5410 p1 = p4;
5411 } /* while (p1 < cur_end) */
5412
5413 /* There's no more cookie on this line.
5414 * We may have marked the last one(s) for deletion.
5415 * We must do this now in two ways :
5416 * - if there is no app cookie, we simply delete the header ;
5417 * - if there are app cookies, we must delete the end of the
5418 * string properly, including the colon/semi-colon before
5419 * the cookie name.
5420 */
5421 if (del_cookie != NULL) {
5422 int delta;
5423 if (app_cookies) {
5424 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5425 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005426 cur_hdr->len += delta;
5427 } else {
5428 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429
5430 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005431 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5432 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005433 cur_hdr->len = 0;
5434 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005435 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005436 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005437 }
5438
5439 /* keep the link from this header to next one */
5440 old_idx = cur_idx;
5441 } /* end of cookie processing on this header */
5442}
5443
5444
Willy Tarreaua15645d2007-03-18 16:22:39 +01005445/* Iterate the same filter through all response headers contained in <rtr>.
5446 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5447 */
5448int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5449{
5450 char term;
5451 char *cur_ptr, *cur_end, *cur_next;
5452 int cur_idx, old_idx, last_hdr;
5453 struct http_txn *txn = &t->txn;
5454 struct hdr_idx_elem *cur_hdr;
5455 int len, delta;
5456
5457 last_hdr = 0;
5458
5459 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5460 old_idx = 0;
5461
5462 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005463 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005464 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005465 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005466 (exp->action == ACT_ALLOW ||
5467 exp->action == ACT_DENY))
5468 return 0;
5469
5470 cur_idx = txn->hdr_idx.v[old_idx].next;
5471 if (!cur_idx)
5472 break;
5473
5474 cur_hdr = &txn->hdr_idx.v[cur_idx];
5475 cur_ptr = cur_next;
5476 cur_end = cur_ptr + cur_hdr->len;
5477 cur_next = cur_end + cur_hdr->cr + 1;
5478
5479 /* Now we have one header between cur_ptr and cur_end,
5480 * and the next header starts at cur_next.
5481 */
5482
5483 /* The annoying part is that pattern matching needs
5484 * that we modify the contents to null-terminate all
5485 * strings before testing them.
5486 */
5487
5488 term = *cur_end;
5489 *cur_end = '\0';
5490
5491 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5492 switch (exp->action) {
5493 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005494 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005495 last_hdr = 1;
5496 break;
5497
5498 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005499 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005500 last_hdr = 1;
5501 break;
5502
5503 case ACT_REPLACE:
5504 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5505 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5506 /* FIXME: if the user adds a newline in the replacement, the
5507 * index will not be recalculated for now, and the new line
5508 * will not be counted as a new header.
5509 */
5510
5511 cur_end += delta;
5512 cur_next += delta;
5513 cur_hdr->len += delta;
5514 txn->rsp.eoh += delta;
5515 break;
5516
5517 case ACT_REMOVE:
5518 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5519 cur_next += delta;
5520
5521 /* FIXME: this should be a separate function */
5522 txn->rsp.eoh += delta;
5523 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5524 txn->hdr_idx.used--;
5525 cur_hdr->len = 0;
5526 cur_end = NULL; /* null-term has been rewritten */
5527 break;
5528
5529 }
5530 }
5531 if (cur_end)
5532 *cur_end = term; /* restore the string terminator */
5533
5534 /* keep the link from this header to next one in case of later
5535 * removal of next header.
5536 */
5537 old_idx = cur_idx;
5538 }
5539 return 0;
5540}
5541
5542
5543/* Apply the filter to the status line in the response buffer <rtr>.
5544 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5545 * or -1 if a replacement resulted in an invalid status line.
5546 */
5547int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5548{
5549 char term;
5550 char *cur_ptr, *cur_end;
5551 int done;
5552 struct http_txn *txn = &t->txn;
5553 int len, delta;
5554
5555
Willy Tarreau3d300592007-03-18 18:34:41 +01005556 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005557 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005558 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559 (exp->action == ACT_ALLOW ||
5560 exp->action == ACT_DENY))
5561 return 0;
5562 else if (exp->action == ACT_REMOVE)
5563 return 0;
5564
5565 done = 0;
5566
Willy Tarreau9cdde232007-05-02 20:58:19 +02005567 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5569
5570 /* Now we have the status line between cur_ptr and cur_end */
5571
5572 /* The annoying part is that pattern matching needs
5573 * that we modify the contents to null-terminate all
5574 * strings before testing them.
5575 */
5576
5577 term = *cur_end;
5578 *cur_end = '\0';
5579
5580 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5581 switch (exp->action) {
5582 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005583 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005584 done = 1;
5585 break;
5586
5587 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005588 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005589 done = 1;
5590 break;
5591
5592 case ACT_REPLACE:
5593 *cur_end = term; /* restore the string terminator */
5594 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5595 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5596 /* FIXME: if the user adds a newline in the replacement, the
5597 * index will not be recalculated for now, and the new line
5598 * will not be counted as a new header.
5599 */
5600
5601 txn->rsp.eoh += delta;
5602 cur_end += delta;
5603
Willy Tarreau9cdde232007-05-02 20:58:19 +02005604 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005605 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005606 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005607 cur_ptr, cur_end + 1,
5608 NULL, NULL);
5609 if (unlikely(!cur_end))
5610 return -1;
5611
5612 /* we have a full respnse and we know that we have either a CR
5613 * or an LF at <ptr>.
5614 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005615 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005616 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5617 /* there is no point trying this regex on headers */
5618 return 1;
5619 }
5620 }
5621 *cur_end = term; /* restore the string terminator */
5622 return done;
5623}
5624
5625
5626
5627/*
5628 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5629 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5630 * unparsable response.
5631 */
5632int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5633{
Willy Tarreau3d300592007-03-18 18:34:41 +01005634 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005635 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005636 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005637 int ret;
5638
5639 /*
5640 * The interleaving of transformations and verdicts
5641 * makes it difficult to decide to continue or stop
5642 * the evaluation.
5643 */
5644
Willy Tarreau3d300592007-03-18 18:34:41 +01005645 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005646 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5647 exp->action == ACT_PASS)) {
5648 exp = exp->next;
5649 continue;
5650 }
5651
5652 /* Apply the filter to the status line. */
5653 ret = apply_filter_to_sts_line(t, rtr, exp);
5654 if (unlikely(ret < 0))
5655 return -1;
5656
5657 if (likely(ret == 0)) {
5658 /* The filter did not match the response, it can be
5659 * iterated through all headers.
5660 */
5661 apply_filter_to_resp_headers(t, rtr, exp);
5662 }
5663 exp = exp->next;
5664 }
5665 return 0;
5666}
5667
5668
5669
5670/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005671 * Manage server-side cookies. It can impact performance by about 2% so it is
5672 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005673 */
5674void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5675{
5676 struct http_txn *txn = &t->txn;
5677 char *p1, *p2, *p3, *p4;
5678
5679 appsess *asession_temp = NULL;
5680 appsess local_asession;
5681
5682 char *cur_ptr, *cur_end, *cur_next;
5683 int cur_idx, old_idx, delta;
5684
Willy Tarreaua15645d2007-03-18 16:22:39 +01005685 /* Iterate through the headers.
5686 * we start with the start line.
5687 */
5688 old_idx = 0;
5689 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5690
5691 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5692 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005693 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694
5695 cur_hdr = &txn->hdr_idx.v[cur_idx];
5696 cur_ptr = cur_next;
5697 cur_end = cur_ptr + cur_hdr->len;
5698 cur_next = cur_end + cur_hdr->cr + 1;
5699
5700 /* We have one full header between cur_ptr and cur_end, and the
5701 * next header starts at cur_next. We're only interested in
5702 * "Cookie:" headers.
5703 */
5704
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005705 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5706 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005707 old_idx = cur_idx;
5708 continue;
5709 }
5710
5711 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713
5714
5715 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005716 if (t->be->cookie_name == NULL &&
5717 t->be->appsession_name == NULL &&
5718 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005719 return;
5720
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005721 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005722
5723 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005724 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5725 break;
5726
5727 /* p1 is at the beginning of the cookie name */
5728 p2 = p1;
5729
5730 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5731 p2++;
5732
5733 if (p2 == cur_end || *p2 == ';') /* next cookie */
5734 break;
5735
5736 p3 = p2 + 1; /* skip the '=' sign */
5737 if (p3 == cur_end)
5738 break;
5739
5740 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005741 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005742 p4++;
5743
5744 /* here, we have the cookie name between p1 and p2,
5745 * and its value between p3 and p4.
5746 * we can process it.
5747 */
5748
5749 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005750 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005751 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005752 (p4 - p1 >= t->be->capture_namelen) &&
5753 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754 int log_len = p4 - p1;
5755
Willy Tarreau086b3b42007-05-13 21:45:51 +02005756 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005757 Alert("HTTP logging : out of memory.\n");
5758 }
5759
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005760 if (log_len > t->be->capture_len)
5761 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005762 memcpy(txn->srv_cookie, p1, log_len);
5763 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005764 }
5765
5766 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005767 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5768 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005769 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005770 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005771
5772 /* If the cookie is in insert mode on a known server, we'll delete
5773 * this occurrence because we'll insert another one later.
5774 * We'll delete it too if the "indirect" option is set and we're in
5775 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005776 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5777 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005778 /* this header must be deleted */
5779 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5780 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5781 txn->hdr_idx.used--;
5782 cur_hdr->len = 0;
5783 cur_next += delta;
5784 txn->rsp.eoh += delta;
5785
Willy Tarreau3d300592007-03-18 18:34:41 +01005786 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787 }
5788 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005789 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005790 /* replace bytes p3->p4 with the cookie name associated
5791 * with this server since we know it.
5792 */
5793 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5794 cur_hdr->len += delta;
5795 cur_next += delta;
5796 txn->rsp.eoh += delta;
5797
Willy Tarreau3d300592007-03-18 18:34:41 +01005798 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005799 }
5800 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005801 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005802 /* insert the cookie name associated with this server
5803 * before existing cookie, and insert a delimitor between them..
5804 */
5805 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5806 cur_hdr->len += delta;
5807 cur_next += delta;
5808 txn->rsp.eoh += delta;
5809
5810 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812 }
5813 }
5814 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005815 else if ((t->be->appsession_name != NULL) &&
5816 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817
5818 /* Cool... it's the right one */
5819
5820 size_t server_id_len = strlen(t->srv->id) + 1;
5821 asession_temp = &local_asession;
5822
Willy Tarreau63963c62007-05-13 21:29:55 +02005823 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005824 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5825 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5826 return;
5827 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005828 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5829 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830 asession_temp->serverid = NULL;
5831
5832 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005833 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5834 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005835 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836 Alert("Not enough Memory process_srv():asession:calloc().\n");
5837 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5838 return;
5839 }
5840 asession_temp->sessid = local_asession.sessid;
5841 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005842 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005843 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5844 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005846 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005847 }
5848
Willy Tarreaua15645d2007-03-18 16:22:39 +01005849 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005850 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5852 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5853 return;
5854 }
5855 asession_temp->serverid[0] = '\0';
5856 }
5857
5858 if (asession_temp->serverid[0] == '\0')
5859 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5860
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005861 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005862 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005864 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005865 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005866#endif
5867 }/* end if ((t->proxy->appsession_name != NULL) ... */
5868 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5869 } /* we're now at the end of the cookie value */
5870
5871 /* keep the link from this header to next one */
5872 old_idx = cur_idx;
5873 } /* end of cookie processing on this header */
5874}
5875
5876
5877
5878/*
5879 * Check if response is cacheable or not. Updates t->flags.
5880 */
5881void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5882{
5883 struct http_txn *txn = &t->txn;
5884 char *p1, *p2;
5885
5886 char *cur_ptr, *cur_end, *cur_next;
5887 int cur_idx;
5888
Willy Tarreau5df51872007-11-25 16:20:08 +01005889 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890 return;
5891
5892 /* Iterate through the headers.
5893 * we start with the start line.
5894 */
5895 cur_idx = 0;
5896 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5897
5898 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5899 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005900 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005901
5902 cur_hdr = &txn->hdr_idx.v[cur_idx];
5903 cur_ptr = cur_next;
5904 cur_end = cur_ptr + cur_hdr->len;
5905 cur_next = cur_end + cur_hdr->cr + 1;
5906
5907 /* We have one full header between cur_ptr and cur_end, and the
5908 * next header starts at cur_next. We're only interested in
5909 * "Cookie:" headers.
5910 */
5911
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005912 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5913 if (val) {
5914 if ((cur_end - (cur_ptr + val) >= 8) &&
5915 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5916 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5917 return;
5918 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005919 }
5920
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005921 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5922 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923 continue;
5924
5925 /* OK, right now we know we have a cache-control header at cur_ptr */
5926
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005927 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005928
5929 if (p1 >= cur_end) /* no more info */
5930 continue;
5931
5932 /* p1 is at the beginning of the value */
5933 p2 = p1;
5934
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005935 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936 p2++;
5937
5938 /* we have a complete value between p1 and p2 */
5939 if (p2 < cur_end && *p2 == '=') {
5940 /* we have something of the form no-cache="set-cookie" */
5941 if ((cur_end - p1 >= 21) &&
5942 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5943 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005944 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005945 continue;
5946 }
5947
5948 /* OK, so we know that either p2 points to the end of string or to a comma */
5949 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5950 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5951 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5952 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005953 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005954 return;
5955 }
5956
5957 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005958 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 continue;
5960 }
5961 }
5962}
5963
5964
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965/*
5966 * Try to retrieve a known appsession in the URI, then the associated server.
5967 * If the server is found, it's assigned to the session.
5968 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005970{
Willy Tarreau3d300592007-03-18 18:34:41 +01005971 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005972 appsess *asession_temp = NULL;
5973 appsess local_asession;
5974 char *request_line;
5975
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005976 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005977 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005979 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005980 return;
5981
5982 /* skip ';' */
5983 request_line++;
5984
5985 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005986 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005987 return;
5988
5989 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005990 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991
5992 /* First try if we already have an appsession */
5993 asession_temp = &local_asession;
5994
Willy Tarreau63963c62007-05-13 21:29:55 +02005995 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005996 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5997 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5998 return;
5999 }
6000
6001 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006002 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
6003 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006004 asession_temp->serverid = NULL;
6005
6006 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01006007 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
6008 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02006009 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006011 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006012 Alert("Not enough memory process_cli():asession:calloc().\n");
6013 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
6014 return;
6015 }
6016 asession_temp->sessid = local_asession.sessid;
6017 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006018 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02006019 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006020 }
6021 else {
6022 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006023 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024 }
Willy Tarreau51041c72007-09-09 21:56:53 +02006025
Willy Tarreau0c303ee2008-07-07 00:09:58 +02006026 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02006028
Willy Tarreau58f10d72006-12-04 02:26:12 +01006029#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006030 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006031 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032#endif
6033 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006034 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006035 Alert("Found Application Session without matching server.\n");
6036 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006037 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006038 while (srv) {
6039 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006040 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01006042 txn->flags &= ~TX_CK_MASK;
6043 txn->flags |= TX_CK_VALID;
6044 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006045 t->srv = srv;
6046 break;
6047 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01006048 txn->flags &= ~TX_CK_MASK;
6049 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006050 }
6051 }
6052 srv = srv->next;
6053 }
6054 }
6055}
6056
6057
Willy Tarreaub2513902006-12-17 14:52:38 +01006058/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006059 * In a GET or HEAD request, check if the requested URI matches the stats uri
6060 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006061 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006062 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006063 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006064 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006065 *
6066 * Returns 1 if the session's state changes, otherwise 0.
6067 */
6068int stats_check_uri_auth(struct session *t, struct proxy *backend)
6069{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006070 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006071 struct uri_auth *uri_auth = backend->uri_auth;
6072 struct user_auth *user;
6073 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006074 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006075
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006076 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006079 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006080 return 0;
6081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006082 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006084 /* the URI is in h */
6085 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006086 return 0;
6087
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006088 h += uri_auth->uri_len;
6089 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6090 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006091 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006092 break;
6093 }
6094 h++;
6095 }
6096
6097 if (uri_auth->refresh) {
6098 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6099 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6100 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006101 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006102 break;
6103 }
6104 h++;
6105 }
6106 }
6107
Willy Tarreau55bb8452007-10-17 18:44:57 +02006108 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6109 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6110 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006111 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006112 break;
6113 }
6114 h++;
6115 }
6116
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006117 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6118
Willy Tarreaub2513902006-12-17 14:52:38 +01006119 /* we are in front of a interceptable URI. Let's check
6120 * if there's an authentication and if it's valid.
6121 */
6122 user = uri_auth->users;
6123 if (!user) {
6124 /* no user auth required, it's OK */
6125 authenticated = 1;
6126 } else {
6127 authenticated = 0;
6128
6129 /* a user list is defined, we have to check.
6130 * skip 21 chars for "Authorization: Basic ".
6131 */
6132
6133 /* FIXME: this should move to an earlier place */
6134 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006135 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6136 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6137 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006138 if (len > 14 &&
6139 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006140 txn->auth_hdr.str = h;
6141 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006142 break;
6143 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006144 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006145 }
6146
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006147 if (txn->auth_hdr.len < 21 ||
6148 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006149 user = NULL;
6150
6151 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006152 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6153 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006154 user->user_pwd, user->user_len)) {
6155 authenticated = 1;
6156 break;
6157 }
6158 user = user->next;
6159 }
6160 }
6161
6162 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006163 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006164
6165 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01006166 msg.str = trash;
6167 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006168 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01006169 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02006170 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006171 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006172 if (!(t->flags & SN_ERR_MASK))
6173 t->flags |= SN_ERR_PRXCOND;
6174 if (!(t->flags & SN_FINST_MASK))
6175 t->flags |= SN_FINST_R;
6176 return 1;
6177 }
6178
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006179 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006180 * data.
6181 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02006182 EV_FD_CLR(t->cli_fd, DIR_RD);
6183 buffer_shutr(t->req);
6184 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02006185 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02006186 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006187 t->data_source = DATA_SRC_STATS;
6188 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006189 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01006190 produce_content(t);
6191 return 1;
6192}
6193
6194
Willy Tarreaubaaee002006-06-26 02:48:02 +02006195/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006196 * Print a debug line with a header
6197 */
6198void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6199{
6200 int len, max;
6201 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +02006202 dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006203 max = end - start;
6204 UBOUND(max, sizeof(trash) - len - 1);
6205 len += strlcpy2(trash + len, start, max + 1);
6206 trash[len++] = '\n';
6207 write(1, trash, len);
6208}
6209
6210
Willy Tarreau8797c062007-05-07 00:55:35 +02006211/************************************************************************/
6212/* The code below is dedicated to ACL parsing and matching */
6213/************************************************************************/
6214
6215
6216
6217
6218/* 1. Check on METHOD
6219 * We use the pre-parsed method if it is known, and store its number as an
6220 * integer. If it is unknown, we use the pointer and the length.
6221 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006222static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006223{
6224 int len, meth;
6225
Willy Tarreauae8b7962007-06-09 23:10:04 +02006226 len = strlen(*text);
6227 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006228
6229 pattern->val.i = meth;
6230 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006231 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006232 if (!pattern->ptr.str)
6233 return 0;
6234 pattern->len = len;
6235 }
6236 return 1;
6237}
6238
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006239static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006240acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6241 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006242{
6243 int meth;
6244 struct http_txn *txn = l7;
6245
Willy Tarreaub6866442008-07-14 23:54:42 +02006246 if (!txn)
6247 return 0;
6248
Willy Tarreauc11416f2007-06-17 16:58:38 +02006249 if (txn->req.msg_state != HTTP_MSG_BODY)
6250 return 0;
6251
Willy Tarreau8797c062007-05-07 00:55:35 +02006252 meth = txn->meth;
6253 test->i = meth;
6254 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006255 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6256 /* ensure the indexes are not affected */
6257 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006258 test->len = txn->req.sl.rq.m_l;
6259 test->ptr = txn->req.sol;
6260 }
6261 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6262 return 1;
6263}
6264
6265static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6266{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006267 int icase;
6268
Willy Tarreau8797c062007-05-07 00:55:35 +02006269 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006270 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006271
6272 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006273 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006274
6275 /* Other method, we must compare the strings */
6276 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006277 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006278
6279 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6280 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6281 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006282 return ACL_PAT_FAIL;
6283 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006284}
6285
6286/* 2. Check on Request/Status Version
6287 * We simply compare strings here.
6288 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006289static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006290{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006291 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006292 if (!pattern->ptr.str)
6293 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006294 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006295 return 1;
6296}
6297
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006298static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006299acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6300 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006301{
6302 struct http_txn *txn = l7;
6303 char *ptr;
6304 int len;
6305
Willy Tarreaub6866442008-07-14 23:54:42 +02006306 if (!txn)
6307 return 0;
6308
Willy Tarreauc11416f2007-06-17 16:58:38 +02006309 if (txn->req.msg_state != HTTP_MSG_BODY)
6310 return 0;
6311
Willy Tarreau8797c062007-05-07 00:55:35 +02006312 len = txn->req.sl.rq.v_l;
6313 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6314
6315 while ((len-- > 0) && (*ptr++ != '/'));
6316 if (len <= 0)
6317 return 0;
6318
6319 test->ptr = ptr;
6320 test->len = len;
6321
6322 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6323 return 1;
6324}
6325
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006326static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006327acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6328 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006329{
6330 struct http_txn *txn = l7;
6331 char *ptr;
6332 int len;
6333
Willy Tarreaub6866442008-07-14 23:54:42 +02006334 if (!txn)
6335 return 0;
6336
Willy Tarreauc11416f2007-06-17 16:58:38 +02006337 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6338 return 0;
6339
Willy Tarreau8797c062007-05-07 00:55:35 +02006340 len = txn->rsp.sl.st.v_l;
6341 ptr = txn->rsp.sol;
6342
6343 while ((len-- > 0) && (*ptr++ != '/'));
6344 if (len <= 0)
6345 return 0;
6346
6347 test->ptr = ptr;
6348 test->len = len;
6349
6350 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6351 return 1;
6352}
6353
6354/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006355static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006356acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6357 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006358{
6359 struct http_txn *txn = l7;
6360 char *ptr;
6361 int len;
6362
Willy Tarreaub6866442008-07-14 23:54:42 +02006363 if (!txn)
6364 return 0;
6365
Willy Tarreauc11416f2007-06-17 16:58:38 +02006366 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6367 return 0;
6368
Willy Tarreau8797c062007-05-07 00:55:35 +02006369 len = txn->rsp.sl.st.c_l;
6370 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6371
6372 test->i = __strl2ui(ptr, len);
6373 test->flags = ACL_TEST_F_VOL_1ST;
6374 return 1;
6375}
6376
6377/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006378static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006379acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6380 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006381{
6382 struct http_txn *txn = l7;
6383
Willy Tarreaub6866442008-07-14 23:54:42 +02006384 if (!txn)
6385 return 0;
6386
Willy Tarreauc11416f2007-06-17 16:58:38 +02006387 if (txn->req.msg_state != HTTP_MSG_BODY)
6388 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006389
Willy Tarreauc11416f2007-06-17 16:58:38 +02006390 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6391 /* ensure the indexes are not affected */
6392 return 0;
6393
Willy Tarreau8797c062007-05-07 00:55:35 +02006394 test->len = txn->req.sl.rq.u_l;
6395 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6396
Willy Tarreauf3d25982007-05-08 22:45:09 +02006397 /* we do not need to set READ_ONLY because the data is in a buffer */
6398 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006399 return 1;
6400}
6401
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006402static int
6403acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6404 struct acl_expr *expr, struct acl_test *test)
6405{
6406 struct http_txn *txn = l7;
6407
Willy Tarreaub6866442008-07-14 23:54:42 +02006408 if (!txn)
6409 return 0;
6410
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006411 if (txn->req.msg_state != HTTP_MSG_BODY)
6412 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006413
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006414 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6415 /* ensure the indexes are not affected */
6416 return 0;
6417
6418 /* Parse HTTP request */
6419 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6420 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6421 test->i = AF_INET;
6422
6423 /*
6424 * If we are parsing url in frontend space, we prepare backend stage
6425 * to not parse again the same url ! optimization lazyness...
6426 */
6427 if (px->options & PR_O_HTTP_PROXY)
6428 l4->flags |= SN_ADDR_SET;
6429
6430 test->flags = ACL_TEST_F_READ_ONLY;
6431 return 1;
6432}
6433
6434static int
6435acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6436 struct acl_expr *expr, struct acl_test *test)
6437{
6438 struct http_txn *txn = l7;
6439
Willy Tarreaub6866442008-07-14 23:54:42 +02006440 if (!txn)
6441 return 0;
6442
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006443 if (txn->req.msg_state != HTTP_MSG_BODY)
6444 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006445
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006446 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6447 /* ensure the indexes are not affected */
6448 return 0;
6449
6450 /* Same optimization as url_ip */
6451 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6452 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6453
6454 if (px->options & PR_O_HTTP_PROXY)
6455 l4->flags |= SN_ADDR_SET;
6456
6457 test->flags = ACL_TEST_F_READ_ONLY;
6458 return 1;
6459}
6460
Willy Tarreauc11416f2007-06-17 16:58:38 +02006461/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6462 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6463 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006464static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006465acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006466 struct acl_expr *expr, struct acl_test *test)
6467{
6468 struct http_txn *txn = l7;
6469 struct hdr_idx *idx = &txn->hdr_idx;
6470 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006471
Willy Tarreaub6866442008-07-14 23:54:42 +02006472 if (!txn)
6473 return 0;
6474
Willy Tarreau33a7e692007-06-10 19:45:56 +02006475 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6476 /* search for header from the beginning */
6477 ctx->idx = 0;
6478
Willy Tarreau33a7e692007-06-10 19:45:56 +02006479 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6480 test->flags |= ACL_TEST_F_FETCH_MORE;
6481 test->flags |= ACL_TEST_F_VOL_HDR;
6482 test->len = ctx->vlen;
6483 test->ptr = (char *)ctx->line + ctx->val;
6484 return 1;
6485 }
6486
6487 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6488 test->flags |= ACL_TEST_F_VOL_HDR;
6489 return 0;
6490}
6491
Willy Tarreau33a7e692007-06-10 19:45:56 +02006492static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006493acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6494 struct acl_expr *expr, struct acl_test *test)
6495{
6496 struct http_txn *txn = l7;
6497
Willy Tarreaub6866442008-07-14 23:54:42 +02006498 if (!txn)
6499 return 0;
6500
Willy Tarreauc11416f2007-06-17 16:58:38 +02006501 if (txn->req.msg_state != HTTP_MSG_BODY)
6502 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006503
Willy Tarreauc11416f2007-06-17 16:58:38 +02006504 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6505 /* ensure the indexes are not affected */
6506 return 0;
6507
6508 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6509}
6510
6511static int
6512acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6513 struct acl_expr *expr, struct acl_test *test)
6514{
6515 struct http_txn *txn = l7;
6516
Willy Tarreaub6866442008-07-14 23:54:42 +02006517 if (!txn)
6518 return 0;
6519
Willy Tarreauc11416f2007-06-17 16:58:38 +02006520 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6521 return 0;
6522
6523 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6524}
6525
6526/* 6. Check on HTTP header count. The number of occurrences is returned.
6527 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6528 */
6529static int
6530acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006531 struct acl_expr *expr, struct acl_test *test)
6532{
6533 struct http_txn *txn = l7;
6534 struct hdr_idx *idx = &txn->hdr_idx;
6535 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006536 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006537
Willy Tarreaub6866442008-07-14 23:54:42 +02006538 if (!txn)
6539 return 0;
6540
Willy Tarreau33a7e692007-06-10 19:45:56 +02006541 ctx.idx = 0;
6542 cnt = 0;
6543 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6544 cnt++;
6545
6546 test->i = cnt;
6547 test->flags = ACL_TEST_F_VOL_HDR;
6548 return 1;
6549}
6550
Willy Tarreauc11416f2007-06-17 16:58:38 +02006551static int
6552acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6553 struct acl_expr *expr, struct acl_test *test)
6554{
6555 struct http_txn *txn = l7;
6556
Willy Tarreaub6866442008-07-14 23:54:42 +02006557 if (!txn)
6558 return 0;
6559
Willy Tarreauc11416f2007-06-17 16:58:38 +02006560 if (txn->req.msg_state != HTTP_MSG_BODY)
6561 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006562
Willy Tarreauc11416f2007-06-17 16:58:38 +02006563 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6564 /* ensure the indexes are not affected */
6565 return 0;
6566
6567 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6568}
6569
6570static int
6571acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6572 struct acl_expr *expr, struct acl_test *test)
6573{
6574 struct http_txn *txn = l7;
6575
Willy Tarreaub6866442008-07-14 23:54:42 +02006576 if (!txn)
6577 return 0;
6578
Willy Tarreauc11416f2007-06-17 16:58:38 +02006579 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6580 return 0;
6581
6582 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6583}
6584
Willy Tarreau33a7e692007-06-10 19:45:56 +02006585/* 7. Check on HTTP header's integer value. The integer value is returned.
6586 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006587 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006588 */
6589static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006590acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006591 struct acl_expr *expr, struct acl_test *test)
6592{
6593 struct http_txn *txn = l7;
6594 struct hdr_idx *idx = &txn->hdr_idx;
6595 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006596
Willy Tarreaub6866442008-07-14 23:54:42 +02006597 if (!txn)
6598 return 0;
6599
Willy Tarreau33a7e692007-06-10 19:45:56 +02006600 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6601 /* search for header from the beginning */
6602 ctx->idx = 0;
6603
Willy Tarreau33a7e692007-06-10 19:45:56 +02006604 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6605 test->flags |= ACL_TEST_F_FETCH_MORE;
6606 test->flags |= ACL_TEST_F_VOL_HDR;
6607 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6608 return 1;
6609 }
6610
6611 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6612 test->flags |= ACL_TEST_F_VOL_HDR;
6613 return 0;
6614}
6615
Willy Tarreauc11416f2007-06-17 16:58:38 +02006616static int
6617acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6618 struct acl_expr *expr, struct acl_test *test)
6619{
6620 struct http_txn *txn = l7;
6621
Willy Tarreaub6866442008-07-14 23:54:42 +02006622 if (!txn)
6623 return 0;
6624
Willy Tarreauc11416f2007-06-17 16:58:38 +02006625 if (txn->req.msg_state != HTTP_MSG_BODY)
6626 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006627
Willy Tarreauc11416f2007-06-17 16:58:38 +02006628 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6629 /* ensure the indexes are not affected */
6630 return 0;
6631
6632 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6633}
6634
6635static int
6636acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6637 struct acl_expr *expr, struct acl_test *test)
6638{
6639 struct http_txn *txn = l7;
6640
Willy Tarreaub6866442008-07-14 23:54:42 +02006641 if (!txn)
6642 return 0;
6643
Willy Tarreauc11416f2007-06-17 16:58:38 +02006644 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6645 return 0;
6646
6647 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6648}
6649
Willy Tarreau737b0c12007-06-10 21:28:46 +02006650/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6651 * the first '/' after the possible hostname, and ends before the possible '?'.
6652 */
6653static int
6654acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6655 struct acl_expr *expr, struct acl_test *test)
6656{
6657 struct http_txn *txn = l7;
6658 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006659
Willy Tarreaub6866442008-07-14 23:54:42 +02006660 if (!txn)
6661 return 0;
6662
Willy Tarreauc11416f2007-06-17 16:58:38 +02006663 if (txn->req.msg_state != HTTP_MSG_BODY)
6664 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006665
Willy Tarreauc11416f2007-06-17 16:58:38 +02006666 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6667 /* ensure the indexes are not affected */
6668 return 0;
6669
Willy Tarreau21d2af32008-02-14 20:25:24 +01006670 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6671 ptr = http_get_path(txn);
6672 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006673 return 0;
6674
6675 /* OK, we got the '/' ! */
6676 test->ptr = ptr;
6677
6678 while (ptr < end && *ptr != '?')
6679 ptr++;
6680
6681 test->len = ptr - test->ptr;
6682
6683 /* we do not need to set READ_ONLY because the data is in a buffer */
6684 test->flags = ACL_TEST_F_VOL_1ST;
6685 return 1;
6686}
6687
6688
Willy Tarreau8797c062007-05-07 00:55:35 +02006689
6690/************************************************************************/
6691/* All supported keywords must be declared here. */
6692/************************************************************************/
6693
6694/* Note: must not be declared <const> as its list will be overwritten */
6695static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006696 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6697 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6698 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6699 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006700
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006701 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6702 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6703 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6704 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6705 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6706 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6707 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6708 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6709 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006710
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006711 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6712 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6713 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6714 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6715 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6716 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6717 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6718 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6719 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6720 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006721
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006722 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6723 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6724 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6725 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6726 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6727 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6728 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6729 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6730 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006731
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006732 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6733 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6734 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6735 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6736 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6737 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6738 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006739
Willy Tarreauf3d25982007-05-08 22:45:09 +02006740 { NULL, NULL, NULL, NULL },
6741
6742#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006743 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6744 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6745 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6746 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6747 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6748 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6749 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6750
Willy Tarreau8797c062007-05-07 00:55:35 +02006751 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6752 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6753 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6754 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6755 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6756 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6757 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6758 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6759
6760 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6761 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6762 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6763 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6764 { NULL, NULL, NULL, NULL },
6765#endif
6766}};
6767
6768
6769__attribute__((constructor))
6770static void __http_protocol_init(void)
6771{
6772 acl_register_keywords(&acl_kws);
6773}
6774
6775
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006777 * Local variables:
6778 * c-indent-level: 8
6779 * c-basic-offset: 8
6780 * End:
6781 */