blob: c065fda21b038b9d85aa1366ccb17bcc19bdeed4 [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) {
749 if (process_srv_data(s))
750 resync |= PROCESS_SRV;
751 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200752
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200753 if (unlikely((s->req->cons->state == SI_ST_CLO) &&
754 (global.mode & MODE_DEBUG) &&
755 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
756 int len;
757 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
758 s->uniq_id, s->be->id, (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd);
759 write(1, trash, len);
760 }
761 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200762 if (rqf != s->req->flags || rpf != s->rep->flags)
763 resync |= PROCESS_ALL & ~PROCESS_SRV;
764 }
765 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200767 if (likely(s->cli_state != CL_STCLOSE ||
768 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100769
770 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
771 session_process_counters(s);
772
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200773 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
774 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775
Willy Tarreau7f875f62008-08-11 17:35:01 +0200776 /* Trick: if a request is being waiting for the server to respond,
777 * and if we know the server can timeout, we don't want the timeout
778 * to expire on the client side first, but we're still interested
779 * in passing data from the client to the server (eg: POST). Thus,
780 * we can cancel the client's request timeout if the server's
781 * request timeout is set and the server has not yet sent a response.
782 */
783
Willy Tarreauba392ce2008-08-16 21:13:23 +0200784 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200785 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200786 s->req->rex = TICK_ETERNITY;
787
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200788 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
789 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200790 if (s->req->analysers)
791 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
793 /* restore t to its place in the task list */
794 task_queue(t);
795
Willy Tarreaua7c52762008-08-16 18:40:18 +0200796#ifdef DEBUG_DEV
797 /* this may only happen when no timeout is set or in case of an FSM bug */
798 if (!t->expire)
799 ABORT_NOW();
800#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200801 *next = t->expire;
802 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
804
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100805 s->fe->feconn--;
806 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200807 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 actconn--;
809
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200810 if (unlikely((global.mode & MODE_DEBUG) &&
811 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200813 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200814 s->uniq_id, s->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200815 (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200816 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 write(1, trash, len);
818 }
819
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200820 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100821 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822
823 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200824 if (s->logs.logwait &&
825 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200826 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
827 if (s->fe->to_log & LW_REQ)
828 http_sess_log(s);
829 else
830 tcp_sess_log(s);
831 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832
833 /* the task MUST not be in the run queue anymore */
834 task_delete(t);
835 session_free(s);
836 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200837 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838}
839
840
Willy Tarreau42250582007-04-01 01:30:43 +0200841extern const char sess_term_cond[8];
842extern const char sess_fin_state[8];
843extern const char *monthname[12];
844const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
845const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
846 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
847 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200848struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200849struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100850
Willy Tarreau42250582007-04-01 01:30:43 +0200851/*
852 * send a log for the session when we have enough info about it.
853 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100854 */
Willy Tarreau42250582007-04-01 01:30:43 +0200855static void http_sess_log(struct session *s)
856{
857 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
858 struct proxy *fe = s->fe;
859 struct proxy *be = s->be;
860 struct proxy *prx_log;
861 struct http_txn *txn = &s->txn;
862 int tolog;
863 char *uri, *h;
864 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200865 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200866 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200867 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200868 int hdr;
869
870 if (fe->logfac1 < 0 && fe->logfac2 < 0)
871 return;
872 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100873
Willy Tarreau42250582007-04-01 01:30:43 +0200874 if (s->cli_addr.ss_family == AF_INET)
875 inet_ntop(AF_INET,
876 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
877 pn, sizeof(pn));
878 else
879 inet_ntop(AF_INET6,
880 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
881 pn, sizeof(pn));
882
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200883 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200884
885 /* FIXME: let's limit ourselves to frontend logging for now. */
886 tolog = fe->to_log;
887
888 h = tmpline;
889 if (fe->to_log & LW_REQHDR &&
890 txn->req.cap &&
891 (h < tmpline + sizeof(tmpline) - 10)) {
892 *(h++) = ' ';
893 *(h++) = '{';
894 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
895 if (hdr)
896 *(h++) = '|';
897 if (txn->req.cap[hdr] != NULL)
898 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
899 '#', hdr_encode_map, txn->req.cap[hdr]);
900 }
901 *(h++) = '}';
902 }
903
904 if (fe->to_log & LW_RSPHDR &&
905 txn->rsp.cap &&
906 (h < tmpline + sizeof(tmpline) - 7)) {
907 *(h++) = ' ';
908 *(h++) = '{';
909 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
910 if (hdr)
911 *(h++) = '|';
912 if (txn->rsp.cap[hdr] != NULL)
913 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
914 '#', hdr_encode_map, txn->rsp.cap[hdr]);
915 }
916 *(h++) = '}';
917 }
918
919 if (h < tmpline + sizeof(tmpline) - 4) {
920 *(h++) = ' ';
921 *(h++) = '"';
922 uri = txn->uri ? txn->uri : "<BADREQ>";
923 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
924 '#', url_encode_map, uri);
925 *(h++) = '"';
926 }
927 *h = '\0';
928
929 svid = (tolog & LW_SVID) ?
930 (s->data_source != DATA_SRC_STATS) ?
931 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
932
Willy Tarreau70089872008-06-13 21:12:51 +0200933 t_request = -1;
934 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
935 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
936
Willy Tarreau42250582007-04-01 01:30:43 +0200937 send_log(prx_log, LOG_INFO,
938 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
939 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100940 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200941 pn,
942 (s->cli_addr.ss_family == AF_INET) ?
943 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
944 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200945 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200946 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200947 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200948 t_request,
949 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200950 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
951 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
952 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
953 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100954 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200955 txn->cli_cookie ? txn->cli_cookie : "-",
956 txn->srv_cookie ? txn->srv_cookie : "-",
957 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
958 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
959 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
960 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
961 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100962 (s->flags & SN_REDISP)?"+":"",
963 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200964 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
965
966 s->logs.logwait = 0;
967}
968
Willy Tarreau117f59e2007-03-04 18:17:17 +0100969
970/*
971 * Capture headers from message starting at <som> according to header list
972 * <cap_hdr>, and fill the <idx> structure appropriately.
973 */
974void capture_headers(char *som, struct hdr_idx *idx,
975 char **cap, struct cap_hdr *cap_hdr)
976{
977 char *eol, *sol, *col, *sov;
978 int cur_idx;
979 struct cap_hdr *h;
980 int len;
981
982 sol = som + hdr_idx_first_pos(idx);
983 cur_idx = hdr_idx_first_idx(idx);
984
985 while (cur_idx) {
986 eol = sol + idx->v[cur_idx].len;
987
988 col = sol;
989 while (col < eol && *col != ':')
990 col++;
991
992 sov = col + 1;
993 while (sov < eol && http_is_lws[(unsigned char)*sov])
994 sov++;
995
996 for (h = cap_hdr; h; h = h->next) {
997 if ((h->namelen == col - sol) &&
998 (strncasecmp(sol, h->name, h->namelen) == 0)) {
999 if (cap[h->index] == NULL)
1000 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001001 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001002
1003 if (cap[h->index] == NULL) {
1004 Alert("HTTP capture : out of memory.\n");
1005 continue;
1006 }
1007
1008 len = eol - sov;
1009 if (len > h->len)
1010 len = h->len;
1011
1012 memcpy(cap[h->index], sov, len);
1013 cap[h->index][len]=0;
1014 }
1015 }
1016 sol = eol + idx->v[cur_idx].cr + 1;
1017 cur_idx = idx->v[cur_idx].next;
1018 }
1019}
1020
1021
Willy Tarreau42250582007-04-01 01:30:43 +02001022/* either we find an LF at <ptr> or we jump to <bad>.
1023 */
1024#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1025
1026/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1027 * otherwise to <http_msg_ood> with <state> set to <st>.
1028 */
1029#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1030 ptr++; \
1031 if (likely(ptr < end)) \
1032 goto good; \
1033 else { \
1034 state = (st); \
1035 goto http_msg_ood; \
1036 } \
1037 } while (0)
1038
1039
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001041 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1043 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1044 * will give undefined results.
1045 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1046 * and that msg->sol points to the beginning of the response.
1047 * If a complete line is found (which implies that at least one CR or LF is
1048 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1049 * returned indicating an incomplete line (which does not mean that parts have
1050 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1051 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1052 * upon next call.
1053 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001054 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1056 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001058 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001059const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1060 unsigned int state, const char *ptr, const char *end,
1061 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001062{
1063 __label__
1064 http_msg_rpver,
1065 http_msg_rpver_sp,
1066 http_msg_rpcode,
1067 http_msg_rpcode_sp,
1068 http_msg_rpreason,
1069 http_msg_rpline_eol,
1070 http_msg_ood, /* out of data */
1071 http_msg_invalid;
1072
1073 switch (state) {
1074 http_msg_rpver:
1075 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001076 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001077 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1078
1079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001080 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1082 }
1083 goto http_msg_invalid;
1084
1085 http_msg_rpver_sp:
1086 case HTTP_MSG_RPVER_SP:
1087 if (likely(!HTTP_IS_LWS(*ptr))) {
1088 msg->sl.st.c = ptr - msg_buf;
1089 goto http_msg_rpcode;
1090 }
1091 if (likely(HTTP_IS_SPHT(*ptr)))
1092 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1093 /* so it's a CR/LF, this is invalid */
1094 goto http_msg_invalid;
1095
1096 http_msg_rpcode:
1097 case HTTP_MSG_RPCODE:
1098 if (likely(!HTTP_IS_LWS(*ptr)))
1099 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1100
1101 if (likely(HTTP_IS_SPHT(*ptr))) {
1102 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1103 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1104 }
1105
1106 /* so it's a CR/LF, so there is no reason phrase */
1107 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1108 http_msg_rsp_reason:
1109 /* FIXME: should we support HTTP responses without any reason phrase ? */
1110 msg->sl.st.r = ptr - msg_buf;
1111 msg->sl.st.r_l = 0;
1112 goto http_msg_rpline_eol;
1113
1114 http_msg_rpcode_sp:
1115 case HTTP_MSG_RPCODE_SP:
1116 if (likely(!HTTP_IS_LWS(*ptr))) {
1117 msg->sl.st.r = ptr - msg_buf;
1118 goto http_msg_rpreason;
1119 }
1120 if (likely(HTTP_IS_SPHT(*ptr)))
1121 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1122 /* so it's a CR/LF, so there is no reason phrase */
1123 goto http_msg_rsp_reason;
1124
1125 http_msg_rpreason:
1126 case HTTP_MSG_RPREASON:
1127 if (likely(!HTTP_IS_CRLF(*ptr)))
1128 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1129 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1130 http_msg_rpline_eol:
1131 /* We have seen the end of line. Note that we do not
1132 * necessarily have the \n yet, but at least we know that we
1133 * have EITHER \r OR \n, otherwise the response would not be
1134 * complete. We can then record the response length and return
1135 * to the caller which will be able to register it.
1136 */
1137 msg->sl.st.l = ptr - msg->sol;
1138 return ptr;
1139
1140#ifdef DEBUG_FULL
1141 default:
1142 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1143 exit(1);
1144#endif
1145 }
1146
1147 http_msg_ood:
1148 /* out of data */
1149 if (ret_state)
1150 *ret_state = state;
1151 if (ret_ptr)
1152 *ret_ptr = (char *)ptr;
1153 return NULL;
1154
1155 http_msg_invalid:
1156 /* invalid message */
1157 if (ret_state)
1158 *ret_state = HTTP_MSG_ERROR;
1159 return NULL;
1160}
1161
1162
1163/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164 * This function parses a request line between <ptr> and <end>, starting with
1165 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1166 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1167 * will give undefined results.
1168 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1169 * and that msg->sol points to the beginning of the request.
1170 * If a complete line is found (which implies that at least one CR or LF is
1171 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1172 * returned indicating an incomplete line (which does not mean that parts have
1173 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1174 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1175 * upon next call.
1176 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001177 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1179 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001180 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001182const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1183 unsigned int state, const char *ptr, const char *end,
1184 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 __label__
1187 http_msg_rqmeth,
1188 http_msg_rqmeth_sp,
1189 http_msg_rquri,
1190 http_msg_rquri_sp,
1191 http_msg_rqver,
1192 http_msg_rqline_eol,
1193 http_msg_ood, /* out of data */
1194 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 switch (state) {
1197 http_msg_rqmeth:
1198 case HTTP_MSG_RQMETH:
1199 if (likely(HTTP_IS_TOKEN(*ptr)))
1200 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001203 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001206
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207 if (likely(HTTP_IS_CRLF(*ptr))) {
1208 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001209 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 http_msg_req09_uri:
1211 msg->sl.rq.u = ptr - msg_buf;
1212 http_msg_req09_uri_e:
1213 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1214 http_msg_req09_ver:
1215 msg->sl.rq.v = ptr - msg_buf;
1216 msg->sl.rq.v_l = 0;
1217 goto http_msg_rqline_eol;
1218 }
1219 goto http_msg_invalid;
1220
1221 http_msg_rqmeth_sp:
1222 case HTTP_MSG_RQMETH_SP:
1223 if (likely(!HTTP_IS_LWS(*ptr))) {
1224 msg->sl.rq.u = ptr - msg_buf;
1225 goto http_msg_rquri;
1226 }
1227 if (likely(HTTP_IS_SPHT(*ptr)))
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1229 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1230 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 http_msg_rquri:
1233 case HTTP_MSG_RQURI:
1234 if (likely(!HTTP_IS_LWS(*ptr)))
1235 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 if (likely(HTTP_IS_SPHT(*ptr))) {
1238 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1243 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 http_msg_rquri_sp:
1246 case HTTP_MSG_RQURI_SP:
1247 if (likely(!HTTP_IS_LWS(*ptr))) {
1248 msg->sl.rq.v = ptr - msg_buf;
1249 goto http_msg_rqver;
1250 }
1251 if (likely(HTTP_IS_SPHT(*ptr)))
1252 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1253 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1254 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 http_msg_rqver:
1257 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001258 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001260
1261 if (likely(HTTP_IS_CRLF(*ptr))) {
1262 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1263 http_msg_rqline_eol:
1264 /* We have seen the end of line. Note that we do not
1265 * necessarily have the \n yet, but at least we know that we
1266 * have EITHER \r OR \n, otherwise the request would not be
1267 * complete. We can then record the request length and return
1268 * to the caller which will be able to register it.
1269 */
1270 msg->sl.rq.l = ptr - msg->sol;
1271 return ptr;
1272 }
1273
1274 /* neither an HTTP_VER token nor a CRLF */
1275 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277#ifdef DEBUG_FULL
1278 default:
1279 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1280 exit(1);
1281#endif
1282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 http_msg_ood:
1285 /* out of data */
1286 if (ret_state)
1287 *ret_state = state;
1288 if (ret_ptr)
1289 *ret_ptr = (char *)ptr;
1290 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 http_msg_invalid:
1293 /* invalid message */
1294 if (ret_state)
1295 *ret_state = HTTP_MSG_ERROR;
1296 return NULL;
1297}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
1299
Willy Tarreau8973c702007-01-21 23:58:29 +01001300/*
1301 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001302 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 * when data are missing and recalled at the exact same location with no
1304 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001305 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1306 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1309{
1310 __label__
1311 http_msg_rqbefore,
1312 http_msg_rqbefore_cr,
1313 http_msg_rqmeth,
1314 http_msg_rqline_end,
1315 http_msg_hdr_first,
1316 http_msg_hdr_name,
1317 http_msg_hdr_l1_sp,
1318 http_msg_hdr_l1_lf,
1319 http_msg_hdr_l1_lws,
1320 http_msg_hdr_val,
1321 http_msg_hdr_l2_lf,
1322 http_msg_hdr_l2_lws,
1323 http_msg_complete_header,
1324 http_msg_last_lf,
1325 http_msg_ood, /* out of data */
1326 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreaue69eada2008-01-27 00:34:10 +01001328 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001329 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001331 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 ptr = buf->lr;
1333 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 if (unlikely(ptr >= end))
1336 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 /*
1340 * First, states that are specific to the response only.
1341 * We check them first so that request and headers are
1342 * closer to each other (accessed more often).
1343 */
1344 http_msg_rpbefore:
1345 case HTTP_MSG_RPBEFORE:
1346 if (likely(HTTP_IS_TOKEN(*ptr))) {
1347 if (likely(ptr == buf->data)) {
1348 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001349 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 } else {
1351#if PARSE_PRESERVE_EMPTY_LINES
1352 /* only skip empty leading lines, don't remove them */
1353 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001354 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355#else
1356 /* Remove empty leading lines, as recommended by
1357 * RFC2616. This takes a lot of time because we
1358 * must move all the buffer backwards, but this
1359 * is rarely needed. The method above will be
1360 * cleaner when we'll be able to start sending
1361 * the request from any place in the buffer.
1362 */
1363 buf->lr = ptr;
1364 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001365 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 msg->sol = buf->data;
1367 ptr = buf->data;
1368 end = buf->r;
1369#endif
1370 }
1371 hdr_idx_init(idx);
1372 state = HTTP_MSG_RPVER;
1373 goto http_msg_rpver;
1374 }
1375
1376 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1377 goto http_msg_invalid;
1378
1379 if (unlikely(*ptr == '\n'))
1380 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1381 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1382 /* stop here */
1383
1384 http_msg_rpbefore_cr:
1385 case HTTP_MSG_RPBEFORE_CR:
1386 EXPECT_LF_HERE(ptr, http_msg_invalid);
1387 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1388 /* stop here */
1389
1390 http_msg_rpver:
1391 case HTTP_MSG_RPVER:
1392 case HTTP_MSG_RPVER_SP:
1393 case HTTP_MSG_RPCODE:
1394 case HTTP_MSG_RPCODE_SP:
1395 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001396 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001397 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 if (unlikely(!ptr))
1399 return;
1400
1401 /* we have a full response and we know that we have either a CR
1402 * or an LF at <ptr>.
1403 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001404 //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 +01001405 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1406
1407 msg->sol = ptr;
1408 if (likely(*ptr == '\r'))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1410 goto http_msg_rpline_end;
1411
1412 http_msg_rpline_end:
1413 case HTTP_MSG_RPLINE_END:
1414 /* msg->sol must point to the first of CR or LF. */
1415 EXPECT_LF_HERE(ptr, http_msg_invalid);
1416 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1417 /* stop here */
1418
1419 /*
1420 * Second, states that are specific to the request only
1421 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_rqbefore:
1423 case HTTP_MSG_RQBEFORE:
1424 if (likely(HTTP_IS_TOKEN(*ptr))) {
1425 if (likely(ptr == buf->data)) {
1426 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001427 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 } else {
1429#if PARSE_PRESERVE_EMPTY_LINES
1430 /* only skip empty leading lines, don't remove them */
1431 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001432 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 /* Remove empty leading lines, as recommended by
1435 * RFC2616. This takes a lot of time because we
1436 * must move all the buffer backwards, but this
1437 * is rarely needed. The method above will be
1438 * cleaner when we'll be able to start sending
1439 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 buf->lr = ptr;
1442 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001443 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 msg->sol = buf->data;
1445 ptr = buf->data;
1446 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001449 /* we will need this when keep-alive will be supported
1450 hdr_idx_init(idx);
1451 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 state = HTTP_MSG_RQMETH;
1453 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1457 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 if (unlikely(*ptr == '\n'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001462 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_rqbefore_cr:
1465 case HTTP_MSG_RQBEFORE_CR:
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 http_msg_rqmeth:
1471 case HTTP_MSG_RQMETH:
1472 case HTTP_MSG_RQMETH_SP:
1473 case HTTP_MSG_RQURI:
1474 case HTTP_MSG_RQURI_SP:
1475 case HTTP_MSG_RQVER:
1476 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (unlikely(!ptr))
1479 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 /* we have a full request and we know that we have either a CR
1482 * or an LF at <ptr>.
1483 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001484 //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 +01001485 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 msg->sol = ptr;
1488 if (likely(*ptr == '\r'))
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 http_msg_rqline_end:
1493 case HTTP_MSG_RQLINE_END:
1494 /* check for HTTP/0.9 request : no version information available.
1495 * msg->sol must point to the first of CR or LF.
1496 */
1497 if (unlikely(msg->sl.rq.v_l == 0))
1498 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 EXPECT_LF_HERE(ptr, http_msg_invalid);
1501 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 /*
1505 * Common states below
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 http_msg_hdr_first:
1508 case HTTP_MSG_HDR_FIRST:
1509 msg->sol = ptr;
1510 if (likely(!HTTP_IS_CRLF(*ptr))) {
1511 goto http_msg_hdr_name;
1512 }
1513
1514 if (likely(*ptr == '\r'))
1515 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1516 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 http_msg_hdr_name:
1519 case HTTP_MSG_HDR_NAME:
1520 /* assumes msg->sol points to the first char */
1521 if (likely(HTTP_IS_TOKEN(*ptr)))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (likely(*ptr == ':')) {
1525 msg->col = ptr - buf->data;
1526 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1527 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 http_msg_hdr_l1_sp:
1532 case HTTP_MSG_HDR_L1_SP:
1533 /* assumes msg->sol points to the first char and msg->col to the colon */
1534 if (likely(HTTP_IS_SPHT(*ptr)))
1535 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 /* header value can be basically anything except CR/LF */
1538 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 if (likely(!HTTP_IS_CRLF(*ptr))) {
1541 goto http_msg_hdr_val;
1542 }
1543
1544 if (likely(*ptr == '\r'))
1545 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1546 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 http_msg_hdr_l1_lf:
1549 case HTTP_MSG_HDR_L1_LF:
1550 EXPECT_LF_HERE(ptr, http_msg_invalid);
1551 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 http_msg_hdr_l1_lws:
1554 case HTTP_MSG_HDR_L1_LWS:
1555 if (likely(HTTP_IS_SPHT(*ptr))) {
1556 /* replace HT,CR,LF with spaces */
1557 for (; buf->data+msg->sov < ptr; msg->sov++)
1558 buf->data[msg->sov] = ' ';
1559 goto http_msg_hdr_l1_sp;
1560 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001561 /* we had a header consisting only in spaces ! */
1562 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 goto http_msg_complete_header;
1564
1565 http_msg_hdr_val:
1566 case HTTP_MSG_HDR_VAL:
1567 /* assumes msg->sol points to the first char, msg->col to the
1568 * colon, and msg->sov points to the first character of the
1569 * value.
1570 */
1571 if (likely(!HTTP_IS_CRLF(*ptr)))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 msg->eol = ptr;
1575 /* Note: we could also copy eol into ->eoh so that we have the
1576 * real header end in case it ends with lots of LWS, but is this
1577 * really needed ?
1578 */
1579 if (likely(*ptr == '\r'))
1580 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1581 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 http_msg_hdr_l2_lf:
1584 case HTTP_MSG_HDR_L2_LF:
1585 EXPECT_LF_HERE(ptr, http_msg_invalid);
1586 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 http_msg_hdr_l2_lws:
1589 case HTTP_MSG_HDR_L2_LWS:
1590 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1591 /* LWS: replace HT,CR,LF with spaces */
1592 for (; msg->eol < ptr; msg->eol++)
1593 *msg->eol = ' ';
1594 goto http_msg_hdr_val;
1595 }
1596 http_msg_complete_header:
1597 /*
1598 * It was a new header, so the last one is finished.
1599 * Assumes msg->sol points to the first char, msg->col to the
1600 * colon, msg->sov points to the first character of the value
1601 * and msg->eol to the first CR or LF so we know how the line
1602 * ends. We insert last header into the index.
1603 */
1604 /*
1605 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1606 write(2, msg->sol, msg->eol-msg->sol);
1607 fprintf(stderr,"\n");
1608 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1611 idx, idx->tail) < 0))
1612 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 msg->sol = ptr;
1615 if (likely(!HTTP_IS_CRLF(*ptr))) {
1616 goto http_msg_hdr_name;
1617 }
1618
1619 if (likely(*ptr == '\r'))
1620 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1621 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 http_msg_last_lf:
1624 case HTTP_MSG_LAST_LF:
1625 /* Assumes msg->sol points to the first of either CR or LF */
1626 EXPECT_LF_HERE(ptr, http_msg_invalid);
1627 ptr++;
1628 buf->lr = ptr;
1629 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001630 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 return;
1632#ifdef DEBUG_FULL
1633 default:
1634 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1635 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001636#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 }
1638 http_msg_ood:
1639 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001640 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 buf->lr = ptr;
1642 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 http_msg_invalid:
1645 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001646 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 return;
1648}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001649
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001650/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001651 * It normally returns zero, but may return 1 if it absolutely needs to be
1652 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001653 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001654 * functions. Its behaviour is rather simple :
1655 * - all enabled analysers are called in turn from the lower to the higher
1656 * bit.
1657 * - if an analyser does not have enough data, it must return without calling
1658 * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure
1659 * that unprocessed data will not be forwarded. But that probably depends on
1660 * the protocol. Generally it is not reset in case of errors.
1661 * - if an analyser has enough data, it just has to pass on to the next
1662 * analyser without touching BF_MAY_FORWARD (it is enabled prior to
1663 * analysis).
1664 * - if an analyser thinks it has no added value anymore staying here, it must
1665 * reset its bit from the analysers flags in order not to be called anymore.
1666 *
1667 * In the future, analysers should be able to indicate that they want to be
1668 * called after XXX bytes have been received (or transfered), and the min of
1669 * all's wishes will be used to ring back (unless a special condition occurs).
1670 *
1671 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001673int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 struct buffer *req = t->req;
1676 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001678 DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
1679 now_ms, __FUNCTION__,
1680 cli_stnames[t->cli_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001681 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1682 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 +02001683 req->rex, rep->wex, req->flags, rep->flags, req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001685 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001686 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001687 struct tcp_rule *rule;
1688 int partial;
1689
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001690 /* We will abort if we encounter a read error. In theory, we
1691 * should not abort if we get a close, it might be valid,
1692 * although very unlikely. FIXME: we'll abort for now, this
1693 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001694 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001695 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001696 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001697 t->fe->failed_req++;
1698 if (!(t->flags & SN_ERR_MASK))
1699 t->flags |= SN_ERR_CLICL;
1700 if (!(t->flags & SN_FINST_MASK))
1701 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001702 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001703 }
1704
1705 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001706 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001707 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001708 t->fe->failed_req++;
1709 if (!(t->flags & SN_ERR_MASK))
1710 t->flags |= SN_ERR_CLITO;
1711 if (!(t->flags & SN_FINST_MASK))
1712 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001713 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001714 }
1715
1716 /* We don't know whether we have enough data, so must proceed
1717 * this way :
1718 * - iterate through all rules in their declaration order
1719 * - if one rule returns MISS, it means the inspect delay is
1720 * not over yet, then return immediately, otherwise consider
1721 * it as a non-match.
1722 * - if one rule returns OK, then return OK
1723 * - if one rule returns KO, then return KO
1724 */
1725
Willy Tarreauffab5b42008-08-17 18:03:28 +02001726 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001727 partial = 0;
1728 else
1729 partial = ACL_PARTIAL;
1730
1731 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1732 int ret = ACL_PAT_PASS;
1733
1734 if (rule->cond) {
1735 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1736 if (ret == ACL_PAT_MISS) {
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001737 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001738 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001739 if (!tick_isset(req->analyse_exp))
1740 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001741 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001743
Willy Tarreaub6866442008-07-14 23:54:42 +02001744 ret = acl_pass(ret);
1745 if (rule->cond->pol == ACL_COND_UNLESS)
1746 ret = !ret;
1747 }
1748
1749 if (ret) {
1750 /* we have a matching rule. */
1751 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001752 buffer_abort(req);
1753 buffer_abort(rep);
1754 //FIXME: this delete this
1755 //fd_delete(t->cli_fd);
1756 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001757 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001758 t->fe->failed_req++;
1759 if (!(t->flags & SN_ERR_MASK))
1760 t->flags |= SN_ERR_PRXCOND;
1761 if (!(t->flags & SN_FINST_MASK))
1762 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001763 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001764 }
1765 /* otherwise accept */
1766 break;
1767 }
1768 }
1769
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001770 /* if we get there, it means we have no rule which matches, or
1771 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001772 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001773 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001774 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001775 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001776
Willy Tarreau2df28e82008-08-17 15:20:19 +02001777 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 /*
1779 * Now parse the partial (or complete) lines.
1780 * We will check the request syntax, and also join multi-line
1781 * headers. An index of all the lines will be elaborated while
1782 * parsing.
1783 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001784 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001787 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 * req->data + req->eoh = end of processed headers / start of current one
1789 * req->data + req->eol = end of current header or line (LF or CRLF)
1790 * req->lr = first non-visited byte
1791 * req->r = end of data
1792 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001795 struct http_txn *txn = &t->txn;
1796 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001800 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001802 /* 1: we might have to print this header in debug mode */
1803 if (unlikely((global.mode & MODE_DEBUG) &&
1804 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001805 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001807
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001808 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 eol = sol + msg->sl.rq.l;
1810 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001811
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001812 sol += hdr_idx_first_pos(&txn->hdr_idx);
1813 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001814
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001816 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001818 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1819 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 }
1821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001822
Willy Tarreau58f10d72006-12-04 02:26:12 +01001823
1824 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826 * If not so, we check the FD and buffer states before leaving.
1827 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1829 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001830 *
1831 */
1832
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001833 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 /*
1835 * First, let's catch bad requests.
1836 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001837 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001839
1840 /* 1: Since we are in header mode, if there's no space
1841 * left for headers, we won't be able to free more
1842 * later, so the session will never terminate. We
1843 * must terminate it now.
1844 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001845 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 /* FIXME: check if URI is set and return Status
1847 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848 */
Willy Tarreau06619262006-12-17 08:37:22 +01001849 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001850 }
1851
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001852 /* 2: have we encountered a close ? */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001853 else if (req->flags & (BF_READ_NULL | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001854 txn->status = 400;
1855 client_retnclose(t, error_message(t, HTTP_ERR_400));
1856 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001857 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001858 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001859
Willy Tarreau58f10d72006-12-04 02:26:12 +01001860 if (!(t->flags & SN_ERR_MASK))
1861 t->flags |= SN_ERR_CLICL;
1862 if (!(t->flags & SN_FINST_MASK))
1863 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001864 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001865 }
1866
1867 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001868 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001870 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001871 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001872 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001873 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001874 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001875 if (!(t->flags & SN_ERR_MASK))
1876 t->flags |= SN_ERR_CLITO;
1877 if (!(t->flags & SN_FINST_MASK))
1878 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001879 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001880 }
1881
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001882 /* 4: have we encountered a read error ? */
1883 else if (req->flags & BF_READ_ERROR) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001884 /* we cannot return any message on error */
1885 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001886 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001887 t->fe->failed_req++;
1888 if (!(t->flags & SN_ERR_MASK))
1889 t->flags |= SN_ERR_CLICL;
1890 if (!(t->flags & SN_FINST_MASK))
1891 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001892 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001894
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001895 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001896 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001897 if (!tick_isset(req->analyse_exp))
1898 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001899
1900 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001901 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902 }
1903
1904
1905 /****************************************************************
1906 * More interesting part now : we know that we have a complete *
1907 * request which at least looks like HTTP. We have an indicator *
1908 * of each header's length, so we can parse them quickly. *
1909 ****************************************************************/
1910
Willy Tarreau2df28e82008-08-17 15:20:19 +02001911 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001912 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001913
Willy Tarreau9cdde232007-05-02 20:58:19 +02001914 /* ensure we keep this pointer to the beginning of the message */
1915 msg->sol = req->data + msg->som;
1916
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 /*
1918 * 1: identify the method
1919 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001920 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001922 /* we can make use of server redirect on GET and HEAD */
1923 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1924 t->flags |= SN_REDIRECTABLE;
1925
Willy Tarreau58f10d72006-12-04 02:26:12 +01001926 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001928 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001929 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001930 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001931 if (unlikely((t->fe->monitor_uri_len != 0) &&
1932 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1933 !memcmp(&req->data[msg->sl.rq.u],
1934 t->fe->monitor_uri,
1935 t->fe->monitor_uri_len))) {
1936 /*
1937 * We have found the monitor URI
1938 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001939 struct acl_cond *cond;
1940 cur_proxy = t->fe;
1941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001943
1944 /* Check if we want to fail this monitor request or not */
1945 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1946 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001947
1948 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001949 if (cond->pol == ACL_COND_UNLESS)
1950 ret = !ret;
1951
1952 if (ret) {
1953 /* we fail this request, let's return 503 service unavail */
1954 txn->status = 503;
1955 client_retnclose(t, error_message(t, HTTP_ERR_503));
1956 goto return_prx_cond;
1957 }
1958 }
1959
1960 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001961 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962 client_retnclose(t, &http_200_chunk);
1963 goto return_prx_cond;
1964 }
1965
1966 /*
1967 * 3: Maybe we have to copy the original REQURI for the logs ?
1968 * Note: we cannot log anymore if the request has been
1969 * classified as invalid.
1970 */
1971 if (unlikely(t->logs.logwait & LW_REQ)) {
1972 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001973 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 int urilen = msg->sl.rq.l;
1975
1976 if (urilen >= REQURI_LEN)
1977 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001978 memcpy(txn->uri, &req->data[msg->som], urilen);
1979 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001980
1981 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001982 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983 } else {
1984 Alert("HTTP logging : out of memory.\n");
1985 }
1986 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001987
Willy Tarreau06619262006-12-17 08:37:22 +01001988
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1990 if (unlikely(msg->sl.rq.v_l == 0)) {
1991 int delta;
1992 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001993 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001994 cur_end = msg->sol + msg->sl.rq.l;
1995 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001997 if (msg->sl.rq.u_l == 0) {
1998 /* if no URI was set, add "/" */
1999 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2000 cur_end += delta;
2001 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002002 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 /* add HTTP version */
2004 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2005 msg->eoh += delta;
2006 cur_end += delta;
2007 cur_end = (char *)http_parse_reqline(msg, req->data,
2008 HTTP_MSG_RQMETH,
2009 msg->sol, cur_end + 1,
2010 NULL, NULL);
2011 if (unlikely(!cur_end))
2012 goto return_bad_req;
2013
2014 /* we have a full HTTP/1.0 request now and we know that
2015 * we have either a CR or an LF at <ptr>.
2016 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002017 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002018 }
2019
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002020
2021 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002022 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002023 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002024 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002025
2026 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002027 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002028 * As opposed to version 1.2, now they will be evaluated in the
2029 * filters order and not in the header order. This means that
2030 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002031 *
2032 * We can now check whether we want to switch to another
2033 * backend, in which case we will re-check the backend's
2034 * filters and various options. In order to support 3-level
2035 * switching, here's how we should proceed :
2036 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002037 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002038 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002039 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002040 * There cannot be any switch from there, so ->be cannot be
2041 * changed anymore.
2042 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002043 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002044 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002045 * The response path will be able to apply either ->be, or
2046 * ->be then ->fe filters in order to match the reverse of
2047 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002048 */
2049
Willy Tarreau06619262006-12-17 08:37:22 +01002050 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002051 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002052 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002053 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002054 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002055
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002056 /* first check whether we have some ACLs set to redirect this request */
2057 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2058 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002059
2060 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002061 if (rule->cond->pol == ACL_COND_UNLESS)
2062 ret = !ret;
2063
2064 if (ret) {
2065 struct chunk rdr = { trash, 0 };
2066 const char *msg_fmt;
2067
2068 /* build redirect message */
2069 switch(rule->code) {
2070 case 303:
2071 rdr.len = strlen(HTTP_303);
2072 msg_fmt = HTTP_303;
2073 break;
2074 case 301:
2075 rdr.len = strlen(HTTP_301);
2076 msg_fmt = HTTP_301;
2077 break;
2078 case 302:
2079 default:
2080 rdr.len = strlen(HTTP_302);
2081 msg_fmt = HTTP_302;
2082 break;
2083 }
2084
2085 if (unlikely(rdr.len > sizeof(trash)))
2086 goto return_bad_req;
2087 memcpy(rdr.str, msg_fmt, rdr.len);
2088
2089 switch(rule->type) {
2090 case REDIRECT_TYPE_PREFIX: {
2091 const char *path;
2092 int pathlen;
2093
2094 path = http_get_path(txn);
2095 /* build message using path */
2096 if (path) {
2097 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2098 } else {
2099 path = "/";
2100 pathlen = 1;
2101 }
2102
2103 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2104 goto return_bad_req;
2105
2106 /* add prefix */
2107 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2108 rdr.len += rule->rdr_len;
2109
2110 /* add path */
2111 memcpy(rdr.str + rdr.len, path, pathlen);
2112 rdr.len += pathlen;
2113 break;
2114 }
2115 case REDIRECT_TYPE_LOCATION:
2116 default:
2117 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2118 goto return_bad_req;
2119
2120 /* add location */
2121 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2122 rdr.len += rule->rdr_len;
2123 break;
2124 }
2125
2126 /* add end of headers */
2127 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2128 rdr.len += 4;
2129
2130 txn->status = rule->code;
2131 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002132 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002133 client_retnclose(t, &rdr);
2134 goto return_prx_cond;
2135 }
2136 }
2137
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002138 /* first check whether we have some ACLs set to block this request */
2139 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002140 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002141
2142 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002143 if (cond->pol == ACL_COND_UNLESS)
2144 ret = !ret;
2145
2146 if (ret) {
2147 txn->status = 403;
2148 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002149 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002150 client_retnclose(t, error_message(t, HTTP_ERR_403));
2151 goto return_prx_cond;
2152 }
2153 }
2154
Willy Tarreau06619262006-12-17 08:37:22 +01002155 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002156 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002157 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2158 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002159 }
2160
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002161 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2162 /* to ensure correct connection accounting on
2163 * the backend, we count the connection for the
2164 * one managing the queue.
2165 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002166 t->be->beconn++;
2167 if (t->be->beconn > t->be->beconn_max)
2168 t->be->beconn_max = t->be->beconn;
2169 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002170 t->flags |= SN_BE_ASSIGNED;
2171 }
2172
Willy Tarreau06619262006-12-17 08:37:22 +01002173 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002174 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002175 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002176 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002177 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002178 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002179 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002180 goto return_prx_cond;
2181 }
2182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002183 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002184 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002185 !(t->flags & SN_CONN_CLOSED)) {
2186 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002187 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002188 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002189
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002190 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002191 old_idx = 0;
2192
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002193 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2194 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002195 cur_ptr = cur_next;
2196 cur_end = cur_ptr + cur_hdr->len;
2197 cur_next = cur_end + cur_hdr->cr + 1;
2198
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002199 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2200 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002201 /* 3 possibilities :
2202 * - we have already set Connection: close,
2203 * so we remove this line.
2204 * - we have not yet set Connection: close,
2205 * but this line indicates close. We leave
2206 * it untouched and set the flag.
2207 * - we have not yet set Connection: close,
2208 * and this line indicates non-close. We
2209 * replace it.
2210 */
2211 if (t->flags & SN_CONN_CLOSED) {
2212 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002213 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002214 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002215 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2216 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002217 cur_hdr->len = 0;
2218 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002219 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2220 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2221 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002222 cur_next += delta;
2223 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002224 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002225 }
2226 t->flags |= SN_CONN_CLOSED;
2227 }
2228 }
2229 old_idx = cur_idx;
2230 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002231 }
2232 /* add request headers from the rule sets in the same order */
2233 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2234 if (unlikely(http_header_add_tail(req,
2235 &txn->req,
2236 &txn->hdr_idx,
2237 rule_set->req_add[cur_idx])) < 0)
2238 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002239 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002241 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002242 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002243 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002244 /* we have to check the URI and auth for this request.
2245 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002246 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002247 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002248 if (stats_check_uri_auth(t, rule_set))
2249 return 1;
2250 }
2251
Willy Tarreau55ea7572007-06-17 19:56:27 +02002252 /* now check whether we have some switching rules for this request */
2253 if (!(t->flags & SN_BE_ASSIGNED)) {
2254 struct switching_rule *rule;
2255
2256 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2257 int ret;
2258
2259 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002260
2261 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002262 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002263 ret = !ret;
2264
2265 if (ret) {
2266 t->be = rule->be.backend;
2267 t->be->beconn++;
2268 if (t->be->beconn > t->be->beconn_max)
2269 t->be->beconn_max = t->be->beconn;
2270 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002271
2272 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002273 t->rep->rto = t->req->wto = t->be->timeout.server;
2274 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002275 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002276 t->flags |= SN_BE_ASSIGNED;
2277 break;
2278 }
2279 }
2280 }
2281
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002282 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2283 /* No backend was set, but there was a default
2284 * backend set in the frontend, so we use it and
2285 * loop again.
2286 */
2287 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002288 t->be->beconn++;
2289 if (t->be->beconn > t->be->beconn_max)
2290 t->be->beconn_max = t->be->beconn;
2291 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002292
2293 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002294 t->rep->rto = t->req->wto = t->be->timeout.server;
2295 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002296 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002297 t->flags |= SN_BE_ASSIGNED;
2298 }
2299 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002300
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002302 if (!(t->flags & SN_BE_ASSIGNED)) {
2303 /* To ensure correct connection accounting on
2304 * the backend, we count the connection for the
2305 * one managing the queue.
2306 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002307 t->be->beconn++;
2308 if (t->be->beconn > t->be->beconn_max)
2309 t->be->beconn_max = t->be->beconn;
2310 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002311 t->flags |= SN_BE_ASSIGNED;
2312 }
2313
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002314 /*
2315 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002316 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002317 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002318 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002319 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002320
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002321 /*
2322 * If HTTP PROXY is set we simply get remote server address
2323 * parsing incoming request.
2324 */
2325 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2326 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2327 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002328
Willy Tarreau2a324282006-12-05 00:05:46 +01002329 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002330 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002331 * so let's do the same now.
2332 */
2333
2334 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002335 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002336 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002337 }
2338
2339
2340 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002341 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002342 * Note that doing so might move headers in the request, but
2343 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002344 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002345 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002346 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2347 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002348 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002349
Willy Tarreau58f10d72006-12-04 02:26:12 +01002350
Willy Tarreau2a324282006-12-05 00:05:46 +01002351 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002352 * 9: add X-Forwarded-For if either the frontend or the backend
2353 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002354 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002355 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002356 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002357 /* Add an X-Forwarded-For header unless the source IP is
2358 * in the 'except' network range.
2359 */
2360 if ((!t->fe->except_mask.s_addr ||
2361 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2362 != t->fe->except_net.s_addr) &&
2363 (!t->be->except_mask.s_addr ||
2364 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2365 != t->be->except_net.s_addr)) {
2366 int len;
2367 unsigned char *pn;
2368 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002369
Ross Westaf72a1d2008-08-03 10:51:45 +02002370 /* Note: we rely on the backend to get the header name to be used for
2371 * x-forwarded-for, because the header is really meant for the backends.
2372 * However, if the backend did not specify any option, we have to rely
2373 * on the frontend's header name.
2374 */
2375 if (t->be->fwdfor_hdr_len) {
2376 len = t->be->fwdfor_hdr_len;
2377 memcpy(trash, t->be->fwdfor_hdr_name, len);
2378 } else {
2379 len = t->fe->fwdfor_hdr_len;
2380 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2381 }
2382 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002383
Ross Westaf72a1d2008-08-03 10:51:45 +02002384 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002385 &txn->hdr_idx, trash, len)) < 0)
2386 goto return_bad_req;
2387 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002388 }
2389 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002390 /* FIXME: for the sake of completeness, we should also support
2391 * 'except' here, although it is mostly useless in this case.
2392 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002393 int len;
2394 char pn[INET6_ADDRSTRLEN];
2395 inet_ntop(AF_INET6,
2396 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2397 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002398
2399 /* 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, ": %s", pn);
2412
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002413 if (unlikely(http_header_add_tail2(req, &txn->req,
2414 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002415 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002416 }
2417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418
Willy Tarreau2a324282006-12-05 00:05:46 +01002419 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002420 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002421 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002422 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002423 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002424 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002425 if ((unlikely(msg->sl.rq.v_l != 8) ||
2426 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2427 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002428 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002429 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002430 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002431 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002432 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2433 * If not assigned, perhaps we are balancing on url_param, but this is a
2434 * POST; and the parameters are in the body, maybe scan there to find our server.
2435 * (unless headers overflowed the buffer?)
2436 */
2437 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2438 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002439 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002440 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2441 /* are there enough bytes here? total == l || r || rlim ?
2442 * len is unsigned, but eoh is int,
2443 * how many bytes of body have we received?
2444 * eoh is the first empty line of the header
2445 */
2446 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002447 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 +02002448
2449 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2450 * We can't assume responsibility for the server's decision,
2451 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2452 * We also can't change our mind later, about which server to choose, so round robin.
2453 */
2454 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2455 struct hdr_ctx ctx;
2456 ctx.idx = 0;
2457 /* Expect is allowed in 1.1, look for it */
2458 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2459 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002460 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002461 /* We can't reliablly stall and wait for data, because of
2462 * .NET clients that don't conform to rfc2616; so, no need for
2463 * the next block to check length expectations.
2464 * We could send 100 status back to the client, but then we need to
2465 * re-write headers, and send the message. And this isn't the right
2466 * place for that action.
2467 * TODO: support Expect elsewhere and delete this block.
2468 */
2469 goto end_check_maybe_wait_for_body;
2470 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002471
2472 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002473 /* nothing to do, we got enough */
2474 } else {
2475 /* limit implies we are supposed to need this many bytes
2476 * to find the parameter. Let's see how many bytes we can wait for.
2477 */
2478 long long hint = len;
2479 struct hdr_ctx ctx;
2480 ctx.idx = 0;
2481 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002482 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2483 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002484 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002485 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002486 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002487 ctx.idx = 0;
2488 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2489 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002490 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002491 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002492 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002493 hint = 0; /* parse failure, untrusted client */
2494 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002495 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002496 msg->hdr_content_len = hint;
2497 else
2498 hint = 0; /* bad client, sent negative length */
2499 }
2500 }
2501 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002502 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002503 hint = t->be->url_param_post_limit;
2504 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002505 if (len < hint) {
2506 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002507 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002508 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002509 /* else... There are no body bytes to wait for */
2510 }
2511 }
2512 }
2513 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514
Willy Tarreau2a324282006-12-05 00:05:46 +01002515 /*************************************************************
2516 * OK, that's finished for the headers. We have done what we *
2517 * could. Let's switch to the DATA state. *
2518 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002519
Willy Tarreaue393fe22008-08-16 22:18:07 +02002520 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002521 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002522
Willy Tarreau1fa31262007-12-03 00:36:16 +01002523 /* When a connection is tarpitted, we use the tarpit timeout,
2524 * which may be the same as the connect timeout if unspecified.
2525 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002526 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002527 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002528 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002529 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002530 /* flush the request so that we can drop the connection early
2531 * if the client closes first.
2532 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002533 req->flags &= ~BF_MAY_FORWARD;
2534 req->analysers |= AN_REQ_HTTP_TARPIT;
2535 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2536 if (!req->analyse_exp)
2537 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002540 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002541 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002542
2543 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002544 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002545 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002546 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002547 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002548 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002549 return_prx_cond:
2550 if (!(t->flags & SN_ERR_MASK))
2551 t->flags |= SN_ERR_PRXCOND;
2552 if (!(t->flags & SN_FINST_MASK))
2553 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002554 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002555 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002556 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002557 }
2558
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002559 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2560 struct http_txn *txn = &t->txn;
2561
2562 /* This connection is being tarpitted. The CLIENT side has
2563 * already set the connect expiration date to the right
2564 * timeout. We just have to check that the client is still
2565 * there and that the timeout has not expired.
2566 */
2567 if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 &&
2568 !tick_is_expired(req->analyse_exp, now_ms))
2569 return 0;
2570
2571 /* We will set the queue timer to the time spent, just for
2572 * logging purposes. We fake a 500 server error, so that the
2573 * attacker will not suspect his connection has been tarpitted.
2574 * It will not cause trouble to the logs because we can exclude
2575 * the tarpitted connections by filtering on the 'PT' status flags.
2576 */
2577 trace_term(t, TT_HTTP_SRV_2);
2578 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2579
2580 txn->status = 500;
2581 if (req->flags != BF_READ_ERROR)
2582 client_retnclose(t, error_message(t, HTTP_ERR_500));
2583
2584 req->analysers = 0;
2585 req->analyse_exp = TICK_ETERNITY;
2586
2587 t->fe->failed_req++;
2588 if (!(t->flags & SN_ERR_MASK))
2589 t->flags |= SN_ERR_PRXCOND;
2590 if (!(t->flags & SN_FINST_MASK))
2591 t->flags |= SN_FINST_T;
2592 return 0;
2593 }
2594
Willy Tarreau2df28e82008-08-17 15:20:19 +02002595 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002596 /* We have to parse the HTTP request body to find any required data.
2597 * "balance url_param check_post" should have been the only way to get
2598 * into this. We were brought here after HTTP header analysis, so all
2599 * related structures are ready.
2600 */
2601 struct http_msg *msg = &t->txn.req;
2602 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2603 long long limit = t->be->url_param_post_limit;
2604 struct hdr_ctx ctx;
2605
2606 ctx.idx = 0;
2607
2608 /* now if we have a length, we'll take the hint */
2609 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2610 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2611 unsigned int chunk = 0;
2612 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2613 char c = msg->sol[body];
2614 if (ishex(c)) {
2615 unsigned int hex = toupper(c) - '0';
2616 if (hex > 9)
2617 hex -= 'A' - '9' - 1;
2618 chunk = (chunk << 4) | hex;
2619 } else
2620 break;
2621 body++;
2622 }
2623 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002624 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002625
2626 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002627 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002628
2629 body += 2; // skip CRLF
2630
2631 /* if we support more then one chunk here, we have to do it again when assigning server
2632 * 1. how much entity data do we have? new var
2633 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2634 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2635 */
2636
2637 if (chunk < limit)
2638 limit = chunk; /* only reading one chunk */
2639 } else {
2640 if (msg->hdr_content_len < limit)
2641 limit = msg->hdr_content_len;
2642 }
2643
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002644 http_body_end:
2645 /* we leave once we know we have nothing left to do. This means that we have
2646 * enough bytes, or that we know we'll not get any more (buffer full, read
2647 * buffer closed).
2648 */
2649 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002650 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002651 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002652 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002653 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002654 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002655 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002656 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002657 else {
2658 /* Not enough data. We'll re-use the http-request
2659 * timeout here. Ideally, we should set the timeout
2660 * relative to the accept() date. We just set the
2661 * request timeout once at the beginning of the
2662 * request.
2663 */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002664 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc52164a2008-08-17 19:17:57 +02002665 if (!tick_isset(req->analyse_exp))
2666 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2667 return 0;
2668 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002669 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002670
Willy Tarreau2df28e82008-08-17 15:20:19 +02002671 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2672 * probably reduce one day's debugging session.
2673 */
2674#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002675 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 +02002676 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2677 __FILE__, __LINE__, req->analysers);
2678 ABORT_NOW();
2679 }
2680#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002681 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002682 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002683}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002684
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002685/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002686 * It normally returns zero, but may return 1 if it absolutely needs to be
2687 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002688 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002689 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002690 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002691int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002692{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002693 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002694 struct buffer *req = t->req;
2695 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002696
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002697 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
2698 now_ms, __FUNCTION__,
2699 cli_stnames[t->cli_state],
Willy Tarreau2df28e82008-08-17 15:20:19 +02002700 req->rex, rep->wex, req->flags, rep->flags, rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002701
Willy Tarreau2df28e82008-08-17 15:20:19 +02002702 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002703 /*
2704 * Now parse the partial (or complete) lines.
2705 * We will check the response syntax, and also join multi-line
2706 * headers. An index of all the lines will be elaborated while
2707 * parsing.
2708 *
2709 * For the parsing, we use a 28 states FSM.
2710 *
2711 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002712 * rep->data + rep->som = beginning of response
2713 * rep->data + rep->eoh = end of processed headers / start of current one
2714 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002715 * rep->lr = first non-visited byte
2716 * rep->r = end of data
2717 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002718
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002719 int cur_idx;
2720 struct http_msg *msg = &txn->rsp;
2721 struct proxy *cur_proxy;
2722
2723 if (likely(rep->lr < rep->r))
2724 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2725
2726 /* 1: we might have to print this header in debug mode */
2727 if (unlikely((global.mode & MODE_DEBUG) &&
2728 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2729 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2730 char *eol, *sol;
2731
2732 sol = rep->data + msg->som;
2733 eol = sol + msg->sl.rq.l;
2734 debug_hdr("srvrep", t, sol, eol);
2735
2736 sol += hdr_idx_first_pos(&txn->hdr_idx);
2737 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2738
2739 while (cur_idx) {
2740 eol = sol + txn->hdr_idx.v[cur_idx].len;
2741 debug_hdr("srvhdr", t, sol, eol);
2742 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2743 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002744 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002745 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002746
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002747 /*
2748 * Now we quickly check if we have found a full valid response.
2749 * If not so, we check the FD and buffer states before leaving.
2750 * A full response is indicated by the fact that we have seen
2751 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2752 * responses are checked first.
2753 *
2754 * Depending on whether the client is still there or not, we
2755 * may send an error response back or not. Note that normally
2756 * we should only check for HTTP status there, and check I/O
2757 * errors somewhere else.
2758 */
2759
2760 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002761 /* Invalid response */
2762 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2763 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002764 //buffer_shutr(rep);
2765 //buffer_shutw(req);
2766 //fd_delete(req->cons->fd);
2767 //req->cons->state = SI_ST_CLO;
2768 buffer_shutr_now(rep);
2769 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002770 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002771 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002772 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002773 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002775 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002776 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002777 txn->status = 502;
2778 client_return(t, error_message(t, HTTP_ERR_502));
2779 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002780 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002781 if (!(t->flags & SN_FINST_MASK))
2782 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002783
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002784 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2785 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002786
Willy Tarreaudafde432008-08-17 01:00:46 +02002787 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002789 /* write error to client, read error or close from server */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002790 if (rep->flags & (BF_WRITE_ERROR|BF_SHUTW|BF_READ_ERROR|BF_SHUTR|BF_READ_NULL)) {
2791 buffer_shutr_now(rep);
2792 buffer_shutw_now(req);
2793 //fd_delete(req->cons->fd);
2794 //req->cons->state = SI_ST_CLO;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002795 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002796 //t->srv->cur_sess--;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002797 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002798 //sess_change_server(t, NULL);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002799 }
2800 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002801 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002802 txn->status = 502;
2803 client_return(t, error_message(t, HTTP_ERR_502));
2804 if (!(t->flags & SN_ERR_MASK))
2805 t->flags |= SN_ERR_SRVCL;
2806 if (!(t->flags & SN_FINST_MASK))
2807 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002808
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002809 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2810 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002811
Willy Tarreaudafde432008-08-17 01:00:46 +02002812 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002813 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002814 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002815 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002816 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002817 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002818 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002819 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002820 buffer_shutr_now(rep);
2821 buffer_shutw_now(req);
2822 //fd_delete(req->cons->fd);
2823 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002824 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002825 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002826 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002827 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002828 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002829 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002830 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002831 txn->status = 504;
2832 client_return(t, error_message(t, HTTP_ERR_504));
2833 if (!(t->flags & SN_ERR_MASK))
2834 t->flags |= SN_ERR_SRVTO;
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 Tarreaudafde432008-08-17 01:00:46 +02002840 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002841 }
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002842
2843 rep->flags &= ~BF_MAY_FORWARD;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002844 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846
Willy Tarreau21d2af32008-02-14 20:25:24 +01002847
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002848 /*****************************************************************
2849 * More interesting part now : we know that we have a complete *
2850 * response which at least looks like HTTP. We have an indicator *
2851 * of each header's length, so we can parse them quickly. *
2852 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002853
Willy Tarreau2df28e82008-08-17 15:20:19 +02002854 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002855
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 /* ensure we keep this pointer to the beginning of the message */
2857 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002858
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002859 /*
2860 * 1: get the status code and check for cacheability.
2861 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 t->logs.logwait &= ~LW_RESP;
2864 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002865
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 switch (txn->status) {
2867 case 200:
2868 case 203:
2869 case 206:
2870 case 300:
2871 case 301:
2872 case 410:
2873 /* RFC2616 @13.4:
2874 * "A response received with a status code of
2875 * 200, 203, 206, 300, 301 or 410 MAY be stored
2876 * by a cache (...) unless a cache-control
2877 * directive prohibits caching."
2878 *
2879 * RFC2616 @9.5: POST method :
2880 * "Responses to this method are not cacheable,
2881 * unless the response includes appropriate
2882 * Cache-Control or Expires header fields."
2883 */
2884 if (likely(txn->meth != HTTP_METH_POST) &&
2885 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2886 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2887 break;
2888 default:
2889 break;
2890 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002891
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002892 /*
2893 * 2: we may need to capture headers
2894 */
2895 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2896 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2897 txn->rsp.cap, t->fe->rsp_cap);
2898
2899 /*
2900 * 3: we will have to evaluate the filters.
2901 * As opposed to version 1.2, now they will be evaluated in the
2902 * filters order and not in the header order. This means that
2903 * each filter has to be validated among all headers.
2904 *
2905 * Filters are tried with ->be first, then with ->fe if it is
2906 * different from ->be.
2907 */
2908
2909 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2910
2911 cur_proxy = t->be;
2912 while (1) {
2913 struct proxy *rule_set = cur_proxy;
2914
2915 /* try headers filters */
2916 if (rule_set->rsp_exp != NULL) {
2917 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2918 return_bad_resp:
2919 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002920 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002921 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002922 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 }
2924 cur_proxy->failed_resp++;
2925 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002926 buffer_shutr_now(rep);
2927 buffer_shutw_now(req);
2928 //fd_delete(req->cons->fd);
2929 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002930 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 txn->status = 502;
2932 client_return(t, error_message(t, HTTP_ERR_502));
2933 if (!(t->flags & SN_ERR_MASK))
2934 t->flags |= SN_ERR_PRXCOND;
2935 if (!(t->flags & SN_FINST_MASK))
2936 t->flags |= SN_FINST_H;
2937 /* We used to have a free connection slot. Since we'll never use it,
2938 * we have to inform the server that it may be used by another session.
2939 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002940 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2941 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002942 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002943 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002944 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002946 /* has the response been denied ? */
2947 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002948 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_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002951 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002952 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 cur_proxy->denied_resp++;
2954 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002955 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002957 /* We might have to check for "Connection:" */
2958 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2959 !(t->flags & SN_CONN_CLOSED)) {
2960 char *cur_ptr, *cur_end, *cur_next;
2961 int cur_idx, old_idx, delta, val;
2962 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002964 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2965 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002966
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002967 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2968 cur_hdr = &txn->hdr_idx.v[cur_idx];
2969 cur_ptr = cur_next;
2970 cur_end = cur_ptr + cur_hdr->len;
2971 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002972
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002973 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2974 if (val) {
2975 /* 3 possibilities :
2976 * - we have already set Connection: close,
2977 * so we remove this line.
2978 * - we have not yet set Connection: close,
2979 * but this line indicates close. We leave
2980 * it untouched and set the flag.
2981 * - we have not yet set Connection: close,
2982 * and this line indicates non-close. We
2983 * replace it.
2984 */
2985 if (t->flags & SN_CONN_CLOSED) {
2986 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2987 txn->rsp.eoh += delta;
2988 cur_next += delta;
2989 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2990 txn->hdr_idx.used--;
2991 cur_hdr->len = 0;
2992 } else {
2993 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2994 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2995 "close", 5);
2996 cur_next += delta;
2997 cur_hdr->len += delta;
2998 txn->rsp.eoh += delta;
2999 }
3000 t->flags |= SN_CONN_CLOSED;
3001 }
3002 }
3003 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003004 }
3005 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003006
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003007 /* add response headers from the rule sets in the same order */
3008 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3009 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3010 rule_set->rsp_add[cur_idx])) < 0)
3011 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003012 }
3013
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003014 /* check whether we're already working on the frontend */
3015 if (cur_proxy == t->fe)
3016 break;
3017 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 /*
3021 * 4: check for server cookie.
3022 */
3023 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3024 || (t->be->options & PR_O_CHK_CACHE))
3025 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003026
Willy Tarreaubaaee002006-06-26 02:48:02 +02003027
Willy Tarreaua15645d2007-03-18 16:22:39 +01003028 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003029 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003030 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003031 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3032 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /*
3035 * 6: add server cookie in the response if needed
3036 */
3037 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3038 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3039 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003041 /* the server is known, it's not the one the client requested, we have to
3042 * insert a set-cookie here, except if we want to insert only on POST
3043 * requests and this one isn't. Note that servers which don't have cookies
3044 * (eg: some backup servers) will return a full cookie removal request.
3045 */
3046 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3047 t->be->cookie_name,
3048 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003049
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003050 if (t->be->cookie_domain)
3051 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003053 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3054 trash, len)) < 0)
3055 goto return_bad_resp;
3056 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003058 /* Here, we will tell an eventual cache on the client side that we don't
3059 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3060 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3061 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3062 */
3063 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003065 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3066
3067 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3068 "Cache-control: private", 22)) < 0)
3069 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003070 }
3071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003074 /*
3075 * 7: check if result will be cacheable with a cookie.
3076 * We'll block the response if security checks have caught
3077 * nasty things such as a cacheable cookie.
3078 */
3079 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3080 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3081 (t->be->options & PR_O_CHK_CACHE)) {
3082
3083 /* we're in presence of a cacheable response containing
3084 * a set-cookie header. We'll block it as requested by
3085 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003088 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003089 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003090 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003091 }
3092 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003094 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3095 t->be->id, t->srv?t->srv->id:"<dispatch>");
3096 send_log(t->be, LOG_ALERT,
3097 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3098 t->be->id, t->srv?t->srv->id:"<dispatch>");
3099 goto return_srv_prx_502;
3100 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101
3102 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003103 * 8: add "Connection: close" if needed and not yet set.
3104 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003105 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003106 if (!(t->flags & SN_CONN_CLOSED) &&
3107 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3108 if ((unlikely(msg->sl.st.v_l != 8) ||
3109 unlikely(req->data[msg->som + 7] != '0')) &&
3110 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3111 "Connection: close", 17)) < 0)
3112 goto return_bad_resp;
3113 t->flags |= SN_CONN_CLOSED;
3114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003115
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003116 /*************************************************************
3117 * OK, that's finished for the headers. We have done what we *
3118 * could. Let's switch to the DATA state. *
3119 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120
Willy Tarreaue393fe22008-08-16 22:18:07 +02003121 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003122 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003124#ifdef CONFIG_HAP_TCPSPLICE
3125 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3126 /* TCP splicing supported by both FE and BE */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003127 tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 }
3129#endif
3130 /* if the user wants to log as soon as possible, without counting
3131 * bytes from the server, then this is the right moment. We have
3132 * to temporarily assign bytes_out to log what we currently have.
3133 */
3134 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3135 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3136 t->logs.bytes_out = txn->rsp.eoh;
3137 if (t->fe->to_log & LW_REQ)
3138 http_sess_log(t);
3139 else
3140 tcp_sess_log(t);
3141 t->logs.bytes_out = 0;
3142 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003143
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003144 /* Note: we must not try to cheat by jumping directly to DATA,
3145 * otherwise we would not let the client side wake up.
3146 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003147
Willy Tarreaudafde432008-08-17 01:00:46 +02003148 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003149 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003150
Willy Tarreau2df28e82008-08-17 15:20:19 +02003151 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3152 * probably reduce one day's debugging session.
3153 */
3154#ifdef DEBUG_DEV
3155 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3156 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3157 __FILE__, __LINE__, rep->analysers);
3158 ABORT_NOW();
3159 }
3160#endif
3161 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003162 return 0;
3163}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003164
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003166 * Manages the client FSM and its socket. It normally returns zero, but may
3167 * return 1 if it absolutely wants to be called again.
3168 *
3169 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003170 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003171 */
3172int process_cli(struct session *t)
3173{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003174 struct buffer *req = t->req;
3175 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003176
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003177 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",
3178 now_ms, __FUNCTION__,
3179 t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3180 cli_stnames[t->cli_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003181 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3182 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 +02003183 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003184 req->flags, rep->flags,
3185 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003186
Willy Tarreaudafde432008-08-17 01:00:46 +02003187 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003188 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3189 * still set this state (and will do until unix sockets are converted).
3190 */
3191 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003192 /* we can skip most of the tests at once if some conditions are not met */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003193 if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3194 (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3195 (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003196 (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3197 (!(rep->flags & BF_SHUTW) &&
3198 (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3199 goto update_timeouts;
3200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003201 /* read or write error */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003202 if (fdtab[t->cli_fd].state == FD_STERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003203 buffer_shutr(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003204 req->flags |= BF_READ_ERROR;
Willy Tarreauba392ce2008-08-16 21:13:23 +02003205 buffer_shutw(rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003206 rep->flags |= BF_WRITE_ERROR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003207 fd_delete(t->cli_fd);
3208 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003209 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003210 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003211 if (!(t->flags & SN_ERR_MASK))
3212 t->flags |= SN_ERR_CLICL;
3213 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003214 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003215 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003216 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003217 t->flags |= SN_FINST_C;
3218 else
3219 t->flags |= SN_FINST_D;
3220 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003221 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003222 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003223 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003224 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003225 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003226 req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003227 buffer_shutr(req);
3228 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003229 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003230 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003231 } else {
3232 /* output was already closed */
3233 fd_delete(t->cli_fd);
3234 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003235 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003236 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003237 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003238 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003239 /* last server read and buffer empty : we only check them when we're
3240 * allowed to forward the data.
3241 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003242 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003243 ((rep->flags & BF_SHUTW_NOW) ||
3244 (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3245 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003246 buffer_shutw(rep);
3247 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003248 EV_FD_CLR(t->cli_fd, DIR_WR);
3249 shutdown(t->cli_fd, SHUT_WR);
3250 /* We must ensure that the read part is still alive when switching to shutw */
3251 /* FIXME: is this still true ? */
3252 EV_FD_SET(t->cli_fd, DIR_RD);
3253 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003254 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003255 } else {
3256 fd_delete(t->cli_fd);
3257 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003258 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003259 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003260 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003261 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003262 /* read timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003263 else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003264 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003265 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003266 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003267 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003268 } else {
3269 /* output was already closed */
3270 fd_delete(t->cli_fd);
3271 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003272 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003273 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003274 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003275 if (!(t->flags & SN_ERR_MASK))
3276 t->flags |= SN_ERR_CLITO;
3277 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003278 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003279 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003280 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003281 t->flags |= SN_FINST_C;
3282 else
3283 t->flags |= SN_FINST_D;
3284 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003285 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003286 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003287 }
3288 /* write timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003289 else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003290 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003291 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003292 EV_FD_CLR(t->cli_fd, DIR_WR);
3293 shutdown(t->cli_fd, SHUT_WR);
3294 /* We must ensure that the read part is still alive when switching to shutw */
3295 /* FIXME: is this still true ? */
3296 EV_FD_SET(t->cli_fd, DIR_RD);
3297 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003298 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003299 } else {
3300 fd_delete(t->cli_fd);
3301 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003302 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003303 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003304 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003305 if (!(t->flags & SN_ERR_MASK))
3306 t->flags |= SN_ERR_CLITO;
3307 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003308 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003309 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003310 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003311 t->flags |= SN_FINST_C;
3312 else
3313 t->flags |= SN_FINST_D;
3314 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003316 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003317 }
3318
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003319 update_timeouts:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003320 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003321 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003322 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003323 /* no room to read more data */
3324 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3325 /* stop reading until we get some space */
3326 req->rex = TICK_ETERNITY;
3327 }
3328 } else {
3329 EV_FD_COND_S(t->cli_fd, DIR_RD);
3330 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3331 }
3332 }
3333
3334 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003335 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003336 /* first, we may have to produce data (eg: stats).
3337 * right now, this is limited to the SHUTR state.
3338 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003339 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003340 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003341 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003342 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003343 fd_delete(t->cli_fd);
3344 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003345 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003346 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003348 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003349
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003350 /* 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 +02003351 if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003352 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3353 /* stop writing */
3354 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003355 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003356 } else {
3357 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003358 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003359 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003360 /* restart writing */
3361 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003362 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003363 /* FIXME: to prevent the client from expiring read timeouts during writes,
3364 * we refresh it, except if it was already infinite. */
3365 req->rex = rep->wex;
3366 }
3367 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003368 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003369 }
3370 return 0; /* other cases change nothing */
3371 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003372 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3374 int len;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003375 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 +02003376 write(1, trash, len);
3377 }
3378 return 0;
3379 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003380#ifdef DEBUG_DEV
3381 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3382 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003383#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003384 return 0;
3385}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386
Willy Tarreaubaaee002006-06-26 02:48:02 +02003387
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003388/* Return 1 if the pending connection has failed and should be retried,
3389 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3390 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003391 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003392int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003393{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003394 struct buffer *req = t->req;
3395 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003396 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003397
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003398 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3399 now_ms, __FUNCTION__,
3400 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003401 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003402 req->flags, rep->flags,
3403 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003404
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003405 if ((req->flags & BF_SHUTW_NOW) ||
3406 (rep->flags & BF_SHUTW) ||
3407 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3408 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
3409 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3410
3411 trace_term(t, TT_HTTP_SRV_5);
3412 req->wex = TICK_ETERNITY;
3413 fd_delete(req->cons->fd);
3414 if (t->srv) {
3415 t->srv->cur_sess--;
3416 sess_change_server(t, NULL);
3417 }
3418 /* note that this must not return any error because it would be able to
3419 * overwrite the client_retnclose() output.
3420 */
3421 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3422
3423 // FIXME: should we set rep->MAY_FORWARD ?
3424 buffer_shutw(req);
3425 buffer_shutr(rep);
3426 req->cons->state = SI_ST_CLO;
3427 if (!req->cons->err_type)
3428 req->cons->err_type = SI_ET_CONN_ABRT;
3429 req->cons->err_loc = t->srv;
3430 return 0;
3431 }
3432
3433 /* check for timeouts and asynchronous connect errors */
3434 if (fdtab[req->cons->fd].state == FD_STERROR) {
3435 conn_err = SI_ET_CONN_ERR;
3436 if (!req->cons->err_type)
3437 req->cons->err_type = SI_ET_CONN_ERR;
3438 }
3439 else if (!(req->flags & BF_WRITE_STATUS)) {
3440 /* nothing happened, maybe we timed out */
3441 if (tick_is_expired(req->wex, now_ms)) {
3442 conn_err = SI_ET_CONN_TO;
3443 if (!req->cons->err_type)
3444 req->cons->err_type = SI_ET_CONN_TO;
3445 }
3446 else
3447 return 0; /* let's wait a bit more */
3448 }
3449
3450 if (conn_err) {
3451 fd_delete(req->cons->fd);
3452 req->cons->state = SI_ST_CLO;
3453
3454 if (t->srv) {
3455 t->srv->cur_sess--;
3456 sess_change_server(t, NULL);
3457 req->cons->err_loc = t->srv;
3458 }
3459
3460 /* ensure that we have enough retries left */
3461 if (srv_count_retry_down(t, conn_err))
3462 return 0;
3463
3464 if (conn_err == SI_ET_CONN_ERR) {
3465 /* we encountered an immediate connection error, and we
3466 * will have to retry connecting to the same server, most
3467 * likely leading to the same result. To avoid this, we
3468 * fake a connection timeout to retry after a turn-around
3469 * time of 1 second. We will wait in the previous if block.
3470 */
3471 req->cons->state = SI_ST_TAR;
3472 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3473 return 0;
3474 }
3475
3476 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3477 /* We're on our last chance, and the REDISP option was specified.
3478 * We will ignore cookie and force to balance or use the dispatcher.
3479 */
3480 /* let's try to offer this slot to anybody */
3481 if (may_dequeue_tasks(t->srv, t->be))
3482 process_srv_queue(t->srv);
3483
3484 /* it's left to the dispatcher to choose a server */
3485 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3486 t->prev_srv = t->srv;
3487 } else {
3488 /* we just want to retry */
3489 if (t->srv)
3490 t->srv->retries++;
3491 t->be->retries++;
3492
3493 /* Now we will try to either reconnect to the same server or
3494 * connect to another server. If the connection gets queued
3495 * because all servers are saturated, then we will go back to
3496 * the idle state where the buffer's consumer is marked as
3497 * unknown.
3498 */
3499 if (srv_retryable_connect(t)) {
3500 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003501 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003502 return 0;
3503 }
3504 }
3505
3506 /* We'll rely on the caller to try to get a connection again */
3507 return 1;
3508 }
3509 else {
3510 /* no error and write OK : connection succeeded */
3511 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3512 req->cons->state = SI_ST_EST;
3513 req->cons->err_type = SI_ET_NONE;
3514 req->cons->err_loc = NULL;
3515
3516 if (req->flags & BF_EMPTY) {
3517 EV_FD_CLR(req->cons->fd, DIR_WR);
3518 req->wex = TICK_ETERNITY;
3519 } else {
3520 EV_FD_SET(req->cons->fd, DIR_WR);
3521 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3522 if (tick_isset(req->wex)) {
3523 /* FIXME: to prevent the server from expiring read timeouts during writes,
3524 * we refresh it. */
3525 rep->rex = req->wex;
3526 }
3527 }
3528
3529 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3530 if (!(rep->flags & BF_HIJACK)) {
3531 EV_FD_SET(req->cons->fd, DIR_RD);
3532 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3533 }
3534 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3535
3536 /* if the user wants to log as soon as possible, without counting
3537 bytes from the server, then this is the right moment. */
3538 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3539 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3540 tcp_sess_log(t);
3541 }
3542#ifdef CONFIG_HAP_TCPSPLICE
3543 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3544 /* TCP splicing supported by both FE and BE */
3545 tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
3546 }
3547#endif
3548 }
3549 else {
3550 rep->analysers |= AN_RTR_HTTP_HDR;
3551 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3552 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3553 /* reset hdr_idx which was already initialized by the request.
3554 * right now, the http parser does it.
3555 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003556 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003557 }
3558
3559 if (!rep->analysers)
3560 t->rep->flags |= BF_MAY_FORWARD;
3561 req->wex = TICK_ETERNITY;
3562 return 0;
3563 }
3564}
3565
3566
3567/*
3568 * This function tries to assign a server to a stream_sock interface.
3569 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3570 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3571 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3572 * it returns SI_ST_ASS, otherwise zero.
3573 */
3574int stream_sock_assign_server(struct session *t)
3575{
3576 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3577 now_ms, __FUNCTION__,
3578 cli_stnames[t->cli_state],
3579 t->rep->rex, t->req->wex,
3580 t->req->flags, t->rep->flags,
3581 t->req->l, t->rep->l);
3582
3583 if (t->req->cons->state == SI_ST_TAR) {
3584 /* connection might be aborted */
3585 if ((t->req->flags & BF_SHUTW_NOW) ||
3586 (t->rep->flags & BF_SHUTW) ||
3587 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3588 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003589
Willy Tarreauf8533202008-08-16 14:55:08 +02003590 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003591 t->req->wex = TICK_ETERNITY;
3592
3593 // FIXME: should we set rep->MAY_FORWARD ?
3594 buffer_shutr(t->rep);
3595 buffer_shutw(t->req);
3596 if (!t->req->cons->err_type)
3597 t->req->cons->err_type = SI_ET_CONN_ABRT;
3598 t->req->cons->state = SI_ST_CLO;
3599 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003600 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003601
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003602 if (!tick_is_expired(t->req->wex, now_ms))
3603 return 0; /* still in turn-around */
3604
3605 t->req->cons->state = SI_ST_INI;
3606 }
3607 else if (t->req->cons->state == SI_ST_QUE) {
3608 if (t->pend_pos) {
3609 /* request still in queue... */
3610 if (tick_is_expired(t->req->wex, now_ms)) {
3611 /* ... and timeout expired */
3612 trace_term(t, TT_HTTP_SRV_3);
3613 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003614 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003615 if (t->srv)
3616 t->srv->failed_conns++;
3617 t->be->failed_conns++;
3618
3619 // FIXME: should we set rep->MAY_FORWARD ?
3620 buffer_shutr(t->rep);
3621 buffer_shutw(t->req);
3622 t->req->flags |= BF_WRITE_TIMEOUT;
3623 if (!t->req->cons->err_type)
3624 t->req->cons->err_type = SI_ET_QUEUE_TO;
3625 t->req->cons->state = SI_ST_CLO;
3626 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003627 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003628 /* connection remains in queue, check if we have to abort it */
3629 if ((t->req->flags & BF_SHUTW_NOW) ||
3630 (t->rep->flags & BF_SHUTW) ||
3631 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3632 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3633 /* give up */
3634 trace_term(t, TT_HTTP_SRV_1);
3635 t->req->wex = TICK_ETERNITY;
3636 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003637
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003638 // FIXME: should we set rep->MAY_FORWARD ?
3639 buffer_shutr(t->rep);
3640 buffer_shutw(t->req);
3641 if (!t->req->cons->err_type)
3642 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3643 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003644 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003645 return 0;
3646 }
3647 /* The connection is not in the queue anymore */
3648 t->req->cons->state = SI_ST_INI;
3649 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003650
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003651 /* we may get here from above */
3652 if (t->req->cons->state == SI_ST_INI) {
3653 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003654
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003655 /* first, check if the connection has been aborted */
3656 if ((t->req->flags & BF_SHUTW_NOW) ||
3657 (t->rep->flags & BF_SHUTW) ||
3658 ((t->req->flags & BF_SHUTR) &&
3659 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003660
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003661 trace_term(t, TT_HTTP_SRV_1);
3662 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003663
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003664 // FIXME: should we set rep->MAY_FORWARD ?
3665 buffer_shutr(t->rep);
3666 buffer_shutw(t->req);
3667 if (!t->req->cons->err_type)
3668 t->req->cons->err_type = SI_ET_CONN_ABRT;
3669 t->req->cons->state = SI_ST_CLO;
3670 return 0;
3671 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003672
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003673 /* try to get a server assigned */
3674 if (srv_redispatch_connect(t) != 0) {
3675 /* we did not get any server, let's check the cause */
3676 if (t->req->cons->state == SI_ST_QUE) {
3677 /* the connection was queued, that's OK */
3678 return 0;
3679 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003680
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003681 trace_term(t, TT_HTTP_SRV_2);
3682 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003683
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003684 // FIXME: should we set rep->MAY_FORWARD ?
3685 buffer_shutr(t->rep);
3686 buffer_shutw(t->req);
3687 t->req->flags |= BF_WRITE_ERROR;
3688 if (!t->req->cons->err_type)
3689 t->req->cons->err_type = SI_ET_CONN_OTHER;
3690 t->req->cons->state = SI_ST_CLO;
3691 return 0;
3692 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003693
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003694 t->req->cons->state = SI_ST_ASS;
3695 /* Once the server is assigned, we have to return because
3696 * the caller might be interested in checking several
3697 * things before connecting.
3698 */
3699 return 1;
3700 }
3701 return 0;
3702}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003703
Willy Tarreauf8533202008-08-16 14:55:08 +02003704
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003705/*
3706 * This function tries to establish a connection to an assigned server. It also
3707 * performs connection retries. It may only be called with t->req->cons->state
3708 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3709 * SI_ST_EST, or SI_ST_CLO.
3710 */
3711int stream_sock_connect_server(struct session *t)
3712{
3713 if (t->req->cons->state == SI_ST_ASS) {
3714 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003715
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003716 if (!srv_retryable_connect(t)) {
3717 /* we need to redispatch */
3718 t->req->cons->state = SI_ST_INI;
3719 return 0;
3720 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003721
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003722 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3723 if (t->req->cons->state != SI_ST_CON) {
3724 /* it was an error */
3725 trace_term(t, TT_HTTP_SRV_4);
3726 t->req->wex = TICK_ETERNITY;
3727
3728 // FIXME: should we set rep->MAY_FORWARD ?
3729 buffer_shutr(t->rep);
3730 buffer_shutw(t->req);
3731 t->req->flags |= BF_WRITE_ERROR;
3732 if (!t->req->cons->err_type)
3733 t->req->cons->err_type = SI_ET_CONN_OTHER;
3734 t->req->cons->state = SI_ST_CLO;
3735 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003736 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003737 /* We have a socket and switched to SI_ST_CON */
3738 }
3739
3740 /* we may also get here from above */
3741 if (t->req->cons->state == SI_ST_CON) {
3742 /* connection in progress or just completed */
3743 if (!tcp_connection_status(t))
3744 return 0;
3745 }
3746 return 0;
3747}
3748
3749
3750/*
3751 * Tries to establish a connection to the server and associate it to the
3752 * request buffer's consumer side. It is assumed that this function will not be
3753 * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally
3754 * returns zero, but may return 1 if it absolutely wants to be called again.
3755 */
3756int process_srv_conn(struct session *t)
3757{
3758 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3759 now_ms, __FUNCTION__,
3760 cli_stnames[t->cli_state],
3761 t->rep->rex, t->req->wex,
3762 t->req->flags, t->rep->flags,
3763 t->req->l, t->rep->l);
3764
3765 do {
3766 if (t->req->cons->state == SI_ST_INI ||
3767 t->req->cons->state == SI_ST_TAR ||
3768 t->req->cons->state == SI_ST_QUE) {
3769 /* try to assign a server */
3770 if (!stream_sock_assign_server(t))
3771 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003772 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003773
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003774 if (t->req->cons->state == SI_ST_ASS &&
3775 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3776 /* Server supporting redirection and it is possible.
3777 * Invalid requests are reported as such. It concerns all
3778 * the largest ones.
3779 */
3780 struct http_txn *txn = &t->txn;
3781 struct chunk rdr;
3782 char *path;
3783 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003784
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003785 /* 1: create the response header */
3786 rdr.len = strlen(HTTP_302);
3787 rdr.str = trash;
3788 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003789
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003790 /* 2: add the server's prefix */
3791 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3792 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003793
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003794 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3795 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003796
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003797 /* 3: add the request URI */
3798 path = http_get_path(txn);
3799 if (!path)
3800 goto cancel_redir;
3801 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3802 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3803 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003804
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003805 memcpy(rdr.str + rdr.len, path, len);
3806 rdr.len += len;
3807 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3808 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003809
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003810 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3811 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003812
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003813 /* FIXME: we should increase a counter of redirects per server and per backend. */
3814 if (t->srv)
3815 t->srv->cum_sess++;
3816
3817 t->req->cons->state = SI_ST_CLO;
3818 return 0;
3819 cancel_redir:
3820 //txn->status = 400;
3821 //t->fe->failed_req++;
3822 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3823 // 400, error_message(t, HTTP_ERR_400));
3824 trace_term(t, TT_HTTP_SRV_4);
3825
3826 // FIXME: should we set rep->MAY_FORWARD ?
3827 buffer_shutw(t->req);
3828 buffer_shutr(t->rep);
3829 if (!t->req->cons->err_type)
3830 t->req->cons->err_type = SI_ET_CONN_OTHER;
3831 t->req->cons->state = SI_ST_CLO;
3832 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003833 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003834
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003835 if (t->req->cons->state == SI_ST_CON ||
3836 t->req->cons->state == SI_ST_ASS) {
3837 stream_sock_connect_server(t);
3838 }
3839 } while (t->req->cons->state != SI_ST_CLO &&
3840 t->req->cons->state != SI_ST_CON &&
3841 t->req->cons->state != SI_ST_EST);
3842 return 0;
3843}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003844
Willy Tarreaubaaee002006-06-26 02:48:02 +02003845
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003846/*
3847 * Manages the server FSM and its socket during the DATA phase. It must not be
3848 * called when a file descriptor is not attached to the buffer. It must only be
3849 * called during SI_ST_EST. It normally returns zero, but may return 1 if it
3850 * absolutely wants to be called again.
3851 */
3852int process_srv_data(struct session *t)
3853{
3854 struct buffer *req = t->req;
3855 struct buffer *rep = t->rep;
3856 int fd = req->cons->fd;
Willy Tarreaudafde432008-08-17 01:00:46 +02003857
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003858 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3859 now_ms, __FUNCTION__,
3860 cli_stnames[t->cli_state],
3861 rep->rex, req->wex,
3862 req->flags, rep->flags,
3863 req->l, rep->l);
3864
3865 if (req->flags & (BF_WRITE_ERROR | BF_WRITE_TIMEOUT) ||
3866 rep->flags & (BF_READ_ERROR | BF_READ_TIMEOUT)) {
3867 /* nothing more to be done here */
3868 fprintf(stderr, "Hey what are you doing there? t=%p fd=%d state=%d\n",
3869 t, t->req->cons->fd, t->req->cons->state);
3870 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003871 }
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003872
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003873 /* we can skip most of the tests at once if some conditions are not met */
3874 /* FIXME: place req->BF_SHUTW_NOW here */
3875 //if (!((fdtab[fd].state == FD_STERROR) ||
3876 // (!(req->flags & BF_SHUTW) &&
3877 // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
3878 // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3879 // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW))))
3880 // goto update_timeouts;
3881
3882 /* read or write error */
3883 /* FIXME: what happens when we have to deal with HTTP ??? */
3884 if (fdtab[fd].state == FD_STERROR) {
3885 trace_term(t, TT_HTTP_SRV_6);
3886 buffer_shutw(req);
3887 req->flags |= BF_WRITE_ERROR;
3888 buffer_shutr(rep);
3889 rep->flags |= BF_READ_ERROR;
3890 fd_delete(fd);
3891 req->cons->state = SI_ST_CLO;
3892 if (t->srv) {
3893 t->srv->cur_sess--;
3894 //t->srv->failed_resp++;
3895 //FIXME: si on ne traite pas l'erreur ici, le serveur est perdu et on ne la comptabilisera plus ensuite.
3896 //il va donc falloir stocker l'info du dernier serveur en erreur pour que les couches du dessus traitent.
3897 sess_change_server(t, NULL);
3898 }
3899 //t->be->failed_resp++;
3900 //if (!rep->analysers) {
3901 // if (!(t->flags & SN_ERR_MASK))
3902 // t->flags |= SN_ERR_SRVCL;
3903 // if (!(t->flags & SN_FINST_MASK))
3904 // t->flags |= SN_FINST_D;
3905 //}
3906 if (may_dequeue_tasks(t->srv, t->be))
3907 process_srv_queue(t->srv);
3908
3909 return 0;
3910 }
3911
3912 /* last read, or end of client write */
3913 if (!(rep->flags & BF_SHUTR) && /* not already done */
3914 rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3915 buffer_shutr(rep);
3916 if (!(req->flags & BF_SHUTW)) {
3917 EV_FD_CLR(fd, DIR_RD);
3918 trace_term(t, TT_HTTP_SRV_7);
3919 } else {
3920 /* output was already closed */
3921 trace_term(t, TT_HTTP_SRV_8);
3922 fd_delete(fd);
3923 req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003924 if (t->srv) {
3925 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003926 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003927 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003928
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003929 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003930 process_srv_queue(t->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003931 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003932 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003933 }
3934 /* end of client read and no more data to send. We can forward
3935 * the close when we're allowed to forward data (anytime right
3936 * now). If we're using option forceclose, then we may also
3937 * shutdown the outgoing write channel once the response starts
3938 * coming from the server.
3939 */
Willy Tarreau461f6622008-08-15 23:43:19 +02003940
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003941 // FIXME: option FORCE_CLOSE should move to upper layer.
3942 if (!(req->flags & BF_SHUTW) && /* not already done */
3943 (req->flags & BF_SHUTW_NOW ||
3944 (req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
3945 (req->flags & BF_SHUTR ||
3946 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))))) {
3947 buffer_shutw(req);
3948 if (!(rep->flags & BF_SHUTR)) {
3949 trace_term(t, TT_HTTP_SRV_9);
3950 EV_FD_CLR(fd, DIR_WR);
3951 shutdown(fd, SHUT_WR);
3952 /* We must ensure that the read part is still alive when switching to shutw */
3953 /* FIXME: is this still true ? */
3954 EV_FD_SET(fd, DIR_RD);
3955 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3956 } else {
3957 trace_term(t, TT_HTTP_SRV_10);
3958 fd_delete(fd);
3959 req->cons->state = SI_ST_CLO;
3960 if (t->srv) {
3961 t->srv->cur_sess--;
3962 sess_change_server(t, NULL);
Willy Tarreau461f6622008-08-15 23:43:19 +02003963 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003964
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003965 if (may_dequeue_tasks(t->srv, t->be))
3966 process_srv_queue(t->srv);
3967 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003968 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003969 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003970
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003971 /* read timeout */
3972 if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == 0 &&
3973 tick_is_expired(rep->rex, now_ms)) {
3974 rep->flags |= BF_READ_TIMEOUT;
3975 //if (!rep->analysers) {
3976 // if (!(t->flags & SN_ERR_MASK))
3977 // t->flags |= SN_ERR_SRVTO;
3978 // if (!(t->flags & SN_FINST_MASK))
3979 // t->flags |= SN_FINST_D;
3980 //}
3981 buffer_shutr(rep);
3982 if (!(req->flags & BF_SHUTW)) {
3983 trace_term(t, TT_HTTP_SRV_11);
3984 EV_FD_CLR(fd, DIR_RD);
3985 } else {
3986 trace_term(t, TT_HTTP_SRV_12);
3987 fd_delete(fd);
3988 req->cons->state = SI_ST_CLO;
3989 if (t->srv) {
3990 t->srv->cur_sess--;
3991 sess_change_server(t, NULL);
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003992 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003993
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003994 if (may_dequeue_tasks(t->srv, t->be))
3995 process_srv_queue(t->srv);
3996 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003997 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003998 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003999
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004000 /* write timeout */
4001 if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == 0 &&
4002 tick_is_expired(req->wex, now_ms)) {
4003 req->flags |= BF_WRITE_TIMEOUT;
4004 //if (!rep->analysers) {
4005 // if (!(t->flags & SN_ERR_MASK))
4006 // t->flags |= SN_ERR_SRVTO;
4007 // if (!(t->flags & SN_FINST_MASK))
4008 // t->flags |= SN_FINST_D;
4009 //}
4010 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02004011 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004012 trace_term(t, TT_HTTP_SRV_13);
4013 EV_FD_CLR(fd, DIR_WR);
4014 shutdown(fd, SHUT_WR);
4015 /* We must ensure that the read part is still alive when switching to shutw */
4016 /* FIXME: is this still needed ? */
4017 EV_FD_SET(fd, DIR_RD);
4018 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4019 } else {
4020 trace_term(t, TT_HTTP_SRV_14);
4021 fd_delete(fd);
4022 req->cons->state = SI_ST_CLO;
4023 if (t->srv) {
4024 t->srv->cur_sess--;
4025 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01004026 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004027
4028 if (may_dequeue_tasks(t->srv, t->be))
4029 process_srv_queue(t->srv);
4030 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004031 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004032 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004033
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004034 update_timeouts:
4035 /* manage read timeout */
4036 if (!(rep->flags & BF_SHUTR)) {
4037 if (rep->flags & (BF_FULL|BF_HIJACK)) {
4038 if (EV_FD_COND_C(fd, DIR_RD))
4039 rep->rex = TICK_ETERNITY;
4040 } else {
4041 EV_FD_COND_S(fd, DIR_RD);
4042 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004043 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004044 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004045
4046 /* manage write timeout */
4047 if (!(req->flags & BF_SHUTW)) {
4048 if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
4049 /* stop writing */
4050 if (EV_FD_COND_C(fd, DIR_WR))
4051 req->wex = TICK_ETERNITY;
4052 } else {
4053 /* buffer not empty, there are still data to be transferred */
4054 EV_FD_COND_S(fd, DIR_WR);
4055 if (!tick_isset(req->wex)) {
4056 /* restart writing */
4057 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4058 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4059 /* FIXME: to prevent the server from expiring read timeouts during writes,
4060 * we refresh it, except if it was already infinite.
4061 */
4062 rep->rex = req->wex;
4063 }
4064 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004065 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004066 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004067 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02004068}
4069
4070
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004071///*
4072// * Manages the client FSM and its socket. It normally returns zero, but may
4073// * return 1 if it absolutely wants to be called again.
4074// *
4075// * Note: process_cli is the ONLY function allowed to set cli_state to anything
4076// * but CL_STCLOSE.
4077// */
4078//int process_cli(struct session *t)
4079//{
4080// struct buffer *req = t->req;
4081// struct buffer *rep = t->rep;
4082//
4083// 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",
4084// now_ms, __FUNCTION__,
4085// cli_stnames[t->cli_state],
4086// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
4087// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
4088// req->rex, rep->wex,
4089// req->flags, rep->flags,
4090// req->l, rep->l);
4091//
4092// update_state:
4093// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
4094// * still set this state (and will do until unix sockets are converted).
4095// */
4096// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
4097// /* we can skip most of the tests at once if some conditions are not met */
4098// if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4099// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4100// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
4101// (!(rep->flags & BF_SHUTW) &&
4102// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
4103// goto update_timeouts;
4104//
4105// /* read or write error */
4106// if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
4107// buffer_shutr(req);
4108// buffer_shutw(rep);
4109// fd_delete(t->cli_fd);
4110// t->cli_state = CL_STCLOSE;
4111// trace_term(t, TT_HTTP_CLI_1);
4112// if (!req->analysers) {
4113// if (!(t->flags & SN_ERR_MASK))
4114// t->flags |= SN_ERR_CLICL;
4115// if (!(t->flags & SN_FINST_MASK)) {
4116// if (t->pend_pos)
4117// t->flags |= SN_FINST_Q;
4118// else if (!(req->flags & BF_CONNECTED))
4119// t->flags |= SN_FINST_C;
4120// else
4121// t->flags |= SN_FINST_D;
4122// }
4123// }
4124// goto update_state;
4125// }
4126// /* last read, or end of server write */
4127// else if (!(req->flags & BF_SHUTR) && /* not already done */
4128// req->flags & (BF_READ_NULL | BF_SHUTW)) {
4129// buffer_shutr(req);
4130// if (!(rep->flags & BF_SHUTW)) {
4131// EV_FD_CLR(t->cli_fd, DIR_RD);
4132// trace_term(t, TT_HTTP_CLI_2);
4133// } else {
4134// /* output was already closed */
4135// fd_delete(t->cli_fd);
4136// t->cli_state = CL_STCLOSE;
4137// trace_term(t, TT_HTTP_CLI_3);
4138// }
4139// goto update_state;
4140// }
4141// /* last server read and buffer empty : we only check them when we're
4142// * allowed to forward the data.
4143// */
4144// else if (!(rep->flags & BF_SHUTW) && /* not already done */
4145// rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
4146// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
4147// buffer_shutw(rep);
4148// if (!(req->flags & BF_SHUTR)) {
4149// EV_FD_CLR(t->cli_fd, DIR_WR);
4150// shutdown(t->cli_fd, SHUT_WR);
4151// /* We must ensure that the read part is still alive when switching to shutw */
4152// /* FIXME: is this still true ? */
4153// EV_FD_SET(t->cli_fd, DIR_RD);
4154// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4155// trace_term(t, TT_HTTP_CLI_4);
4156// } else {
4157// fd_delete(t->cli_fd);
4158// t->cli_state = CL_STCLOSE;
4159// trace_term(t, TT_HTTP_CLI_5);
4160// }
4161// goto update_state;
4162// }
4163// /* read timeout */
4164// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4165// buffer_shutr(req);
4166// if (!(rep->flags & BF_SHUTW)) {
4167// EV_FD_CLR(t->cli_fd, DIR_RD);
4168// trace_term(t, TT_HTTP_CLI_6);
4169// } else {
4170// /* output was already closed */
4171// fd_delete(t->cli_fd);
4172// t->cli_state = CL_STCLOSE;
4173// trace_term(t, TT_HTTP_CLI_7);
4174// }
4175// if (!req->analysers) {
4176// if (!(t->flags & SN_ERR_MASK))
4177// t->flags |= SN_ERR_CLITO;
4178// if (!(t->flags & SN_FINST_MASK)) {
4179// if (t->pend_pos)
4180// t->flags |= SN_FINST_Q;
4181// else if (!(req->flags & BF_CONNECTED))
4182// t->flags |= SN_FINST_C;
4183// else
4184// t->flags |= SN_FINST_D;
4185// }
4186// }
4187// goto update_state;
4188// }
4189// /* write timeout */
4190// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4191// buffer_shutw(rep);
4192// if (!(req->flags & BF_SHUTR)) {
4193// EV_FD_CLR(t->cli_fd, DIR_WR);
4194// shutdown(t->cli_fd, SHUT_WR);
4195// /* We must ensure that the read part is still alive when switching to shutw */
4196// /* FIXME: is this still true ? */
4197// EV_FD_SET(t->cli_fd, DIR_RD);
4198// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4199// trace_term(t, TT_HTTP_CLI_8);
4200// } else {
4201// fd_delete(t->cli_fd);
4202// t->cli_state = CL_STCLOSE;
4203// trace_term(t, TT_HTTP_CLI_9);
4204// }
4205// if (!req->analysers) {
4206// if (!(t->flags & SN_ERR_MASK))
4207// t->flags |= SN_ERR_CLITO;
4208// if (!(t->flags & SN_FINST_MASK)) {
4209// if (t->pend_pos)
4210// t->flags |= SN_FINST_Q;
4211// else if (!(req->flags & BF_CONNECTED))
4212// t->flags |= SN_FINST_C;
4213// else
4214// t->flags |= SN_FINST_D;
4215// }
4216// }
4217// goto update_state;
4218// }
4219//
4220// update_timeouts:
4221// /* manage read timeout */
4222// if (!(req->flags & BF_SHUTR)) {
4223// if (req->flags & BF_FULL) {
4224// /* no room to read more data */
4225// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
4226// /* stop reading until we get some space */
4227// req->rex = TICK_ETERNITY;
4228// }
4229// } else {
4230// EV_FD_COND_S(t->cli_fd, DIR_RD);
4231// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4232// }
4233// }
4234//
4235// /* manage write timeout */
4236// if (!(rep->flags & BF_SHUTW)) {
4237// /* first, we may have to produce data (eg: stats).
4238// * right now, this is limited to the SHUTR state.
4239// */
4240// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
4241// produce_content(t);
4242// if (rep->flags & BF_EMPTY) {
4243// buffer_shutw(rep);
4244// fd_delete(t->cli_fd);
4245// t->cli_state = CL_STCLOSE;
4246// trace_term(t, TT_HTTP_CLI_10);
4247// goto update_state;
4248// }
4249// }
4250//
4251// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
4252// if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
4253// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
4254// /* stop writing */
4255// rep->wex = TICK_ETERNITY;
4256// }
4257// } else {
4258// /* buffer not empty */
4259// EV_FD_COND_S(t->cli_fd, DIR_WR);
4260// if (!tick_isset(rep->wex)) {
4261// /* restart writing */
4262// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
4263// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
4264// /* FIXME: to prevent the client from expiring read timeouts during writes,
4265// * we refresh it, except if it was already infinite. */
4266// req->rex = rep->wex;
4267// }
4268// }
4269// }
4270// }
4271// return 0; /* other cases change nothing */
4272// }
4273// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
4274// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
4275// int len;
4276// 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);
4277// write(1, trash, len);
4278// }
4279// return 0;
4280// }
4281//#ifdef DEBUG_DEV
4282// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
4283// ABORT_NOW();
4284//#endif
4285// return 0;
4286//}
4287//
4288//
4289///* Return 1 if we could get a new connection for session t, otherwise zero */
4290//int tcp_get_connection(struct session *t)
4291//{
4292// struct http_txn *txn = &t->txn;
4293// struct buffer *req = t->req;
4294// struct buffer *rep = t->rep;
4295//
4296// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4297// now_ms, __FUNCTION__,
4298// cli_stnames[t->cli_state],
4299// rep->rex, req->wex,
4300// req->flags, rep->flags,
4301// req->l, rep->l);
4302//
4303//
4304// if ((rep->flags & BF_SHUTW) ||
4305// ((req->flags & BF_SHUTR) &&
4306// (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4307// req->wex = TICK_ETERNITY;
4308// if (t->pend_pos)
4309// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4310// /* note that this must not return any error because it would be able to
4311// * overwrite the client_retnclose() output.
4312// */
4313// if (txn->flags & TX_CLTARPIT)
4314// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
4315// else
4316// srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
4317//
4318// trace_term(t, TT_HTTP_SRV_1);
4319// return 0;
4320// }
4321//
4322// /* stop here if we're not allowed to connect */
4323// if (!(req->flags & BF_MAY_FORWARD))
4324// return 0;
4325//
4326// /* the client allows the server to connect */
4327// if (txn->flags & TX_CLTARPIT) {
4328// /* This connection is being tarpitted. The CLIENT side has
4329// * already set the connect expiration date to the right
4330// * timeout. We just have to check that it has not expired.
4331// */
4332// if (!(req->flags & BF_WRITE_TIMEOUT))
4333// return 0;
4334//
4335// /* We will set the queue timer to the time spent, just for
4336// * logging purposes. We fake a 500 server error, so that the
4337// * attacker will not suspect his connection has been tarpitted.
4338// * It will not cause trouble to the logs because we can exclude
4339// * the tarpitted connections by filtering on the 'PT' status flags.
4340// */
4341// req->wex = TICK_ETERNITY;
4342// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4343// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
4344// 500, error_message(t, HTTP_ERR_500));
4345// trace_term(t, TT_HTTP_SRV_2);
4346// return 0;
4347// }
4348//
4349// /* Right now, we will need to create a connection to the server.
4350// * We might already have tried, and got a connection pending, in
4351// * which case we will not do anything till it's pending. It's up
4352// * to any other session to release it and wake us up again.
4353// */
4354// if (t->pend_pos) {
4355// if (!(req->flags & BF_WRITE_TIMEOUT)) {
4356// return 0;
4357// } else {
4358// /* we've been waiting too long here */
4359// req->wex = TICK_ETERNITY;
4360// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4361// srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
4362// 503, error_message(t, HTTP_ERR_503));
4363// trace_term(t, TT_HTTP_SRV_3);
4364// if (t->srv)
4365// t->srv->failed_conns++;
4366// t->be->failed_conns++;
4367// return 0;
4368// }
4369// }
4370//
4371// do {
4372// if (srv_redispatch_connect(t) != 0)
4373// return 0;
4374//
4375// if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
4376// /* Server supporting redirection and it is possible.
4377// * Invalid requests are reported as such. It concerns all
4378// * the largest ones.
4379// */
4380// struct chunk rdr;
4381// char *path;
4382// int len;
4383//
4384// /* 1: create the response header */
4385// rdr.len = strlen(HTTP_302);
4386// rdr.str = trash;
4387// memcpy(rdr.str, HTTP_302, rdr.len);
4388//
4389// /* 2: add the server's prefix */
4390// if (rdr.len + t->srv->rdr_len > sizeof(trash))
4391// goto cancel_redir;
4392//
4393// memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
4394// rdr.len += t->srv->rdr_len;
4395//
4396// /* 3: add the request URI */
4397// path = http_get_path(txn);
4398// if (!path)
4399// goto cancel_redir;
4400// len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
4401// if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
4402// goto cancel_redir;
4403//
4404// memcpy(rdr.str + rdr.len, path, len);
4405// rdr.len += len;
4406// memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
4407// rdr.len += 4;
4408//
4409// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
4410// trace_term(t, TT_HTTP_SRV_3);
4411//
4412// /* FIXME: we should increase a counter of redirects per server and per backend. */
4413// if (t->srv)
4414// t->srv->cum_sess++;
4415// return 0;
4416// cancel_redir:
4417// txn->status = 400;
4418// t->fe->failed_req++;
4419// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
4420// 400, error_message(t, HTTP_ERR_400));
4421// trace_term(t, TT_HTTP_SRV_4);
4422// return 0;
4423// }
4424//
4425// /* try to (re-)connect to the server, and fail if we expire the
4426// * number of retries.
4427// */
4428// if (srv_retryable_connect(t)) {
4429// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4430// if (!(req->cons.flags & BC_KNOWN))
4431// return 0;
4432// /* We got an FD */
4433// return 1;
4434// }
4435// } while (1);
4436//}
4437//
4438//
4439///* Return 1 if the pending connection has failed and should be retried,
4440// * otherwise zero.
4441// */
4442//int tcp_connection_failed(struct session *t)
4443//{
4444// struct buffer *req = t->req;
4445// struct buffer *rep = t->rep;
4446// int conn_err;
4447//
4448// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4449// now_ms, __FUNCTION__,
4450// cli_stnames[t->cli_state],
4451// rep->rex, req->wex,
4452// req->flags, rep->flags,
4453// req->l, rep->l);
4454//
4455// if ((rep->flags & BF_SHUTW) ||
4456// ((req->flags & BF_SHUTR) &&
4457// ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
4458// t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4459// req->wex = TICK_ETERNITY;
4460// if (!(t->flags & SN_CONN_TAR)) {
4461// /* if we are in turn-around, we have already closed the FD */
4462// fd_delete(req->cons->fd);
4463// req->cons->state = SI_ST_CLO;
4464// if (t->srv) {
4465// t->srv->cur_sess--;
4466// sess_change_server(t, NULL);
4467// }
4468// }
4469//
4470// /* note that this must not return any error because it would be able to
4471// * overwrite the client_retnclose() output.
4472// */
4473// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
4474// trace_term(t, TT_HTTP_SRV_5);
4475// return 0;
4476// }
4477//
4478// if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT)))
4479// return 0; /* nothing changed */
4480//
4481// if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
4482// /* timeout, asynchronous connect error or first write error */
4483// if (t->flags & SN_CONN_TAR) {
4484// /* We are doing a turn-around waiting for a new connection attempt. */
4485// if (!(req->flags & BF_WRITE_TIMEOUT))
4486// return 0;
4487// t->flags &= ~SN_CONN_TAR;
4488// }
4489// else {
4490// fd_delete(req->cons->fd);
4491// req->cons->state = SI_ST_CLO;
4492// if (t->srv) {
4493// t->srv->cur_sess--;
4494// sess_change_server(t, NULL);
4495// }
4496//
4497// if (!(req->flags & BF_WRITE_STATUS))
4498// conn_err = SN_ERR_SRVTO; // it was a connect timeout.
4499// else
4500// conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
4501//
4502// /* ensure that we have enough retries left */
4503// if (srv_count_retry_down(t, conn_err))
4504// return 0;
4505//
4506// if (req->flags & BF_WRITE_ERROR) {
4507// /* we encountered an immediate connection error, and we
4508// * will have to retry connecting to the same server, most
4509// * likely leading to the same result. To avoid this, we
4510// * fake a connection timeout to retry after a turn-around
4511// * time of 1 second. We will wait in the previous if block.
4512// */
4513// t->flags |= SN_CONN_TAR;
4514// req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
4515// return 0;
4516// }
4517// }
4518//
4519// if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
4520// /* We're on our last chance, and the REDISP option was specified.
4521// * We will ignore cookie and force to balance or use the dispatcher.
4522// */
4523// /* let's try to offer this slot to anybody */
4524// if (may_dequeue_tasks(t->srv, t->be))
4525// process_srv_queue(t->srv);
4526//
4527// /* it's left to the dispatcher to choose a server */
4528// t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
4529// t->prev_srv = t->srv;
4530//
4531// /* first, get a connection */
4532// if (srv_redispatch_connect(t)) {
4533// if (req->cons.flags & BC_KNOWN)
4534// return 0;
4535// /* we need to get a connection */
4536// return 1;
4537// }
4538// } else {
4539// if (t->srv)
4540// t->srv->retries++;
4541// t->be->retries++;
4542// }
4543//
4544// do {
4545// /* Now we will try to either reconnect to the same server or
4546// * connect to another server. If the connection gets queued
4547// * because all servers are saturated, then we will go back to
4548// * the idle state where the buffer's consumer is marked as
4549// * unknown.
4550// */
4551// if (srv_retryable_connect(t)) {
4552// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4553// if (req->cons.flags & BC_KNOWN)
4554// return 0;
4555// /* we did not get a connection */
4556// return 1;
4557// }
4558//
4559// /* we need to redispatch the connection to another server */
4560// if (srv_redispatch_connect(t)) {
4561// if (req->cons.flags & BC_KNOWN)
4562// return 0;
4563// /* we need to get a connection */
4564// return 1;
4565// }
4566// } while (1);
4567// }
4568// else { /* no error and write OK */
4569// t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
4570//
4571// if (req->flags & BF_EMPTY) {
4572// EV_FD_CLR(req->cons->fd, DIR_WR);
4573// req->wex = TICK_ETERNITY;
4574// } else {
4575// EV_FD_SET(req->cons->fd, DIR_WR);
4576// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4577// if (tick_isset(req->wex)) {
4578// /* FIXME: to prevent the server from expiring read timeouts during writes,
4579// * we refresh it. */
4580// rep->rex = req->wex;
4581// }
4582// }
4583//
4584// if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
4585// EV_FD_SET(req->cons->fd, DIR_RD);
4586// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4587// buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
4588//
4589// /* if the user wants to log as soon as possible, without counting
4590// bytes from the server, then this is the right moment. */
4591// if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4592// t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
4593// tcp_sess_log(t);
4594// }
4595//#ifdef CONFIG_HAP_TCPSPLICE
4596// if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
4597// /* TCP splicing supported by both FE and BE */
4598// tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
4599// }
4600//#endif
4601// }
4602// else {
4603// rep->analysers |= AN_RTR_HTTP_HDR;
4604// buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
4605// t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
4606// /* reset hdr_idx which was already initialized by the request.
4607// * right now, the http parser does it.
4608// * hdr_idx_init(&t->txn.hdr_idx);
4609// */
4610// }
4611//
4612// req->flags |= BF_CONNECTED;
4613// if (!rep->analysers)
4614// t->rep->flags |= BF_MAY_FORWARD;
4615// req->wex = TICK_ETERNITY;
4616// return 0;
4617// }
4618//}
4619//
4620//
4621///*
4622// * Tries to establish a connection to the server and associate it to the
4623// * request buffer's consumer side. It normally returns zero, but may return 1
4624// * if it absolutely wants to be called again.
4625// */
4626//int process_srv_conn(struct session *t)
4627//{
4628// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4629// now_ms, __FUNCTION__,
4630// cli_stnames[t->cli_state],
4631// t->rep->rex, t->req->wex,
4632// t->req->flags, t->rep->flags,
4633// t->req->l, t->rep->l);
4634//
4635// while (!(t->req->flags & BF_CONNECTED)) {
4636// if (!(t->req->cons.flags & BC_KNOWN)) {
4637// /* no connection in progress, get a new one */
4638// if (!tcp_get_connection(t))
4639// break;
4640// } else {
4641// /* connection in progress or just completed */
4642// if (!tcp_connection_failed(t))
4643// break;
4644// }
4645// }
4646// return 0;
4647//}
4648//
4649//
4650///*
4651// * Manages the server FSM and its socket during the DATA phase. It must not
4652// * be called when a file descriptor is not attached to the buffer. It normally
4653// * returns zero, but may return 1 if it absolutely wants to be called again.
4654// */
4655//int process_srv_data(struct session *t)
4656//{
4657// struct buffer *req = t->req;
4658// struct buffer *rep = t->rep;
4659//
4660// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4661// now_ms, __FUNCTION__,
4662// cli_stnames[t->cli_state],
4663// rep->rex, req->wex,
4664// req->flags, rep->flags,
4665// req->l, rep->l);
4666//
4667// /* we can skip most of the tests at once if some conditions are not met */
4668// if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4669// (!(req->flags & BF_SHUTW) &&
4670// (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
4671// (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4672// (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
4673// goto update_timeouts;
4674//
4675// /* read or write error */
4676// /* FIXME: what happens when we have to deal with HTTP ??? */
4677// if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
4678// buffer_shutr(rep);
4679// buffer_shutw(req);
4680// fd_delete(req->cons->fd);
4681// req->cons->state = SI_ST_CLO;
4682// if (t->srv) {
4683// t->srv->cur_sess--;
4684// t->srv->failed_resp++;
4685// sess_change_server(t, NULL);
4686// }
4687// t->be->failed_resp++;
4688// trace_term(t, TT_HTTP_SRV_6);
4689// if (!rep->analysers) {
4690// if (!(t->flags & SN_ERR_MASK))
4691// t->flags |= SN_ERR_SRVCL;
4692// if (!(t->flags & SN_FINST_MASK))
4693// t->flags |= SN_FINST_D;
4694// }
4695// if (may_dequeue_tasks(t->srv, t->be))
4696// process_srv_queue(t->srv);
4697//
4698// return 0;
4699// }
4700//
4701// /* last read, or end of client write */
4702// if (!(rep->flags & BF_SHUTR) && /* not already done */
4703// rep->flags & (BF_READ_NULL | BF_SHUTW)) {
4704// buffer_shutr(rep);
4705// if (!(req->flags & BF_SHUTW)) {
4706// EV_FD_CLR(req->cons->fd, DIR_RD);
4707// trace_term(t, TT_HTTP_SRV_7);
4708// } else {
4709// /* output was already closed */
4710// fd_delete(req->cons->fd);
4711// req->cons->state = SI_ST_CLO;
4712// if (t->srv) {
4713// t->srv->cur_sess--;
4714// sess_change_server(t, NULL);
4715// }
4716// trace_term(t, TT_HTTP_SRV_8);
4717//
4718// if (may_dequeue_tasks(t->srv, t->be))
4719// process_srv_queue(t->srv);
4720// return 0;
4721// }
4722// }
4723// /* end of client read and no more data to send. We can forward
4724// * the close when we're allowed to forward data (anytime right
4725// * now). If we're using option forceclose, then we may also
4726// * shutdown the outgoing write channel once the response starts
4727// * coming from the server.
4728// */
4729// if (!(req->flags & BF_SHUTW) && /* not already done */
4730// req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
4731// (req->flags & BF_SHUTR ||
4732// (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
4733// buffer_shutw(req);
4734// if (!(rep->flags & BF_SHUTR)) {
4735// EV_FD_CLR(req->cons->fd, DIR_WR);
4736// shutdown(req->cons->fd, SHUT_WR);
4737// trace_term(t, TT_HTTP_SRV_9);
4738// /* We must ensure that the read part is still alive when switching to shutw */
4739// /* FIXME: is this still true ? */
4740// EV_FD_SET(req->cons->fd, DIR_RD);
4741// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4742// } else {
4743// fd_delete(req->cons->fd);
4744// req->cons->state = SI_ST_CLO;
4745// if (t->srv) {
4746// t->srv->cur_sess--;
4747// sess_change_server(t, NULL);
4748// }
4749// trace_term(t, TT_HTTP_SRV_10);
4750//
4751// if (may_dequeue_tasks(t->srv, t->be))
4752// process_srv_queue(t->srv);
4753// return 0;
4754// }
4755// }
4756//
4757// /* read timeout */
4758// if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4759// if (!rep->analysers) {
4760// if (!(t->flags & SN_ERR_MASK))
4761// t->flags |= SN_ERR_SRVTO;
4762// if (!(t->flags & SN_FINST_MASK))
4763// t->flags |= SN_FINST_D;
4764// }
4765// buffer_shutr(rep);
4766// if (!(req->flags & BF_SHUTW)) {
4767// EV_FD_CLR(req->cons->fd, DIR_RD);
4768// trace_term(t, TT_HTTP_SRV_11);
4769// } else {
4770// fd_delete(req->cons->fd);
4771// req->cons->state = SI_ST_CLO;
4772// if (t->srv) {
4773// t->srv->cur_sess--;
4774// sess_change_server(t, NULL);
4775// }
4776// trace_term(t, TT_HTTP_SRV_12);
4777//
4778// if (may_dequeue_tasks(t->srv, t->be))
4779// process_srv_queue(t->srv);
4780// return 0;
4781// }
4782// }
4783//
4784// /* write timeout */
4785// if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4786// if (!rep->analysers) {
4787// if (!(t->flags & SN_ERR_MASK))
4788// t->flags |= SN_ERR_SRVTO;
4789// if (!(t->flags & SN_FINST_MASK))
4790// t->flags |= SN_FINST_D;
4791// }
4792// buffer_shutw(req);
4793// if (!(rep->flags & BF_SHUTR)) {
4794// EV_FD_CLR(req->cons->fd, DIR_WR);
4795// shutdown(req->cons->fd, SHUT_WR);
4796// /* We must ensure that the read part is still alive when switching to shutw */
4797// /* FIXME: is this still needed ? */
4798// EV_FD_SET(req->cons->fd, DIR_RD);
4799// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4800// trace_term(t, TT_HTTP_SRV_13);
4801// } else {
4802// fd_delete(req->cons->fd);
4803// req->cons->state = SI_ST_CLO;
4804// if (t->srv) {
4805// t->srv->cur_sess--;
4806// sess_change_server(t, NULL);
4807// }
4808// trace_term(t, TT_HTTP_SRV_14);
4809//
4810// if (may_dequeue_tasks(t->srv, t->be))
4811// process_srv_queue(t->srv);
4812// return 0;
4813// }
4814// }
4815//
4816// update_timeouts:
4817// /* manage read timeout */
4818// if (!(rep->flags & BF_SHUTR)) {
4819// if (rep->flags & BF_FULL) {
4820// if (EV_FD_COND_C(req->cons->fd, DIR_RD))
4821// rep->rex = TICK_ETERNITY;
4822// } else {
4823// EV_FD_COND_S(req->cons->fd, DIR_RD);
4824// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4825// }
4826// }
4827//
4828// /* manage write timeout */
4829// if (!(req->flags & BF_SHUTW)) {
4830// if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
4831// /* stop writing */
4832// if (EV_FD_COND_C(req->cons->fd, DIR_WR))
4833// req->wex = TICK_ETERNITY;
4834// } else {
4835// /* buffer not empty, there are still data to be transferred */
4836// EV_FD_COND_S(req->cons->fd, DIR_WR);
4837// if (!tick_isset(req->wex)) {
4838// /* restart writing */
4839// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4840// if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4841// /* FIXME: to prevent the server from expiring read timeouts during writes,
4842// * we refresh it, except if it was already infinite.
4843// */
4844// rep->rex = req->wex;
4845// }
4846// }
4847// }
4848// }
4849// return 0; /* other cases change nothing */
4850//}
4851//
4852
Willy Tarreaubaaee002006-06-26 02:48:02 +02004853/*
4854 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004855 * called with client socket shut down on input. Right now, only statistics can
4856 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02004857 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004858 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004859 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004860 */
4861int produce_content(struct session *s)
4862{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004863 if (s->data_source == DATA_SRC_NONE) {
4864 s->flags &= ~SN_SELF_GEN;
4865 return 1;
4866 }
4867 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004868 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004869 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004870 if (ret >= 0)
4871 return ret;
4872 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004873 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004874
Willy Tarreau91861262007-10-17 17:06:05 +02004875 /* unknown data source or internal error */
4876 s->txn.status = 500;
4877 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004878 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004879 if (!(s->flags & SN_ERR_MASK))
4880 s->flags |= SN_ERR_PRXCOND;
4881 if (!(s->flags & SN_FINST_MASK))
4882 s->flags |= SN_FINST_R;
4883 s->flags &= ~SN_SELF_GEN;
4884 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004885}
4886
4887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888/* Iterate the same filter through all request headers.
4889 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004890 * Since it can manage the switch to another backend, it updates the per-proxy
4891 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004892 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004893int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004894{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 char term;
4896 char *cur_ptr, *cur_end, *cur_next;
4897 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004898 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004899 struct hdr_idx_elem *cur_hdr;
4900 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004901
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004902 last_hdr = 0;
4903
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004904 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004905 old_idx = 0;
4906
4907 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004908 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004909 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004910 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004911 (exp->action == ACT_ALLOW ||
4912 exp->action == ACT_DENY ||
4913 exp->action == ACT_TARPIT))
4914 return 0;
4915
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004916 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004917 if (!cur_idx)
4918 break;
4919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004920 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004921 cur_ptr = cur_next;
4922 cur_end = cur_ptr + cur_hdr->len;
4923 cur_next = cur_end + cur_hdr->cr + 1;
4924
4925 /* Now we have one header between cur_ptr and cur_end,
4926 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004927 */
4928
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004929 /* The annoying part is that pattern matching needs
4930 * that we modify the contents to null-terminate all
4931 * strings before testing them.
4932 */
4933
4934 term = *cur_end;
4935 *cur_end = '\0';
4936
4937 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4938 switch (exp->action) {
4939 case ACT_SETBE:
4940 /* It is not possible to jump a second time.
4941 * FIXME: should we return an HTTP/500 here so that
4942 * the admin knows there's a problem ?
4943 */
4944 if (t->be != t->fe)
4945 break;
4946
4947 /* Swithing Proxy */
4948 t->be = (struct proxy *) exp->replace;
4949
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004950 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004951 * because we have associated req_cap and rsp_cap to the
4952 * frontend, and the beconn will be updated later.
4953 */
4954
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004955 t->rep->rto = t->req->wto = t->be->timeout.server;
4956 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004957 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 last_hdr = 1;
4959 break;
4960
4961 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004962 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004963 last_hdr = 1;
4964 break;
4965
4966 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004967 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004968 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004969 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004970 break;
4971
4972 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004973 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004975 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004976 break;
4977
4978 case ACT_REPLACE:
4979 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4980 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4981 /* FIXME: if the user adds a newline in the replacement, the
4982 * index will not be recalculated for now, and the new line
4983 * will not be counted as a new header.
4984 */
4985
4986 cur_end += delta;
4987 cur_next += delta;
4988 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004989 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 break;
4991
4992 case ACT_REMOVE:
4993 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4994 cur_next += delta;
4995
4996 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004997 txn->req.eoh += delta;
4998 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4999 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005000 cur_hdr->len = 0;
5001 cur_end = NULL; /* null-term has been rewritten */
5002 break;
5003
5004 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005005 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005006 if (cur_end)
5007 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005009 /* keep the link from this header to next one in case of later
5010 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005011 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005012 old_idx = cur_idx;
5013 }
5014 return 0;
5015}
5016
5017
5018/* Apply the filter to the request line.
5019 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5020 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005021 * Since it can manage the switch to another backend, it updates the per-proxy
5022 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005023 */
5024int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5025{
5026 char term;
5027 char *cur_ptr, *cur_end;
5028 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005029 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005030 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031
Willy Tarreau58f10d72006-12-04 02:26:12 +01005032
Willy Tarreau3d300592007-03-18 18:34:41 +01005033 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005034 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005035 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005036 (exp->action == ACT_ALLOW ||
5037 exp->action == ACT_DENY ||
5038 exp->action == ACT_TARPIT))
5039 return 0;
5040 else if (exp->action == ACT_REMOVE)
5041 return 0;
5042
5043 done = 0;
5044
Willy Tarreau9cdde232007-05-02 20:58:19 +02005045 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005046 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005047
5048 /* Now we have the request line between cur_ptr and cur_end */
5049
5050 /* The annoying part is that pattern matching needs
5051 * that we modify the contents to null-terminate all
5052 * strings before testing them.
5053 */
5054
5055 term = *cur_end;
5056 *cur_end = '\0';
5057
5058 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5059 switch (exp->action) {
5060 case ACT_SETBE:
5061 /* It is not possible to jump a second time.
5062 * FIXME: should we return an HTTP/500 here so that
5063 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005064 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005065 if (t->be != t->fe)
5066 break;
5067
5068 /* Swithing Proxy */
5069 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 /* right now, the backend switch is not too much complicated
5072 * because we have associated req_cap and rsp_cap to the
5073 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005074 */
5075
Willy Tarreaud7c30f92007-12-03 01:38:36 +01005076 t->rep->rto = t->req->wto = t->be->timeout.server;
5077 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02005078 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005079 done = 1;
5080 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005082 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005083 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005084 done = 1;
5085 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005087 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005088 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005089 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005090 done = 1;
5091 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005093 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005094 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005095 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005096 done = 1;
5097 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005099 case ACT_REPLACE:
5100 *cur_end = term; /* restore the string terminator */
5101 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5102 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5103 /* FIXME: if the user adds a newline in the replacement, the
5104 * index will not be recalculated for now, and the new line
5105 * will not be counted as a new header.
5106 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005107
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005108 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005109 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005110
Willy Tarreau9cdde232007-05-02 20:58:19 +02005111 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005112 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005113 HTTP_MSG_RQMETH,
5114 cur_ptr, cur_end + 1,
5115 NULL, NULL);
5116 if (unlikely(!cur_end))
5117 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005119 /* we have a full request and we know that we have either a CR
5120 * or an LF at <ptr>.
5121 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005122 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5123 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005124 /* there is no point trying this regex on headers */
5125 return 1;
5126 }
5127 }
5128 *cur_end = term; /* restore the string terminator */
5129 return done;
5130}
Willy Tarreau97de6242006-12-27 17:18:38 +01005131
Willy Tarreau58f10d72006-12-04 02:26:12 +01005132
Willy Tarreau58f10d72006-12-04 02:26:12 +01005133
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005134/*
5135 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5136 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005137 * unparsable request. Since it can manage the switch to another backend, it
5138 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005139 */
5140int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5141{
Willy Tarreau3d300592007-03-18 18:34:41 +01005142 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005143 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005144 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005145 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005147 /*
5148 * The interleaving of transformations and verdicts
5149 * makes it difficult to decide to continue or stop
5150 * the evaluation.
5151 */
5152
Willy Tarreau3d300592007-03-18 18:34:41 +01005153 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005154 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5155 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5156 exp = exp->next;
5157 continue;
5158 }
5159
5160 /* Apply the filter to the request line. */
5161 ret = apply_filter_to_req_line(t, req, exp);
5162 if (unlikely(ret < 0))
5163 return -1;
5164
5165 if (likely(ret == 0)) {
5166 /* The filter did not match the request, it can be
5167 * iterated through all headers.
5168 */
5169 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005170 }
5171 exp = exp->next;
5172 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005173 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005174}
5175
5176
Willy Tarreaua15645d2007-03-18 16:22:39 +01005177
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005179 * Manage client-side cookie. It can impact performance by about 2% so it is
5180 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005181 */
5182void manage_client_side_cookies(struct session *t, struct buffer *req)
5183{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005184 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005185 char *p1, *p2, *p3, *p4;
5186 char *del_colon, *del_cookie, *colon;
5187 int app_cookies;
5188
5189 appsess *asession_temp = NULL;
5190 appsess local_asession;
5191
5192 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005193 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005194
Willy Tarreau2a324282006-12-05 00:05:46 +01005195 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005196 * we start with the start line.
5197 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005198 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005199 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005200
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005201 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005203 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005205 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206 cur_ptr = cur_next;
5207 cur_end = cur_ptr + cur_hdr->len;
5208 cur_next = cur_end + cur_hdr->cr + 1;
5209
5210 /* We have one full header between cur_ptr and cur_end, and the
5211 * next header starts at cur_next. We're only interested in
5212 * "Cookie:" headers.
5213 */
5214
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005215 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5216 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217 old_idx = cur_idx;
5218 continue;
5219 }
5220
5221 /* Now look for cookies. Conforming to RFC2109, we have to support
5222 * attributes whose name begin with a '$', and associate them with
5223 * the right cookie, if we want to delete this cookie.
5224 * So there are 3 cases for each cookie read :
5225 * 1) it's a special attribute, beginning with a '$' : ignore it.
5226 * 2) it's a server id cookie that we *MAY* want to delete : save
5227 * some pointers on it (last semi-colon, beginning of cookie...)
5228 * 3) it's an application cookie : we *MAY* have to delete a previous
5229 * "special" cookie.
5230 * At the end of loop, if a "special" cookie remains, we may have to
5231 * remove it. If no application cookie persists in the header, we
5232 * *MUST* delete it
5233 */
5234
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005235 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005236
Willy Tarreau58f10d72006-12-04 02:26:12 +01005237 /* del_cookie == NULL => nothing to be deleted */
5238 del_colon = del_cookie = NULL;
5239 app_cookies = 0;
5240
5241 while (p1 < cur_end) {
5242 /* skip spaces and colons, but keep an eye on these ones */
5243 while (p1 < cur_end) {
5244 if (*p1 == ';' || *p1 == ',')
5245 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005246 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 break;
5248 p1++;
5249 }
5250
5251 if (p1 == cur_end)
5252 break;
5253
5254 /* p1 is at the beginning of the cookie name */
5255 p2 = p1;
5256 while (p2 < cur_end && *p2 != '=')
5257 p2++;
5258
5259 if (p2 == cur_end)
5260 break;
5261
5262 p3 = p2 + 1; /* skips the '=' sign */
5263 if (p3 == cur_end)
5264 break;
5265
5266 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005267 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005268 p4++;
5269
5270 /* here, we have the cookie name between p1 and p2,
5271 * and its value between p3 and p4.
5272 * we can process it :
5273 *
5274 * Cookie: NAME=VALUE;
5275 * | || || |
5276 * | || || +--> p4
5277 * | || |+-------> p3
5278 * | || +--------> p2
5279 * | |+------------> p1
5280 * | +-------------> colon
5281 * +--------------------> cur_ptr
5282 */
5283
5284 if (*p1 == '$') {
5285 /* skip this one */
5286 }
5287 else {
5288 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005289 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005290 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005291 (p4 - p1 >= t->fe->capture_namelen) &&
5292 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293 int log_len = p4 - p1;
5294
Willy Tarreau086b3b42007-05-13 21:45:51 +02005295 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005296 Alert("HTTP logging : out of memory.\n");
5297 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005298 if (log_len > t->fe->capture_len)
5299 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005300 memcpy(txn->cli_cookie, p1, log_len);
5301 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005302 }
5303 }
5304
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005305 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5306 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005307 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005308 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005309 char *delim;
5310
5311 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5312 * have the server ID betweek p3 and delim, and the original cookie between
5313 * delim+1 and p4. Otherwise, delim==p4 :
5314 *
5315 * Cookie: NAME=SRV~VALUE;
5316 * | || || | |
5317 * | || || | +--> p4
5318 * | || || +--------> delim
5319 * | || |+-----------> p3
5320 * | || +------------> p2
5321 * | |+----------------> p1
5322 * | +-----------------> colon
5323 * +------------------------> cur_ptr
5324 */
5325
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005326 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005327 for (delim = p3; delim < p4; delim++)
5328 if (*delim == COOKIE_DELIM)
5329 break;
5330 }
5331 else
5332 delim = p4;
5333
5334
5335 /* Here, we'll look for the first running server which supports the cookie.
5336 * This allows to share a same cookie between several servers, for example
5337 * to dedicate backup servers to specific servers only.
5338 * However, to prevent clients from sticking to cookie-less backup server
5339 * when they have incidentely learned an empty cookie, we simply ignore
5340 * empty cookies and mark them as invalid.
5341 */
5342 if (delim == p3)
5343 srv = NULL;
5344
5345 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005346 if (srv->cookie && (srv->cklen == delim - p3) &&
5347 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005348 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005350 txn->flags &= ~TX_CK_MASK;
5351 txn->flags |= TX_CK_VALID;
5352 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005353 t->srv = srv;
5354 break;
5355 } else {
5356 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005357 txn->flags &= ~TX_CK_MASK;
5358 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005359 }
5360 }
5361 srv = srv->next;
5362 }
5363
Willy Tarreau3d300592007-03-18 18:34:41 +01005364 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005365 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005366 txn->flags &= ~TX_CK_MASK;
5367 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005368 }
5369
5370 /* depending on the cookie mode, we may have to either :
5371 * - delete the complete cookie if we're in insert+indirect mode, so that
5372 * the server never sees it ;
5373 * - remove the server id from the cookie value, and tag the cookie as an
5374 * application cookie so that it does not get accidentely removed later,
5375 * if we're in cookie prefix mode
5376 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005377 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005378 int delta; /* negative */
5379
5380 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5381 p4 += delta;
5382 cur_end += delta;
5383 cur_next += delta;
5384 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005385 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005386
5387 del_cookie = del_colon = NULL;
5388 app_cookies++; /* protect the header from deletion */
5389 }
5390 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005391 (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 +01005392 del_cookie = p1;
5393 del_colon = colon;
5394 }
5395 } else {
5396 /* now we know that we must keep this cookie since it's
5397 * not ours. But if we wanted to delete our cookie
5398 * earlier, we cannot remove the complete header, but we
5399 * can remove the previous block itself.
5400 */
5401 app_cookies++;
5402
5403 if (del_cookie != NULL) {
5404 int delta; /* negative */
5405
5406 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5407 p4 += delta;
5408 cur_end += delta;
5409 cur_next += delta;
5410 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005411 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005412 del_cookie = del_colon = NULL;
5413 }
5414 }
5415
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005416 if ((t->be->appsession_name != NULL) &&
5417 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005418 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005419
Willy Tarreau58f10d72006-12-04 02:26:12 +01005420 /* Cool... it's the right one */
5421
5422 asession_temp = &local_asession;
5423
Willy Tarreau63963c62007-05-13 21:29:55 +02005424 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005425 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5426 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5427 return;
5428 }
5429
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005430 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5431 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005432 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02005433
Willy Tarreau58f10d72006-12-04 02:26:12 +01005434 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005435 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5436 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005437 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005439 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005440 Alert("Not enough memory process_cli():asession:calloc().\n");
5441 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5442 return;
5443 }
5444
5445 asession_temp->sessid = local_asession.sessid;
5446 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005447 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005448 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449 } else {
5450 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005451 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005452 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005453 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005454 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005455 Alert("Found Application Session without matching server.\n");
5456 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005457 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005458 while (srv) {
5459 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005460 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005461 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005462 txn->flags &= ~TX_CK_MASK;
5463 txn->flags |= TX_CK_VALID;
5464 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005465 t->srv = srv;
5466 break;
5467 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005468 txn->flags &= ~TX_CK_MASK;
5469 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005470 }
5471 }
5472 srv = srv->next;
5473 }/* end while(srv) */
5474 }/* end else if server == NULL */
5475
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005476 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005477 asession_temp->request_count++;
5478#if defined(DEBUG_HASH)
5479 Alert("manage_client_side_cookies\n");
5480 appsession_hash_dump(&(t->be->htbl_proxy));
5481#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005482 }/* end if ((t->proxy->appsession_name != NULL) ... */
5483 }
5484
5485 /* we'll have to look for another cookie ... */
5486 p1 = p4;
5487 } /* while (p1 < cur_end) */
5488
5489 /* There's no more cookie on this line.
5490 * We may have marked the last one(s) for deletion.
5491 * We must do this now in two ways :
5492 * - if there is no app cookie, we simply delete the header ;
5493 * - if there are app cookies, we must delete the end of the
5494 * string properly, including the colon/semi-colon before
5495 * the cookie name.
5496 */
5497 if (del_cookie != NULL) {
5498 int delta;
5499 if (app_cookies) {
5500 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5501 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005502 cur_hdr->len += delta;
5503 } else {
5504 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005505
5506 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005507 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5508 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005509 cur_hdr->len = 0;
5510 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005511 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005512 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005513 }
5514
5515 /* keep the link from this header to next one */
5516 old_idx = cur_idx;
5517 } /* end of cookie processing on this header */
5518}
5519
5520
Willy Tarreaua15645d2007-03-18 16:22:39 +01005521/* Iterate the same filter through all response headers contained in <rtr>.
5522 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5523 */
5524int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5525{
5526 char term;
5527 char *cur_ptr, *cur_end, *cur_next;
5528 int cur_idx, old_idx, last_hdr;
5529 struct http_txn *txn = &t->txn;
5530 struct hdr_idx_elem *cur_hdr;
5531 int len, delta;
5532
5533 last_hdr = 0;
5534
5535 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5536 old_idx = 0;
5537
5538 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005539 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005540 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005541 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005542 (exp->action == ACT_ALLOW ||
5543 exp->action == ACT_DENY))
5544 return 0;
5545
5546 cur_idx = txn->hdr_idx.v[old_idx].next;
5547 if (!cur_idx)
5548 break;
5549
5550 cur_hdr = &txn->hdr_idx.v[cur_idx];
5551 cur_ptr = cur_next;
5552 cur_end = cur_ptr + cur_hdr->len;
5553 cur_next = cur_end + cur_hdr->cr + 1;
5554
5555 /* Now we have one header between cur_ptr and cur_end,
5556 * and the next header starts at cur_next.
5557 */
5558
5559 /* The annoying part is that pattern matching needs
5560 * that we modify the contents to null-terminate all
5561 * strings before testing them.
5562 */
5563
5564 term = *cur_end;
5565 *cur_end = '\0';
5566
5567 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5568 switch (exp->action) {
5569 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005570 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 last_hdr = 1;
5572 break;
5573
5574 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005575 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005576 last_hdr = 1;
5577 break;
5578
5579 case ACT_REPLACE:
5580 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5581 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5582 /* FIXME: if the user adds a newline in the replacement, the
5583 * index will not be recalculated for now, and the new line
5584 * will not be counted as a new header.
5585 */
5586
5587 cur_end += delta;
5588 cur_next += delta;
5589 cur_hdr->len += delta;
5590 txn->rsp.eoh += delta;
5591 break;
5592
5593 case ACT_REMOVE:
5594 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5595 cur_next += delta;
5596
5597 /* FIXME: this should be a separate function */
5598 txn->rsp.eoh += delta;
5599 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5600 txn->hdr_idx.used--;
5601 cur_hdr->len = 0;
5602 cur_end = NULL; /* null-term has been rewritten */
5603 break;
5604
5605 }
5606 }
5607 if (cur_end)
5608 *cur_end = term; /* restore the string terminator */
5609
5610 /* keep the link from this header to next one in case of later
5611 * removal of next header.
5612 */
5613 old_idx = cur_idx;
5614 }
5615 return 0;
5616}
5617
5618
5619/* Apply the filter to the status line in the response buffer <rtr>.
5620 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5621 * or -1 if a replacement resulted in an invalid status line.
5622 */
5623int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5624{
5625 char term;
5626 char *cur_ptr, *cur_end;
5627 int done;
5628 struct http_txn *txn = &t->txn;
5629 int len, delta;
5630
5631
Willy Tarreau3d300592007-03-18 18:34:41 +01005632 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005633 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005634 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005635 (exp->action == ACT_ALLOW ||
5636 exp->action == ACT_DENY))
5637 return 0;
5638 else if (exp->action == ACT_REMOVE)
5639 return 0;
5640
5641 done = 0;
5642
Willy Tarreau9cdde232007-05-02 20:58:19 +02005643 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5645
5646 /* Now we have the status line between cur_ptr and cur_end */
5647
5648 /* The annoying part is that pattern matching needs
5649 * that we modify the contents to null-terminate all
5650 * strings before testing them.
5651 */
5652
5653 term = *cur_end;
5654 *cur_end = '\0';
5655
5656 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5657 switch (exp->action) {
5658 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005659 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005660 done = 1;
5661 break;
5662
5663 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005664 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005665 done = 1;
5666 break;
5667
5668 case ACT_REPLACE:
5669 *cur_end = term; /* restore the string terminator */
5670 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5671 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5672 /* FIXME: if the user adds a newline in the replacement, the
5673 * index will not be recalculated for now, and the new line
5674 * will not be counted as a new header.
5675 */
5676
5677 txn->rsp.eoh += delta;
5678 cur_end += delta;
5679
Willy Tarreau9cdde232007-05-02 20:58:19 +02005680 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005681 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005682 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005683 cur_ptr, cur_end + 1,
5684 NULL, NULL);
5685 if (unlikely(!cur_end))
5686 return -1;
5687
5688 /* we have a full respnse and we know that we have either a CR
5689 * or an LF at <ptr>.
5690 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005691 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005692 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5693 /* there is no point trying this regex on headers */
5694 return 1;
5695 }
5696 }
5697 *cur_end = term; /* restore the string terminator */
5698 return done;
5699}
5700
5701
5702
5703/*
5704 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5705 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5706 * unparsable response.
5707 */
5708int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5709{
Willy Tarreau3d300592007-03-18 18:34:41 +01005710 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713 int ret;
5714
5715 /*
5716 * The interleaving of transformations and verdicts
5717 * makes it difficult to decide to continue or stop
5718 * the evaluation.
5719 */
5720
Willy Tarreau3d300592007-03-18 18:34:41 +01005721 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005722 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5723 exp->action == ACT_PASS)) {
5724 exp = exp->next;
5725 continue;
5726 }
5727
5728 /* Apply the filter to the status line. */
5729 ret = apply_filter_to_sts_line(t, rtr, exp);
5730 if (unlikely(ret < 0))
5731 return -1;
5732
5733 if (likely(ret == 0)) {
5734 /* The filter did not match the response, it can be
5735 * iterated through all headers.
5736 */
5737 apply_filter_to_resp_headers(t, rtr, exp);
5738 }
5739 exp = exp->next;
5740 }
5741 return 0;
5742}
5743
5744
5745
5746/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005747 * Manage server-side cookies. It can impact performance by about 2% so it is
5748 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005749 */
5750void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5751{
5752 struct http_txn *txn = &t->txn;
5753 char *p1, *p2, *p3, *p4;
5754
5755 appsess *asession_temp = NULL;
5756 appsess local_asession;
5757
5758 char *cur_ptr, *cur_end, *cur_next;
5759 int cur_idx, old_idx, delta;
5760
Willy Tarreaua15645d2007-03-18 16:22:39 +01005761 /* Iterate through the headers.
5762 * we start with the start line.
5763 */
5764 old_idx = 0;
5765 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5766
5767 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5768 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005769 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005770
5771 cur_hdr = &txn->hdr_idx.v[cur_idx];
5772 cur_ptr = cur_next;
5773 cur_end = cur_ptr + cur_hdr->len;
5774 cur_next = cur_end + cur_hdr->cr + 1;
5775
5776 /* We have one full header between cur_ptr and cur_end, and the
5777 * next header starts at cur_next. We're only interested in
5778 * "Cookie:" headers.
5779 */
5780
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005781 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5782 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783 old_idx = cur_idx;
5784 continue;
5785 }
5786
5787 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005788 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005789
5790
5791 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005792 if (t->be->cookie_name == NULL &&
5793 t->be->appsession_name == NULL &&
5794 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005795 return;
5796
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005797 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005798
5799 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005800 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5801 break;
5802
5803 /* p1 is at the beginning of the cookie name */
5804 p2 = p1;
5805
5806 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5807 p2++;
5808
5809 if (p2 == cur_end || *p2 == ';') /* next cookie */
5810 break;
5811
5812 p3 = p2 + 1; /* skip the '=' sign */
5813 if (p3 == cur_end)
5814 break;
5815
5816 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005817 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818 p4++;
5819
5820 /* here, we have the cookie name between p1 and p2,
5821 * and its value between p3 and p4.
5822 * we can process it.
5823 */
5824
5825 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005826 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005827 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005828 (p4 - p1 >= t->be->capture_namelen) &&
5829 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830 int log_len = p4 - p1;
5831
Willy Tarreau086b3b42007-05-13 21:45:51 +02005832 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005833 Alert("HTTP logging : out of memory.\n");
5834 }
5835
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005836 if (log_len > t->be->capture_len)
5837 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005838 memcpy(txn->srv_cookie, p1, log_len);
5839 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005840 }
5841
5842 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005843 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5844 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005846 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005847
5848 /* If the cookie is in insert mode on a known server, we'll delete
5849 * this occurrence because we'll insert another one later.
5850 * We'll delete it too if the "indirect" option is set and we're in
5851 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005852 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5853 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005854 /* this header must be deleted */
5855 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5856 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5857 txn->hdr_idx.used--;
5858 cur_hdr->len = 0;
5859 cur_next += delta;
5860 txn->rsp.eoh += delta;
5861
Willy Tarreau3d300592007-03-18 18:34:41 +01005862 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863 }
5864 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005865 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005866 /* replace bytes p3->p4 with the cookie name associated
5867 * with this server since we know it.
5868 */
5869 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5870 cur_hdr->len += delta;
5871 cur_next += delta;
5872 txn->rsp.eoh += delta;
5873
Willy Tarreau3d300592007-03-18 18:34:41 +01005874 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875 }
5876 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005877 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005878 /* insert the cookie name associated with this server
5879 * before existing cookie, and insert a delimitor between them..
5880 */
5881 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5882 cur_hdr->len += delta;
5883 cur_next += delta;
5884 txn->rsp.eoh += delta;
5885
5886 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005887 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888 }
5889 }
5890 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005891 else if ((t->be->appsession_name != NULL) &&
5892 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005893
5894 /* Cool... it's the right one */
5895
5896 size_t server_id_len = strlen(t->srv->id) + 1;
5897 asession_temp = &local_asession;
5898
Willy Tarreau63963c62007-05-13 21:29:55 +02005899 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5901 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5902 return;
5903 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005904 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5905 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906 asession_temp->serverid = NULL;
5907
5908 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005909 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5910 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005911 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912 Alert("Not enough Memory process_srv():asession:calloc().\n");
5913 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5914 return;
5915 }
5916 asession_temp->sessid = local_asession.sessid;
5917 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005918 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005919 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5920 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005921 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005922 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005923 }
5924
Willy Tarreaua15645d2007-03-18 16:22:39 +01005925 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005926 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005927 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5928 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5929 return;
5930 }
5931 asession_temp->serverid[0] = '\0';
5932 }
5933
5934 if (asession_temp->serverid[0] == '\0')
5935 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5936
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005937 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005938 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005940 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005941 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005942#endif
5943 }/* end if ((t->proxy->appsession_name != NULL) ... */
5944 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5945 } /* we're now at the end of the cookie value */
5946
5947 /* keep the link from this header to next one */
5948 old_idx = cur_idx;
5949 } /* end of cookie processing on this header */
5950}
5951
5952
5953
5954/*
5955 * Check if response is cacheable or not. Updates t->flags.
5956 */
5957void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5958{
5959 struct http_txn *txn = &t->txn;
5960 char *p1, *p2;
5961
5962 char *cur_ptr, *cur_end, *cur_next;
5963 int cur_idx;
5964
Willy Tarreau5df51872007-11-25 16:20:08 +01005965 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005966 return;
5967
5968 /* Iterate through the headers.
5969 * we start with the start line.
5970 */
5971 cur_idx = 0;
5972 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5973
5974 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5975 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005976 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005977
5978 cur_hdr = &txn->hdr_idx.v[cur_idx];
5979 cur_ptr = cur_next;
5980 cur_end = cur_ptr + cur_hdr->len;
5981 cur_next = cur_end + cur_hdr->cr + 1;
5982
5983 /* We have one full header between cur_ptr and cur_end, and the
5984 * next header starts at cur_next. We're only interested in
5985 * "Cookie:" headers.
5986 */
5987
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005988 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5989 if (val) {
5990 if ((cur_end - (cur_ptr + val) >= 8) &&
5991 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5992 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5993 return;
5994 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005995 }
5996
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005997 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5998 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999 continue;
6000
6001 /* OK, right now we know we have a cache-control header at cur_ptr */
6002
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006003 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004
6005 if (p1 >= cur_end) /* no more info */
6006 continue;
6007
6008 /* p1 is at the beginning of the value */
6009 p2 = p1;
6010
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006011 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006012 p2++;
6013
6014 /* we have a complete value between p1 and p2 */
6015 if (p2 < cur_end && *p2 == '=') {
6016 /* we have something of the form no-cache="set-cookie" */
6017 if ((cur_end - p1 >= 21) &&
6018 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6019 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006020 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006021 continue;
6022 }
6023
6024 /* OK, so we know that either p2 points to the end of string or to a comma */
6025 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6026 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6027 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6028 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006029 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006030 return;
6031 }
6032
6033 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006034 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006035 continue;
6036 }
6037 }
6038}
6039
6040
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041/*
6042 * Try to retrieve a known appsession in the URI, then the associated server.
6043 * If the server is found, it's assigned to the session.
6044 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046{
Willy Tarreau3d300592007-03-18 18:34:41 +01006047 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006048 appsess *asession_temp = NULL;
6049 appsess local_asession;
6050 char *request_line;
6051
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006052 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01006053 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006055 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056 return;
6057
6058 /* skip ';' */
6059 request_line++;
6060
6061 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006062 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063 return;
6064
6065 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006066 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006067
6068 /* First try if we already have an appsession */
6069 asession_temp = &local_asession;
6070
Willy Tarreau63963c62007-05-13 21:29:55 +02006071 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
6073 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
6074 return;
6075 }
6076
6077 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006078 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
6079 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080 asession_temp->serverid = NULL;
6081
6082 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01006083 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
6084 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02006085 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006086 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006087 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 Alert("Not enough memory process_cli():asession:calloc().\n");
6089 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
6090 return;
6091 }
6092 asession_temp->sessid = local_asession.sessid;
6093 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006094 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02006095 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096 }
6097 else {
6098 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006099 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100 }
Willy Tarreau51041c72007-09-09 21:56:53 +02006101
Willy Tarreau0c303ee2008-07-07 00:09:58 +02006102 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02006104
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006106 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006107 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108#endif
6109 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006110 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111 Alert("Found Application Session without matching server.\n");
6112 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006113 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114 while (srv) {
6115 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006116 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006117 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01006118 txn->flags &= ~TX_CK_MASK;
6119 txn->flags |= TX_CK_VALID;
6120 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121 t->srv = srv;
6122 break;
6123 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01006124 txn->flags &= ~TX_CK_MASK;
6125 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126 }
6127 }
6128 srv = srv->next;
6129 }
6130 }
6131}
6132
6133
Willy Tarreaub2513902006-12-17 14:52:38 +01006134/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006135 * In a GET or HEAD request, check if the requested URI matches the stats uri
6136 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006137 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006138 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006139 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006140 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006141 *
6142 * Returns 1 if the session's state changes, otherwise 0.
6143 */
6144int stats_check_uri_auth(struct session *t, struct proxy *backend)
6145{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006146 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006147 struct uri_auth *uri_auth = backend->uri_auth;
6148 struct user_auth *user;
6149 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006150 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006151
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006152 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006154 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006155 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006156 return 0;
6157
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006158 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006160 /* the URI is in h */
6161 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006162 return 0;
6163
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006164 h += uri_auth->uri_len;
6165 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6166 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006167 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006168 break;
6169 }
6170 h++;
6171 }
6172
6173 if (uri_auth->refresh) {
6174 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6175 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6176 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006177 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006178 break;
6179 }
6180 h++;
6181 }
6182 }
6183
Willy Tarreau55bb8452007-10-17 18:44:57 +02006184 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6185 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6186 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006187 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006188 break;
6189 }
6190 h++;
6191 }
6192
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006193 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6194
Willy Tarreaub2513902006-12-17 14:52:38 +01006195 /* we are in front of a interceptable URI. Let's check
6196 * if there's an authentication and if it's valid.
6197 */
6198 user = uri_auth->users;
6199 if (!user) {
6200 /* no user auth required, it's OK */
6201 authenticated = 1;
6202 } else {
6203 authenticated = 0;
6204
6205 /* a user list is defined, we have to check.
6206 * skip 21 chars for "Authorization: Basic ".
6207 */
6208
6209 /* FIXME: this should move to an earlier place */
6210 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006211 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6212 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6213 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006214 if (len > 14 &&
6215 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006216 txn->auth_hdr.str = h;
6217 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006218 break;
6219 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006220 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006221 }
6222
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006223 if (txn->auth_hdr.len < 21 ||
6224 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006225 user = NULL;
6226
6227 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006228 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6229 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006230 user->user_pwd, user->user_len)) {
6231 authenticated = 1;
6232 break;
6233 }
6234 user = user->next;
6235 }
6236 }
6237
6238 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006239 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006240
6241 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01006242 msg.str = trash;
6243 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006244 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01006245 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02006246 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006247 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006248 if (!(t->flags & SN_ERR_MASK))
6249 t->flags |= SN_ERR_PRXCOND;
6250 if (!(t->flags & SN_FINST_MASK))
6251 t->flags |= SN_FINST_R;
6252 return 1;
6253 }
6254
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006255 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006256 * data.
6257 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02006258 EV_FD_CLR(t->cli_fd, DIR_RD);
6259 buffer_shutr(t->req);
6260 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02006261 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02006262 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006263 t->data_source = DATA_SRC_STATS;
6264 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006265 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01006266 produce_content(t);
6267 return 1;
6268}
6269
6270
Willy Tarreaubaaee002006-06-26 02:48:02 +02006271/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006272 * Print a debug line with a header
6273 */
6274void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6275{
6276 int len, max;
6277 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +02006278 dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279 max = end - start;
6280 UBOUND(max, sizeof(trash) - len - 1);
6281 len += strlcpy2(trash + len, start, max + 1);
6282 trash[len++] = '\n';
6283 write(1, trash, len);
6284}
6285
6286
Willy Tarreau8797c062007-05-07 00:55:35 +02006287/************************************************************************/
6288/* The code below is dedicated to ACL parsing and matching */
6289/************************************************************************/
6290
6291
6292
6293
6294/* 1. Check on METHOD
6295 * We use the pre-parsed method if it is known, and store its number as an
6296 * integer. If it is unknown, we use the pointer and the length.
6297 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006298static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006299{
6300 int len, meth;
6301
Willy Tarreauae8b7962007-06-09 23:10:04 +02006302 len = strlen(*text);
6303 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006304
6305 pattern->val.i = meth;
6306 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006307 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006308 if (!pattern->ptr.str)
6309 return 0;
6310 pattern->len = len;
6311 }
6312 return 1;
6313}
6314
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006315static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006316acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6317 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006318{
6319 int meth;
6320 struct http_txn *txn = l7;
6321
Willy Tarreaub6866442008-07-14 23:54:42 +02006322 if (!txn)
6323 return 0;
6324
Willy Tarreauc11416f2007-06-17 16:58:38 +02006325 if (txn->req.msg_state != HTTP_MSG_BODY)
6326 return 0;
6327
Willy Tarreau8797c062007-05-07 00:55:35 +02006328 meth = txn->meth;
6329 test->i = meth;
6330 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006331 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6332 /* ensure the indexes are not affected */
6333 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006334 test->len = txn->req.sl.rq.m_l;
6335 test->ptr = txn->req.sol;
6336 }
6337 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6338 return 1;
6339}
6340
6341static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6342{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006343 int icase;
6344
Willy Tarreau8797c062007-05-07 00:55:35 +02006345 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006346 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006347
6348 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006349 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006350
6351 /* Other method, we must compare the strings */
6352 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006353 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006354
6355 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6356 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6357 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006358 return ACL_PAT_FAIL;
6359 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006360}
6361
6362/* 2. Check on Request/Status Version
6363 * We simply compare strings here.
6364 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006365static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006366{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006367 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006368 if (!pattern->ptr.str)
6369 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006370 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006371 return 1;
6372}
6373
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006374static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006375acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6376 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006377{
6378 struct http_txn *txn = l7;
6379 char *ptr;
6380 int len;
6381
Willy Tarreaub6866442008-07-14 23:54:42 +02006382 if (!txn)
6383 return 0;
6384
Willy Tarreauc11416f2007-06-17 16:58:38 +02006385 if (txn->req.msg_state != HTTP_MSG_BODY)
6386 return 0;
6387
Willy Tarreau8797c062007-05-07 00:55:35 +02006388 len = txn->req.sl.rq.v_l;
6389 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6390
6391 while ((len-- > 0) && (*ptr++ != '/'));
6392 if (len <= 0)
6393 return 0;
6394
6395 test->ptr = ptr;
6396 test->len = len;
6397
6398 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6399 return 1;
6400}
6401
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006402static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006403acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6404 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006405{
6406 struct http_txn *txn = l7;
6407 char *ptr;
6408 int len;
6409
Willy Tarreaub6866442008-07-14 23:54:42 +02006410 if (!txn)
6411 return 0;
6412
Willy Tarreauc11416f2007-06-17 16:58:38 +02006413 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6414 return 0;
6415
Willy Tarreau8797c062007-05-07 00:55:35 +02006416 len = txn->rsp.sl.st.v_l;
6417 ptr = txn->rsp.sol;
6418
6419 while ((len-- > 0) && (*ptr++ != '/'));
6420 if (len <= 0)
6421 return 0;
6422
6423 test->ptr = ptr;
6424 test->len = len;
6425
6426 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6427 return 1;
6428}
6429
6430/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006431static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006432acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6433 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006434{
6435 struct http_txn *txn = l7;
6436 char *ptr;
6437 int len;
6438
Willy Tarreaub6866442008-07-14 23:54:42 +02006439 if (!txn)
6440 return 0;
6441
Willy Tarreauc11416f2007-06-17 16:58:38 +02006442 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6443 return 0;
6444
Willy Tarreau8797c062007-05-07 00:55:35 +02006445 len = txn->rsp.sl.st.c_l;
6446 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6447
6448 test->i = __strl2ui(ptr, len);
6449 test->flags = ACL_TEST_F_VOL_1ST;
6450 return 1;
6451}
6452
6453/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006454static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006455acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6456 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006457{
6458 struct http_txn *txn = l7;
6459
Willy Tarreaub6866442008-07-14 23:54:42 +02006460 if (!txn)
6461 return 0;
6462
Willy Tarreauc11416f2007-06-17 16:58:38 +02006463 if (txn->req.msg_state != HTTP_MSG_BODY)
6464 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006465
Willy Tarreauc11416f2007-06-17 16:58:38 +02006466 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6467 /* ensure the indexes are not affected */
6468 return 0;
6469
Willy Tarreau8797c062007-05-07 00:55:35 +02006470 test->len = txn->req.sl.rq.u_l;
6471 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6472
Willy Tarreauf3d25982007-05-08 22:45:09 +02006473 /* we do not need to set READ_ONLY because the data is in a buffer */
6474 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006475 return 1;
6476}
6477
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006478static int
6479acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6480 struct acl_expr *expr, struct acl_test *test)
6481{
6482 struct http_txn *txn = l7;
6483
Willy Tarreaub6866442008-07-14 23:54:42 +02006484 if (!txn)
6485 return 0;
6486
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006487 if (txn->req.msg_state != HTTP_MSG_BODY)
6488 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006489
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006490 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6491 /* ensure the indexes are not affected */
6492 return 0;
6493
6494 /* Parse HTTP request */
6495 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6496 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6497 test->i = AF_INET;
6498
6499 /*
6500 * If we are parsing url in frontend space, we prepare backend stage
6501 * to not parse again the same url ! optimization lazyness...
6502 */
6503 if (px->options & PR_O_HTTP_PROXY)
6504 l4->flags |= SN_ADDR_SET;
6505
6506 test->flags = ACL_TEST_F_READ_ONLY;
6507 return 1;
6508}
6509
6510static int
6511acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6512 struct acl_expr *expr, struct acl_test *test)
6513{
6514 struct http_txn *txn = l7;
6515
Willy Tarreaub6866442008-07-14 23:54:42 +02006516 if (!txn)
6517 return 0;
6518
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006519 if (txn->req.msg_state != HTTP_MSG_BODY)
6520 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006521
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006522 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6523 /* ensure the indexes are not affected */
6524 return 0;
6525
6526 /* Same optimization as url_ip */
6527 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6528 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6529
6530 if (px->options & PR_O_HTTP_PROXY)
6531 l4->flags |= SN_ADDR_SET;
6532
6533 test->flags = ACL_TEST_F_READ_ONLY;
6534 return 1;
6535}
6536
Willy Tarreauc11416f2007-06-17 16:58:38 +02006537/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6538 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6539 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006540static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006541acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006542 struct acl_expr *expr, struct acl_test *test)
6543{
6544 struct http_txn *txn = l7;
6545 struct hdr_idx *idx = &txn->hdr_idx;
6546 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006547
Willy Tarreaub6866442008-07-14 23:54:42 +02006548 if (!txn)
6549 return 0;
6550
Willy Tarreau33a7e692007-06-10 19:45:56 +02006551 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6552 /* search for header from the beginning */
6553 ctx->idx = 0;
6554
Willy Tarreau33a7e692007-06-10 19:45:56 +02006555 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6556 test->flags |= ACL_TEST_F_FETCH_MORE;
6557 test->flags |= ACL_TEST_F_VOL_HDR;
6558 test->len = ctx->vlen;
6559 test->ptr = (char *)ctx->line + ctx->val;
6560 return 1;
6561 }
6562
6563 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6564 test->flags |= ACL_TEST_F_VOL_HDR;
6565 return 0;
6566}
6567
Willy Tarreau33a7e692007-06-10 19:45:56 +02006568static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006569acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6570 struct acl_expr *expr, struct acl_test *test)
6571{
6572 struct http_txn *txn = l7;
6573
Willy Tarreaub6866442008-07-14 23:54:42 +02006574 if (!txn)
6575 return 0;
6576
Willy Tarreauc11416f2007-06-17 16:58:38 +02006577 if (txn->req.msg_state != HTTP_MSG_BODY)
6578 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006579
Willy Tarreauc11416f2007-06-17 16:58:38 +02006580 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6581 /* ensure the indexes are not affected */
6582 return 0;
6583
6584 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6585}
6586
6587static int
6588acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6589 struct acl_expr *expr, struct acl_test *test)
6590{
6591 struct http_txn *txn = l7;
6592
Willy Tarreaub6866442008-07-14 23:54:42 +02006593 if (!txn)
6594 return 0;
6595
Willy Tarreauc11416f2007-06-17 16:58:38 +02006596 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6597 return 0;
6598
6599 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6600}
6601
6602/* 6. Check on HTTP header count. The number of occurrences is returned.
6603 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6604 */
6605static int
6606acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006607 struct acl_expr *expr, struct acl_test *test)
6608{
6609 struct http_txn *txn = l7;
6610 struct hdr_idx *idx = &txn->hdr_idx;
6611 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006612 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006613
Willy Tarreaub6866442008-07-14 23:54:42 +02006614 if (!txn)
6615 return 0;
6616
Willy Tarreau33a7e692007-06-10 19:45:56 +02006617 ctx.idx = 0;
6618 cnt = 0;
6619 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6620 cnt++;
6621
6622 test->i = cnt;
6623 test->flags = ACL_TEST_F_VOL_HDR;
6624 return 1;
6625}
6626
Willy Tarreauc11416f2007-06-17 16:58:38 +02006627static int
6628acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6629 struct acl_expr *expr, struct acl_test *test)
6630{
6631 struct http_txn *txn = l7;
6632
Willy Tarreaub6866442008-07-14 23:54:42 +02006633 if (!txn)
6634 return 0;
6635
Willy Tarreauc11416f2007-06-17 16:58:38 +02006636 if (txn->req.msg_state != HTTP_MSG_BODY)
6637 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006638
Willy Tarreauc11416f2007-06-17 16:58:38 +02006639 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6640 /* ensure the indexes are not affected */
6641 return 0;
6642
6643 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6644}
6645
6646static int
6647acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6648 struct acl_expr *expr, struct acl_test *test)
6649{
6650 struct http_txn *txn = l7;
6651
Willy Tarreaub6866442008-07-14 23:54:42 +02006652 if (!txn)
6653 return 0;
6654
Willy Tarreauc11416f2007-06-17 16:58:38 +02006655 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6656 return 0;
6657
6658 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6659}
6660
Willy Tarreau33a7e692007-06-10 19:45:56 +02006661/* 7. Check on HTTP header's integer value. The integer value is returned.
6662 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006663 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006664 */
6665static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006666acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006667 struct acl_expr *expr, struct acl_test *test)
6668{
6669 struct http_txn *txn = l7;
6670 struct hdr_idx *idx = &txn->hdr_idx;
6671 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006672
Willy Tarreaub6866442008-07-14 23:54:42 +02006673 if (!txn)
6674 return 0;
6675
Willy Tarreau33a7e692007-06-10 19:45:56 +02006676 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6677 /* search for header from the beginning */
6678 ctx->idx = 0;
6679
Willy Tarreau33a7e692007-06-10 19:45:56 +02006680 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6681 test->flags |= ACL_TEST_F_FETCH_MORE;
6682 test->flags |= ACL_TEST_F_VOL_HDR;
6683 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6684 return 1;
6685 }
6686
6687 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6688 test->flags |= ACL_TEST_F_VOL_HDR;
6689 return 0;
6690}
6691
Willy Tarreauc11416f2007-06-17 16:58:38 +02006692static int
6693acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6694 struct acl_expr *expr, struct acl_test *test)
6695{
6696 struct http_txn *txn = l7;
6697
Willy Tarreaub6866442008-07-14 23:54:42 +02006698 if (!txn)
6699 return 0;
6700
Willy Tarreauc11416f2007-06-17 16:58:38 +02006701 if (txn->req.msg_state != HTTP_MSG_BODY)
6702 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006703
Willy Tarreauc11416f2007-06-17 16:58:38 +02006704 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6705 /* ensure the indexes are not affected */
6706 return 0;
6707
6708 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6709}
6710
6711static int
6712acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6713 struct acl_expr *expr, struct acl_test *test)
6714{
6715 struct http_txn *txn = l7;
6716
Willy Tarreaub6866442008-07-14 23:54:42 +02006717 if (!txn)
6718 return 0;
6719
Willy Tarreauc11416f2007-06-17 16:58:38 +02006720 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6721 return 0;
6722
6723 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6724}
6725
Willy Tarreau737b0c12007-06-10 21:28:46 +02006726/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6727 * the first '/' after the possible hostname, and ends before the possible '?'.
6728 */
6729static int
6730acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6731 struct acl_expr *expr, struct acl_test *test)
6732{
6733 struct http_txn *txn = l7;
6734 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006735
Willy Tarreaub6866442008-07-14 23:54:42 +02006736 if (!txn)
6737 return 0;
6738
Willy Tarreauc11416f2007-06-17 16:58:38 +02006739 if (txn->req.msg_state != HTTP_MSG_BODY)
6740 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006741
Willy Tarreauc11416f2007-06-17 16:58:38 +02006742 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6743 /* ensure the indexes are not affected */
6744 return 0;
6745
Willy Tarreau21d2af32008-02-14 20:25:24 +01006746 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6747 ptr = http_get_path(txn);
6748 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006749 return 0;
6750
6751 /* OK, we got the '/' ! */
6752 test->ptr = ptr;
6753
6754 while (ptr < end && *ptr != '?')
6755 ptr++;
6756
6757 test->len = ptr - test->ptr;
6758
6759 /* we do not need to set READ_ONLY because the data is in a buffer */
6760 test->flags = ACL_TEST_F_VOL_1ST;
6761 return 1;
6762}
6763
6764
Willy Tarreau8797c062007-05-07 00:55:35 +02006765
6766/************************************************************************/
6767/* All supported keywords must be declared here. */
6768/************************************************************************/
6769
6770/* Note: must not be declared <const> as its list will be overwritten */
6771static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006772 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6773 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6774 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6775 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006776
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006777 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6778 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6779 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6780 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6781 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6782 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6783 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6784 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6785 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006786
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006787 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6788 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6789 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6790 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6791 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6792 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6793 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6794 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6795 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6796 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006797
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006798 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6799 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6800 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6801 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6802 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6803 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6804 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6805 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6806 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006807
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006808 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6809 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6810 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6811 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6812 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6813 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6814 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006815
Willy Tarreauf3d25982007-05-08 22:45:09 +02006816 { NULL, NULL, NULL, NULL },
6817
6818#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006819 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6820 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6821 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6822 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6823 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6824 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6825 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6826
Willy Tarreau8797c062007-05-07 00:55:35 +02006827 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6828 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6829 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6830 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6831 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6832 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6833 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6834 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6835
6836 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6837 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6838 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6839 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6840 { NULL, NULL, NULL, NULL },
6841#endif
6842}};
6843
6844
6845__attribute__((constructor))
6846static void __http_protocol_init(void)
6847{
6848 acl_register_keywords(&acl_kws);
6849}
6850
6851
Willy Tarreau58f10d72006-12-04 02:26:12 +01006852/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006853 * Local variables:
6854 * c-indent-level: 8
6855 * c-basic-offset: 8
6856 * End:
6857 */