blob: f4581226b5f0c563ed748d700fd7356ac1036371 [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);
Willy Tarreauf8533202008-08-16 14:55:08 +02003250 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003251 } else {
3252 fd_delete(t->cli_fd);
3253 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003254 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003255 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003256 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003257 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003258 /* read timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003259 else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003260 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003261 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003262 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003263 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264 } else {
3265 /* output was already closed */
3266 fd_delete(t->cli_fd);
3267 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003268 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003270 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003271 if (!(t->flags & SN_ERR_MASK))
3272 t->flags |= SN_ERR_CLITO;
3273 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003274 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003275 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003276 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003277 t->flags |= SN_FINST_C;
3278 else
3279 t->flags |= SN_FINST_D;
3280 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003281 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003282 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003283 }
3284 /* write timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003285 else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003286 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003287 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003288 EV_FD_CLR(t->cli_fd, DIR_WR);
3289 shutdown(t->cli_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003290 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003291 } else {
3292 fd_delete(t->cli_fd);
3293 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003294 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003295 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003296 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003297 if (!(t->flags & SN_ERR_MASK))
3298 t->flags |= SN_ERR_CLITO;
3299 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003300 if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003301 t->flags |= SN_FINST_Q;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003302 else if (req->cons->err_type <= SI_ET_CONN_OTHER)
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003303 t->flags |= SN_FINST_C;
3304 else
3305 t->flags |= SN_FINST_D;
3306 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003307 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003308 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003309 }
3310
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003311 update_timeouts:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003312 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003313 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003314 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 /* no room to read more data */
3316 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3317 /* stop reading until we get some space */
3318 req->rex = TICK_ETERNITY;
3319 }
3320 } else {
3321 EV_FD_COND_S(t->cli_fd, DIR_RD);
3322 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3323 }
3324 }
3325
3326 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003327 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003328 /* first, we may have to produce data (eg: stats).
3329 * right now, this is limited to the SHUTR state.
3330 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003331 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003332 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003333 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003334 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003335 fd_delete(t->cli_fd);
3336 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003337 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003338 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003340 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003341
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003342 /* 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 +02003343 if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003344 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3345 /* stop writing */
3346 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003348 } else {
3349 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003350 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003351 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003352 /* restart writing */
3353 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003354 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003355 /* FIXME: to prevent the client from expiring read timeouts during writes,
3356 * we refresh it, except if it was already infinite. */
3357 req->rex = rep->wex;
3358 }
3359 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003360 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003361 }
3362 return 0; /* other cases change nothing */
3363 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003364 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003365 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3366 int len;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003367 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 +02003368 write(1, trash, len);
3369 }
3370 return 0;
3371 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003372#ifdef DEBUG_DEV
3373 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3374 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003375#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003376 return 0;
3377}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003378
Willy Tarreaubaaee002006-06-26 02:48:02 +02003379
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003380/* Return 1 if the pending connection has failed and should be retried,
3381 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3382 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003383 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003384int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003385{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003386 struct buffer *req = t->req;
3387 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003388 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003389
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003390 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3391 now_ms, __FUNCTION__,
3392 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003393 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003394 req->flags, rep->flags,
3395 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003396
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003397 if ((req->flags & BF_SHUTW_NOW) ||
3398 (rep->flags & BF_SHUTW) ||
3399 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3400 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
3401 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3402
3403 trace_term(t, TT_HTTP_SRV_5);
3404 req->wex = TICK_ETERNITY;
3405 fd_delete(req->cons->fd);
3406 if (t->srv) {
3407 t->srv->cur_sess--;
3408 sess_change_server(t, NULL);
3409 }
3410 /* note that this must not return any error because it would be able to
3411 * overwrite the client_retnclose() output.
3412 */
3413 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3414
3415 // FIXME: should we set rep->MAY_FORWARD ?
3416 buffer_shutw(req);
3417 buffer_shutr(rep);
3418 req->cons->state = SI_ST_CLO;
3419 if (!req->cons->err_type)
3420 req->cons->err_type = SI_ET_CONN_ABRT;
3421 req->cons->err_loc = t->srv;
3422 return 0;
3423 }
3424
3425 /* check for timeouts and asynchronous connect errors */
3426 if (fdtab[req->cons->fd].state == FD_STERROR) {
3427 conn_err = SI_ET_CONN_ERR;
3428 if (!req->cons->err_type)
3429 req->cons->err_type = SI_ET_CONN_ERR;
3430 }
3431 else if (!(req->flags & BF_WRITE_STATUS)) {
3432 /* nothing happened, maybe we timed out */
3433 if (tick_is_expired(req->wex, now_ms)) {
3434 conn_err = SI_ET_CONN_TO;
3435 if (!req->cons->err_type)
3436 req->cons->err_type = SI_ET_CONN_TO;
3437 }
3438 else
3439 return 0; /* let's wait a bit more */
3440 }
3441
3442 if (conn_err) {
3443 fd_delete(req->cons->fd);
3444 req->cons->state = SI_ST_CLO;
3445
3446 if (t->srv) {
3447 t->srv->cur_sess--;
3448 sess_change_server(t, NULL);
3449 req->cons->err_loc = t->srv;
3450 }
3451
3452 /* ensure that we have enough retries left */
3453 if (srv_count_retry_down(t, conn_err))
3454 return 0;
3455
3456 if (conn_err == SI_ET_CONN_ERR) {
3457 /* we encountered an immediate connection error, and we
3458 * will have to retry connecting to the same server, most
3459 * likely leading to the same result. To avoid this, we
3460 * fake a connection timeout to retry after a turn-around
3461 * time of 1 second. We will wait in the previous if block.
3462 */
3463 req->cons->state = SI_ST_TAR;
3464 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3465 return 0;
3466 }
3467
3468 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3469 /* We're on our last chance, and the REDISP option was specified.
3470 * We will ignore cookie and force to balance or use the dispatcher.
3471 */
3472 /* let's try to offer this slot to anybody */
3473 if (may_dequeue_tasks(t->srv, t->be))
3474 process_srv_queue(t->srv);
3475
3476 /* it's left to the dispatcher to choose a server */
3477 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3478 t->prev_srv = t->srv;
3479 } else {
3480 /* we just want to retry */
3481 if (t->srv)
3482 t->srv->retries++;
3483 t->be->retries++;
3484
3485 /* Now we will try to either reconnect to the same server or
3486 * connect to another server. If the connection gets queued
3487 * because all servers are saturated, then we will go back to
3488 * the idle state where the buffer's consumer is marked as
3489 * unknown.
3490 */
3491 if (srv_retryable_connect(t)) {
3492 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003493 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003494 return 0;
3495 }
3496 }
3497
3498 /* We'll rely on the caller to try to get a connection again */
3499 return 1;
3500 }
3501 else {
3502 /* no error and write OK : connection succeeded */
3503 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3504 req->cons->state = SI_ST_EST;
3505 req->cons->err_type = SI_ET_NONE;
3506 req->cons->err_loc = NULL;
3507
3508 if (req->flags & BF_EMPTY) {
3509 EV_FD_CLR(req->cons->fd, DIR_WR);
3510 req->wex = TICK_ETERNITY;
3511 } else {
3512 EV_FD_SET(req->cons->fd, DIR_WR);
3513 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3514 if (tick_isset(req->wex)) {
3515 /* FIXME: to prevent the server from expiring read timeouts during writes,
3516 * we refresh it. */
3517 rep->rex = req->wex;
3518 }
3519 }
3520
3521 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3522 if (!(rep->flags & BF_HIJACK)) {
3523 EV_FD_SET(req->cons->fd, DIR_RD);
3524 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3525 }
3526 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3527
3528 /* if the user wants to log as soon as possible, without counting
3529 bytes from the server, then this is the right moment. */
3530 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3531 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3532 tcp_sess_log(t);
3533 }
3534#ifdef CONFIG_HAP_TCPSPLICE
3535 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3536 /* TCP splicing supported by both FE and BE */
3537 tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
3538 }
3539#endif
3540 }
3541 else {
3542 rep->analysers |= AN_RTR_HTTP_HDR;
3543 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3544 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3545 /* reset hdr_idx which was already initialized by the request.
3546 * right now, the http parser does it.
3547 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003548 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003549 }
3550
3551 if (!rep->analysers)
3552 t->rep->flags |= BF_MAY_FORWARD;
3553 req->wex = TICK_ETERNITY;
3554 return 0;
3555 }
3556}
3557
3558
3559/*
3560 * This function tries to assign a server to a stream_sock interface.
3561 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3562 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3563 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3564 * it returns SI_ST_ASS, otherwise zero.
3565 */
3566int stream_sock_assign_server(struct session *t)
3567{
3568 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3569 now_ms, __FUNCTION__,
3570 cli_stnames[t->cli_state],
3571 t->rep->rex, t->req->wex,
3572 t->req->flags, t->rep->flags,
3573 t->req->l, t->rep->l);
3574
3575 if (t->req->cons->state == SI_ST_TAR) {
3576 /* connection might be aborted */
3577 if ((t->req->flags & BF_SHUTW_NOW) ||
3578 (t->rep->flags & BF_SHUTW) ||
3579 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3580 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003581
Willy Tarreauf8533202008-08-16 14:55:08 +02003582 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003583 t->req->wex = TICK_ETERNITY;
3584
3585 // FIXME: should we set rep->MAY_FORWARD ?
3586 buffer_shutr(t->rep);
3587 buffer_shutw(t->req);
3588 if (!t->req->cons->err_type)
3589 t->req->cons->err_type = SI_ET_CONN_ABRT;
3590 t->req->cons->state = SI_ST_CLO;
3591 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003592 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003593
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003594 if (!tick_is_expired(t->req->wex, now_ms))
3595 return 0; /* still in turn-around */
3596
3597 t->req->cons->state = SI_ST_INI;
3598 }
3599 else if (t->req->cons->state == SI_ST_QUE) {
3600 if (t->pend_pos) {
3601 /* request still in queue... */
3602 if (tick_is_expired(t->req->wex, now_ms)) {
3603 /* ... and timeout expired */
3604 trace_term(t, TT_HTTP_SRV_3);
3605 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003606 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003607 if (t->srv)
3608 t->srv->failed_conns++;
3609 t->be->failed_conns++;
3610
3611 // FIXME: should we set rep->MAY_FORWARD ?
3612 buffer_shutr(t->rep);
3613 buffer_shutw(t->req);
3614 t->req->flags |= BF_WRITE_TIMEOUT;
3615 if (!t->req->cons->err_type)
3616 t->req->cons->err_type = SI_ET_QUEUE_TO;
3617 t->req->cons->state = SI_ST_CLO;
3618 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003619 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003620 /* connection remains in queue, check if we have to abort it */
3621 if ((t->req->flags & BF_SHUTW_NOW) ||
3622 (t->rep->flags & BF_SHUTW) ||
3623 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3624 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3625 /* give up */
3626 trace_term(t, TT_HTTP_SRV_1);
3627 t->req->wex = TICK_ETERNITY;
3628 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003630 // FIXME: should we set rep->MAY_FORWARD ?
3631 buffer_shutr(t->rep);
3632 buffer_shutw(t->req);
3633 if (!t->req->cons->err_type)
3634 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3635 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003636 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003637 return 0;
3638 }
3639 /* The connection is not in the queue anymore */
3640 t->req->cons->state = SI_ST_INI;
3641 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003642
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003643 /* we may get here from above */
3644 if (t->req->cons->state == SI_ST_INI) {
3645 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003646
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003647 /* first, check if the connection has been aborted */
3648 if ((t->req->flags & BF_SHUTW_NOW) ||
3649 (t->rep->flags & BF_SHUTW) ||
3650 ((t->req->flags & BF_SHUTR) &&
3651 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003652
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003653 trace_term(t, TT_HTTP_SRV_1);
3654 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003655
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003656 // FIXME: should we set rep->MAY_FORWARD ?
3657 buffer_shutr(t->rep);
3658 buffer_shutw(t->req);
3659 if (!t->req->cons->err_type)
3660 t->req->cons->err_type = SI_ET_CONN_ABRT;
3661 t->req->cons->state = SI_ST_CLO;
3662 return 0;
3663 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003664
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003665 /* try to get a server assigned */
3666 if (srv_redispatch_connect(t) != 0) {
3667 /* we did not get any server, let's check the cause */
3668 if (t->req->cons->state == SI_ST_QUE) {
3669 /* the connection was queued, that's OK */
3670 return 0;
3671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003672
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003673 trace_term(t, TT_HTTP_SRV_2);
3674 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003675
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003676 // FIXME: should we set rep->MAY_FORWARD ?
3677 buffer_shutr(t->rep);
3678 buffer_shutw(t->req);
3679 t->req->flags |= BF_WRITE_ERROR;
3680 if (!t->req->cons->err_type)
3681 t->req->cons->err_type = SI_ET_CONN_OTHER;
3682 t->req->cons->state = SI_ST_CLO;
3683 return 0;
3684 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003685
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003686 t->req->cons->state = SI_ST_ASS;
3687 /* Once the server is assigned, we have to return because
3688 * the caller might be interested in checking several
3689 * things before connecting.
3690 */
3691 return 1;
3692 }
3693 return 0;
3694}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003695
Willy Tarreauf8533202008-08-16 14:55:08 +02003696
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003697/*
3698 * This function tries to establish a connection to an assigned server. It also
3699 * performs connection retries. It may only be called with t->req->cons->state
3700 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3701 * SI_ST_EST, or SI_ST_CLO.
3702 */
3703int stream_sock_connect_server(struct session *t)
3704{
3705 if (t->req->cons->state == SI_ST_ASS) {
3706 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003707
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003708 if (!srv_retryable_connect(t)) {
3709 /* we need to redispatch */
3710 t->req->cons->state = SI_ST_INI;
3711 return 0;
3712 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003713
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003714 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3715 if (t->req->cons->state != SI_ST_CON) {
3716 /* it was an error */
3717 trace_term(t, TT_HTTP_SRV_4);
3718 t->req->wex = TICK_ETERNITY;
3719
3720 // FIXME: should we set rep->MAY_FORWARD ?
3721 buffer_shutr(t->rep);
3722 buffer_shutw(t->req);
3723 t->req->flags |= BF_WRITE_ERROR;
3724 if (!t->req->cons->err_type)
3725 t->req->cons->err_type = SI_ET_CONN_OTHER;
3726 t->req->cons->state = SI_ST_CLO;
3727 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003728 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003729 /* We have a socket and switched to SI_ST_CON */
3730 }
3731
3732 /* we may also get here from above */
3733 if (t->req->cons->state == SI_ST_CON) {
3734 /* connection in progress or just completed */
3735 if (!tcp_connection_status(t))
3736 return 0;
3737 }
3738 return 0;
3739}
3740
3741
3742/*
3743 * Tries to establish a connection to the server and associate it to the
3744 * request buffer's consumer side. It is assumed that this function will not be
3745 * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally
3746 * returns zero, but may return 1 if it absolutely wants to be called again.
3747 */
3748int process_srv_conn(struct session *t)
3749{
3750 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3751 now_ms, __FUNCTION__,
3752 cli_stnames[t->cli_state],
3753 t->rep->rex, t->req->wex,
3754 t->req->flags, t->rep->flags,
3755 t->req->l, t->rep->l);
3756
3757 do {
3758 if (t->req->cons->state == SI_ST_INI ||
3759 t->req->cons->state == SI_ST_TAR ||
3760 t->req->cons->state == SI_ST_QUE) {
3761 /* try to assign a server */
3762 if (!stream_sock_assign_server(t))
3763 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003764 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003765
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003766 if (t->req->cons->state == SI_ST_ASS &&
3767 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3768 /* Server supporting redirection and it is possible.
3769 * Invalid requests are reported as such. It concerns all
3770 * the largest ones.
3771 */
3772 struct http_txn *txn = &t->txn;
3773 struct chunk rdr;
3774 char *path;
3775 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003776
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003777 /* 1: create the response header */
3778 rdr.len = strlen(HTTP_302);
3779 rdr.str = trash;
3780 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003782 /* 2: add the server's prefix */
3783 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3784 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003786 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3787 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003788
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003789 /* 3: add the request URI */
3790 path = http_get_path(txn);
3791 if (!path)
3792 goto cancel_redir;
3793 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3794 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3795 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003796
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003797 memcpy(rdr.str + rdr.len, path, len);
3798 rdr.len += len;
3799 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3800 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003801
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003802 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3803 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003804
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003805 /* FIXME: we should increase a counter of redirects per server and per backend. */
3806 if (t->srv)
3807 t->srv->cum_sess++;
3808
3809 t->req->cons->state = SI_ST_CLO;
3810 return 0;
3811 cancel_redir:
3812 //txn->status = 400;
3813 //t->fe->failed_req++;
3814 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3815 // 400, error_message(t, HTTP_ERR_400));
3816 trace_term(t, TT_HTTP_SRV_4);
3817
3818 // FIXME: should we set rep->MAY_FORWARD ?
3819 buffer_shutw(t->req);
3820 buffer_shutr(t->rep);
3821 if (!t->req->cons->err_type)
3822 t->req->cons->err_type = SI_ET_CONN_OTHER;
3823 t->req->cons->state = SI_ST_CLO;
3824 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003825 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003826
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003827 if (t->req->cons->state == SI_ST_CON ||
3828 t->req->cons->state == SI_ST_ASS) {
3829 stream_sock_connect_server(t);
3830 }
3831 } while (t->req->cons->state != SI_ST_CLO &&
3832 t->req->cons->state != SI_ST_CON &&
3833 t->req->cons->state != SI_ST_EST);
3834 return 0;
3835}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003836
Willy Tarreaubaaee002006-06-26 02:48:02 +02003837
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003838/*
3839 * Manages the server FSM and its socket during the DATA phase. It must not be
3840 * called when a file descriptor is not attached to the buffer. It must only be
3841 * called during SI_ST_EST. It normally returns zero, but may return 1 if it
3842 * absolutely wants to be called again.
3843 */
3844int process_srv_data(struct session *t)
3845{
3846 struct buffer *req = t->req;
3847 struct buffer *rep = t->rep;
3848 int fd = req->cons->fd;
Willy Tarreaudafde432008-08-17 01:00:46 +02003849
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003850 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3851 now_ms, __FUNCTION__,
3852 cli_stnames[t->cli_state],
3853 rep->rex, req->wex,
3854 req->flags, rep->flags,
3855 req->l, rep->l);
3856
3857 if (req->flags & (BF_WRITE_ERROR | BF_WRITE_TIMEOUT) ||
3858 rep->flags & (BF_READ_ERROR | BF_READ_TIMEOUT)) {
3859 /* nothing more to be done here */
3860 fprintf(stderr, "Hey what are you doing there? t=%p fd=%d state=%d\n",
3861 t, t->req->cons->fd, t->req->cons->state);
3862 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003863 }
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003864
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003865 /* we can skip most of the tests at once if some conditions are not met */
3866 /* FIXME: place req->BF_SHUTW_NOW here */
3867 //if (!((fdtab[fd].state == FD_STERROR) ||
3868 // (!(req->flags & BF_SHUTW) &&
3869 // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
3870 // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3871 // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW))))
3872 // goto update_timeouts;
3873
3874 /* read or write error */
3875 /* FIXME: what happens when we have to deal with HTTP ??? */
3876 if (fdtab[fd].state == FD_STERROR) {
3877 trace_term(t, TT_HTTP_SRV_6);
3878 buffer_shutw(req);
3879 req->flags |= BF_WRITE_ERROR;
3880 buffer_shutr(rep);
3881 rep->flags |= BF_READ_ERROR;
3882 fd_delete(fd);
3883 req->cons->state = SI_ST_CLO;
3884 if (t->srv) {
3885 t->srv->cur_sess--;
3886 //t->srv->failed_resp++;
3887 //FIXME: si on ne traite pas l'erreur ici, le serveur est perdu et on ne la comptabilisera plus ensuite.
3888 //il va donc falloir stocker l'info du dernier serveur en erreur pour que les couches du dessus traitent.
3889 sess_change_server(t, NULL);
3890 }
3891 //t->be->failed_resp++;
3892 //if (!rep->analysers) {
3893 // if (!(t->flags & SN_ERR_MASK))
3894 // t->flags |= SN_ERR_SRVCL;
3895 // if (!(t->flags & SN_FINST_MASK))
3896 // t->flags |= SN_FINST_D;
3897 //}
3898 if (may_dequeue_tasks(t->srv, t->be))
3899 process_srv_queue(t->srv);
3900
3901 return 0;
3902 }
3903
3904 /* last read, or end of client write */
3905 if (!(rep->flags & BF_SHUTR) && /* not already done */
3906 rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3907 buffer_shutr(rep);
3908 if (!(req->flags & BF_SHUTW)) {
3909 EV_FD_CLR(fd, DIR_RD);
3910 trace_term(t, TT_HTTP_SRV_7);
3911 } else {
3912 /* output was already closed */
3913 trace_term(t, TT_HTTP_SRV_8);
3914 fd_delete(fd);
3915 req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003916 if (t->srv) {
3917 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003918 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003919 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003920
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003921 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003922 process_srv_queue(t->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003923 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003924 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003925 }
3926 /* end of client read and no more data to send. We can forward
3927 * the close when we're allowed to forward data (anytime right
3928 * now). If we're using option forceclose, then we may also
3929 * shutdown the outgoing write channel once the response starts
3930 * coming from the server.
3931 */
Willy Tarreau461f6622008-08-15 23:43:19 +02003932
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003933 // FIXME: option FORCE_CLOSE should move to upper layer.
3934 if (!(req->flags & BF_SHUTW) && /* not already done */
3935 (req->flags & BF_SHUTW_NOW ||
3936 (req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
3937 (req->flags & BF_SHUTR ||
3938 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))))) {
3939 buffer_shutw(req);
3940 if (!(rep->flags & BF_SHUTR)) {
3941 trace_term(t, TT_HTTP_SRV_9);
3942 EV_FD_CLR(fd, DIR_WR);
3943 shutdown(fd, SHUT_WR);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003944 } else {
3945 trace_term(t, TT_HTTP_SRV_10);
3946 fd_delete(fd);
3947 req->cons->state = SI_ST_CLO;
3948 if (t->srv) {
3949 t->srv->cur_sess--;
3950 sess_change_server(t, NULL);
Willy Tarreau461f6622008-08-15 23:43:19 +02003951 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003952
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003953 if (may_dequeue_tasks(t->srv, t->be))
3954 process_srv_queue(t->srv);
3955 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003956 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003957 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003958
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003959 /* read timeout */
3960 if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == 0 &&
3961 tick_is_expired(rep->rex, now_ms)) {
3962 rep->flags |= BF_READ_TIMEOUT;
3963 //if (!rep->analysers) {
3964 // if (!(t->flags & SN_ERR_MASK))
3965 // t->flags |= SN_ERR_SRVTO;
3966 // if (!(t->flags & SN_FINST_MASK))
3967 // t->flags |= SN_FINST_D;
3968 //}
3969 buffer_shutr(rep);
3970 if (!(req->flags & BF_SHUTW)) {
3971 trace_term(t, TT_HTTP_SRV_11);
3972 EV_FD_CLR(fd, DIR_RD);
3973 } else {
3974 trace_term(t, TT_HTTP_SRV_12);
3975 fd_delete(fd);
3976 req->cons->state = SI_ST_CLO;
3977 if (t->srv) {
3978 t->srv->cur_sess--;
3979 sess_change_server(t, NULL);
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003980 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003981
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003982 if (may_dequeue_tasks(t->srv, t->be))
3983 process_srv_queue(t->srv);
3984 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003985 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003987
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003988 /* write timeout */
3989 if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == 0 &&
3990 tick_is_expired(req->wex, now_ms)) {
3991 req->flags |= BF_WRITE_TIMEOUT;
3992 //if (!rep->analysers) {
3993 // if (!(t->flags & SN_ERR_MASK))
3994 // t->flags |= SN_ERR_SRVTO;
3995 // if (!(t->flags & SN_FINST_MASK))
3996 // t->flags |= SN_FINST_D;
3997 //}
3998 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003999 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004000 trace_term(t, TT_HTTP_SRV_13);
4001 EV_FD_CLR(fd, DIR_WR);
4002 shutdown(fd, SHUT_WR);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004003 } else {
4004 trace_term(t, TT_HTTP_SRV_14);
4005 fd_delete(fd);
4006 req->cons->state = SI_ST_CLO;
4007 if (t->srv) {
4008 t->srv->cur_sess--;
4009 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01004010 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004011
4012 if (may_dequeue_tasks(t->srv, t->be))
4013 process_srv_queue(t->srv);
4014 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004015 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004016 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004017
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004018 update_timeouts:
4019 /* manage read timeout */
4020 if (!(rep->flags & BF_SHUTR)) {
4021 if (rep->flags & (BF_FULL|BF_HIJACK)) {
4022 if (EV_FD_COND_C(fd, DIR_RD))
4023 rep->rex = TICK_ETERNITY;
4024 } else {
4025 EV_FD_COND_S(fd, DIR_RD);
4026 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004027 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004028 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004029
4030 /* manage write timeout */
4031 if (!(req->flags & BF_SHUTW)) {
4032 if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
4033 /* stop writing */
4034 if (EV_FD_COND_C(fd, DIR_WR))
4035 req->wex = TICK_ETERNITY;
4036 } else {
4037 /* buffer not empty, there are still data to be transferred */
4038 EV_FD_COND_S(fd, DIR_WR);
4039 if (!tick_isset(req->wex)) {
4040 /* restart writing */
4041 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4042 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4043 /* FIXME: to prevent the server from expiring read timeouts during writes,
4044 * we refresh it, except if it was already infinite.
4045 */
4046 rep->rex = req->wex;
4047 }
4048 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004049 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004050 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004051 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02004052}
4053
4054
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004055///*
4056// * Manages the client FSM and its socket. It normally returns zero, but may
4057// * return 1 if it absolutely wants to be called again.
4058// *
4059// * Note: process_cli is the ONLY function allowed to set cli_state to anything
4060// * but CL_STCLOSE.
4061// */
4062//int process_cli(struct session *t)
4063//{
4064// struct buffer *req = t->req;
4065// struct buffer *rep = t->rep;
4066//
4067// 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",
4068// now_ms, __FUNCTION__,
4069// cli_stnames[t->cli_state],
4070// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
4071// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
4072// req->rex, rep->wex,
4073// req->flags, rep->flags,
4074// req->l, rep->l);
4075//
4076// update_state:
4077// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
4078// * still set this state (and will do until unix sockets are converted).
4079// */
4080// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
4081// /* we can skip most of the tests at once if some conditions are not met */
4082// if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4083// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4084// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
4085// (!(rep->flags & BF_SHUTW) &&
4086// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
4087// goto update_timeouts;
4088//
4089// /* read or write error */
4090// if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
4091// buffer_shutr(req);
4092// buffer_shutw(rep);
4093// fd_delete(t->cli_fd);
4094// t->cli_state = CL_STCLOSE;
4095// trace_term(t, TT_HTTP_CLI_1);
4096// if (!req->analysers) {
4097// if (!(t->flags & SN_ERR_MASK))
4098// t->flags |= SN_ERR_CLICL;
4099// if (!(t->flags & SN_FINST_MASK)) {
4100// if (t->pend_pos)
4101// t->flags |= SN_FINST_Q;
4102// else if (!(req->flags & BF_CONNECTED))
4103// t->flags |= SN_FINST_C;
4104// else
4105// t->flags |= SN_FINST_D;
4106// }
4107// }
4108// goto update_state;
4109// }
4110// /* last read, or end of server write */
4111// else if (!(req->flags & BF_SHUTR) && /* not already done */
4112// req->flags & (BF_READ_NULL | BF_SHUTW)) {
4113// buffer_shutr(req);
4114// if (!(rep->flags & BF_SHUTW)) {
4115// EV_FD_CLR(t->cli_fd, DIR_RD);
4116// trace_term(t, TT_HTTP_CLI_2);
4117// } else {
4118// /* output was already closed */
4119// fd_delete(t->cli_fd);
4120// t->cli_state = CL_STCLOSE;
4121// trace_term(t, TT_HTTP_CLI_3);
4122// }
4123// goto update_state;
4124// }
4125// /* last server read and buffer empty : we only check them when we're
4126// * allowed to forward the data.
4127// */
4128// else if (!(rep->flags & BF_SHUTW) && /* not already done */
4129// rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
4130// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
4131// buffer_shutw(rep);
4132// if (!(req->flags & BF_SHUTR)) {
4133// EV_FD_CLR(t->cli_fd, DIR_WR);
4134// shutdown(t->cli_fd, SHUT_WR);
4135// /* We must ensure that the read part is still alive when switching to shutw */
4136// /* FIXME: is this still true ? */
4137// EV_FD_SET(t->cli_fd, DIR_RD);
4138// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4139// trace_term(t, TT_HTTP_CLI_4);
4140// } else {
4141// fd_delete(t->cli_fd);
4142// t->cli_state = CL_STCLOSE;
4143// trace_term(t, TT_HTTP_CLI_5);
4144// }
4145// goto update_state;
4146// }
4147// /* read timeout */
4148// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4149// buffer_shutr(req);
4150// if (!(rep->flags & BF_SHUTW)) {
4151// EV_FD_CLR(t->cli_fd, DIR_RD);
4152// trace_term(t, TT_HTTP_CLI_6);
4153// } else {
4154// /* output was already closed */
4155// fd_delete(t->cli_fd);
4156// t->cli_state = CL_STCLOSE;
4157// trace_term(t, TT_HTTP_CLI_7);
4158// }
4159// if (!req->analysers) {
4160// if (!(t->flags & SN_ERR_MASK))
4161// t->flags |= SN_ERR_CLITO;
4162// if (!(t->flags & SN_FINST_MASK)) {
4163// if (t->pend_pos)
4164// t->flags |= SN_FINST_Q;
4165// else if (!(req->flags & BF_CONNECTED))
4166// t->flags |= SN_FINST_C;
4167// else
4168// t->flags |= SN_FINST_D;
4169// }
4170// }
4171// goto update_state;
4172// }
4173// /* write timeout */
4174// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4175// buffer_shutw(rep);
4176// if (!(req->flags & BF_SHUTR)) {
4177// EV_FD_CLR(t->cli_fd, DIR_WR);
4178// shutdown(t->cli_fd, SHUT_WR);
4179// /* We must ensure that the read part is still alive when switching to shutw */
4180// /* FIXME: is this still true ? */
4181// EV_FD_SET(t->cli_fd, DIR_RD);
4182// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4183// trace_term(t, TT_HTTP_CLI_8);
4184// } else {
4185// fd_delete(t->cli_fd);
4186// t->cli_state = CL_STCLOSE;
4187// trace_term(t, TT_HTTP_CLI_9);
4188// }
4189// if (!req->analysers) {
4190// if (!(t->flags & SN_ERR_MASK))
4191// t->flags |= SN_ERR_CLITO;
4192// if (!(t->flags & SN_FINST_MASK)) {
4193// if (t->pend_pos)
4194// t->flags |= SN_FINST_Q;
4195// else if (!(req->flags & BF_CONNECTED))
4196// t->flags |= SN_FINST_C;
4197// else
4198// t->flags |= SN_FINST_D;
4199// }
4200// }
4201// goto update_state;
4202// }
4203//
4204// update_timeouts:
4205// /* manage read timeout */
4206// if (!(req->flags & BF_SHUTR)) {
4207// if (req->flags & BF_FULL) {
4208// /* no room to read more data */
4209// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
4210// /* stop reading until we get some space */
4211// req->rex = TICK_ETERNITY;
4212// }
4213// } else {
4214// EV_FD_COND_S(t->cli_fd, DIR_RD);
4215// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
4216// }
4217// }
4218//
4219// /* manage write timeout */
4220// if (!(rep->flags & BF_SHUTW)) {
4221// /* first, we may have to produce data (eg: stats).
4222// * right now, this is limited to the SHUTR state.
4223// */
4224// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
4225// produce_content(t);
4226// if (rep->flags & BF_EMPTY) {
4227// buffer_shutw(rep);
4228// fd_delete(t->cli_fd);
4229// t->cli_state = CL_STCLOSE;
4230// trace_term(t, TT_HTTP_CLI_10);
4231// goto update_state;
4232// }
4233// }
4234//
4235// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
4236// if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
4237// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
4238// /* stop writing */
4239// rep->wex = TICK_ETERNITY;
4240// }
4241// } else {
4242// /* buffer not empty */
4243// EV_FD_COND_S(t->cli_fd, DIR_WR);
4244// if (!tick_isset(rep->wex)) {
4245// /* restart writing */
4246// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
4247// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
4248// /* FIXME: to prevent the client from expiring read timeouts during writes,
4249// * we refresh it, except if it was already infinite. */
4250// req->rex = rep->wex;
4251// }
4252// }
4253// }
4254// }
4255// return 0; /* other cases change nothing */
4256// }
4257// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
4258// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
4259// int len;
4260// 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);
4261// write(1, trash, len);
4262// }
4263// return 0;
4264// }
4265//#ifdef DEBUG_DEV
4266// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
4267// ABORT_NOW();
4268//#endif
4269// return 0;
4270//}
4271//
4272//
4273///* Return 1 if we could get a new connection for session t, otherwise zero */
4274//int tcp_get_connection(struct session *t)
4275//{
4276// struct http_txn *txn = &t->txn;
4277// struct buffer *req = t->req;
4278// struct buffer *rep = t->rep;
4279//
4280// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4281// now_ms, __FUNCTION__,
4282// cli_stnames[t->cli_state],
4283// rep->rex, req->wex,
4284// req->flags, rep->flags,
4285// req->l, rep->l);
4286//
4287//
4288// if ((rep->flags & BF_SHUTW) ||
4289// ((req->flags & BF_SHUTR) &&
4290// (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4291// req->wex = TICK_ETERNITY;
4292// if (t->pend_pos)
4293// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4294// /* note that this must not return any error because it would be able to
4295// * overwrite the client_retnclose() output.
4296// */
4297// if (txn->flags & TX_CLTARPIT)
4298// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
4299// else
4300// srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
4301//
4302// trace_term(t, TT_HTTP_SRV_1);
4303// return 0;
4304// }
4305//
4306// /* stop here if we're not allowed to connect */
4307// if (!(req->flags & BF_MAY_FORWARD))
4308// return 0;
4309//
4310// /* the client allows the server to connect */
4311// if (txn->flags & TX_CLTARPIT) {
4312// /* This connection is being tarpitted. The CLIENT side has
4313// * already set the connect expiration date to the right
4314// * timeout. We just have to check that it has not expired.
4315// */
4316// if (!(req->flags & BF_WRITE_TIMEOUT))
4317// return 0;
4318//
4319// /* We will set the queue timer to the time spent, just for
4320// * logging purposes. We fake a 500 server error, so that the
4321// * attacker will not suspect his connection has been tarpitted.
4322// * It will not cause trouble to the logs because we can exclude
4323// * the tarpitted connections by filtering on the 'PT' status flags.
4324// */
4325// req->wex = TICK_ETERNITY;
4326// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4327// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
4328// 500, error_message(t, HTTP_ERR_500));
4329// trace_term(t, TT_HTTP_SRV_2);
4330// return 0;
4331// }
4332//
4333// /* Right now, we will need to create a connection to the server.
4334// * We might already have tried, and got a connection pending, in
4335// * which case we will not do anything till it's pending. It's up
4336// * to any other session to release it and wake us up again.
4337// */
4338// if (t->pend_pos) {
4339// if (!(req->flags & BF_WRITE_TIMEOUT)) {
4340// return 0;
4341// } else {
4342// /* we've been waiting too long here */
4343// req->wex = TICK_ETERNITY;
4344// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4345// srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
4346// 503, error_message(t, HTTP_ERR_503));
4347// trace_term(t, TT_HTTP_SRV_3);
4348// if (t->srv)
4349// t->srv->failed_conns++;
4350// t->be->failed_conns++;
4351// return 0;
4352// }
4353// }
4354//
4355// do {
4356// if (srv_redispatch_connect(t) != 0)
4357// return 0;
4358//
4359// if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
4360// /* Server supporting redirection and it is possible.
4361// * Invalid requests are reported as such. It concerns all
4362// * the largest ones.
4363// */
4364// struct chunk rdr;
4365// char *path;
4366// int len;
4367//
4368// /* 1: create the response header */
4369// rdr.len = strlen(HTTP_302);
4370// rdr.str = trash;
4371// memcpy(rdr.str, HTTP_302, rdr.len);
4372//
4373// /* 2: add the server's prefix */
4374// if (rdr.len + t->srv->rdr_len > sizeof(trash))
4375// goto cancel_redir;
4376//
4377// memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
4378// rdr.len += t->srv->rdr_len;
4379//
4380// /* 3: add the request URI */
4381// path = http_get_path(txn);
4382// if (!path)
4383// goto cancel_redir;
4384// len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
4385// if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
4386// goto cancel_redir;
4387//
4388// memcpy(rdr.str + rdr.len, path, len);
4389// rdr.len += len;
4390// memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
4391// rdr.len += 4;
4392//
4393// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
4394// trace_term(t, TT_HTTP_SRV_3);
4395//
4396// /* FIXME: we should increase a counter of redirects per server and per backend. */
4397// if (t->srv)
4398// t->srv->cum_sess++;
4399// return 0;
4400// cancel_redir:
4401// txn->status = 400;
4402// t->fe->failed_req++;
4403// srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
4404// 400, error_message(t, HTTP_ERR_400));
4405// trace_term(t, TT_HTTP_SRV_4);
4406// return 0;
4407// }
4408//
4409// /* try to (re-)connect to the server, and fail if we expire the
4410// * number of retries.
4411// */
4412// if (srv_retryable_connect(t)) {
4413// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4414// if (!(req->cons.flags & BC_KNOWN))
4415// return 0;
4416// /* We got an FD */
4417// return 1;
4418// }
4419// } while (1);
4420//}
4421//
4422//
4423///* Return 1 if the pending connection has failed and should be retried,
4424// * otherwise zero.
4425// */
4426//int tcp_connection_failed(struct session *t)
4427//{
4428// struct buffer *req = t->req;
4429// struct buffer *rep = t->rep;
4430// int conn_err;
4431//
4432// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4433// now_ms, __FUNCTION__,
4434// cli_stnames[t->cli_state],
4435// rep->rex, req->wex,
4436// req->flags, rep->flags,
4437// req->l, rep->l);
4438//
4439// if ((rep->flags & BF_SHUTW) ||
4440// ((req->flags & BF_SHUTR) &&
4441// ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
4442// t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
4443// req->wex = TICK_ETERNITY;
4444// if (!(t->flags & SN_CONN_TAR)) {
4445// /* if we are in turn-around, we have already closed the FD */
4446// fd_delete(req->cons->fd);
4447// req->cons->state = SI_ST_CLO;
4448// if (t->srv) {
4449// t->srv->cur_sess--;
4450// sess_change_server(t, NULL);
4451// }
4452// }
4453//
4454// /* note that this must not return any error because it would be able to
4455// * overwrite the client_retnclose() output.
4456// */
4457// srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
4458// trace_term(t, TT_HTTP_SRV_5);
4459// return 0;
4460// }
4461//
4462// if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT)))
4463// return 0; /* nothing changed */
4464//
4465// if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
4466// /* timeout, asynchronous connect error or first write error */
4467// if (t->flags & SN_CONN_TAR) {
4468// /* We are doing a turn-around waiting for a new connection attempt. */
4469// if (!(req->flags & BF_WRITE_TIMEOUT))
4470// return 0;
4471// t->flags &= ~SN_CONN_TAR;
4472// }
4473// else {
4474// fd_delete(req->cons->fd);
4475// req->cons->state = SI_ST_CLO;
4476// if (t->srv) {
4477// t->srv->cur_sess--;
4478// sess_change_server(t, NULL);
4479// }
4480//
4481// if (!(req->flags & BF_WRITE_STATUS))
4482// conn_err = SN_ERR_SRVTO; // it was a connect timeout.
4483// else
4484// conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
4485//
4486// /* ensure that we have enough retries left */
4487// if (srv_count_retry_down(t, conn_err))
4488// return 0;
4489//
4490// if (req->flags & BF_WRITE_ERROR) {
4491// /* we encountered an immediate connection error, and we
4492// * will have to retry connecting to the same server, most
4493// * likely leading to the same result. To avoid this, we
4494// * fake a connection timeout to retry after a turn-around
4495// * time of 1 second. We will wait in the previous if block.
4496// */
4497// t->flags |= SN_CONN_TAR;
4498// req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
4499// return 0;
4500// }
4501// }
4502//
4503// if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
4504// /* We're on our last chance, and the REDISP option was specified.
4505// * We will ignore cookie and force to balance or use the dispatcher.
4506// */
4507// /* let's try to offer this slot to anybody */
4508// if (may_dequeue_tasks(t->srv, t->be))
4509// process_srv_queue(t->srv);
4510//
4511// /* it's left to the dispatcher to choose a server */
4512// t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
4513// t->prev_srv = t->srv;
4514//
4515// /* first, get a connection */
4516// if (srv_redispatch_connect(t)) {
4517// if (req->cons.flags & BC_KNOWN)
4518// return 0;
4519// /* we need to get a connection */
4520// return 1;
4521// }
4522// } else {
4523// if (t->srv)
4524// t->srv->retries++;
4525// t->be->retries++;
4526// }
4527//
4528// do {
4529// /* Now we will try to either reconnect to the same server or
4530// * connect to another server. If the connection gets queued
4531// * because all servers are saturated, then we will go back to
4532// * the idle state where the buffer's consumer is marked as
4533// * unknown.
4534// */
4535// if (srv_retryable_connect(t)) {
4536// t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
4537// if (req->cons.flags & BC_KNOWN)
4538// return 0;
4539// /* we did not get a connection */
4540// return 1;
4541// }
4542//
4543// /* we need to redispatch the connection to another server */
4544// if (srv_redispatch_connect(t)) {
4545// if (req->cons.flags & BC_KNOWN)
4546// return 0;
4547// /* we need to get a connection */
4548// return 1;
4549// }
4550// } while (1);
4551// }
4552// else { /* no error and write OK */
4553// t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
4554//
4555// if (req->flags & BF_EMPTY) {
4556// EV_FD_CLR(req->cons->fd, DIR_WR);
4557// req->wex = TICK_ETERNITY;
4558// } else {
4559// EV_FD_SET(req->cons->fd, DIR_WR);
4560// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4561// if (tick_isset(req->wex)) {
4562// /* FIXME: to prevent the server from expiring read timeouts during writes,
4563// * we refresh it. */
4564// rep->rex = req->wex;
4565// }
4566// }
4567//
4568// if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
4569// EV_FD_SET(req->cons->fd, DIR_RD);
4570// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4571// buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
4572//
4573// /* if the user wants to log as soon as possible, without counting
4574// bytes from the server, then this is the right moment. */
4575// if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4576// t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
4577// tcp_sess_log(t);
4578// }
4579//#ifdef CONFIG_HAP_TCPSPLICE
4580// if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
4581// /* TCP splicing supported by both FE and BE */
4582// tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0);
4583// }
4584//#endif
4585// }
4586// else {
4587// rep->analysers |= AN_RTR_HTTP_HDR;
4588// buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
4589// t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
4590// /* reset hdr_idx which was already initialized by the request.
4591// * right now, the http parser does it.
4592// * hdr_idx_init(&t->txn.hdr_idx);
4593// */
4594// }
4595//
4596// req->flags |= BF_CONNECTED;
4597// if (!rep->analysers)
4598// t->rep->flags |= BF_MAY_FORWARD;
4599// req->wex = TICK_ETERNITY;
4600// return 0;
4601// }
4602//}
4603//
4604//
4605///*
4606// * Tries to establish a connection to the server and associate it to the
4607// * request buffer's consumer side. It normally returns zero, but may return 1
4608// * if it absolutely wants to be called again.
4609// */
4610//int process_srv_conn(struct session *t)
4611//{
4612// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4613// now_ms, __FUNCTION__,
4614// cli_stnames[t->cli_state],
4615// t->rep->rex, t->req->wex,
4616// t->req->flags, t->rep->flags,
4617// t->req->l, t->rep->l);
4618//
4619// while (!(t->req->flags & BF_CONNECTED)) {
4620// if (!(t->req->cons.flags & BC_KNOWN)) {
4621// /* no connection in progress, get a new one */
4622// if (!tcp_get_connection(t))
4623// break;
4624// } else {
4625// /* connection in progress or just completed */
4626// if (!tcp_connection_failed(t))
4627// break;
4628// }
4629// }
4630// return 0;
4631//}
4632//
4633//
4634///*
4635// * Manages the server FSM and its socket during the DATA phase. It must not
4636// * be called when a file descriptor is not attached to the buffer. It normally
4637// * returns zero, but may return 1 if it absolutely wants to be called again.
4638// */
4639//int process_srv_data(struct session *t)
4640//{
4641// struct buffer *req = t->req;
4642// struct buffer *rep = t->rep;
4643//
4644// DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
4645// now_ms, __FUNCTION__,
4646// cli_stnames[t->cli_state],
4647// rep->rex, req->wex,
4648// req->flags, rep->flags,
4649// req->l, rep->l);
4650//
4651// /* we can skip most of the tests at once if some conditions are not met */
4652// if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
4653// (!(req->flags & BF_SHUTW) &&
4654// (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
4655// (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
4656// (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
4657// goto update_timeouts;
4658//
4659// /* read or write error */
4660// /* FIXME: what happens when we have to deal with HTTP ??? */
4661// if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
4662// buffer_shutr(rep);
4663// buffer_shutw(req);
4664// fd_delete(req->cons->fd);
4665// req->cons->state = SI_ST_CLO;
4666// if (t->srv) {
4667// t->srv->cur_sess--;
4668// t->srv->failed_resp++;
4669// sess_change_server(t, NULL);
4670// }
4671// t->be->failed_resp++;
4672// trace_term(t, TT_HTTP_SRV_6);
4673// if (!rep->analysers) {
4674// if (!(t->flags & SN_ERR_MASK))
4675// t->flags |= SN_ERR_SRVCL;
4676// if (!(t->flags & SN_FINST_MASK))
4677// t->flags |= SN_FINST_D;
4678// }
4679// if (may_dequeue_tasks(t->srv, t->be))
4680// process_srv_queue(t->srv);
4681//
4682// return 0;
4683// }
4684//
4685// /* last read, or end of client write */
4686// if (!(rep->flags & BF_SHUTR) && /* not already done */
4687// rep->flags & (BF_READ_NULL | BF_SHUTW)) {
4688// buffer_shutr(rep);
4689// if (!(req->flags & BF_SHUTW)) {
4690// EV_FD_CLR(req->cons->fd, DIR_RD);
4691// trace_term(t, TT_HTTP_SRV_7);
4692// } else {
4693// /* output was already closed */
4694// fd_delete(req->cons->fd);
4695// req->cons->state = SI_ST_CLO;
4696// if (t->srv) {
4697// t->srv->cur_sess--;
4698// sess_change_server(t, NULL);
4699// }
4700// trace_term(t, TT_HTTP_SRV_8);
4701//
4702// if (may_dequeue_tasks(t->srv, t->be))
4703// process_srv_queue(t->srv);
4704// return 0;
4705// }
4706// }
4707// /* end of client read and no more data to send. We can forward
4708// * the close when we're allowed to forward data (anytime right
4709// * now). If we're using option forceclose, then we may also
4710// * shutdown the outgoing write channel once the response starts
4711// * coming from the server.
4712// */
4713// if (!(req->flags & BF_SHUTW) && /* not already done */
4714// req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
4715// (req->flags & BF_SHUTR ||
4716// (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
4717// buffer_shutw(req);
4718// if (!(rep->flags & BF_SHUTR)) {
4719// EV_FD_CLR(req->cons->fd, DIR_WR);
4720// shutdown(req->cons->fd, SHUT_WR);
4721// trace_term(t, TT_HTTP_SRV_9);
4722// /* We must ensure that the read part is still alive when switching to shutw */
4723// /* FIXME: is this still true ? */
4724// EV_FD_SET(req->cons->fd, DIR_RD);
4725// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4726// } else {
4727// fd_delete(req->cons->fd);
4728// req->cons->state = SI_ST_CLO;
4729// if (t->srv) {
4730// t->srv->cur_sess--;
4731// sess_change_server(t, NULL);
4732// }
4733// trace_term(t, TT_HTTP_SRV_10);
4734//
4735// if (may_dequeue_tasks(t->srv, t->be))
4736// process_srv_queue(t->srv);
4737// return 0;
4738// }
4739// }
4740//
4741// /* read timeout */
4742// if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
4743// if (!rep->analysers) {
4744// if (!(t->flags & SN_ERR_MASK))
4745// t->flags |= SN_ERR_SRVTO;
4746// if (!(t->flags & SN_FINST_MASK))
4747// t->flags |= SN_FINST_D;
4748// }
4749// buffer_shutr(rep);
4750// if (!(req->flags & BF_SHUTW)) {
4751// EV_FD_CLR(req->cons->fd, DIR_RD);
4752// trace_term(t, TT_HTTP_SRV_11);
4753// } else {
4754// fd_delete(req->cons->fd);
4755// req->cons->state = SI_ST_CLO;
4756// if (t->srv) {
4757// t->srv->cur_sess--;
4758// sess_change_server(t, NULL);
4759// }
4760// trace_term(t, TT_HTTP_SRV_12);
4761//
4762// if (may_dequeue_tasks(t->srv, t->be))
4763// process_srv_queue(t->srv);
4764// return 0;
4765// }
4766// }
4767//
4768// /* write timeout */
4769// if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
4770// if (!rep->analysers) {
4771// if (!(t->flags & SN_ERR_MASK))
4772// t->flags |= SN_ERR_SRVTO;
4773// if (!(t->flags & SN_FINST_MASK))
4774// t->flags |= SN_FINST_D;
4775// }
4776// buffer_shutw(req);
4777// if (!(rep->flags & BF_SHUTR)) {
4778// EV_FD_CLR(req->cons->fd, DIR_WR);
4779// shutdown(req->cons->fd, SHUT_WR);
4780// /* We must ensure that the read part is still alive when switching to shutw */
4781// /* FIXME: is this still needed ? */
4782// EV_FD_SET(req->cons->fd, DIR_RD);
4783// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4784// trace_term(t, TT_HTTP_SRV_13);
4785// } else {
4786// fd_delete(req->cons->fd);
4787// req->cons->state = SI_ST_CLO;
4788// if (t->srv) {
4789// t->srv->cur_sess--;
4790// sess_change_server(t, NULL);
4791// }
4792// trace_term(t, TT_HTTP_SRV_14);
4793//
4794// if (may_dequeue_tasks(t->srv, t->be))
4795// process_srv_queue(t->srv);
4796// return 0;
4797// }
4798// }
4799//
4800// update_timeouts:
4801// /* manage read timeout */
4802// if (!(rep->flags & BF_SHUTR)) {
4803// if (rep->flags & BF_FULL) {
4804// if (EV_FD_COND_C(req->cons->fd, DIR_RD))
4805// rep->rex = TICK_ETERNITY;
4806// } else {
4807// EV_FD_COND_S(req->cons->fd, DIR_RD);
4808// rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
4809// }
4810// }
4811//
4812// /* manage write timeout */
4813// if (!(req->flags & BF_SHUTW)) {
4814// if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
4815// /* stop writing */
4816// if (EV_FD_COND_C(req->cons->fd, DIR_WR))
4817// req->wex = TICK_ETERNITY;
4818// } else {
4819// /* buffer not empty, there are still data to be transferred */
4820// EV_FD_COND_S(req->cons->fd, DIR_WR);
4821// if (!tick_isset(req->wex)) {
4822// /* restart writing */
4823// req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
4824// if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
4825// /* FIXME: to prevent the server from expiring read timeouts during writes,
4826// * we refresh it, except if it was already infinite.
4827// */
4828// rep->rex = req->wex;
4829// }
4830// }
4831// }
4832// }
4833// return 0; /* other cases change nothing */
4834//}
4835//
4836
Willy Tarreaubaaee002006-06-26 02:48:02 +02004837/*
4838 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004839 * called with client socket shut down on input. Right now, only statistics can
4840 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02004841 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004842 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004843 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004844 */
4845int produce_content(struct session *s)
4846{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004847 if (s->data_source == DATA_SRC_NONE) {
4848 s->flags &= ~SN_SELF_GEN;
4849 return 1;
4850 }
4851 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004852 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004853 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004854 if (ret >= 0)
4855 return ret;
4856 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004857 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004858
Willy Tarreau91861262007-10-17 17:06:05 +02004859 /* unknown data source or internal error */
4860 s->txn.status = 500;
4861 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004862 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004863 if (!(s->flags & SN_ERR_MASK))
4864 s->flags |= SN_ERR_PRXCOND;
4865 if (!(s->flags & SN_FINST_MASK))
4866 s->flags |= SN_FINST_R;
4867 s->flags &= ~SN_SELF_GEN;
4868 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004869}
4870
4871
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004872/* Iterate the same filter through all request headers.
4873 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004874 * Since it can manage the switch to another backend, it updates the per-proxy
4875 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004876 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004877int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004878{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004879 char term;
4880 char *cur_ptr, *cur_end, *cur_next;
4881 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004882 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004883 struct hdr_idx_elem *cur_hdr;
4884 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004886 last_hdr = 0;
4887
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004888 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004889 old_idx = 0;
4890
4891 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004892 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004893 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004894 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 (exp->action == ACT_ALLOW ||
4896 exp->action == ACT_DENY ||
4897 exp->action == ACT_TARPIT))
4898 return 0;
4899
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004900 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004901 if (!cur_idx)
4902 break;
4903
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004904 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004905 cur_ptr = cur_next;
4906 cur_end = cur_ptr + cur_hdr->len;
4907 cur_next = cur_end + cur_hdr->cr + 1;
4908
4909 /* Now we have one header between cur_ptr and cur_end,
4910 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004911 */
4912
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004913 /* The annoying part is that pattern matching needs
4914 * that we modify the contents to null-terminate all
4915 * strings before testing them.
4916 */
4917
4918 term = *cur_end;
4919 *cur_end = '\0';
4920
4921 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4922 switch (exp->action) {
4923 case ACT_SETBE:
4924 /* It is not possible to jump a second time.
4925 * FIXME: should we return an HTTP/500 here so that
4926 * the admin knows there's a problem ?
4927 */
4928 if (t->be != t->fe)
4929 break;
4930
4931 /* Swithing Proxy */
4932 t->be = (struct proxy *) exp->replace;
4933
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004934 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004935 * because we have associated req_cap and rsp_cap to the
4936 * frontend, and the beconn will be updated later.
4937 */
4938
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004939 t->rep->rto = t->req->wto = t->be->timeout.server;
4940 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004941 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004942 last_hdr = 1;
4943 break;
4944
4945 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004946 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004947 last_hdr = 1;
4948 break;
4949
4950 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004951 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004952 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004953 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 break;
4955
4956 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004959 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004960 break;
4961
4962 case ACT_REPLACE:
4963 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4964 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4965 /* FIXME: if the user adds a newline in the replacement, the
4966 * index will not be recalculated for now, and the new line
4967 * will not be counted as a new header.
4968 */
4969
4970 cur_end += delta;
4971 cur_next += delta;
4972 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004973 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 break;
4975
4976 case ACT_REMOVE:
4977 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4978 cur_next += delta;
4979
4980 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004981 txn->req.eoh += delta;
4982 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4983 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004984 cur_hdr->len = 0;
4985 cur_end = NULL; /* null-term has been rewritten */
4986 break;
4987
4988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004989 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 if (cur_end)
4991 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004993 /* keep the link from this header to next one in case of later
4994 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004995 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004996 old_idx = cur_idx;
4997 }
4998 return 0;
4999}
5000
5001
5002/* Apply the filter to the request line.
5003 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5004 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005 * Since it can manage the switch to another backend, it updates the per-proxy
5006 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005007 */
5008int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5009{
5010 char term;
5011 char *cur_ptr, *cur_end;
5012 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005013 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016
Willy Tarreau3d300592007-03-18 18:34:41 +01005017 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005018 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005019 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 (exp->action == ACT_ALLOW ||
5021 exp->action == ACT_DENY ||
5022 exp->action == ACT_TARPIT))
5023 return 0;
5024 else if (exp->action == ACT_REMOVE)
5025 return 0;
5026
5027 done = 0;
5028
Willy Tarreau9cdde232007-05-02 20:58:19 +02005029 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005030 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031
5032 /* Now we have the request line between cur_ptr and cur_end */
5033
5034 /* The annoying part is that pattern matching needs
5035 * that we modify the contents to null-terminate all
5036 * strings before testing them.
5037 */
5038
5039 term = *cur_end;
5040 *cur_end = '\0';
5041
5042 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5043 switch (exp->action) {
5044 case ACT_SETBE:
5045 /* It is not possible to jump a second time.
5046 * FIXME: should we return an HTTP/500 here so that
5047 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005048 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005049 if (t->be != t->fe)
5050 break;
5051
5052 /* Swithing Proxy */
5053 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005055 /* right now, the backend switch is not too much complicated
5056 * because we have associated req_cap and rsp_cap to the
5057 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005058 */
5059
Willy Tarreaud7c30f92007-12-03 01:38:36 +01005060 t->rep->rto = t->req->wto = t->be->timeout.server;
5061 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02005062 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 done = 1;
5064 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005066 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005067 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005068 done = 1;
5069 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005072 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005073 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005074 done = 1;
5075 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005077 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005078 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005079 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005080 done = 1;
5081 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005083 case ACT_REPLACE:
5084 *cur_end = term; /* restore the string terminator */
5085 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5086 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5087 /* FIXME: if the user adds a newline in the replacement, the
5088 * index will not be recalculated for now, and the new line
5089 * will not be counted as a new header.
5090 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005091
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005092 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005093 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005094
Willy Tarreau9cdde232007-05-02 20:58:19 +02005095 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005096 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 HTTP_MSG_RQMETH,
5098 cur_ptr, cur_end + 1,
5099 NULL, NULL);
5100 if (unlikely(!cur_end))
5101 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005103 /* we have a full request and we know that we have either a CR
5104 * or an LF at <ptr>.
5105 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005106 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5107 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005108 /* there is no point trying this regex on headers */
5109 return 1;
5110 }
5111 }
5112 *cur_end = term; /* restore the string terminator */
5113 return done;
5114}
Willy Tarreau97de6242006-12-27 17:18:38 +01005115
Willy Tarreau58f10d72006-12-04 02:26:12 +01005116
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005118/*
5119 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5120 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005121 * unparsable request. Since it can manage the switch to another backend, it
5122 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005123 */
5124int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5125{
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005131 /*
5132 * The interleaving of transformations and verdicts
5133 * makes it difficult to decide to continue or stop
5134 * the evaluation.
5135 */
5136
Willy Tarreau3d300592007-03-18 18:34:41 +01005137 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005138 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5139 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5140 exp = exp->next;
5141 continue;
5142 }
5143
5144 /* Apply the filter to the request line. */
5145 ret = apply_filter_to_req_line(t, req, exp);
5146 if (unlikely(ret < 0))
5147 return -1;
5148
5149 if (likely(ret == 0)) {
5150 /* The filter did not match the request, it can be
5151 * iterated through all headers.
5152 */
5153 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005154 }
5155 exp = exp->next;
5156 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158}
5159
5160
Willy Tarreaua15645d2007-03-18 16:22:39 +01005161
Willy Tarreau58f10d72006-12-04 02:26:12 +01005162/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005163 * Manage client-side cookie. It can impact performance by about 2% so it is
5164 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005165 */
5166void manage_client_side_cookies(struct session *t, struct buffer *req)
5167{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005168 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005169 char *p1, *p2, *p3, *p4;
5170 char *del_colon, *del_cookie, *colon;
5171 int app_cookies;
5172
5173 appsess *asession_temp = NULL;
5174 appsess local_asession;
5175
5176 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005177 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178
Willy Tarreau2a324282006-12-05 00:05:46 +01005179 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005180 * we start with the start line.
5181 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005182 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005183 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005184
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005185 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005186 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005187 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005188
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005189 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005190 cur_ptr = cur_next;
5191 cur_end = cur_ptr + cur_hdr->len;
5192 cur_next = cur_end + cur_hdr->cr + 1;
5193
5194 /* We have one full header between cur_ptr and cur_end, and the
5195 * next header starts at cur_next. We're only interested in
5196 * "Cookie:" headers.
5197 */
5198
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005199 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5200 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005201 old_idx = cur_idx;
5202 continue;
5203 }
5204
5205 /* Now look for cookies. Conforming to RFC2109, we have to support
5206 * attributes whose name begin with a '$', and associate them with
5207 * the right cookie, if we want to delete this cookie.
5208 * So there are 3 cases for each cookie read :
5209 * 1) it's a special attribute, beginning with a '$' : ignore it.
5210 * 2) it's a server id cookie that we *MAY* want to delete : save
5211 * some pointers on it (last semi-colon, beginning of cookie...)
5212 * 3) it's an application cookie : we *MAY* have to delete a previous
5213 * "special" cookie.
5214 * At the end of loop, if a "special" cookie remains, we may have to
5215 * remove it. If no application cookie persists in the header, we
5216 * *MUST* delete it
5217 */
5218
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005219 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005220
Willy Tarreau58f10d72006-12-04 02:26:12 +01005221 /* del_cookie == NULL => nothing to be deleted */
5222 del_colon = del_cookie = NULL;
5223 app_cookies = 0;
5224
5225 while (p1 < cur_end) {
5226 /* skip spaces and colons, but keep an eye on these ones */
5227 while (p1 < cur_end) {
5228 if (*p1 == ';' || *p1 == ',')
5229 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005230 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005231 break;
5232 p1++;
5233 }
5234
5235 if (p1 == cur_end)
5236 break;
5237
5238 /* p1 is at the beginning of the cookie name */
5239 p2 = p1;
5240 while (p2 < cur_end && *p2 != '=')
5241 p2++;
5242
5243 if (p2 == cur_end)
5244 break;
5245
5246 p3 = p2 + 1; /* skips the '=' sign */
5247 if (p3 == cur_end)
5248 break;
5249
5250 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005251 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005252 p4++;
5253
5254 /* here, we have the cookie name between p1 and p2,
5255 * and its value between p3 and p4.
5256 * we can process it :
5257 *
5258 * Cookie: NAME=VALUE;
5259 * | || || |
5260 * | || || +--> p4
5261 * | || |+-------> p3
5262 * | || +--------> p2
5263 * | |+------------> p1
5264 * | +-------------> colon
5265 * +--------------------> cur_ptr
5266 */
5267
5268 if (*p1 == '$') {
5269 /* skip this one */
5270 }
5271 else {
5272 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005273 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005274 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005275 (p4 - p1 >= t->fe->capture_namelen) &&
5276 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005277 int log_len = p4 - p1;
5278
Willy Tarreau086b3b42007-05-13 21:45:51 +02005279 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005280 Alert("HTTP logging : out of memory.\n");
5281 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005282 if (log_len > t->fe->capture_len)
5283 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005284 memcpy(txn->cli_cookie, p1, log_len);
5285 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005286 }
5287 }
5288
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005289 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5290 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005291 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005292 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293 char *delim;
5294
5295 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5296 * have the server ID betweek p3 and delim, and the original cookie between
5297 * delim+1 and p4. Otherwise, delim==p4 :
5298 *
5299 * Cookie: NAME=SRV~VALUE;
5300 * | || || | |
5301 * | || || | +--> p4
5302 * | || || +--------> delim
5303 * | || |+-----------> p3
5304 * | || +------------> p2
5305 * | |+----------------> p1
5306 * | +-----------------> colon
5307 * +------------------------> cur_ptr
5308 */
5309
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005310 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005311 for (delim = p3; delim < p4; delim++)
5312 if (*delim == COOKIE_DELIM)
5313 break;
5314 }
5315 else
5316 delim = p4;
5317
5318
5319 /* Here, we'll look for the first running server which supports the cookie.
5320 * This allows to share a same cookie between several servers, for example
5321 * to dedicate backup servers to specific servers only.
5322 * However, to prevent clients from sticking to cookie-less backup server
5323 * when they have incidentely learned an empty cookie, we simply ignore
5324 * empty cookies and mark them as invalid.
5325 */
5326 if (delim == p3)
5327 srv = NULL;
5328
5329 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005330 if (srv->cookie && (srv->cklen == delim - p3) &&
5331 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005332 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005333 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005334 txn->flags &= ~TX_CK_MASK;
5335 txn->flags |= TX_CK_VALID;
5336 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005337 t->srv = srv;
5338 break;
5339 } else {
5340 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005341 txn->flags &= ~TX_CK_MASK;
5342 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005343 }
5344 }
5345 srv = srv->next;
5346 }
5347
Willy Tarreau3d300592007-03-18 18:34:41 +01005348 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005350 txn->flags &= ~TX_CK_MASK;
5351 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005352 }
5353
5354 /* depending on the cookie mode, we may have to either :
5355 * - delete the complete cookie if we're in insert+indirect mode, so that
5356 * the server never sees it ;
5357 * - remove the server id from the cookie value, and tag the cookie as an
5358 * application cookie so that it does not get accidentely removed later,
5359 * if we're in cookie prefix mode
5360 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005361 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005362 int delta; /* negative */
5363
5364 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5365 p4 += delta;
5366 cur_end += delta;
5367 cur_next += delta;
5368 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005369 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005370
5371 del_cookie = del_colon = NULL;
5372 app_cookies++; /* protect the header from deletion */
5373 }
5374 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005375 (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 +01005376 del_cookie = p1;
5377 del_colon = colon;
5378 }
5379 } else {
5380 /* now we know that we must keep this cookie since it's
5381 * not ours. But if we wanted to delete our cookie
5382 * earlier, we cannot remove the complete header, but we
5383 * can remove the previous block itself.
5384 */
5385 app_cookies++;
5386
5387 if (del_cookie != NULL) {
5388 int delta; /* negative */
5389
5390 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5391 p4 += delta;
5392 cur_end += delta;
5393 cur_next += delta;
5394 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005395 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005396 del_cookie = del_colon = NULL;
5397 }
5398 }
5399
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005400 if ((t->be->appsession_name != NULL) &&
5401 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005402 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005403
Willy Tarreau58f10d72006-12-04 02:26:12 +01005404 /* Cool... it's the right one */
5405
5406 asession_temp = &local_asession;
5407
Willy Tarreau63963c62007-05-13 21:29:55 +02005408 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005409 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5410 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5411 return;
5412 }
5413
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005414 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5415 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005416 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02005417
Willy Tarreau58f10d72006-12-04 02:26:12 +01005418 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005419 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5420 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005421 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005422 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005423 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005424 Alert("Not enough memory process_cli():asession:calloc().\n");
5425 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5426 return;
5427 }
5428
5429 asession_temp->sessid = local_asession.sessid;
5430 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005431 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005432 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005433 } else {
5434 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005435 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005437 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005438 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005439 Alert("Found Application Session without matching server.\n");
5440 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005441 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442 while (srv) {
5443 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005444 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005445 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005446 txn->flags &= ~TX_CK_MASK;
5447 txn->flags |= TX_CK_VALID;
5448 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449 t->srv = srv;
5450 break;
5451 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005452 txn->flags &= ~TX_CK_MASK;
5453 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005454 }
5455 }
5456 srv = srv->next;
5457 }/* end while(srv) */
5458 }/* end else if server == NULL */
5459
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005460 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005461 asession_temp->request_count++;
5462#if defined(DEBUG_HASH)
5463 Alert("manage_client_side_cookies\n");
5464 appsession_hash_dump(&(t->be->htbl_proxy));
5465#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005466 }/* end if ((t->proxy->appsession_name != NULL) ... */
5467 }
5468
5469 /* we'll have to look for another cookie ... */
5470 p1 = p4;
5471 } /* while (p1 < cur_end) */
5472
5473 /* There's no more cookie on this line.
5474 * We may have marked the last one(s) for deletion.
5475 * We must do this now in two ways :
5476 * - if there is no app cookie, we simply delete the header ;
5477 * - if there are app cookies, we must delete the end of the
5478 * string properly, including the colon/semi-colon before
5479 * the cookie name.
5480 */
5481 if (del_cookie != NULL) {
5482 int delta;
5483 if (app_cookies) {
5484 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5485 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005486 cur_hdr->len += delta;
5487 } else {
5488 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005489
5490 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005491 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5492 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493 cur_hdr->len = 0;
5494 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005495 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005496 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005497 }
5498
5499 /* keep the link from this header to next one */
5500 old_idx = cur_idx;
5501 } /* end of cookie processing on this header */
5502}
5503
5504
Willy Tarreaua15645d2007-03-18 16:22:39 +01005505/* Iterate the same filter through all response headers contained in <rtr>.
5506 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5507 */
5508int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5509{
5510 char term;
5511 char *cur_ptr, *cur_end, *cur_next;
5512 int cur_idx, old_idx, last_hdr;
5513 struct http_txn *txn = &t->txn;
5514 struct hdr_idx_elem *cur_hdr;
5515 int len, delta;
5516
5517 last_hdr = 0;
5518
5519 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5520 old_idx = 0;
5521
5522 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005523 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005524 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005525 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005526 (exp->action == ACT_ALLOW ||
5527 exp->action == ACT_DENY))
5528 return 0;
5529
5530 cur_idx = txn->hdr_idx.v[old_idx].next;
5531 if (!cur_idx)
5532 break;
5533
5534 cur_hdr = &txn->hdr_idx.v[cur_idx];
5535 cur_ptr = cur_next;
5536 cur_end = cur_ptr + cur_hdr->len;
5537 cur_next = cur_end + cur_hdr->cr + 1;
5538
5539 /* Now we have one header between cur_ptr and cur_end,
5540 * and the next header starts at cur_next.
5541 */
5542
5543 /* The annoying part is that pattern matching needs
5544 * that we modify the contents to null-terminate all
5545 * strings before testing them.
5546 */
5547
5548 term = *cur_end;
5549 *cur_end = '\0';
5550
5551 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5552 switch (exp->action) {
5553 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005554 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005555 last_hdr = 1;
5556 break;
5557
5558 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005559 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005560 last_hdr = 1;
5561 break;
5562
5563 case ACT_REPLACE:
5564 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5565 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5566 /* FIXME: if the user adds a newline in the replacement, the
5567 * index will not be recalculated for now, and the new line
5568 * will not be counted as a new header.
5569 */
5570
5571 cur_end += delta;
5572 cur_next += delta;
5573 cur_hdr->len += delta;
5574 txn->rsp.eoh += delta;
5575 break;
5576
5577 case ACT_REMOVE:
5578 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5579 cur_next += delta;
5580
5581 /* FIXME: this should be a separate function */
5582 txn->rsp.eoh += delta;
5583 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5584 txn->hdr_idx.used--;
5585 cur_hdr->len = 0;
5586 cur_end = NULL; /* null-term has been rewritten */
5587 break;
5588
5589 }
5590 }
5591 if (cur_end)
5592 *cur_end = term; /* restore the string terminator */
5593
5594 /* keep the link from this header to next one in case of later
5595 * removal of next header.
5596 */
5597 old_idx = cur_idx;
5598 }
5599 return 0;
5600}
5601
5602
5603/* Apply the filter to the status line in the response buffer <rtr>.
5604 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5605 * or -1 if a replacement resulted in an invalid status line.
5606 */
5607int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5608{
5609 char term;
5610 char *cur_ptr, *cur_end;
5611 int done;
5612 struct http_txn *txn = &t->txn;
5613 int len, delta;
5614
5615
Willy Tarreau3d300592007-03-18 18:34:41 +01005616 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005617 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005618 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005619 (exp->action == ACT_ALLOW ||
5620 exp->action == ACT_DENY))
5621 return 0;
5622 else if (exp->action == ACT_REMOVE)
5623 return 0;
5624
5625 done = 0;
5626
Willy Tarreau9cdde232007-05-02 20:58:19 +02005627 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005628 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5629
5630 /* Now we have the status line between cur_ptr and cur_end */
5631
5632 /* The annoying part is that pattern matching needs
5633 * that we modify the contents to null-terminate all
5634 * strings before testing them.
5635 */
5636
5637 term = *cur_end;
5638 *cur_end = '\0';
5639
5640 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5641 switch (exp->action) {
5642 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005643 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644 done = 1;
5645 break;
5646
5647 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005648 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005649 done = 1;
5650 break;
5651
5652 case ACT_REPLACE:
5653 *cur_end = term; /* restore the string terminator */
5654 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5655 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5656 /* FIXME: if the user adds a newline in the replacement, the
5657 * index will not be recalculated for now, and the new line
5658 * will not be counted as a new header.
5659 */
5660
5661 txn->rsp.eoh += delta;
5662 cur_end += delta;
5663
Willy Tarreau9cdde232007-05-02 20:58:19 +02005664 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005665 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005666 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005667 cur_ptr, cur_end + 1,
5668 NULL, NULL);
5669 if (unlikely(!cur_end))
5670 return -1;
5671
5672 /* we have a full respnse and we know that we have either a CR
5673 * or an LF at <ptr>.
5674 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005675 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5677 /* there is no point trying this regex on headers */
5678 return 1;
5679 }
5680 }
5681 *cur_end = term; /* restore the string terminator */
5682 return done;
5683}
5684
5685
5686
5687/*
5688 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5689 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5690 * unparsable response.
5691 */
5692int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5693{
Willy Tarreau3d300592007-03-18 18:34:41 +01005694 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005695 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005696 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 int ret;
5698
5699 /*
5700 * The interleaving of transformations and verdicts
5701 * makes it difficult to decide to continue or stop
5702 * the evaluation.
5703 */
5704
Willy Tarreau3d300592007-03-18 18:34:41 +01005705 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005706 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5707 exp->action == ACT_PASS)) {
5708 exp = exp->next;
5709 continue;
5710 }
5711
5712 /* Apply the filter to the status line. */
5713 ret = apply_filter_to_sts_line(t, rtr, exp);
5714 if (unlikely(ret < 0))
5715 return -1;
5716
5717 if (likely(ret == 0)) {
5718 /* The filter did not match the response, it can be
5719 * iterated through all headers.
5720 */
5721 apply_filter_to_resp_headers(t, rtr, exp);
5722 }
5723 exp = exp->next;
5724 }
5725 return 0;
5726}
5727
5728
5729
5730/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005731 * Manage server-side cookies. It can impact performance by about 2% so it is
5732 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005733 */
5734void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5735{
5736 struct http_txn *txn = &t->txn;
5737 char *p1, *p2, *p3, *p4;
5738
5739 appsess *asession_temp = NULL;
5740 appsess local_asession;
5741
5742 char *cur_ptr, *cur_end, *cur_next;
5743 int cur_idx, old_idx, delta;
5744
Willy Tarreaua15645d2007-03-18 16:22:39 +01005745 /* Iterate through the headers.
5746 * we start with the start line.
5747 */
5748 old_idx = 0;
5749 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5750
5751 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5752 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005753 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754
5755 cur_hdr = &txn->hdr_idx.v[cur_idx];
5756 cur_ptr = cur_next;
5757 cur_end = cur_ptr + cur_hdr->len;
5758 cur_next = cur_end + cur_hdr->cr + 1;
5759
5760 /* We have one full header between cur_ptr and cur_end, and the
5761 * next header starts at cur_next. We're only interested in
5762 * "Cookie:" headers.
5763 */
5764
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005765 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5766 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005767 old_idx = cur_idx;
5768 continue;
5769 }
5770
5771 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005772 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005773
5774
5775 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005776 if (t->be->cookie_name == NULL &&
5777 t->be->appsession_name == NULL &&
5778 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005779 return;
5780
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005781 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005782
5783 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5785 break;
5786
5787 /* p1 is at the beginning of the cookie name */
5788 p2 = p1;
5789
5790 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5791 p2++;
5792
5793 if (p2 == cur_end || *p2 == ';') /* next cookie */
5794 break;
5795
5796 p3 = p2 + 1; /* skip the '=' sign */
5797 if (p3 == cur_end)
5798 break;
5799
5800 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005801 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005802 p4++;
5803
5804 /* here, we have the cookie name between p1 and p2,
5805 * and its value between p3 and p4.
5806 * we can process it.
5807 */
5808
5809 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005810 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005811 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005812 (p4 - p1 >= t->be->capture_namelen) &&
5813 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005814 int log_len = p4 - p1;
5815
Willy Tarreau086b3b42007-05-13 21:45:51 +02005816 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817 Alert("HTTP logging : out of memory.\n");
5818 }
5819
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005820 if (log_len > t->be->capture_len)
5821 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005822 memcpy(txn->srv_cookie, p1, log_len);
5823 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005824 }
5825
5826 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005827 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5828 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005829 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005830 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005831
5832 /* If the cookie is in insert mode on a known server, we'll delete
5833 * this occurrence because we'll insert another one later.
5834 * We'll delete it too if the "indirect" option is set and we're in
5835 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005836 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5837 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005838 /* this header must be deleted */
5839 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5840 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5841 txn->hdr_idx.used--;
5842 cur_hdr->len = 0;
5843 cur_next += delta;
5844 txn->rsp.eoh += delta;
5845
Willy Tarreau3d300592007-03-18 18:34:41 +01005846 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005847 }
5848 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005849 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005850 /* replace bytes p3->p4 with the cookie name associated
5851 * with this server since we know it.
5852 */
5853 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5854 cur_hdr->len += delta;
5855 cur_next += delta;
5856 txn->rsp.eoh += delta;
5857
Willy Tarreau3d300592007-03-18 18:34:41 +01005858 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859 }
5860 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005861 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005862 /* insert the cookie name associated with this server
5863 * before existing cookie, and insert a delimitor between them..
5864 */
5865 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5866 cur_hdr->len += delta;
5867 cur_next += delta;
5868 txn->rsp.eoh += delta;
5869
5870 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005871 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872 }
5873 }
5874 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005875 else if ((t->be->appsession_name != NULL) &&
5876 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877
5878 /* Cool... it's the right one */
5879
5880 size_t server_id_len = strlen(t->srv->id) + 1;
5881 asession_temp = &local_asession;
5882
Willy Tarreau63963c62007-05-13 21:29:55 +02005883 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005884 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5885 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5886 return;
5887 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005888 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5889 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890 asession_temp->serverid = NULL;
5891
5892 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005893 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5894 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005895 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005896 Alert("Not enough Memory process_srv():asession:calloc().\n");
5897 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5898 return;
5899 }
5900 asession_temp->sessid = local_asession.sessid;
5901 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005902 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005903 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5904 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005905 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005906 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005907 }
5908
Willy Tarreaua15645d2007-03-18 16:22:39 +01005909 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005910 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5912 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5913 return;
5914 }
5915 asession_temp->serverid[0] = '\0';
5916 }
5917
5918 if (asession_temp->serverid[0] == '\0')
5919 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5920
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005921 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005922 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005924 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005925 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005926#endif
5927 }/* end if ((t->proxy->appsession_name != NULL) ... */
5928 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5929 } /* we're now at the end of the cookie value */
5930
5931 /* keep the link from this header to next one */
5932 old_idx = cur_idx;
5933 } /* end of cookie processing on this header */
5934}
5935
5936
5937
5938/*
5939 * Check if response is cacheable or not. Updates t->flags.
5940 */
5941void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5942{
5943 struct http_txn *txn = &t->txn;
5944 char *p1, *p2;
5945
5946 char *cur_ptr, *cur_end, *cur_next;
5947 int cur_idx;
5948
Willy Tarreau5df51872007-11-25 16:20:08 +01005949 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005950 return;
5951
5952 /* Iterate through the headers.
5953 * we start with the start line.
5954 */
5955 cur_idx = 0;
5956 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5957
5958 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5959 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005960 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005961
5962 cur_hdr = &txn->hdr_idx.v[cur_idx];
5963 cur_ptr = cur_next;
5964 cur_end = cur_ptr + cur_hdr->len;
5965 cur_next = cur_end + cur_hdr->cr + 1;
5966
5967 /* We have one full header between cur_ptr and cur_end, and the
5968 * next header starts at cur_next. We're only interested in
5969 * "Cookie:" headers.
5970 */
5971
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005972 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5973 if (val) {
5974 if ((cur_end - (cur_ptr + val) >= 8) &&
5975 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5976 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5977 return;
5978 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979 }
5980
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005981 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5982 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005983 continue;
5984
5985 /* OK, right now we know we have a cache-control header at cur_ptr */
5986
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005987 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005988
5989 if (p1 >= cur_end) /* no more info */
5990 continue;
5991
5992 /* p1 is at the beginning of the value */
5993 p2 = p1;
5994
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005995 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996 p2++;
5997
5998 /* we have a complete value between p1 and p2 */
5999 if (p2 < cur_end && *p2 == '=') {
6000 /* we have something of the form no-cache="set-cookie" */
6001 if ((cur_end - p1 >= 21) &&
6002 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6003 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006004 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005 continue;
6006 }
6007
6008 /* OK, so we know that either p2 points to the end of string or to a comma */
6009 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6010 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6011 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6012 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006013 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006014 return;
6015 }
6016
6017 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006018 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006019 continue;
6020 }
6021 }
6022}
6023
6024
Willy Tarreau58f10d72006-12-04 02:26:12 +01006025/*
6026 * Try to retrieve a known appsession in the URI, then the associated server.
6027 * If the server is found, it's assigned to the session.
6028 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006029void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006030{
Willy Tarreau3d300592007-03-18 18:34:41 +01006031 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032 appsess *asession_temp = NULL;
6033 appsess local_asession;
6034 char *request_line;
6035
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006036 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01006037 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006039 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040 return;
6041
6042 /* skip ';' */
6043 request_line++;
6044
6045 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006046 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006047 return;
6048
6049 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006050 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
6052 /* First try if we already have an appsession */
6053 asession_temp = &local_asession;
6054
Willy Tarreau63963c62007-05-13 21:29:55 +02006055 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
6057 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
6058 return;
6059 }
6060
6061 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006062 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
6063 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006064 asession_temp->serverid = NULL;
6065
6066 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01006067 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
6068 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02006069 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006071 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072 Alert("Not enough memory process_cli():asession:calloc().\n");
6073 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
6074 return;
6075 }
6076 asession_temp->sessid = local_asession.sessid;
6077 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006078 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02006079 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080 }
6081 else {
6082 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02006083 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084 }
Willy Tarreau51041c72007-09-09 21:56:53 +02006085
Willy Tarreau0c303ee2008-07-07 00:09:58 +02006086 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02006088
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006090 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006091 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092#endif
6093 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006094 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006095 Alert("Found Application Session without matching server.\n");
6096 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006097 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098 while (srv) {
6099 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006100 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01006102 txn->flags &= ~TX_CK_MASK;
6103 txn->flags |= TX_CK_VALID;
6104 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105 t->srv = srv;
6106 break;
6107 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01006108 txn->flags &= ~TX_CK_MASK;
6109 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110 }
6111 }
6112 srv = srv->next;
6113 }
6114 }
6115}
6116
6117
Willy Tarreaub2513902006-12-17 14:52:38 +01006118/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006119 * In a GET or HEAD request, check if the requested URI matches the stats uri
6120 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006121 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006122 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006123 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006124 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006125 *
6126 * Returns 1 if the session's state changes, otherwise 0.
6127 */
6128int stats_check_uri_auth(struct session *t, struct proxy *backend)
6129{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006130 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006131 struct uri_auth *uri_auth = backend->uri_auth;
6132 struct user_auth *user;
6133 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006134 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006135
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006136 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006139 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006140 return 0;
6141
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006142 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006143
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006144 /* the URI is in h */
6145 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006146 return 0;
6147
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006148 h += uri_auth->uri_len;
6149 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6150 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006151 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006152 break;
6153 }
6154 h++;
6155 }
6156
6157 if (uri_auth->refresh) {
6158 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6159 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6160 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006161 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006162 break;
6163 }
6164 h++;
6165 }
6166 }
6167
Willy Tarreau55bb8452007-10-17 18:44:57 +02006168 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6169 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6170 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006171 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006172 break;
6173 }
6174 h++;
6175 }
6176
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006177 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6178
Willy Tarreaub2513902006-12-17 14:52:38 +01006179 /* we are in front of a interceptable URI. Let's check
6180 * if there's an authentication and if it's valid.
6181 */
6182 user = uri_auth->users;
6183 if (!user) {
6184 /* no user auth required, it's OK */
6185 authenticated = 1;
6186 } else {
6187 authenticated = 0;
6188
6189 /* a user list is defined, we have to check.
6190 * skip 21 chars for "Authorization: Basic ".
6191 */
6192
6193 /* FIXME: this should move to an earlier place */
6194 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006195 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6196 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6197 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006198 if (len > 14 &&
6199 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006200 txn->auth_hdr.str = h;
6201 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006202 break;
6203 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006204 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006205 }
6206
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006207 if (txn->auth_hdr.len < 21 ||
6208 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006209 user = NULL;
6210
6211 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006212 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6213 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006214 user->user_pwd, user->user_len)) {
6215 authenticated = 1;
6216 break;
6217 }
6218 user = user->next;
6219 }
6220 }
6221
6222 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006223 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006224
6225 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01006226 msg.str = trash;
6227 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006228 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01006229 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02006230 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006231 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 if (!(t->flags & SN_ERR_MASK))
6233 t->flags |= SN_ERR_PRXCOND;
6234 if (!(t->flags & SN_FINST_MASK))
6235 t->flags |= SN_FINST_R;
6236 return 1;
6237 }
6238
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006239 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006240 * data.
6241 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02006242 EV_FD_CLR(t->cli_fd, DIR_RD);
6243 buffer_shutr(t->req);
6244 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02006245 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02006246 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006247 t->data_source = DATA_SRC_STATS;
6248 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006249 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01006250 produce_content(t);
6251 return 1;
6252}
6253
6254
Willy Tarreaubaaee002006-06-26 02:48:02 +02006255/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 * Print a debug line with a header
6257 */
6258void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6259{
6260 int len, max;
6261 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufa7e1022008-10-19 07:30:41 +02006262 dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263 max = end - start;
6264 UBOUND(max, sizeof(trash) - len - 1);
6265 len += strlcpy2(trash + len, start, max + 1);
6266 trash[len++] = '\n';
6267 write(1, trash, len);
6268}
6269
6270
Willy Tarreau8797c062007-05-07 00:55:35 +02006271/************************************************************************/
6272/* The code below is dedicated to ACL parsing and matching */
6273/************************************************************************/
6274
6275
6276
6277
6278/* 1. Check on METHOD
6279 * We use the pre-parsed method if it is known, and store its number as an
6280 * integer. If it is unknown, we use the pointer and the length.
6281 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006282static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006283{
6284 int len, meth;
6285
Willy Tarreauae8b7962007-06-09 23:10:04 +02006286 len = strlen(*text);
6287 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006288
6289 pattern->val.i = meth;
6290 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006291 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006292 if (!pattern->ptr.str)
6293 return 0;
6294 pattern->len = len;
6295 }
6296 return 1;
6297}
6298
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006299static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006300acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6301 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006302{
6303 int meth;
6304 struct http_txn *txn = l7;
6305
Willy Tarreaub6866442008-07-14 23:54:42 +02006306 if (!txn)
6307 return 0;
6308
Willy Tarreauc11416f2007-06-17 16:58:38 +02006309 if (txn->req.msg_state != HTTP_MSG_BODY)
6310 return 0;
6311
Willy Tarreau8797c062007-05-07 00:55:35 +02006312 meth = txn->meth;
6313 test->i = meth;
6314 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006315 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6316 /* ensure the indexes are not affected */
6317 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006318 test->len = txn->req.sl.rq.m_l;
6319 test->ptr = txn->req.sol;
6320 }
6321 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6322 return 1;
6323}
6324
6325static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6326{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006327 int icase;
6328
Willy Tarreau8797c062007-05-07 00:55:35 +02006329 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006330 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006331
6332 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006333 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006334
6335 /* Other method, we must compare the strings */
6336 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006337 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006338
6339 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6340 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6341 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006342 return ACL_PAT_FAIL;
6343 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006344}
6345
6346/* 2. Check on Request/Status Version
6347 * We simply compare strings here.
6348 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006349static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006350{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006351 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006352 if (!pattern->ptr.str)
6353 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006354 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006355 return 1;
6356}
6357
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006358static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006359acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6360 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006361{
6362 struct http_txn *txn = l7;
6363 char *ptr;
6364 int len;
6365
Willy Tarreaub6866442008-07-14 23:54:42 +02006366 if (!txn)
6367 return 0;
6368
Willy Tarreauc11416f2007-06-17 16:58:38 +02006369 if (txn->req.msg_state != HTTP_MSG_BODY)
6370 return 0;
6371
Willy Tarreau8797c062007-05-07 00:55:35 +02006372 len = txn->req.sl.rq.v_l;
6373 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6374
6375 while ((len-- > 0) && (*ptr++ != '/'));
6376 if (len <= 0)
6377 return 0;
6378
6379 test->ptr = ptr;
6380 test->len = len;
6381
6382 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6383 return 1;
6384}
6385
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006386static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006387acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6388 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006389{
6390 struct http_txn *txn = l7;
6391 char *ptr;
6392 int len;
6393
Willy Tarreaub6866442008-07-14 23:54:42 +02006394 if (!txn)
6395 return 0;
6396
Willy Tarreauc11416f2007-06-17 16:58:38 +02006397 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6398 return 0;
6399
Willy Tarreau8797c062007-05-07 00:55:35 +02006400 len = txn->rsp.sl.st.v_l;
6401 ptr = txn->rsp.sol;
6402
6403 while ((len-- > 0) && (*ptr++ != '/'));
6404 if (len <= 0)
6405 return 0;
6406
6407 test->ptr = ptr;
6408 test->len = len;
6409
6410 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6411 return 1;
6412}
6413
6414/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006415static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006416acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6417 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006418{
6419 struct http_txn *txn = l7;
6420 char *ptr;
6421 int len;
6422
Willy Tarreaub6866442008-07-14 23:54:42 +02006423 if (!txn)
6424 return 0;
6425
Willy Tarreauc11416f2007-06-17 16:58:38 +02006426 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6427 return 0;
6428
Willy Tarreau8797c062007-05-07 00:55:35 +02006429 len = txn->rsp.sl.st.c_l;
6430 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6431
6432 test->i = __strl2ui(ptr, len);
6433 test->flags = ACL_TEST_F_VOL_1ST;
6434 return 1;
6435}
6436
6437/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006438static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006439acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6440 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006441{
6442 struct http_txn *txn = l7;
6443
Willy Tarreaub6866442008-07-14 23:54:42 +02006444 if (!txn)
6445 return 0;
6446
Willy Tarreauc11416f2007-06-17 16:58:38 +02006447 if (txn->req.msg_state != HTTP_MSG_BODY)
6448 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006449
Willy Tarreauc11416f2007-06-17 16:58:38 +02006450 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6451 /* ensure the indexes are not affected */
6452 return 0;
6453
Willy Tarreau8797c062007-05-07 00:55:35 +02006454 test->len = txn->req.sl.rq.u_l;
6455 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6456
Willy Tarreauf3d25982007-05-08 22:45:09 +02006457 /* we do not need to set READ_ONLY because the data is in a buffer */
6458 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006459 return 1;
6460}
6461
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006462static int
6463acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6464 struct acl_expr *expr, struct acl_test *test)
6465{
6466 struct http_txn *txn = l7;
6467
Willy Tarreaub6866442008-07-14 23:54:42 +02006468 if (!txn)
6469 return 0;
6470
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006471 if (txn->req.msg_state != HTTP_MSG_BODY)
6472 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006473
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006474 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6475 /* ensure the indexes are not affected */
6476 return 0;
6477
6478 /* Parse HTTP request */
6479 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6480 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6481 test->i = AF_INET;
6482
6483 /*
6484 * If we are parsing url in frontend space, we prepare backend stage
6485 * to not parse again the same url ! optimization lazyness...
6486 */
6487 if (px->options & PR_O_HTTP_PROXY)
6488 l4->flags |= SN_ADDR_SET;
6489
6490 test->flags = ACL_TEST_F_READ_ONLY;
6491 return 1;
6492}
6493
6494static int
6495acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6496 struct acl_expr *expr, struct acl_test *test)
6497{
6498 struct http_txn *txn = l7;
6499
Willy Tarreaub6866442008-07-14 23:54:42 +02006500 if (!txn)
6501 return 0;
6502
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006503 if (txn->req.msg_state != HTTP_MSG_BODY)
6504 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006505
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006506 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6507 /* ensure the indexes are not affected */
6508 return 0;
6509
6510 /* Same optimization as url_ip */
6511 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6512 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6513
6514 if (px->options & PR_O_HTTP_PROXY)
6515 l4->flags |= SN_ADDR_SET;
6516
6517 test->flags = ACL_TEST_F_READ_ONLY;
6518 return 1;
6519}
6520
Willy Tarreauc11416f2007-06-17 16:58:38 +02006521/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6522 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6523 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006524static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006525acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006526 struct acl_expr *expr, struct acl_test *test)
6527{
6528 struct http_txn *txn = l7;
6529 struct hdr_idx *idx = &txn->hdr_idx;
6530 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006531
Willy Tarreaub6866442008-07-14 23:54:42 +02006532 if (!txn)
6533 return 0;
6534
Willy Tarreau33a7e692007-06-10 19:45:56 +02006535 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6536 /* search for header from the beginning */
6537 ctx->idx = 0;
6538
Willy Tarreau33a7e692007-06-10 19:45:56 +02006539 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6540 test->flags |= ACL_TEST_F_FETCH_MORE;
6541 test->flags |= ACL_TEST_F_VOL_HDR;
6542 test->len = ctx->vlen;
6543 test->ptr = (char *)ctx->line + ctx->val;
6544 return 1;
6545 }
6546
6547 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6548 test->flags |= ACL_TEST_F_VOL_HDR;
6549 return 0;
6550}
6551
Willy Tarreau33a7e692007-06-10 19:45:56 +02006552static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006553acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6554 struct acl_expr *expr, struct acl_test *test)
6555{
6556 struct http_txn *txn = l7;
6557
Willy Tarreaub6866442008-07-14 23:54:42 +02006558 if (!txn)
6559 return 0;
6560
Willy Tarreauc11416f2007-06-17 16:58:38 +02006561 if (txn->req.msg_state != HTTP_MSG_BODY)
6562 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006563
Willy Tarreauc11416f2007-06-17 16:58:38 +02006564 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6565 /* ensure the indexes are not affected */
6566 return 0;
6567
6568 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6569}
6570
6571static int
6572acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6573 struct acl_expr *expr, struct acl_test *test)
6574{
6575 struct http_txn *txn = l7;
6576
Willy Tarreaub6866442008-07-14 23:54:42 +02006577 if (!txn)
6578 return 0;
6579
Willy Tarreauc11416f2007-06-17 16:58:38 +02006580 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6581 return 0;
6582
6583 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6584}
6585
6586/* 6. Check on HTTP header count. The number of occurrences is returned.
6587 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6588 */
6589static int
6590acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006591 struct acl_expr *expr, struct acl_test *test)
6592{
6593 struct http_txn *txn = l7;
6594 struct hdr_idx *idx = &txn->hdr_idx;
6595 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006596 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006597
Willy Tarreaub6866442008-07-14 23:54:42 +02006598 if (!txn)
6599 return 0;
6600
Willy Tarreau33a7e692007-06-10 19:45:56 +02006601 ctx.idx = 0;
6602 cnt = 0;
6603 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6604 cnt++;
6605
6606 test->i = cnt;
6607 test->flags = ACL_TEST_F_VOL_HDR;
6608 return 1;
6609}
6610
Willy Tarreauc11416f2007-06-17 16:58:38 +02006611static int
6612acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6613 struct acl_expr *expr, struct acl_test *test)
6614{
6615 struct http_txn *txn = l7;
6616
Willy Tarreaub6866442008-07-14 23:54:42 +02006617 if (!txn)
6618 return 0;
6619
Willy Tarreauc11416f2007-06-17 16:58:38 +02006620 if (txn->req.msg_state != HTTP_MSG_BODY)
6621 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006622
Willy Tarreauc11416f2007-06-17 16:58:38 +02006623 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6624 /* ensure the indexes are not affected */
6625 return 0;
6626
6627 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6628}
6629
6630static int
6631acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6632 struct acl_expr *expr, struct acl_test *test)
6633{
6634 struct http_txn *txn = l7;
6635
Willy Tarreaub6866442008-07-14 23:54:42 +02006636 if (!txn)
6637 return 0;
6638
Willy Tarreauc11416f2007-06-17 16:58:38 +02006639 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6640 return 0;
6641
6642 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6643}
6644
Willy Tarreau33a7e692007-06-10 19:45:56 +02006645/* 7. Check on HTTP header's integer value. The integer value is returned.
6646 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006647 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006648 */
6649static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006650acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006651 struct acl_expr *expr, struct acl_test *test)
6652{
6653 struct http_txn *txn = l7;
6654 struct hdr_idx *idx = &txn->hdr_idx;
6655 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006656
Willy Tarreaub6866442008-07-14 23:54:42 +02006657 if (!txn)
6658 return 0;
6659
Willy Tarreau33a7e692007-06-10 19:45:56 +02006660 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6661 /* search for header from the beginning */
6662 ctx->idx = 0;
6663
Willy Tarreau33a7e692007-06-10 19:45:56 +02006664 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6665 test->flags |= ACL_TEST_F_FETCH_MORE;
6666 test->flags |= ACL_TEST_F_VOL_HDR;
6667 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6668 return 1;
6669 }
6670
6671 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6672 test->flags |= ACL_TEST_F_VOL_HDR;
6673 return 0;
6674}
6675
Willy Tarreauc11416f2007-06-17 16:58:38 +02006676static int
6677acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6678 struct acl_expr *expr, struct acl_test *test)
6679{
6680 struct http_txn *txn = l7;
6681
Willy Tarreaub6866442008-07-14 23:54:42 +02006682 if (!txn)
6683 return 0;
6684
Willy Tarreauc11416f2007-06-17 16:58:38 +02006685 if (txn->req.msg_state != HTTP_MSG_BODY)
6686 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006687
Willy Tarreauc11416f2007-06-17 16:58:38 +02006688 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6689 /* ensure the indexes are not affected */
6690 return 0;
6691
6692 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6693}
6694
6695static int
6696acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6697 struct acl_expr *expr, struct acl_test *test)
6698{
6699 struct http_txn *txn = l7;
6700
Willy Tarreaub6866442008-07-14 23:54:42 +02006701 if (!txn)
6702 return 0;
6703
Willy Tarreauc11416f2007-06-17 16:58:38 +02006704 if (txn->rsp.msg_state != HTTP_MSG_BODY)
6705 return 0;
6706
6707 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6708}
6709
Willy Tarreau737b0c12007-06-10 21:28:46 +02006710/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6711 * the first '/' after the possible hostname, and ends before the possible '?'.
6712 */
6713static int
6714acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6715 struct acl_expr *expr, struct acl_test *test)
6716{
6717 struct http_txn *txn = l7;
6718 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006719
Willy Tarreaub6866442008-07-14 23:54:42 +02006720 if (!txn)
6721 return 0;
6722
Willy Tarreauc11416f2007-06-17 16:58:38 +02006723 if (txn->req.msg_state != HTTP_MSG_BODY)
6724 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006725
Willy Tarreauc11416f2007-06-17 16:58:38 +02006726 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6727 /* ensure the indexes are not affected */
6728 return 0;
6729
Willy Tarreau21d2af32008-02-14 20:25:24 +01006730 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6731 ptr = http_get_path(txn);
6732 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006733 return 0;
6734
6735 /* OK, we got the '/' ! */
6736 test->ptr = ptr;
6737
6738 while (ptr < end && *ptr != '?')
6739 ptr++;
6740
6741 test->len = ptr - test->ptr;
6742
6743 /* we do not need to set READ_ONLY because the data is in a buffer */
6744 test->flags = ACL_TEST_F_VOL_1ST;
6745 return 1;
6746}
6747
6748
Willy Tarreau8797c062007-05-07 00:55:35 +02006749
6750/************************************************************************/
6751/* All supported keywords must be declared here. */
6752/************************************************************************/
6753
6754/* Note: must not be declared <const> as its list will be overwritten */
6755static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006756 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6757 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6758 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6759 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006760
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006761 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6762 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6763 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6764 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6765 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6766 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6767 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6768 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6769 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006770
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006771 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6772 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6773 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6774 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6775 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6776 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6777 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6778 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6779 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6780 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006781
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006782 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6783 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6784 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6785 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6786 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6787 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6788 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6789 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6790 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006791
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006792 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6793 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6794 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6795 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6796 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6797 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6798 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006799
Willy Tarreauf3d25982007-05-08 22:45:09 +02006800 { NULL, NULL, NULL, NULL },
6801
6802#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006803 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6804 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6805 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6806 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6807 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6808 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6809 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6810
Willy Tarreau8797c062007-05-07 00:55:35 +02006811 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6812 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6813 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6814 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6815 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6816 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6817 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6818 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6819
6820 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6821 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6822 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6823 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6824 { NULL, NULL, NULL, NULL },
6825#endif
6826}};
6827
6828
6829__attribute__((constructor))
6830static void __http_protocol_init(void)
6831{
6832 acl_register_keywords(&acl_kws);
6833}
6834
6835
Willy Tarreau58f10d72006-12-04 02:26:12 +01006836/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006837 * Local variables:
6838 * c-indent-level: 8
6839 * c-basic-offset: 8
6840 * End:
6841 */