blob: a027504099d7c87fcaf8aed5df4cdd48a8b3b95d [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 Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200549 buffer_shutw_done(t->req);
550 buffer_shutr_done(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648/* Processes the client and server jobs of a session task, then
649 * puts it back to the wait queue in a clean state, or
650 * cleans up its resources if it must be deleted. Returns
651 * the time the task accepts to wait, or TIME_ETERNITY for
652 * infinity.
653 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200654void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655{
656 struct session *s = t->context;
657 int fsm_resync = 0;
658
659 do {
660 fsm_resync = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200661
662 //fprintf(stderr,"before_cli:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 fsm_resync |= process_cli(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200664
665 //fprintf(stderr,"before_req:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
666 if (s->analysis & AN_REQ_ANY)
667 fsm_resync |= process_request(s);
668
669 //fprintf(stderr,"before_srv:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 fsm_resync |= process_srv(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200671
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200672 //fprintf(stderr,"before_rep:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
673 if (s->analysis & AN_RTR_ANY)
674 fsm_resync |= process_response(s);
675
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200676 //fprintf(stderr,"endof_loop:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 } while (fsm_resync);
678
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200679 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100680
681 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
682 session_process_counters(s);
683
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200684 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
685 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686
Willy Tarreau7f875f62008-08-11 17:35:01 +0200687 /* Trick: if a request is being waiting for the server to respond,
688 * and if we know the server can timeout, we don't want the timeout
689 * to expire on the client side first, but we're still interested
690 * in passing data from the client to the server (eg: POST). Thus,
691 * we can cancel the client's request timeout if the server's
692 * request timeout is set and the server has not yet sent a response.
693 */
694
695 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR_STATUS)) == 0 &&
696 (tick_isset(s->req->cex) || tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
697 s->req->rex = TICK_ETERNITY;
698
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200699 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
700 tick_first(s->rep->rex, s->rep->wex));
701 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200702 if (s->analysis & AN_REQ_ANY) {
703 if (s->analysis & AN_REQ_INSPECT)
704 t->expire = tick_first(t->expire, s->inspect_exp);
705 else if (s->analysis & AN_REQ_HTTP_HDR)
706 t->expire = tick_first(t->expire, s->txn.exp);
707 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708
709 /* restore t to its place in the task list */
710 task_queue(t);
711
Willy Tarreaud825eef2007-05-12 22:35:00 +0200712 *next = t->expire;
713 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
715
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100716 s->fe->feconn--;
717 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200718 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 actconn--;
720
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200721 if (unlikely((global.mode & MODE_DEBUG) &&
722 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100724 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200725 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100726 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 write(1, trash, len);
728 }
729
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200730 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100731 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732
733 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200734 if (s->logs.logwait &&
735 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200736 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
737 if (s->fe->to_log & LW_REQ)
738 http_sess_log(s);
739 else
740 tcp_sess_log(s);
741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742
743 /* the task MUST not be in the run queue anymore */
744 task_delete(t);
745 session_free(s);
746 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200747 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748}
749
750
Willy Tarreau42250582007-04-01 01:30:43 +0200751extern const char sess_term_cond[8];
752extern const char sess_fin_state[8];
753extern const char *monthname[12];
754const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
755const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
756 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
757 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200758struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200759struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760
Willy Tarreau42250582007-04-01 01:30:43 +0200761/*
762 * send a log for the session when we have enough info about it.
763 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100764 */
Willy Tarreau42250582007-04-01 01:30:43 +0200765static void http_sess_log(struct session *s)
766{
767 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
768 struct proxy *fe = s->fe;
769 struct proxy *be = s->be;
770 struct proxy *prx_log;
771 struct http_txn *txn = &s->txn;
772 int tolog;
773 char *uri, *h;
774 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200775 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200776 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200777 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200778 int hdr;
779
780 if (fe->logfac1 < 0 && fe->logfac2 < 0)
781 return;
782 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100783
Willy Tarreau42250582007-04-01 01:30:43 +0200784 if (s->cli_addr.ss_family == AF_INET)
785 inet_ntop(AF_INET,
786 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
787 pn, sizeof(pn));
788 else
789 inet_ntop(AF_INET6,
790 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
791 pn, sizeof(pn));
792
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200793 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200794
795 /* FIXME: let's limit ourselves to frontend logging for now. */
796 tolog = fe->to_log;
797
798 h = tmpline;
799 if (fe->to_log & LW_REQHDR &&
800 txn->req.cap &&
801 (h < tmpline + sizeof(tmpline) - 10)) {
802 *(h++) = ' ';
803 *(h++) = '{';
804 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
805 if (hdr)
806 *(h++) = '|';
807 if (txn->req.cap[hdr] != NULL)
808 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
809 '#', hdr_encode_map, txn->req.cap[hdr]);
810 }
811 *(h++) = '}';
812 }
813
814 if (fe->to_log & LW_RSPHDR &&
815 txn->rsp.cap &&
816 (h < tmpline + sizeof(tmpline) - 7)) {
817 *(h++) = ' ';
818 *(h++) = '{';
819 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
820 if (hdr)
821 *(h++) = '|';
822 if (txn->rsp.cap[hdr] != NULL)
823 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
824 '#', hdr_encode_map, txn->rsp.cap[hdr]);
825 }
826 *(h++) = '}';
827 }
828
829 if (h < tmpline + sizeof(tmpline) - 4) {
830 *(h++) = ' ';
831 *(h++) = '"';
832 uri = txn->uri ? txn->uri : "<BADREQ>";
833 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
834 '#', url_encode_map, uri);
835 *(h++) = '"';
836 }
837 *h = '\0';
838
839 svid = (tolog & LW_SVID) ?
840 (s->data_source != DATA_SRC_STATS) ?
841 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
842
Willy Tarreau70089872008-06-13 21:12:51 +0200843 t_request = -1;
844 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
845 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
846
Willy Tarreau42250582007-04-01 01:30:43 +0200847 send_log(prx_log, LOG_INFO,
848 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
849 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100850 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200851 pn,
852 (s->cli_addr.ss_family == AF_INET) ?
853 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
854 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200855 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200856 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200857 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200858 t_request,
859 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200860 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
861 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
862 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
863 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100864 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200865 txn->cli_cookie ? txn->cli_cookie : "-",
866 txn->srv_cookie ? txn->srv_cookie : "-",
867 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
868 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
869 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
870 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
871 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100872 (s->flags & SN_REDISP)?"+":"",
873 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200874 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
875
876 s->logs.logwait = 0;
877}
878
Willy Tarreau117f59e2007-03-04 18:17:17 +0100879
880/*
881 * Capture headers from message starting at <som> according to header list
882 * <cap_hdr>, and fill the <idx> structure appropriately.
883 */
884void capture_headers(char *som, struct hdr_idx *idx,
885 char **cap, struct cap_hdr *cap_hdr)
886{
887 char *eol, *sol, *col, *sov;
888 int cur_idx;
889 struct cap_hdr *h;
890 int len;
891
892 sol = som + hdr_idx_first_pos(idx);
893 cur_idx = hdr_idx_first_idx(idx);
894
895 while (cur_idx) {
896 eol = sol + idx->v[cur_idx].len;
897
898 col = sol;
899 while (col < eol && *col != ':')
900 col++;
901
902 sov = col + 1;
903 while (sov < eol && http_is_lws[(unsigned char)*sov])
904 sov++;
905
906 for (h = cap_hdr; h; h = h->next) {
907 if ((h->namelen == col - sol) &&
908 (strncasecmp(sol, h->name, h->namelen) == 0)) {
909 if (cap[h->index] == NULL)
910 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200911 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100912
913 if (cap[h->index] == NULL) {
914 Alert("HTTP capture : out of memory.\n");
915 continue;
916 }
917
918 len = eol - sov;
919 if (len > h->len)
920 len = h->len;
921
922 memcpy(cap[h->index], sov, len);
923 cap[h->index][len]=0;
924 }
925 }
926 sol = eol + idx->v[cur_idx].cr + 1;
927 cur_idx = idx->v[cur_idx].next;
928 }
929}
930
931
Willy Tarreau42250582007-04-01 01:30:43 +0200932/* either we find an LF at <ptr> or we jump to <bad>.
933 */
934#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
935
936/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
937 * otherwise to <http_msg_ood> with <state> set to <st>.
938 */
939#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
940 ptr++; \
941 if (likely(ptr < end)) \
942 goto good; \
943 else { \
944 state = (st); \
945 goto http_msg_ood; \
946 } \
947 } while (0)
948
949
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100951 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
953 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
954 * will give undefined results.
955 * Note that it is upon the caller's responsibility to ensure that ptr < end,
956 * and that msg->sol points to the beginning of the response.
957 * If a complete line is found (which implies that at least one CR or LF is
958 * found before <end>, the updated <ptr> is returned, otherwise NULL is
959 * returned indicating an incomplete line (which does not mean that parts have
960 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
961 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
962 * upon next call.
963 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200964 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
966 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200967 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100969const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
970 unsigned int state, const char *ptr, const char *end,
971 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100972{
973 __label__
974 http_msg_rpver,
975 http_msg_rpver_sp,
976 http_msg_rpcode,
977 http_msg_rpcode_sp,
978 http_msg_rpreason,
979 http_msg_rpline_eol,
980 http_msg_ood, /* out of data */
981 http_msg_invalid;
982
983 switch (state) {
984 http_msg_rpver:
985 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100986 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
988
989 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100990 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
992 }
993 goto http_msg_invalid;
994
995 http_msg_rpver_sp:
996 case HTTP_MSG_RPVER_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.st.c = ptr - msg_buf;
999 goto http_msg_rpcode;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1003 /* so it's a CR/LF, this is invalid */
1004 goto http_msg_invalid;
1005
1006 http_msg_rpcode:
1007 case HTTP_MSG_RPCODE:
1008 if (likely(!HTTP_IS_LWS(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1010
1011 if (likely(HTTP_IS_SPHT(*ptr))) {
1012 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1014 }
1015
1016 /* so it's a CR/LF, so there is no reason phrase */
1017 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1018 http_msg_rsp_reason:
1019 /* FIXME: should we support HTTP responses without any reason phrase ? */
1020 msg->sl.st.r = ptr - msg_buf;
1021 msg->sl.st.r_l = 0;
1022 goto http_msg_rpline_eol;
1023
1024 http_msg_rpcode_sp:
1025 case HTTP_MSG_RPCODE_SP:
1026 if (likely(!HTTP_IS_LWS(*ptr))) {
1027 msg->sl.st.r = ptr - msg_buf;
1028 goto http_msg_rpreason;
1029 }
1030 if (likely(HTTP_IS_SPHT(*ptr)))
1031 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1032 /* so it's a CR/LF, so there is no reason phrase */
1033 goto http_msg_rsp_reason;
1034
1035 http_msg_rpreason:
1036 case HTTP_MSG_RPREASON:
1037 if (likely(!HTTP_IS_CRLF(*ptr)))
1038 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1039 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1040 http_msg_rpline_eol:
1041 /* We have seen the end of line. Note that we do not
1042 * necessarily have the \n yet, but at least we know that we
1043 * have EITHER \r OR \n, otherwise the response would not be
1044 * complete. We can then record the response length and return
1045 * to the caller which will be able to register it.
1046 */
1047 msg->sl.st.l = ptr - msg->sol;
1048 return ptr;
1049
1050#ifdef DEBUG_FULL
1051 default:
1052 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1053 exit(1);
1054#endif
1055 }
1056
1057 http_msg_ood:
1058 /* out of data */
1059 if (ret_state)
1060 *ret_state = state;
1061 if (ret_ptr)
1062 *ret_ptr = (char *)ptr;
1063 return NULL;
1064
1065 http_msg_invalid:
1066 /* invalid message */
1067 if (ret_state)
1068 *ret_state = HTTP_MSG_ERROR;
1069 return NULL;
1070}
1071
1072
1073/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 * This function parses a request line between <ptr> and <end>, starting with
1075 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1076 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1077 * will give undefined results.
1078 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1079 * and that msg->sol points to the beginning of the request.
1080 * If a complete line is found (which implies that at least one CR or LF is
1081 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1082 * returned indicating an incomplete line (which does not mean that parts have
1083 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1084 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1085 * upon next call.
1086 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001087 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1089 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001090 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001092const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1093 unsigned int state, const char *ptr, const char *end,
1094 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001095{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 __label__
1097 http_msg_rqmeth,
1098 http_msg_rqmeth_sp,
1099 http_msg_rquri,
1100 http_msg_rquri_sp,
1101 http_msg_rqver,
1102 http_msg_rqline_eol,
1103 http_msg_ood, /* out of data */
1104 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 switch (state) {
1107 http_msg_rqmeth:
1108 case HTTP_MSG_RQMETH:
1109 if (likely(HTTP_IS_TOKEN(*ptr)))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001113 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1115 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 if (likely(HTTP_IS_CRLF(*ptr))) {
1118 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001119 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 http_msg_req09_uri:
1121 msg->sl.rq.u = ptr - msg_buf;
1122 http_msg_req09_uri_e:
1123 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1124 http_msg_req09_ver:
1125 msg->sl.rq.v = ptr - msg_buf;
1126 msg->sl.rq.v_l = 0;
1127 goto http_msg_rqline_eol;
1128 }
1129 goto http_msg_invalid;
1130
1131 http_msg_rqmeth_sp:
1132 case HTTP_MSG_RQMETH_SP:
1133 if (likely(!HTTP_IS_LWS(*ptr))) {
1134 msg->sl.rq.u = ptr - msg_buf;
1135 goto http_msg_rquri;
1136 }
1137 if (likely(HTTP_IS_SPHT(*ptr)))
1138 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1139 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1140 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 http_msg_rquri:
1143 case HTTP_MSG_RQURI:
1144 if (likely(!HTTP_IS_LWS(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 if (likely(HTTP_IS_SPHT(*ptr))) {
1148 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1149 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1153 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 http_msg_rquri_sp:
1156 case HTTP_MSG_RQURI_SP:
1157 if (likely(!HTTP_IS_LWS(*ptr))) {
1158 msg->sl.rq.v = ptr - msg_buf;
1159 goto http_msg_rqver;
1160 }
1161 if (likely(HTTP_IS_SPHT(*ptr)))
1162 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1163 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1164 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 http_msg_rqver:
1167 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001168 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001170
1171 if (likely(HTTP_IS_CRLF(*ptr))) {
1172 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1173 http_msg_rqline_eol:
1174 /* We have seen the end of line. Note that we do not
1175 * necessarily have the \n yet, but at least we know that we
1176 * have EITHER \r OR \n, otherwise the request would not be
1177 * complete. We can then record the request length and return
1178 * to the caller which will be able to register it.
1179 */
1180 msg->sl.rq.l = ptr - msg->sol;
1181 return ptr;
1182 }
1183
1184 /* neither an HTTP_VER token nor a CRLF */
1185 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187#ifdef DEBUG_FULL
1188 default:
1189 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1190 exit(1);
1191#endif
1192 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_ood:
1195 /* out of data */
1196 if (ret_state)
1197 *ret_state = state;
1198 if (ret_ptr)
1199 *ret_ptr = (char *)ptr;
1200 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 http_msg_invalid:
1203 /* invalid message */
1204 if (ret_state)
1205 *ret_state = HTTP_MSG_ERROR;
1206 return NULL;
1207}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001208
1209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210/*
1211 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001212 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 * when data are missing and recalled at the exact same location with no
1214 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001215 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1216 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1219{
1220 __label__
1221 http_msg_rqbefore,
1222 http_msg_rqbefore_cr,
1223 http_msg_rqmeth,
1224 http_msg_rqline_end,
1225 http_msg_hdr_first,
1226 http_msg_hdr_name,
1227 http_msg_hdr_l1_sp,
1228 http_msg_hdr_l1_lf,
1229 http_msg_hdr_l1_lws,
1230 http_msg_hdr_val,
1231 http_msg_hdr_l2_lf,
1232 http_msg_hdr_l2_lws,
1233 http_msg_complete_header,
1234 http_msg_last_lf,
1235 http_msg_ood, /* out of data */
1236 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001237
Willy Tarreaue69eada2008-01-27 00:34:10 +01001238 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001241 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 ptr = buf->lr;
1243 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 if (unlikely(ptr >= end))
1246 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 /*
1250 * First, states that are specific to the response only.
1251 * We check them first so that request and headers are
1252 * closer to each other (accessed more often).
1253 */
1254 http_msg_rpbefore:
1255 case HTTP_MSG_RPBEFORE:
1256 if (likely(HTTP_IS_TOKEN(*ptr))) {
1257 if (likely(ptr == buf->data)) {
1258 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001259 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 } else {
1261#if PARSE_PRESERVE_EMPTY_LINES
1262 /* only skip empty leading lines, don't remove them */
1263 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001264 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001265#else
1266 /* Remove empty leading lines, as recommended by
1267 * RFC2616. This takes a lot of time because we
1268 * must move all the buffer backwards, but this
1269 * is rarely needed. The method above will be
1270 * cleaner when we'll be able to start sending
1271 * the request from any place in the buffer.
1272 */
1273 buf->lr = ptr;
1274 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001275 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 msg->sol = buf->data;
1277 ptr = buf->data;
1278 end = buf->r;
1279#endif
1280 }
1281 hdr_idx_init(idx);
1282 state = HTTP_MSG_RPVER;
1283 goto http_msg_rpver;
1284 }
1285
1286 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1287 goto http_msg_invalid;
1288
1289 if (unlikely(*ptr == '\n'))
1290 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1292 /* stop here */
1293
1294 http_msg_rpbefore_cr:
1295 case HTTP_MSG_RPBEFORE_CR:
1296 EXPECT_LF_HERE(ptr, http_msg_invalid);
1297 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1298 /* stop here */
1299
1300 http_msg_rpver:
1301 case HTTP_MSG_RPVER:
1302 case HTTP_MSG_RPVER_SP:
1303 case HTTP_MSG_RPCODE:
1304 case HTTP_MSG_RPCODE_SP:
1305 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001306 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001307 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 if (unlikely(!ptr))
1309 return;
1310
1311 /* we have a full response and we know that we have either a CR
1312 * or an LF at <ptr>.
1313 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1316
1317 msg->sol = ptr;
1318 if (likely(*ptr == '\r'))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1320 goto http_msg_rpline_end;
1321
1322 http_msg_rpline_end:
1323 case HTTP_MSG_RPLINE_END:
1324 /* msg->sol must point to the first of CR or LF. */
1325 EXPECT_LF_HERE(ptr, http_msg_invalid);
1326 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1327 /* stop here */
1328
1329 /*
1330 * Second, states that are specific to the request only
1331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 http_msg_rqbefore:
1333 case HTTP_MSG_RQBEFORE:
1334 if (likely(HTTP_IS_TOKEN(*ptr))) {
1335 if (likely(ptr == buf->data)) {
1336 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 } else {
1339#if PARSE_PRESERVE_EMPTY_LINES
1340 /* only skip empty leading lines, don't remove them */
1341 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001342 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001343#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 /* Remove empty leading lines, as recommended by
1345 * RFC2616. This takes a lot of time because we
1346 * must move all the buffer backwards, but this
1347 * is rarely needed. The method above will be
1348 * cleaner when we'll be able to start sending
1349 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001350 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 buf->lr = ptr;
1352 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001353 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 msg->sol = buf->data;
1355 ptr = buf->data;
1356 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001359 /* we will need this when keep-alive will be supported
1360 hdr_idx_init(idx);
1361 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 state = HTTP_MSG_RQMETH;
1363 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1367 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 if (unlikely(*ptr == '\n'))
1370 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 http_msg_rqbefore_cr:
1375 case HTTP_MSG_RQBEFORE_CR:
1376 EXPECT_LF_HERE(ptr, http_msg_invalid);
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 http_msg_rqmeth:
1381 case HTTP_MSG_RQMETH:
1382 case HTTP_MSG_RQMETH_SP:
1383 case HTTP_MSG_RQURI:
1384 case HTTP_MSG_RQURI_SP:
1385 case HTTP_MSG_RQVER:
1386 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001387 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(!ptr))
1389 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 /* we have a full request and we know that we have either a CR
1392 * or an LF at <ptr>.
1393 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 //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 +01001395 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 msg->sol = ptr;
1398 if (likely(*ptr == '\r'))
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 http_msg_rqline_end:
1403 case HTTP_MSG_RQLINE_END:
1404 /* check for HTTP/0.9 request : no version information available.
1405 * msg->sol must point to the first of CR or LF.
1406 */
1407 if (unlikely(msg->sl.rq.v_l == 0))
1408 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 EXPECT_LF_HERE(ptr, http_msg_invalid);
1411 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001412 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 /*
1415 * Common states below
1416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 http_msg_hdr_first:
1418 case HTTP_MSG_HDR_FIRST:
1419 msg->sol = ptr;
1420 if (likely(!HTTP_IS_CRLF(*ptr))) {
1421 goto http_msg_hdr_name;
1422 }
1423
1424 if (likely(*ptr == '\r'))
1425 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1426 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_hdr_name:
1429 case HTTP_MSG_HDR_NAME:
1430 /* assumes msg->sol points to the first char */
1431 if (likely(HTTP_IS_TOKEN(*ptr)))
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(*ptr == ':')) {
1435 msg->col = ptr - buf->data;
1436 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1437 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 http_msg_hdr_l1_sp:
1442 case HTTP_MSG_HDR_L1_SP:
1443 /* assumes msg->sol points to the first char and msg->col to the colon */
1444 if (likely(HTTP_IS_SPHT(*ptr)))
1445 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 /* header value can be basically anything except CR/LF */
1448 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 if (likely(!HTTP_IS_CRLF(*ptr))) {
1451 goto http_msg_hdr_val;
1452 }
1453
1454 if (likely(*ptr == '\r'))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1456 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 http_msg_hdr_l1_lf:
1459 case HTTP_MSG_HDR_L1_LF:
1460 EXPECT_LF_HERE(ptr, http_msg_invalid);
1461 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 http_msg_hdr_l1_lws:
1464 case HTTP_MSG_HDR_L1_LWS:
1465 if (likely(HTTP_IS_SPHT(*ptr))) {
1466 /* replace HT,CR,LF with spaces */
1467 for (; buf->data+msg->sov < ptr; msg->sov++)
1468 buf->data[msg->sov] = ' ';
1469 goto http_msg_hdr_l1_sp;
1470 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001471 /* we had a header consisting only in spaces ! */
1472 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 goto http_msg_complete_header;
1474
1475 http_msg_hdr_val:
1476 case HTTP_MSG_HDR_VAL:
1477 /* assumes msg->sol points to the first char, msg->col to the
1478 * colon, and msg->sov points to the first character of the
1479 * value.
1480 */
1481 if (likely(!HTTP_IS_CRLF(*ptr)))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 msg->eol = ptr;
1485 /* Note: we could also copy eol into ->eoh so that we have the
1486 * real header end in case it ends with lots of LWS, but is this
1487 * really needed ?
1488 */
1489 if (likely(*ptr == '\r'))
1490 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1491 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 http_msg_hdr_l2_lf:
1494 case HTTP_MSG_HDR_L2_LF:
1495 EXPECT_LF_HERE(ptr, http_msg_invalid);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 http_msg_hdr_l2_lws:
1499 case HTTP_MSG_HDR_L2_LWS:
1500 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1501 /* LWS: replace HT,CR,LF with spaces */
1502 for (; msg->eol < ptr; msg->eol++)
1503 *msg->eol = ' ';
1504 goto http_msg_hdr_val;
1505 }
1506 http_msg_complete_header:
1507 /*
1508 * It was a new header, so the last one is finished.
1509 * Assumes msg->sol points to the first char, msg->col to the
1510 * colon, msg->sov points to the first character of the value
1511 * and msg->eol to the first CR or LF so we know how the line
1512 * ends. We insert last header into the index.
1513 */
1514 /*
1515 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1516 write(2, msg->sol, msg->eol-msg->sol);
1517 fprintf(stderr,"\n");
1518 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1521 idx, idx->tail) < 0))
1522 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 msg->sol = ptr;
1525 if (likely(!HTTP_IS_CRLF(*ptr))) {
1526 goto http_msg_hdr_name;
1527 }
1528
1529 if (likely(*ptr == '\r'))
1530 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1531 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 http_msg_last_lf:
1534 case HTTP_MSG_LAST_LF:
1535 /* Assumes msg->sol points to the first of either CR or LF */
1536 EXPECT_LF_HERE(ptr, http_msg_invalid);
1537 ptr++;
1538 buf->lr = ptr;
1539 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001540 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 return;
1542#ifdef DEBUG_FULL
1543 default:
1544 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1545 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001546#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 }
1548 http_msg_ood:
1549 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 buf->lr = ptr;
1552 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 http_msg_invalid:
1555 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001556 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 return;
1558}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001559
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001560/* This function performs all the processing enabled for the current request.
1561 * It returns 1 if it changes its state and it believes that another function
1562 * must be updated, otherwise zero. It might make sense to explode it into
1563 * several other functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001565int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 struct buffer *req = t->req;
1568 struct buffer *rep = t->rep;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001569 int fsm_resync = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreaue46ab552008-08-13 19:19:37 +02001571 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
1572 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001573 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreauf161a342007-04-08 16:59:42 +02001574 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001575 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001577 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001578 struct tcp_rule *rule;
1579 int partial;
1580
1581 /* We will abort if we encounter a read error. In theory,
1582 * we should not abort if we get a close, it might be
1583 * valid, also very unlikely. FIXME: we'll abort for now,
1584 * this will be easier to change later.
1585 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001586 if (req->flags & BF_READ_ERROR) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001587 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001588 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001589 t->fe->failed_req++;
1590 if (!(t->flags & SN_ERR_MASK))
1591 t->flags |= SN_ERR_CLICL;
1592 if (!(t->flags & SN_FINST_MASK))
1593 t->flags |= SN_FINST_R;
1594 return 1;
1595 }
1596
1597 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001598 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001599 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001600 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001601 t->fe->failed_req++;
1602 if (!(t->flags & SN_ERR_MASK))
1603 t->flags |= SN_ERR_CLITO;
1604 if (!(t->flags & SN_FINST_MASK))
1605 t->flags |= SN_FINST_R;
1606 return 1;
1607 }
1608
1609 /* We don't know whether we have enough data, so must proceed
1610 * this way :
1611 * - iterate through all rules in their declaration order
1612 * - if one rule returns MISS, it means the inspect delay is
1613 * not over yet, then return immediately, otherwise consider
1614 * it as a non-match.
1615 * - if one rule returns OK, then return OK
1616 * - if one rule returns KO, then return KO
1617 */
1618
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001619 if (req->flags & (BF_READ_NULL | BF_SHUTR_STATUS) ||
1620 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001621 partial = 0;
1622 else
1623 partial = ACL_PARTIAL;
1624
1625 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1626 int ret = ACL_PAT_PASS;
1627
1628 if (rule->cond) {
1629 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1630 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001631 /* just set the request timeout once at the beginning of the request */
1632 if (!tick_isset(t->inspect_exp))
1633 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
1634
1635 return fsm_resync;
Willy Tarreaub6866442008-07-14 23:54:42 +02001636 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001637
Willy Tarreaub6866442008-07-14 23:54:42 +02001638 ret = acl_pass(ret);
1639 if (rule->cond->pol == ACL_COND_UNLESS)
1640 ret = !ret;
1641 }
1642
1643 if (ret) {
1644 /* we have a matching rule. */
1645 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001646 buffer_shutr_done(req);
1647 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001648 fd_delete(t->cli_fd);
1649 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001650 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001651 t->fe->failed_req++;
1652 if (!(t->flags & SN_ERR_MASK))
1653 t->flags |= SN_ERR_PRXCOND;
1654 if (!(t->flags & SN_FINST_MASK))
1655 t->flags |= SN_FINST_R;
1656 t->inspect_exp = TICK_ETERNITY;
1657 return 1;
1658 }
1659 /* otherwise accept */
1660 break;
1661 }
1662 }
1663
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001664 /* if we get there, it means we have no rule which matches, or
1665 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001666 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001667 t->analysis &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001668 t->inspect_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001669 fsm_resync = 1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001670 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001671
1672 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 /*
1674 * Now parse the partial (or complete) lines.
1675 * We will check the request syntax, and also join multi-line
1676 * headers. An index of all the lines will be elaborated while
1677 * parsing.
1678 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001679 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001682 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 * req->data + req->eoh = end of processed headers / start of current one
1684 * req->data + req->eol = end of current header or line (LF or CRLF)
1685 * req->lr = first non-visited byte
1686 * req->r = end of data
1687 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001690 struct http_txn *txn = &t->txn;
1691 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001695 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 /* 1: we might have to print this header in debug mode */
1698 if (unlikely((global.mode & MODE_DEBUG) &&
1699 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001700 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001703 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 eol = sol + msg->sl.rq.l;
1705 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001706
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001707 sol += hdr_idx_first_pos(&txn->hdr_idx);
1708 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001711 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001713 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1714 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 }
1716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001717
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718
1719 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001721 * If not so, we check the FD and buffer states before leaving.
1722 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1724 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001725 *
1726 */
1727
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 /*
1730 * First, let's catch bad requests.
1731 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001732 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001734
1735 /* 1: Since we are in header mode, if there's no space
1736 * left for headers, we won't be able to free more
1737 * later, so the session will never terminate. We
1738 * must terminate it now.
1739 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 if (unlikely(req->l >= req->rlim - req->data)) {
1741 /* FIXME: check if URI is set and return Status
1742 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001743 */
Willy Tarreau06619262006-12-17 08:37:22 +01001744 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745 }
1746
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001747 /* 2: have we encountered a close ? */
1748 else if (req->flags & BF_READ_NULL) {
1749 txn->status = 400;
1750 client_retnclose(t, error_message(t, HTTP_ERR_400));
1751 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001752 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001753 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001754
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755 if (!(t->flags & SN_ERR_MASK))
1756 t->flags |= SN_ERR_CLICL;
1757 if (!(t->flags & SN_FINST_MASK))
1758 t->flags |= SN_FINST_R;
1759 return 1;
1760 }
1761
1762 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001763 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001764 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001765 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001766 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001767 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001768 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001769 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001770 if (!(t->flags & SN_ERR_MASK))
1771 t->flags |= SN_ERR_CLITO;
1772 if (!(t->flags & SN_FINST_MASK))
1773 t->flags |= SN_FINST_R;
1774 return 1;
1775 }
1776
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001777 /* 4: have we encountered a read error or did we have to shutdown ? */
1778 else if (req->flags & (BF_READ_ERROR | BF_SHUTR_STATUS)) {
1779 /* we cannot return any message on error */
1780 msg->msg_state = HTTP_MSG_ERROR;
1781 t->analysis &= ~AN_REQ_ANY;
1782 t->fe->failed_req++;
1783 if (!(t->flags & SN_ERR_MASK))
1784 t->flags |= SN_ERR_CLICL;
1785 if (!(t->flags & SN_FINST_MASK))
1786 t->flags |= SN_FINST_R;
1787 return 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001789
1790 /* just set the request timeout once at the beginning of the request */
1791 if (!tick_isset(t->txn.exp))
1792 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1793
1794 /* we're not ready yet */
1795 return fsm_resync;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796 }
1797
1798
1799 /****************************************************************
1800 * More interesting part now : we know that we have a complete *
1801 * request which at least looks like HTTP. We have an indicator *
1802 * of each header's length, so we can parse them quickly. *
1803 ****************************************************************/
1804
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001805 t->analysis &= ~AN_REQ_HTTP_HDR;
1806
Willy Tarreau9cdde232007-05-02 20:58:19 +02001807 /* ensure we keep this pointer to the beginning of the message */
1808 msg->sol = req->data + msg->som;
1809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 /*
1811 * 1: identify the method
1812 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001813 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001814
1815 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001817 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 if (unlikely((t->fe->monitor_uri_len != 0) &&
1821 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1822 !memcmp(&req->data[msg->sl.rq.u],
1823 t->fe->monitor_uri,
1824 t->fe->monitor_uri_len))) {
1825 /*
1826 * We have found the monitor URI
1827 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001828 struct acl_cond *cond;
1829 cur_proxy = t->fe;
1830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001832
1833 /* Check if we want to fail this monitor request or not */
1834 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1835 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001836
1837 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001838 if (cond->pol == ACL_COND_UNLESS)
1839 ret = !ret;
1840
1841 if (ret) {
1842 /* we fail this request, let's return 503 service unavail */
1843 txn->status = 503;
1844 client_retnclose(t, error_message(t, HTTP_ERR_503));
1845 goto return_prx_cond;
1846 }
1847 }
1848
1849 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001850 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 client_retnclose(t, &http_200_chunk);
1852 goto return_prx_cond;
1853 }
1854
1855 /*
1856 * 3: Maybe we have to copy the original REQURI for the logs ?
1857 * Note: we cannot log anymore if the request has been
1858 * classified as invalid.
1859 */
1860 if (unlikely(t->logs.logwait & LW_REQ)) {
1861 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001862 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 int urilen = msg->sl.rq.l;
1864
1865 if (urilen >= REQURI_LEN)
1866 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001867 memcpy(txn->uri, &req->data[msg->som], urilen);
1868 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869
1870 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001871 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 } else {
1873 Alert("HTTP logging : out of memory.\n");
1874 }
1875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001876
Willy Tarreau06619262006-12-17 08:37:22 +01001877
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1879 if (unlikely(msg->sl.rq.v_l == 0)) {
1880 int delta;
1881 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001882 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 cur_end = msg->sol + msg->sl.rq.l;
1884 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 if (msg->sl.rq.u_l == 0) {
1887 /* if no URI was set, add "/" */
1888 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1889 cur_end += delta;
1890 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001891 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 /* add HTTP version */
1893 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1894 msg->eoh += delta;
1895 cur_end += delta;
1896 cur_end = (char *)http_parse_reqline(msg, req->data,
1897 HTTP_MSG_RQMETH,
1898 msg->sol, cur_end + 1,
1899 NULL, NULL);
1900 if (unlikely(!cur_end))
1901 goto return_bad_req;
1902
1903 /* we have a full HTTP/1.0 request now and we know that
1904 * we have either a CR or an LF at <ptr>.
1905 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001906 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001907 }
1908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909
1910 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001911 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001912 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001913 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001914
1915 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001917 * As opposed to version 1.2, now they will be evaluated in the
1918 * filters order and not in the header order. This means that
1919 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001920 *
1921 * We can now check whether we want to switch to another
1922 * backend, in which case we will re-check the backend's
1923 * filters and various options. In order to support 3-level
1924 * switching, here's how we should proceed :
1925 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001926 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001927 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001928 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001929 * There cannot be any switch from there, so ->be cannot be
1930 * changed anymore.
1931 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001932 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001933 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001934 * The response path will be able to apply either ->be, or
1935 * ->be then ->fe filters in order to match the reverse of
1936 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001937 */
1938
Willy Tarreau06619262006-12-17 08:37:22 +01001939 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001940 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001941 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001942 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001943 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001944
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001945 /* first check whether we have some ACLs set to redirect this request */
1946 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1947 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001948
1949 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001950 if (rule->cond->pol == ACL_COND_UNLESS)
1951 ret = !ret;
1952
1953 if (ret) {
1954 struct chunk rdr = { trash, 0 };
1955 const char *msg_fmt;
1956
1957 /* build redirect message */
1958 switch(rule->code) {
1959 case 303:
1960 rdr.len = strlen(HTTP_303);
1961 msg_fmt = HTTP_303;
1962 break;
1963 case 301:
1964 rdr.len = strlen(HTTP_301);
1965 msg_fmt = HTTP_301;
1966 break;
1967 case 302:
1968 default:
1969 rdr.len = strlen(HTTP_302);
1970 msg_fmt = HTTP_302;
1971 break;
1972 }
1973
1974 if (unlikely(rdr.len > sizeof(trash)))
1975 goto return_bad_req;
1976 memcpy(rdr.str, msg_fmt, rdr.len);
1977
1978 switch(rule->type) {
1979 case REDIRECT_TYPE_PREFIX: {
1980 const char *path;
1981 int pathlen;
1982
1983 path = http_get_path(txn);
1984 /* build message using path */
1985 if (path) {
1986 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1987 } else {
1988 path = "/";
1989 pathlen = 1;
1990 }
1991
1992 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1993 goto return_bad_req;
1994
1995 /* add prefix */
1996 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1997 rdr.len += rule->rdr_len;
1998
1999 /* add path */
2000 memcpy(rdr.str + rdr.len, path, pathlen);
2001 rdr.len += pathlen;
2002 break;
2003 }
2004 case REDIRECT_TYPE_LOCATION:
2005 default:
2006 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2007 goto return_bad_req;
2008
2009 /* add location */
2010 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2011 rdr.len += rule->rdr_len;
2012 break;
2013 }
2014
2015 /* add end of headers */
2016 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2017 rdr.len += 4;
2018
2019 txn->status = rule->code;
2020 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002021 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002022 client_retnclose(t, &rdr);
2023 goto return_prx_cond;
2024 }
2025 }
2026
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002027 /* first check whether we have some ACLs set to block this request */
2028 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002029 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002030
2031 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002032 if (cond->pol == ACL_COND_UNLESS)
2033 ret = !ret;
2034
2035 if (ret) {
2036 txn->status = 403;
2037 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002038 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002039 client_retnclose(t, error_message(t, HTTP_ERR_403));
2040 goto return_prx_cond;
2041 }
2042 }
2043
Willy Tarreau06619262006-12-17 08:37:22 +01002044 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002045 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2047 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002048 }
2049
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002050 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2051 /* to ensure correct connection accounting on
2052 * the backend, we count the connection for the
2053 * one managing the queue.
2054 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002055 t->be->beconn++;
2056 if (t->be->beconn > t->be->beconn_max)
2057 t->be->beconn_max = t->be->beconn;
2058 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002059 t->flags |= SN_BE_ASSIGNED;
2060 }
2061
Willy Tarreau06619262006-12-17 08:37:22 +01002062 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002063 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002064 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002065 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002066 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002067 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002068 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002069 goto return_prx_cond;
2070 }
2071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002072 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002073 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002074 !(t->flags & SN_CONN_CLOSED)) {
2075 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002076 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002077 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002078
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002079 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002080 old_idx = 0;
2081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002082 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2083 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002084 cur_ptr = cur_next;
2085 cur_end = cur_ptr + cur_hdr->len;
2086 cur_next = cur_end + cur_hdr->cr + 1;
2087
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002088 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2089 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002090 /* 3 possibilities :
2091 * - we have already set Connection: close,
2092 * so we remove this line.
2093 * - we have not yet set Connection: close,
2094 * but this line indicates close. We leave
2095 * it untouched and set the flag.
2096 * - we have not yet set Connection: close,
2097 * and this line indicates non-close. We
2098 * replace it.
2099 */
2100 if (t->flags & SN_CONN_CLOSED) {
2101 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002102 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002104 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2105 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002106 cur_hdr->len = 0;
2107 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002108 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2109 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2110 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 cur_next += delta;
2112 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002113 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002114 }
2115 t->flags |= SN_CONN_CLOSED;
2116 }
2117 }
2118 old_idx = cur_idx;
2119 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002120 }
2121 /* add request headers from the rule sets in the same order */
2122 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2123 if (unlikely(http_header_add_tail(req,
2124 &txn->req,
2125 &txn->hdr_idx,
2126 rule_set->req_add[cur_idx])) < 0)
2127 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002128 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002131 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002132 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002133 /* we have to check the URI and auth for this request.
2134 * FIXME!!! that one is rather dangerous, we want to
2135 * make it follow standard rules (eg: clear t->analysis).
2136 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002137 if (stats_check_uri_auth(t, rule_set))
2138 return 1;
2139 }
2140
Willy Tarreau55ea7572007-06-17 19:56:27 +02002141 /* now check whether we have some switching rules for this request */
2142 if (!(t->flags & SN_BE_ASSIGNED)) {
2143 struct switching_rule *rule;
2144
2145 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2146 int ret;
2147
2148 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002149
2150 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002151 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002152 ret = !ret;
2153
2154 if (ret) {
2155 t->be = rule->be.backend;
2156 t->be->beconn++;
2157 if (t->be->beconn > t->be->beconn_max)
2158 t->be->beconn_max = t->be->beconn;
2159 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002160
2161 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002162 t->rep->rto = t->req->wto = t->be->timeout.server;
2163 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002164 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002165 t->flags |= SN_BE_ASSIGNED;
2166 break;
2167 }
2168 }
2169 }
2170
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002171 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2172 /* No backend was set, but there was a default
2173 * backend set in the frontend, so we use it and
2174 * loop again.
2175 */
2176 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002177 t->be->beconn++;
2178 if (t->be->beconn > t->be->beconn_max)
2179 t->be->beconn_max = t->be->beconn;
2180 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002181
2182 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002183 t->rep->rto = t->req->wto = t->be->timeout.server;
2184 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002185 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002186 t->flags |= SN_BE_ASSIGNED;
2187 }
2188 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002189
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002191 if (!(t->flags & SN_BE_ASSIGNED)) {
2192 /* To ensure correct connection accounting on
2193 * the backend, we count the connection for the
2194 * one managing the queue.
2195 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002196 t->be->beconn++;
2197 if (t->be->beconn > t->be->beconn_max)
2198 t->be->beconn_max = t->be->beconn;
2199 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002200 t->flags |= SN_BE_ASSIGNED;
2201 }
2202
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002203 /*
2204 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002205 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002206 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002207 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002208 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002210 /*
2211 * If HTTP PROXY is set we simply get remote server address
2212 * parsing incoming request.
2213 */
2214 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2215 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217
Willy Tarreau2a324282006-12-05 00:05:46 +01002218 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002219 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002220 * so let's do the same now.
2221 */
2222
2223 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002224 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002225 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002226 }
2227
2228
2229 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002230 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002231 * Note that doing so might move headers in the request, but
2232 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002233 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002234 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002235 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2236 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002237 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002238
Willy Tarreau58f10d72006-12-04 02:26:12 +01002239
Willy Tarreau2a324282006-12-05 00:05:46 +01002240 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002241 * 9: add X-Forwarded-For if either the frontend or the backend
2242 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002243 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002244 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002245 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002246 /* Add an X-Forwarded-For header unless the source IP is
2247 * in the 'except' network range.
2248 */
2249 if ((!t->fe->except_mask.s_addr ||
2250 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2251 != t->fe->except_net.s_addr) &&
2252 (!t->be->except_mask.s_addr ||
2253 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2254 != t->be->except_net.s_addr)) {
2255 int len;
2256 unsigned char *pn;
2257 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002258
Ross Westaf72a1d2008-08-03 10:51:45 +02002259 /* Note: we rely on the backend to get the header name to be used for
2260 * x-forwarded-for, because the header is really meant for the backends.
2261 * However, if the backend did not specify any option, we have to rely
2262 * on the frontend's header name.
2263 */
2264 if (t->be->fwdfor_hdr_len) {
2265 len = t->be->fwdfor_hdr_len;
2266 memcpy(trash, t->be->fwdfor_hdr_name, len);
2267 } else {
2268 len = t->fe->fwdfor_hdr_len;
2269 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2270 }
2271 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002272
Ross Westaf72a1d2008-08-03 10:51:45 +02002273 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002274 &txn->hdr_idx, trash, len)) < 0)
2275 goto return_bad_req;
2276 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002277 }
2278 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002279 /* FIXME: for the sake of completeness, we should also support
2280 * 'except' here, although it is mostly useless in this case.
2281 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002282 int len;
2283 char pn[INET6_ADDRSTRLEN];
2284 inet_ntop(AF_INET6,
2285 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2286 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002287
2288 /* Note: we rely on the backend to get the header name to be used for
2289 * x-forwarded-for, because the header is really meant for the backends.
2290 * However, if the backend did not specify any option, we have to rely
2291 * on the frontend's header name.
2292 */
2293 if (t->be->fwdfor_hdr_len) {
2294 len = t->be->fwdfor_hdr_len;
2295 memcpy(trash, t->be->fwdfor_hdr_name, len);
2296 } else {
2297 len = t->fe->fwdfor_hdr_len;
2298 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2299 }
2300 len += sprintf(trash + len, ": %s", pn);
2301
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002302 if (unlikely(http_header_add_tail2(req, &txn->req,
2303 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002304 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002305 }
2306 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002309 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002310 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002311 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002312 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002313 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002314 if ((unlikely(msg->sl.rq.v_l != 8) ||
2315 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2316 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002317 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002318 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002319 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002320 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002321 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2322 * If not assigned, perhaps we are balancing on url_param, but this is a
2323 * POST; and the parameters are in the body, maybe scan there to find our server.
2324 * (unless headers overflowed the buffer?)
2325 */
2326 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2327 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002328 t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002329 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2330 /* are there enough bytes here? total == l || r || rlim ?
2331 * len is unsigned, but eoh is int,
2332 * how many bytes of body have we received?
2333 * eoh is the first empty line of the header
2334 */
2335 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002336 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 +02002337
2338 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2339 * We can't assume responsibility for the server's decision,
2340 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2341 * We also can't change our mind later, about which server to choose, so round robin.
2342 */
2343 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2344 struct hdr_ctx ctx;
2345 ctx.idx = 0;
2346 /* Expect is allowed in 1.1, look for it */
2347 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2348 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002349 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002350 /* We can't reliablly stall and wait for data, because of
2351 * .NET clients that don't conform to rfc2616; so, no need for
2352 * the next block to check length expectations.
2353 * We could send 100 status back to the client, but then we need to
2354 * re-write headers, and send the message. And this isn't the right
2355 * place for that action.
2356 * TODO: support Expect elsewhere and delete this block.
2357 */
2358 goto end_check_maybe_wait_for_body;
2359 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002360
2361 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002362 /* nothing to do, we got enough */
2363 } else {
2364 /* limit implies we are supposed to need this many bytes
2365 * to find the parameter. Let's see how many bytes we can wait for.
2366 */
2367 long long hint = len;
2368 struct hdr_ctx ctx;
2369 ctx.idx = 0;
2370 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002371 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002372 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002373 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002374 ctx.idx = 0;
2375 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2376 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002377 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002378 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002379 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002380 hint = 0; /* parse failure, untrusted client */
2381 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002382 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002383 msg->hdr_content_len = hint;
2384 else
2385 hint = 0; /* bad client, sent negative length */
2386 }
2387 }
2388 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002389 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002390 hint = t->be->url_param_post_limit;
2391 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002392 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002393 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002394 /* else... There are no body bytes to wait for */
2395 }
2396 }
2397 }
2398 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002399
Willy Tarreau2a324282006-12-05 00:05:46 +01002400 /*************************************************************
2401 * OK, that's finished for the headers. We have done what we *
2402 * could. Let's switch to the DATA state. *
2403 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404
Willy Tarreauadfb8562008-08-11 15:24:42 +02002405 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002406 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002407
Willy Tarreau1fa31262007-12-03 00:36:16 +01002408 /* When a connection is tarpitted, we use the tarpit timeout,
2409 * which may be the same as the connect timeout if unspecified.
2410 * If unset, then set it to zero because we really want it to
2411 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002412 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002413 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002414 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002415 t->req->l = 0;
2416 /* flush the request so that we can drop the connection early
2417 * if the client closes first.
2418 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002419 req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2420 if (!req->cex)
2421 req->cex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002422 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002424 /* OK let's go on with the BODY now */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002425 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002426 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002427
2428 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002429 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002430 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002431 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002432 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002433 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002434 return_prx_cond:
2435 if (!(t->flags & SN_ERR_MASK))
2436 t->flags |= SN_ERR_PRXCOND;
2437 if (!(t->flags & SN_FINST_MASK))
2438 t->flags |= SN_FINST_R;
2439 return 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002440 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002441 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002442 }
2443
2444 if (t->analysis & AN_REQ_HTTP_BODY) {
2445 /* We have to parse the HTTP request body to find any required data.
2446 * "balance url_param check_post" should have been the only way to get
2447 * into this. We were brought here after HTTP header analysis, so all
2448 * related structures are ready.
2449 */
2450 struct http_msg *msg = &t->txn.req;
2451 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2452 long long limit = t->be->url_param_post_limit;
2453 struct hdr_ctx ctx;
2454
2455 ctx.idx = 0;
2456
2457 /* now if we have a length, we'll take the hint */
2458 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2459 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2460 unsigned int chunk = 0;
2461 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2462 char c = msg->sol[body];
2463 if (ishex(c)) {
2464 unsigned int hex = toupper(c) - '0';
2465 if (hex > 9)
2466 hex -= 'A' - '9' - 1;
2467 chunk = (chunk << 4) | hex;
2468 } else
2469 break;
2470 body++;
2471 }
2472 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002473 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002474
2475 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002476 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002477
2478 body += 2; // skip CRLF
2479
2480 /* if we support more then one chunk here, we have to do it again when assigning server
2481 * 1. how much entity data do we have? new var
2482 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2483 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2484 */
2485
2486 if (chunk < limit)
2487 limit = chunk; /* only reading one chunk */
2488 } else {
2489 if (msg->hdr_content_len < limit)
2490 limit = msg->hdr_content_len;
2491 }
2492
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002493 http_body_end:
2494 /* we leave once we know we have nothing left to do. This means that we have
2495 * enough bytes, or that we know we'll not get any more (buffer full, read
2496 * buffer closed).
2497 */
2498 if (req->l - body >= limit || /* enough bytes! */
2499 req->l >= req->rlim - req->data || /* full */
2500 req->flags & (BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
2501 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002502 t->logs.tv_request = now; /* update the request timer to reflect full request */
2503 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002504 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002505 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002506 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002507
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002508 return fsm_resync;
2509}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002510
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002511/* This function performs all the processing enabled for the current response.
2512 * It returns 1 if it changes its state and it believes that another function
2513 * must be updated, otherwise zero. It might make sense to explode it into
2514 * several other functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002515 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002516int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002517{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002518 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002519 struct buffer *req = t->req;
2520 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002521
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002522 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002523 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002524 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002525 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
2526 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002527
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002528 if (t->analysis & AN_RTR_HTTP_HDR) { /* receiving server headers */
2529 /*
2530 * Now parse the partial (or complete) lines.
2531 * We will check the response syntax, and also join multi-line
2532 * headers. An index of all the lines will be elaborated while
2533 * parsing.
2534 *
2535 * For the parsing, we use a 28 states FSM.
2536 *
2537 * Here is the information we currently have :
2538 * rep->data + req->som = beginning of response
2539 * rep->data + req->eoh = end of processed headers / start of current one
2540 * rep->data + req->eol = end of current header or line (LF or CRLF)
2541 * rep->lr = first non-visited byte
2542 * rep->r = end of data
2543 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002544
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002545 int cur_idx;
2546 struct http_msg *msg = &txn->rsp;
2547 struct proxy *cur_proxy;
2548
2549 if (likely(rep->lr < rep->r))
2550 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2551
2552 /* 1: we might have to print this header in debug mode */
2553 if (unlikely((global.mode & MODE_DEBUG) &&
2554 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2555 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2556 char *eol, *sol;
2557
2558 sol = rep->data + msg->som;
2559 eol = sol + msg->sl.rq.l;
2560 debug_hdr("srvrep", t, sol, eol);
2561
2562 sol += hdr_idx_first_pos(&txn->hdr_idx);
2563 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2564
2565 while (cur_idx) {
2566 eol = sol + txn->hdr_idx.v[cur_idx].len;
2567 debug_hdr("srvhdr", t, sol, eol);
2568 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2569 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002570 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002572
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002573
2574 if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) {
2575 EV_FD_COND_S(t->srv_fd, DIR_RD);
2576 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
2577 * full. We cannot loop here since stream_sock_read will disable it only if
2578 * rep->l == rlim-data
2579 */
2580 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002581 }
2582
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002583
2584 /*
2585 * Now we quickly check if we have found a full valid response.
2586 * If not so, we check the FD and buffer states before leaving.
2587 * A full response is indicated by the fact that we have seen
2588 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2589 * responses are checked first.
2590 *
2591 * Depending on whether the client is still there or not, we
2592 * may send an error response back or not. Note that normally
2593 * we should only check for HTTP status there, and check I/O
2594 * errors somewhere else.
2595 */
2596
2597 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2598
2599 /* Invalid response, or read error or write error */
2600 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2601 (req->flags & BF_WRITE_ERROR) ||
2602 (rep->flags & BF_READ_ERROR))) {
2603 buffer_shutr_done(rep);
2604 buffer_shutw_done(req);
2605 fd_delete(t->srv_fd);
2606 if (t->srv) {
2607 t->srv->cur_sess--;
2608 t->srv->failed_resp++;
2609 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002610 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002611 t->be->failed_resp++;
2612 t->srv_state = SV_STCLOSE;
2613 t->analysis &= ~AN_RTR_ANY;
2614 rep->flags |= BF_MAY_FORWARD;
2615 txn->status = 502;
2616 client_return(t, error_message(t, HTTP_ERR_502));
2617 if (!(t->flags & SN_ERR_MASK))
2618 t->flags |= SN_ERR_SRVCL;
2619 if (!(t->flags & SN_FINST_MASK))
2620 t->flags |= SN_FINST_H;
2621 /* We used to have a free connection slot. Since we'll never use it,
2622 * we have to inform the server that it may be used by another session.
2623 */
2624 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2625 process_srv_queue(t->srv);
2626
2627 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002628 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002629
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002630 /* end of client write or end of server read.
2631 * since we are in header mode, if there's no space left for headers, we
2632 * won't be able to free more later, so the session will never terminate.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002633 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002634 else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) ||
2635 rep->l >= rep->rlim - rep->data)) {
2636 EV_FD_CLR(t->srv_fd, DIR_RD);
2637 buffer_shutr_done(rep);
2638 t->srv_state = SV_STSHUTR;
2639 t->analysis &= ~AN_RTR_ANY;
2640 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2641 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002642 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002643
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002644 /* read timeout : return a 504 to the client. */
2645 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
2646 tick_is_expired(rep->rex, now_ms))) {
2647 buffer_shutr_done(rep);
2648 buffer_shutw_done(req);
2649 fd_delete(t->srv_fd);
2650 if (t->srv) {
2651 t->srv->cur_sess--;
2652 t->srv->failed_resp++;
2653 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002654 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002655 t->be->failed_resp++;
2656 t->srv_state = SV_STCLOSE;
2657 t->analysis &= ~AN_RTR_ANY;
2658 rep->flags |= BF_MAY_FORWARD;
2659 txn->status = 504;
2660 client_return(t, error_message(t, HTTP_ERR_504));
2661 if (!(t->flags & SN_ERR_MASK))
2662 t->flags |= SN_ERR_SRVTO;
2663 if (!(t->flags & SN_FINST_MASK))
2664 t->flags |= SN_FINST_H;
2665 /* We used to have a free connection slot. Since we'll never use it,
2666 * we have to inform the server that it may be used by another session.
2667 */
2668 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2669 process_srv_queue(t->srv);
2670 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002673 /* last client read and buffer empty */
2674 /* FIXME!!! here, we don't want to switch to SHUTW if the
2675 * client shuts read too early, because we may still have
2676 * some work to do on the headers.
2677 * The side-effect is that if the client completely closes its
2678 * connection during SV_STHEADER, the connection to the server
2679 * is kept until a response comes back or the timeout is reached.
2680 * This sometimes causes fast loops when the request buffer is
2681 * full, so we still perform the transition right now. It will
2682 * make sense later anyway.
2683 */
2684 else if (unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002685
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002686 EV_FD_CLR(t->srv_fd, DIR_WR);
2687 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002688
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002689 /* We must ensure that the read part is still
2690 * alive when switching to shutw */
2691 EV_FD_SET(t->srv_fd, DIR_RD);
2692 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauee991362007-05-14 14:37:50 +02002693
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002694 shutdown(t->srv_fd, SHUT_WR);
2695 t->srv_state = SV_STSHUTW;
2696 t->analysis &= ~AN_RTR_ANY;
2697 return 1;
2698 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002699
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002700 /* write timeout */
2701 /* FIXME!!! here, we don't want to switch to SHUTW if the
2702 * client shuts read too early, because we may still have
2703 * some work to do on the headers.
2704 */
2705 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
2706 tick_is_expired(req->wex, now_ms))) {
2707 EV_FD_CLR(t->srv_fd, DIR_WR);
2708 buffer_shutw_done(req);
2709 shutdown(t->srv_fd, SHUT_WR);
2710 /* We must ensure that the read part is still alive
2711 * when switching to shutw */
2712 EV_FD_SET(t->srv_fd, DIR_RD);
2713 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002714
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002715 t->srv_state = SV_STSHUTW;
2716 t->analysis &= ~AN_RTR_ANY;
2717 if (!(t->flags & SN_ERR_MASK))
2718 t->flags |= SN_ERR_SRVTO;
2719 if (!(t->flags & SN_FINST_MASK))
2720 t->flags |= SN_FINST_H;
Willy Tarreaub8750a82006-09-03 09:56:00 +02002721 return 1;
2722 }
2723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002724 /*
2725 * And now the non-error cases.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002726 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002727
2728 /* Data remaining in the request buffer.
2729 * This happens during the first pass here, and during
2730 * long posts.
2731 */
2732 else if (likely(req->l)) {
2733 if (!tick_isset(req->wex)) {
2734 EV_FD_COND_S(t->srv_fd, DIR_WR);
2735 /* restart writing */
2736 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
2737 if (tick_isset(req->wex)) {
2738 /* FIXME: to prevent the server from expiring read timeouts during writes,
2739 * we refresh it. */
2740 rep->rex = req->wex;
2741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002742 }
2743 }
2744
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002745 /* nothing left in the request buffer */
2746 else {
2747 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2748 /* stop writing */
2749 req->wex = TICK_ETERNITY;
2750 }
2751 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002752
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002753 /* return 0 if nothing changed */
2754 return !(t->analysis & AN_RTR_ANY);
2755 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002756
Willy Tarreau21d2af32008-02-14 20:25:24 +01002757
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002758 /*****************************************************************
2759 * More interesting part now : we know that we have a complete *
2760 * response which at least looks like HTTP. We have an indicator *
2761 * of each header's length, so we can parse them quickly. *
2762 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002764 /* ensure we keep this pointer to the beginning of the message */
2765 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002767 /*
2768 * 1: get the status code and check for cacheability.
2769 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002770
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002771 t->logs.logwait &= ~LW_RESP;
2772 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002773
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002774 switch (txn->status) {
2775 case 200:
2776 case 203:
2777 case 206:
2778 case 300:
2779 case 301:
2780 case 410:
2781 /* RFC2616 @13.4:
2782 * "A response received with a status code of
2783 * 200, 203, 206, 300, 301 or 410 MAY be stored
2784 * by a cache (...) unless a cache-control
2785 * directive prohibits caching."
2786 *
2787 * RFC2616 @9.5: POST method :
2788 * "Responses to this method are not cacheable,
2789 * unless the response includes appropriate
2790 * Cache-Control or Expires header fields."
2791 */
2792 if (likely(txn->meth != HTTP_METH_POST) &&
2793 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2794 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2795 break;
2796 default:
2797 break;
2798 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002799
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002800 /*
2801 * 2: we may need to capture headers
2802 */
2803 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2804 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2805 txn->rsp.cap, t->fe->rsp_cap);
2806
2807 /*
2808 * 3: we will have to evaluate the filters.
2809 * As opposed to version 1.2, now they will be evaluated in the
2810 * filters order and not in the header order. This means that
2811 * each filter has to be validated among all headers.
2812 *
2813 * Filters are tried with ->be first, then with ->fe if it is
2814 * different from ->be.
2815 */
2816
2817 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2818
2819 cur_proxy = t->be;
2820 while (1) {
2821 struct proxy *rule_set = cur_proxy;
2822
2823 /* try headers filters */
2824 if (rule_set->rsp_exp != NULL) {
2825 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2826 return_bad_resp:
2827 if (t->srv) {
2828 t->srv->cur_sess--;
2829 t->srv->failed_resp++;
2830 sess_change_server(t, NULL);
2831 }
2832 cur_proxy->failed_resp++;
2833 return_srv_prx_502:
2834 buffer_shutr_done(rep);
2835 buffer_shutw_done(req);
2836 fd_delete(t->srv_fd);
2837 t->srv_state = SV_STCLOSE;
2838 t->analysis &= ~AN_RTR_ANY;
2839 rep->flags |= BF_MAY_FORWARD;
2840 txn->status = 502;
2841 client_return(t, error_message(t, HTTP_ERR_502));
2842 if (!(t->flags & SN_ERR_MASK))
2843 t->flags |= SN_ERR_PRXCOND;
2844 if (!(t->flags & SN_FINST_MASK))
2845 t->flags |= SN_FINST_H;
2846 /* We used to have a free connection slot. Since we'll never use it,
2847 * we have to inform the server that it may be used by another session.
2848 */
2849 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2850 process_srv_queue(t->srv);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002851 return 1;
2852 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002853 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002855 /* has the response been denied ? */
2856 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002857 if (t->srv) {
2858 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002859 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002860 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002861 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 cur_proxy->denied_resp++;
2863 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002864 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 /* We might have to check for "Connection:" */
2867 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2868 !(t->flags & SN_CONN_CLOSED)) {
2869 char *cur_ptr, *cur_end, *cur_next;
2870 int cur_idx, old_idx, delta, val;
2871 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002873 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2874 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002875
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002876 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2877 cur_hdr = &txn->hdr_idx.v[cur_idx];
2878 cur_ptr = cur_next;
2879 cur_end = cur_ptr + cur_hdr->len;
2880 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002882 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2883 if (val) {
2884 /* 3 possibilities :
2885 * - we have already set Connection: close,
2886 * so we remove this line.
2887 * - we have not yet set Connection: close,
2888 * but this line indicates close. We leave
2889 * it untouched and set the flag.
2890 * - we have not yet set Connection: close,
2891 * and this line indicates non-close. We
2892 * replace it.
2893 */
2894 if (t->flags & SN_CONN_CLOSED) {
2895 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2896 txn->rsp.eoh += delta;
2897 cur_next += delta;
2898 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2899 txn->hdr_idx.used--;
2900 cur_hdr->len = 0;
2901 } else {
2902 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2903 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2904 "close", 5);
2905 cur_next += delta;
2906 cur_hdr->len += delta;
2907 txn->rsp.eoh += delta;
2908 }
2909 t->flags |= SN_CONN_CLOSED;
2910 }
2911 }
2912 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002913 }
2914 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002916 /* add response headers from the rule sets in the same order */
2917 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2918 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2919 rule_set->rsp_add[cur_idx])) < 0)
2920 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002921 }
2922
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 /* check whether we're already working on the frontend */
2924 if (cur_proxy == t->fe)
2925 break;
2926 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002929 /*
2930 * 4: check for server cookie.
2931 */
2932 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2933 || (t->be->options & PR_O_CHK_CACHE))
2934 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002935
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936
Willy Tarreaua15645d2007-03-18 16:22:39 +01002937 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002939 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002940 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2941 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002943 /*
2944 * 6: add server cookie in the response if needed
2945 */
2946 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2947 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2948 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002950 /* the server is known, it's not the one the client requested, we have to
2951 * insert a set-cookie here, except if we want to insert only on POST
2952 * requests and this one isn't. Note that servers which don't have cookies
2953 * (eg: some backup servers) will return a full cookie removal request.
2954 */
2955 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2956 t->be->cookie_name,
2957 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 if (t->be->cookie_domain)
2960 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002962 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2963 trash, len)) < 0)
2964 goto return_bad_resp;
2965 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002966
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002967 /* Here, we will tell an eventual cache on the client side that we don't
2968 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2969 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2970 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2971 */
2972 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002974 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2975
2976 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2977 "Cache-control: private", 22)) < 0)
2978 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002979 }
2980 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002983 /*
2984 * 7: check if result will be cacheable with a cookie.
2985 * We'll block the response if security checks have caught
2986 * nasty things such as a cacheable cookie.
2987 */
2988 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2989 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2990 (t->be->options & PR_O_CHK_CACHE)) {
2991
2992 /* we're in presence of a cacheable response containing
2993 * a set-cookie header. We'll block it as requested by
2994 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002995 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002996 if (t->srv) {
2997 t->srv->cur_sess--;
2998 t->srv->failed_secu++;
2999 sess_change_server(t, NULL);
3000 }
3001 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003002
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003003 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3004 t->be->id, t->srv?t->srv->id:"<dispatch>");
3005 send_log(t->be, LOG_ALERT,
3006 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3007 t->be->id, t->srv?t->srv->id:"<dispatch>");
3008 goto return_srv_prx_502;
3009 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003010
3011 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003012 * 8: add "Connection: close" if needed and not yet set.
3013 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003014 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003015 if (!(t->flags & SN_CONN_CLOSED) &&
3016 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3017 if ((unlikely(msg->sl.st.v_l != 8) ||
3018 unlikely(req->data[msg->som + 7] != '0')) &&
3019 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3020 "Connection: close", 17)) < 0)
3021 goto return_bad_resp;
3022 t->flags |= SN_CONN_CLOSED;
3023 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003024
Willy Tarreaua15645d2007-03-18 16:22:39 +01003025
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003026 /*************************************************************
3027 * OK, that's finished for the headers. We have done what we *
3028 * could. Let's switch to the DATA state. *
3029 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003031 t->srv_state = SV_STDATA;
3032 t->analysis &= ~AN_RTR_ANY;
3033 rep->flags |= BF_MAY_FORWARD;
3034 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
3035 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003036
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003037 /* client connection already closed or option 'forceclose' required :
3038 * we close the server's outgoing connection right now.
3039 */
3040 if ((req->l == 0) &&
3041 (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) {
3042 EV_FD_CLR(t->srv_fd, DIR_WR);
3043 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003044
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003045 /* We must ensure that the read part is still alive when switching
3046 * to shutw */
3047 EV_FD_SET(t->srv_fd, DIR_RD);
3048 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003049
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003050 shutdown(t->srv_fd, SHUT_WR);
3051 t->srv_state = SV_STSHUTW;
3052 t->analysis &= ~AN_RTR_ANY;
3053 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003054
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003055#ifdef CONFIG_HAP_TCPSPLICE
3056 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3057 /* TCP splicing supported by both FE and BE */
3058 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3059 }
3060#endif
3061 /* if the user wants to log as soon as possible, without counting
3062 * bytes from the server, then this is the right moment. We have
3063 * to temporarily assign bytes_out to log what we currently have.
3064 */
3065 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3066 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3067 t->logs.bytes_out = txn->rsp.eoh;
3068 if (t->fe->to_log & LW_REQ)
3069 http_sess_log(t);
3070 else
3071 tcp_sess_log(t);
3072 t->logs.bytes_out = 0;
3073 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003074
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003075 /* Note: we must not try to cheat by jumping directly to DATA,
3076 * otherwise we would not let the client side wake up.
3077 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003078
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003079 return 1;
3080 }
3081 return 0;
3082}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003083
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003084/*
3085 * manages the client FSM and its socket. BTW, it also tries to handle the
3086 * cookie. It returns 1 if a state has changed (and a resync may be needed),
3087 * 0 else.
3088 */
3089int process_cli(struct session *t)
3090{
3091 int s = t->srv_state;
3092 int c = t->cli_state;
3093 struct buffer *req = t->req;
3094 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003095
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003096 DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x\n",
3097 now_ms,
3098 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3099 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
3100 req->rex, rep->wex,
3101 req->flags, rep->flags);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003103 /* if no analysis remains, it's time to forward the connection */
3104 if (!(t->analysis & AN_REQ_ANY) && !(req->flags & (BF_MAY_CONNECT|BF_MAY_FORWARD)))
3105 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 if (c == CL_STDATA || c == CL_STSHUTR || c == CL_STSHUTW) {
3108 /* read or write error */
3109 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
3110 buffer_shutr_done(req);
3111 buffer_shutw_done(rep);
3112 fd_delete(t->cli_fd);
3113 t->cli_state = CL_STCLOSE;
3114 if (!(t->flags & SN_ERR_MASK))
3115 t->flags |= SN_ERR_CLICL;
3116 if (!(t->flags & SN_FINST_MASK)) {
3117 if (t->analysis & AN_REQ_ANY)
3118 t->flags |= SN_FINST_R;
3119 else if (t->pend_pos)
3120 t->flags |= SN_FINST_Q;
3121 else if (s == SV_STCONN)
3122 t->flags |= SN_FINST_C;
3123 else
3124 t->flags |= SN_FINST_D;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003125 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003126 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003127 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 /* last read, or end of server write */
3129 else if (!(req->flags & BF_SHUTR_STATUS) && /* already done */
3130 req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
3131 buffer_shutr_done(req);
3132 if (!(rep->flags & BF_SHUTW_STATUS)) {
3133 EV_FD_CLR(t->cli_fd, DIR_RD);
3134 t->cli_state = CL_STSHUTR;
3135 } else {
3136 /* output was already closed */
3137 fd_delete(t->cli_fd);
3138 t->cli_state = CL_STCLOSE;
3139 }
3140 return 1;
3141 }
3142 /* last server read and buffer empty */
3143 else if (!(rep->flags & BF_SHUTW_STATUS) && /* already done */
3144 rep->l == 0 && rep->flags & BF_SHUTR_STATUS && !(t->flags & SN_SELF_GEN)) {
3145 buffer_shutw_done(rep);
3146 if (!(req->flags & BF_SHUTR_STATUS)) {
3147 EV_FD_CLR(t->cli_fd, DIR_WR);
3148 shutdown(t->cli_fd, SHUT_WR);
3149 /* We must ensure that the read part is still alive when switching to shutw */
3150 /* FIXME: is this still true ? */
3151 EV_FD_SET(t->cli_fd, DIR_RD);
3152 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3153 t->cli_state = CL_STSHUTW;
3154 } else {
3155 fd_delete(t->cli_fd);
3156 t->cli_state = CL_STCLOSE;
3157 }
3158 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003159 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003160 /* read timeout */
3161 else if (tick_is_expired(req->rex, now_ms)) {
3162 buffer_shutr_done(req);
3163 req->flags |= BF_READ_TIMEOUT;
3164 if (!(rep->flags & BF_SHUTW_STATUS)) {
3165 EV_FD_CLR(t->cli_fd, DIR_RD);
3166 t->cli_state = CL_STSHUTR;
3167 } else {
3168 /* output was already closed */
3169 fd_delete(t->cli_fd);
3170 t->cli_state = CL_STCLOSE;
3171 }
3172 if (!(t->flags & SN_ERR_MASK))
3173 t->flags |= SN_ERR_CLITO;
3174 if (!(t->flags & SN_FINST_MASK)) {
3175 if (t->analysis & AN_REQ_ANY)
3176 t->flags |= SN_FINST_R;
3177 else if (t->pend_pos)
3178 t->flags |= SN_FINST_Q;
3179 else if (s == SV_STCONN)
3180 t->flags |= SN_FINST_C;
3181 else
3182 t->flags |= SN_FINST_D;
3183 }
3184 return 1;
3185 }
3186 /* write timeout */
3187 else if (tick_is_expired(rep->wex, now_ms)) {
3188 buffer_shutw_done(rep);
3189 rep->flags |= BF_WRITE_TIMEOUT;
3190 if (!(req->flags & BF_SHUTR_STATUS)) {
3191 EV_FD_CLR(t->cli_fd, DIR_WR);
3192 shutdown(t->cli_fd, SHUT_WR);
3193 /* We must ensure that the read part is still alive when switching to shutw */
3194 /* FIXME: is this still true ? */
3195 EV_FD_SET(t->cli_fd, DIR_RD);
3196 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3197 t->cli_state = CL_STSHUTW;
3198 } else {
3199 fd_delete(t->cli_fd);
3200 t->cli_state = CL_STCLOSE;
3201 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003202
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003203 if (!(t->flags & SN_ERR_MASK))
3204 t->flags |= SN_ERR_CLITO;
3205 if (!(t->flags & SN_FINST_MASK)) {
3206 if (t->analysis & AN_REQ_ANY)
3207 t->flags |= SN_FINST_R;
3208 else if (t->pend_pos)
3209 t->flags |= SN_FINST_Q;
3210 else if (s == SV_STCONN)
3211 t->flags |= SN_FINST_C;
3212 else
3213 t->flags |= SN_FINST_D;
3214 }
3215 return 1;
3216 }
3217
3218 /* manage read timeout */
3219 if (!(req->flags & BF_SHUTR_STATUS)) {
3220 if (req->l >= req->rlim - req->data) {
3221 /* no room to read more data */
3222 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3223 /* stop reading until we get some space */
3224 req->rex = TICK_ETERNITY;
3225 }
3226 } else {
3227 EV_FD_COND_S(t->cli_fd, DIR_RD);
3228 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3229 }
3230 }
3231
3232 /* manage write timeout */
3233 if (!(rep->flags & BF_SHUTW_STATUS)) {
3234 /* first, we may have to produce data (eg: stats).
3235 * right now, this is limited to the SHUTR state.
3236 */
3237 if (req->flags & BF_SHUTR_STATUS && t->flags & SN_SELF_GEN) {
3238 produce_content(t);
3239 if (rep->l == 0) {
3240 buffer_shutw_done(rep);
3241 fd_delete(t->cli_fd);
3242 t->cli_state = CL_STCLOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003243 return 1;
3244 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003245 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003246
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003247 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3248 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
3249 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3250 /* stop writing */
3251 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003253 } else {
3254 /* buffer not empty */
3255 if (!tick_isset(rep->wex)) {
3256 EV_FD_COND_S(t->cli_fd, DIR_WR);
3257 /* restart writing */
3258 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3259 if (!(req->flags & BF_SHUTR_STATUS) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3260 /* FIXME: to prevent the client from expiring read timeouts during writes,
3261 * we refresh it, except if it was already infinite. */
3262 req->rex = rep->wex;
3263 }
3264 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003265 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003266 }
3267 return 0; /* other cases change nothing */
3268 }
3269 else { /* CL_STCLOSE: nothing to do */
3270 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3271 int len;
3272 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
3273 write(1, trash, len);
3274 }
3275 return 0;
3276 }
3277 return 0;
3278}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003279
Willy Tarreaubaaee002006-06-26 02:48:02 +02003280
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003281/*
3282 * manages the server FSM and its socket. It returns 1 if a state has changed
3283 * (and a resync may be needed), 0 else.
3284 */
3285int process_srv(struct session *t)
3286{
3287 int s = t->srv_state;
3288 struct http_txn *txn = &t->txn;
3289 struct buffer *req = t->req;
3290 struct buffer *rep = t->rep;
3291 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003293 DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x\n",
3294 now_ms,
3295 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3296 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
3297 rep->rex, req->wex,
3298 req->flags, rep->flags);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003299
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003300 if (s == SV_STIDLE) {
3301 if ((rep->flags & BF_SHUTW_STATUS) ||
3302 ((req->flags & BF_SHUTR_STATUS) &&
3303 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3304 req->cex = TICK_ETERNITY;
3305 if (t->pend_pos)
3306 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3307 /* note that this must not return any error because it would be able to
3308 * overwrite the client_retnclose() output.
3309 */
3310 if (txn->flags & TX_CLTARPIT)
3311 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3312 else
3313 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3314
3315 return 1;
3316 }
3317 else if (req->flags & BF_MAY_CONNECT) {
3318 /* the client allows the server to connect */
3319 if (txn->flags & TX_CLTARPIT) {
3320 /* This connection is being tarpitted. The CLIENT side has
3321 * already set the connect expiration date to the right
3322 * timeout. We just have to check that it has not expired.
3323 */
3324 if (!tick_is_expired(req->cex, now_ms))
3325 return 0;
3326
3327 /* We will set the queue timer to the time spent, just for
3328 * logging purposes. We fake a 500 server error, so that the
3329 * attacker will not suspect his connection has been tarpitted.
3330 * It will not cause trouble to the logs because we can exclude
3331 * the tarpitted connections by filtering on the 'PT' status flags.
3332 */
3333 req->cex = TICK_ETERNITY;
3334 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3335 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3336 500, error_message(t, HTTP_ERR_500));
3337 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003338 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003339
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003340 /* Right now, we will need to create a connection to the server.
3341 * We might already have tried, and got a connection pending, in
3342 * which case we will not do anything till it's pending. It's up
3343 * to any other session to release it and wake us up again.
3344 */
3345 if (t->pend_pos) {
3346 if (!tick_is_expired(req->cex, now_ms)) {
3347 return 0;
3348 } else {
3349 /* we've been waiting too long here */
3350 req->cex = TICK_ETERNITY;
3351 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3352 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3353 503, error_message(t, HTTP_ERR_503));
3354 if (t->srv)
3355 t->srv->failed_conns++;
3356 t->be->failed_conns++;
3357 return 1;
3358 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003359 }
3360
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003361 do {
3362 /* first, get a connection */
3363 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3364 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003366 if (srv_redispatch_connect(t))
3367 return t->srv_state != SV_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003368
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003369 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3370 /* Server supporting redirection and it is possible.
3371 * Invalid requests are reported as such. It concerns all
3372 * the largest ones.
3373 */
3374 struct chunk rdr;
3375 char *path;
3376 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003377
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003378 /* 1: create the response header */
3379 rdr.len = strlen(HTTP_302);
3380 rdr.str = trash;
3381 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003382
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003383 /* 2: add the server's prefix */
3384 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3385 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003387 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3388 rdr.len += t->srv->rdr_len;
3389
3390 /* 3: add the request URI */
3391 path = http_get_path(txn);
3392 if (!path)
3393 goto cancel_redir;
3394 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3395 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3396 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003398 memcpy(rdr.str + rdr.len, path, len);
3399 rdr.len += len;
3400 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3401 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003402
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003403 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3404 /* FIXME: we should increase a counter of redirects per server and per backend. */
3405 if (t->srv)
3406 t->srv->cum_sess++;
3407 return 1;
3408 cancel_redir:
3409 txn->status = 400;
3410 t->fe->failed_req++;
3411 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3412 400, error_message(t, HTTP_ERR_400));
3413 return 1;
3414 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003415
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003416 /* try to (re-)connect to the server, and fail if we expire the
3417 * number of retries.
3418 */
3419 if (srv_retryable_connect(t)) {
3420 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3421 return t->srv_state != SV_STIDLE;
3422 }
3423 } while (1);
3424 }
3425 }
3426 else if (s == SV_STCONN) { /* connection in progress */
3427 if ((rep->flags & BF_SHUTW_STATUS) ||
3428 ((req->flags & BF_SHUTR_STATUS) &&
3429 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
3430 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3431 req->cex = TICK_ETERNITY;
3432 if (!(t->flags & SN_CONN_TAR)) {
3433 /* if we are in turn-around, we have already closed the FD */
3434 fd_delete(t->srv_fd);
3435 if (t->srv) {
3436 t->srv->cur_sess--;
3437 sess_change_server(t, NULL);
3438 }
3439 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003440
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003441 /* note that this must not return any error because it would be able to
3442 * overwrite the client_retnclose() output.
3443 */
3444 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3445 return 1;
3446 }
3447 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
3448 return 0; /* nothing changed */
3449 }
3450 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3451 /* timeout, asynchronous connect error or first write error */
3452 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003453
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003454 if (t->flags & SN_CONN_TAR) {
3455 /* We are doing a turn-around waiting for a new connection attempt. */
3456 if (!tick_is_expired(req->cex, now_ms))
3457 return 0;
3458 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003460 else {
3461 fd_delete(t->srv_fd);
3462 if (t->srv) {
3463 t->srv->cur_sess--;
3464 sess_change_server(t, NULL);
3465 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003466
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 if (!(req->flags & BF_WRITE_STATUS))
3468 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3469 else
3470 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003471
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003472 /* ensure that we have enough retries left */
3473 if (srv_count_retry_down(t, conn_err))
3474 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003475
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003476 if (req->flags & BF_WRITE_ERROR) {
3477 /* we encountered an immediate connection error, and we
3478 * will have to retry connecting to the same server, most
3479 * likely leading to the same result. To avoid this, we
3480 * fake a connection timeout to retry after a turn-around
3481 * time of 1 second. We will wait in the previous if block.
3482 */
3483 t->flags |= SN_CONN_TAR;
3484 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
3485 return 0;
3486 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003489 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3490 /* We're on our last chance, and the REDISP option was specified.
3491 * We will ignore cookie and force to balance or use the dispatcher.
3492 */
3493 /* let's try to offer this slot to anybody */
3494 if (may_dequeue_tasks(t->srv, t->be))
3495 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003496
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003497 /* it's left to the dispatcher to choose a server */
3498 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3499 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003500
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003501 /* first, get a connection */
3502 if (srv_redispatch_connect(t))
3503 return t->srv_state != SV_STCONN;
3504 } else {
3505 if (t->srv)
3506 t->srv->retries++;
3507 t->be->retries++;
3508 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003510 do {
3511 /* Now we will try to either reconnect to the same server or
3512 * connect to another server. If the connection gets queued
3513 * because all servers are saturated, then we will go back to
3514 * the SV_STIDLE state.
3515 */
3516 if (srv_retryable_connect(t)) {
3517 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3518 return t->srv_state != SV_STCONN;
3519 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003520
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003521 /* we need to redispatch the connection to another server */
3522 if (srv_redispatch_connect(t))
3523 return t->srv_state != SV_STCONN;
3524 } while (1);
3525 }
3526 else { /* no error or write 0 */
3527 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003528
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003529 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
3530 if (req->l == 0) /* nothing to write */ {
3531 EV_FD_CLR(t->srv_fd, DIR_WR);
3532 req->wex = TICK_ETERNITY;
3533 } else /* need the right to write */ {
3534 EV_FD_SET(t->srv_fd, DIR_WR);
3535 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3536 if (tick_isset(req->wex)) {
3537 /* FIXME: to prevent the server from expiring read timeouts during writes,
3538 * we refresh it. */
3539 rep->rex = req->wex;
3540 }
3541 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003542
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003543 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3544 EV_FD_SET(t->srv_fd, DIR_RD);
3545 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3546 t->srv_state = SV_STDATA;
3547 rep->flags |= BF_MAY_FORWARD;
3548 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003549
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003550 /* if the user wants to log as soon as possible, without counting
3551 bytes from the server, then this is the right moment. */
3552 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3553 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3554 tcp_sess_log(t);
3555 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003556#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003557 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3558 /* TCP splicing supported by both FE and BE */
3559 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3560 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003561#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003562 }
3563 else {
3564 t->srv_state = SV_STDATA;
3565 t->analysis |= AN_RTR_HTTP_HDR;
3566 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
3567 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3568 /* reset hdr_idx which was already initialized by the request.
3569 * right now, the http parser does it.
3570 * hdr_idx_init(&t->txn.hdr_idx);
3571 */
3572 }
3573 req->cex = TICK_ETERNITY;
3574 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003575 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003576 }
3577 else if (s == SV_STDATA) {
3578 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003579 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003580 buffer_shutr_done(rep);
3581 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003582 fd_delete(t->srv_fd);
3583 if (t->srv) {
3584 t->srv->cur_sess--;
3585 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003586 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003587 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003588 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003589 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003590 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003591 if (!(t->flags & SN_ERR_MASK))
3592 t->flags |= SN_ERR_SRVCL;
3593 if (!(t->flags & SN_FINST_MASK))
3594 t->flags |= SN_FINST_D;
3595 /* We used to have a free connection slot. Since we'll never use it,
3596 * we have to inform the server that it may be used by another session.
3597 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003598 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003599 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003600
3601 return 1;
3602 }
3603 /* last read, or end of client write */
Willy Tarreau6468d922008-08-03 19:15:35 +02003604 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003605 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003606 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003607 t->srv_state = SV_STSHUTR;
3608 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3609 return 1;
3610 }
3611 /* end of client read and no more data to send */
Willy Tarreau6468d922008-08-03 19:15:35 +02003612 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003613 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003614 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003615 shutdown(t->srv_fd, SHUT_WR);
3616 /* We must ensure that the read part is still alive when switching
3617 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003618 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003619 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003620
3621 t->srv_state = SV_STSHUTW;
3622 return 1;
3623 }
3624 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003625 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003626 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003627 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003628 t->srv_state = SV_STSHUTR;
3629 if (!(t->flags & SN_ERR_MASK))
3630 t->flags |= SN_ERR_SRVTO;
3631 if (!(t->flags & SN_FINST_MASK))
3632 t->flags |= SN_FINST_D;
3633 return 1;
3634 }
3635 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003636 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003637 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003638 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003639 shutdown(t->srv_fd, SHUT_WR);
3640 /* We must ensure that the read part is still alive when switching
3641 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003642 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003643 rep->cex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003644 t->srv_state = SV_STSHUTW;
3645 if (!(t->flags & SN_ERR_MASK))
3646 t->flags |= SN_ERR_SRVTO;
3647 if (!(t->flags & SN_FINST_MASK))
3648 t->flags |= SN_FINST_D;
3649 return 1;
3650 }
3651
3652 /* recompute request time-outs */
3653 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003654 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3655 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003656 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003657 }
3658 }
3659 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003660 if (!tick_isset(req->wex)) {
3661 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003662 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003663 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003664 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003665 /* FIXME: to prevent the server from expiring read timeouts during writes,
3666 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003667 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003668 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003669 }
3670 }
3671
3672 /* recompute response time-outs */
3673 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003674 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003675 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003676 }
3677 }
3678 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003679 if (!tick_isset(rep->rex)) {
3680 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003681 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682 }
3683 }
3684
3685 return 0; /* other cases change nothing */
3686 }
3687 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003688 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003689 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003690 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003691 fd_delete(t->srv_fd);
3692 if (t->srv) {
3693 t->srv->cur_sess--;
3694 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003695 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003696 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003697 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003698 //close(t->srv_fd);
3699 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003700 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003701 if (!(t->flags & SN_ERR_MASK))
3702 t->flags |= SN_ERR_SRVCL;
3703 if (!(t->flags & SN_FINST_MASK))
3704 t->flags |= SN_FINST_D;
3705 /* We used to have a free connection slot. Since we'll never use it,
3706 * we have to inform the server that it may be used by another session.
3707 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003708 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003709 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003710
3711 return 1;
3712 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003713 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003714 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003715 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003716 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003717 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003718 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003719 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003720 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003721 //close(t->srv_fd);
3722 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003723 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003724 /* We used to have a free connection slot. Since we'll never use it,
3725 * we have to inform the server that it may be used by another session.
3726 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003727 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003728 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003729
3730 return 1;
3731 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003732 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003733 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003734 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003735 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003736 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003737 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003738 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003739 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003740 //close(t->srv_fd);
3741 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003742 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003743 if (!(t->flags & SN_ERR_MASK))
3744 t->flags |= SN_ERR_SRVTO;
3745 if (!(t->flags & SN_FINST_MASK))
3746 t->flags |= SN_FINST_D;
3747 /* We used to have a free connection slot. Since we'll never use it,
3748 * we have to inform the server that it may be used by another session.
3749 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003750 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003751 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003752
3753 return 1;
3754 }
3755 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003756 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3757 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003758 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759 }
3760 }
3761 else { /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003762 if (!tick_isset(req->wex)) {
3763 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003764 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003765 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003766 }
3767 }
3768 return 0;
3769 }
3770 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003771 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003772 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003773 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003774 fd_delete(t->srv_fd);
3775 if (t->srv) {
3776 t->srv->cur_sess--;
3777 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003778 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003779 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003780 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781 //close(t->srv_fd);
3782 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003783 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003784 if (!(t->flags & SN_ERR_MASK))
3785 t->flags |= SN_ERR_SRVCL;
3786 if (!(t->flags & SN_FINST_MASK))
3787 t->flags |= SN_FINST_D;
3788 /* We used to have a free connection slot. Since we'll never use it,
3789 * we have to inform the server that it may be used by another session.
3790 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003791 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003792 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003793
3794 return 1;
3795 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003796 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003797 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003798 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003799 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003800 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003801 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003802 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003803 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003804 //close(t->srv_fd);
3805 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003806 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003807 /* We used to have a free connection slot. Since we'll never use it,
3808 * we have to inform the server that it may be used by another session.
3809 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003810 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003811 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003812
3813 return 1;
3814 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003815 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003816 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003817 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003818 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003819 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003820 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003821 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003822 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003823 //close(t->srv_fd);
3824 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003825 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003826 if (!(t->flags & SN_ERR_MASK))
3827 t->flags |= SN_ERR_SRVTO;
3828 if (!(t->flags & SN_FINST_MASK))
3829 t->flags |= SN_FINST_D;
3830 /* We used to have a free connection slot. Since we'll never use it,
3831 * we have to inform the server that it may be used by another session.
3832 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003833 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003834 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003835
3836 return 1;
3837 }
3838 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003839 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003840 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003841 }
3842 }
3843 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003844 if (!tick_isset(rep->rex)) {
3845 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003846 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003847 }
3848 }
3849 return 0;
3850 }
3851 else { /* SV_STCLOSE : nothing to do */
3852 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3853 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003854 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003855 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003856 write(1, trash, len);
3857 }
3858 return 0;
3859 }
3860 return 0;
3861}
3862
3863
3864/*
3865 * Produces data for the session <s> depending on its source. Expects to be
3866 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3867 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3868 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003869 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreaubaaee002006-06-26 02:48:02 +02003870 * if it changes the session state from CL_STSHUTR, otherwise 0.
3871 */
3872int produce_content(struct session *s)
3873{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003874 if (s->data_source == DATA_SRC_NONE) {
3875 s->flags &= ~SN_SELF_GEN;
3876 return 1;
3877 }
3878 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003879 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003880 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003881 if (ret >= 0)
3882 return ret;
3883 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003884 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003885
Willy Tarreau91861262007-10-17 17:06:05 +02003886 /* unknown data source or internal error */
3887 s->txn.status = 500;
3888 client_retnclose(s, error_message(s, HTTP_ERR_500));
3889 if (!(s->flags & SN_ERR_MASK))
3890 s->flags |= SN_ERR_PRXCOND;
3891 if (!(s->flags & SN_FINST_MASK))
3892 s->flags |= SN_FINST_R;
3893 s->flags &= ~SN_SELF_GEN;
3894 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003895}
3896
3897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003898/* Iterate the same filter through all request headers.
3899 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003900 * Since it can manage the switch to another backend, it updates the per-proxy
3901 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003902 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003903int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003904{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003905 char term;
3906 char *cur_ptr, *cur_end, *cur_next;
3907 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003908 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003909 struct hdr_idx_elem *cur_hdr;
3910 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003912 last_hdr = 0;
3913
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003914 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003915 old_idx = 0;
3916
3917 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003918 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003919 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003920 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003921 (exp->action == ACT_ALLOW ||
3922 exp->action == ACT_DENY ||
3923 exp->action == ACT_TARPIT))
3924 return 0;
3925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003926 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003927 if (!cur_idx)
3928 break;
3929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003930 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003931 cur_ptr = cur_next;
3932 cur_end = cur_ptr + cur_hdr->len;
3933 cur_next = cur_end + cur_hdr->cr + 1;
3934
3935 /* Now we have one header between cur_ptr and cur_end,
3936 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003937 */
3938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003939 /* The annoying part is that pattern matching needs
3940 * that we modify the contents to null-terminate all
3941 * strings before testing them.
3942 */
3943
3944 term = *cur_end;
3945 *cur_end = '\0';
3946
3947 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3948 switch (exp->action) {
3949 case ACT_SETBE:
3950 /* It is not possible to jump a second time.
3951 * FIXME: should we return an HTTP/500 here so that
3952 * the admin knows there's a problem ?
3953 */
3954 if (t->be != t->fe)
3955 break;
3956
3957 /* Swithing Proxy */
3958 t->be = (struct proxy *) exp->replace;
3959
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003960 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 * because we have associated req_cap and rsp_cap to the
3962 * frontend, and the beconn will be updated later.
3963 */
3964
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003965 t->rep->rto = t->req->wto = t->be->timeout.server;
3966 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003967 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003968 last_hdr = 1;
3969 break;
3970
3971 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003972 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003973 last_hdr = 1;
3974 break;
3975
3976 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003977 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003978 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003979 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 break;
3981
3982 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003983 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003984 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003985 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 break;
3987
3988 case ACT_REPLACE:
3989 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3990 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3991 /* FIXME: if the user adds a newline in the replacement, the
3992 * index will not be recalculated for now, and the new line
3993 * will not be counted as a new header.
3994 */
3995
3996 cur_end += delta;
3997 cur_next += delta;
3998 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003999 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004000 break;
4001
4002 case ACT_REMOVE:
4003 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4004 cur_next += delta;
4005
4006 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004007 txn->req.eoh += delta;
4008 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4009 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 cur_hdr->len = 0;
4011 cur_end = NULL; /* null-term has been rewritten */
4012 break;
4013
4014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004015 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 if (cur_end)
4017 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004019 /* keep the link from this header to next one in case of later
4020 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004021 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004022 old_idx = cur_idx;
4023 }
4024 return 0;
4025}
4026
4027
4028/* Apply the filter to the request line.
4029 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4030 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004031 * Since it can manage the switch to another backend, it updates the per-proxy
4032 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 */
4034int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4035{
4036 char term;
4037 char *cur_ptr, *cur_end;
4038 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004039 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004040 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004041
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042
Willy Tarreau3d300592007-03-18 18:34:41 +01004043 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004044 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004045 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 (exp->action == ACT_ALLOW ||
4047 exp->action == ACT_DENY ||
4048 exp->action == ACT_TARPIT))
4049 return 0;
4050 else if (exp->action == ACT_REMOVE)
4051 return 0;
4052
4053 done = 0;
4054
Willy Tarreau9cdde232007-05-02 20:58:19 +02004055 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004056 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004057
4058 /* Now we have the request line between cur_ptr and cur_end */
4059
4060 /* The annoying part is that pattern matching needs
4061 * that we modify the contents to null-terminate all
4062 * strings before testing them.
4063 */
4064
4065 term = *cur_end;
4066 *cur_end = '\0';
4067
4068 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4069 switch (exp->action) {
4070 case ACT_SETBE:
4071 /* It is not possible to jump a second time.
4072 * FIXME: should we return an HTTP/500 here so that
4073 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004075 if (t->be != t->fe)
4076 break;
4077
4078 /* Swithing Proxy */
4079 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004081 /* right now, the backend switch is not too much complicated
4082 * because we have associated req_cap and rsp_cap to the
4083 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004084 */
4085
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004086 t->rep->rto = t->req->wto = t->be->timeout.server;
4087 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004088 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004089 done = 1;
4090 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004092 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004093 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004094 done = 1;
4095 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004097 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004098 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004099 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004100 done = 1;
4101 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004103 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004104 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004105 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004106 done = 1;
4107 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004109 case ACT_REPLACE:
4110 *cur_end = term; /* restore the string terminator */
4111 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4112 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4113 /* FIXME: if the user adds a newline in the replacement, the
4114 * index will not be recalculated for now, and the new line
4115 * will not be counted as a new header.
4116 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004117
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004118 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004119 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004120
Willy Tarreau9cdde232007-05-02 20:58:19 +02004121 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004122 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004123 HTTP_MSG_RQMETH,
4124 cur_ptr, cur_end + 1,
4125 NULL, NULL);
4126 if (unlikely(!cur_end))
4127 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 /* we have a full request and we know that we have either a CR
4130 * or an LF at <ptr>.
4131 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004132 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4133 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 /* there is no point trying this regex on headers */
4135 return 1;
4136 }
4137 }
4138 *cur_end = term; /* restore the string terminator */
4139 return done;
4140}
Willy Tarreau97de6242006-12-27 17:18:38 +01004141
Willy Tarreau58f10d72006-12-04 02:26:12 +01004142
Willy Tarreau58f10d72006-12-04 02:26:12 +01004143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004144/*
4145 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4146 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004147 * unparsable request. Since it can manage the switch to another backend, it
4148 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004149 */
4150int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4151{
Willy Tarreau3d300592007-03-18 18:34:41 +01004152 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004153 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004154 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004155 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004157 /*
4158 * The interleaving of transformations and verdicts
4159 * makes it difficult to decide to continue or stop
4160 * the evaluation.
4161 */
4162
Willy Tarreau3d300592007-03-18 18:34:41 +01004163 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4165 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4166 exp = exp->next;
4167 continue;
4168 }
4169
4170 /* Apply the filter to the request line. */
4171 ret = apply_filter_to_req_line(t, req, exp);
4172 if (unlikely(ret < 0))
4173 return -1;
4174
4175 if (likely(ret == 0)) {
4176 /* The filter did not match the request, it can be
4177 * iterated through all headers.
4178 */
4179 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180 }
4181 exp = exp->next;
4182 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004183 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184}
4185
4186
Willy Tarreaua15645d2007-03-18 16:22:39 +01004187
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004189 * Manage client-side cookie. It can impact performance by about 2% so it is
4190 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004191 */
4192void manage_client_side_cookies(struct session *t, struct buffer *req)
4193{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004194 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004195 char *p1, *p2, *p3, *p4;
4196 char *del_colon, *del_cookie, *colon;
4197 int app_cookies;
4198
4199 appsess *asession_temp = NULL;
4200 appsess local_asession;
4201
4202 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004203 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204
Willy Tarreau2a324282006-12-05 00:05:46 +01004205 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 * we start with the start line.
4207 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004208 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004209 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004210
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004211 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004212 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004213 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004215 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216 cur_ptr = cur_next;
4217 cur_end = cur_ptr + cur_hdr->len;
4218 cur_next = cur_end + cur_hdr->cr + 1;
4219
4220 /* We have one full header between cur_ptr and cur_end, and the
4221 * next header starts at cur_next. We're only interested in
4222 * "Cookie:" headers.
4223 */
4224
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004225 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4226 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 old_idx = cur_idx;
4228 continue;
4229 }
4230
4231 /* Now look for cookies. Conforming to RFC2109, we have to support
4232 * attributes whose name begin with a '$', and associate them with
4233 * the right cookie, if we want to delete this cookie.
4234 * So there are 3 cases for each cookie read :
4235 * 1) it's a special attribute, beginning with a '$' : ignore it.
4236 * 2) it's a server id cookie that we *MAY* want to delete : save
4237 * some pointers on it (last semi-colon, beginning of cookie...)
4238 * 3) it's an application cookie : we *MAY* have to delete a previous
4239 * "special" cookie.
4240 * At the end of loop, if a "special" cookie remains, we may have to
4241 * remove it. If no application cookie persists in the header, we
4242 * *MUST* delete it
4243 */
4244
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004245 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246
Willy Tarreau58f10d72006-12-04 02:26:12 +01004247 /* del_cookie == NULL => nothing to be deleted */
4248 del_colon = del_cookie = NULL;
4249 app_cookies = 0;
4250
4251 while (p1 < cur_end) {
4252 /* skip spaces and colons, but keep an eye on these ones */
4253 while (p1 < cur_end) {
4254 if (*p1 == ';' || *p1 == ',')
4255 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004256 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257 break;
4258 p1++;
4259 }
4260
4261 if (p1 == cur_end)
4262 break;
4263
4264 /* p1 is at the beginning of the cookie name */
4265 p2 = p1;
4266 while (p2 < cur_end && *p2 != '=')
4267 p2++;
4268
4269 if (p2 == cur_end)
4270 break;
4271
4272 p3 = p2 + 1; /* skips the '=' sign */
4273 if (p3 == cur_end)
4274 break;
4275
4276 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004277 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278 p4++;
4279
4280 /* here, we have the cookie name between p1 and p2,
4281 * and its value between p3 and p4.
4282 * we can process it :
4283 *
4284 * Cookie: NAME=VALUE;
4285 * | || || |
4286 * | || || +--> p4
4287 * | || |+-------> p3
4288 * | || +--------> p2
4289 * | |+------------> p1
4290 * | +-------------> colon
4291 * +--------------------> cur_ptr
4292 */
4293
4294 if (*p1 == '$') {
4295 /* skip this one */
4296 }
4297 else {
4298 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004299 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004300 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004301 (p4 - p1 >= t->fe->capture_namelen) &&
4302 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303 int log_len = p4 - p1;
4304
Willy Tarreau086b3b42007-05-13 21:45:51 +02004305 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306 Alert("HTTP logging : out of memory.\n");
4307 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004308 if (log_len > t->fe->capture_len)
4309 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004310 memcpy(txn->cli_cookie, p1, log_len);
4311 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004312 }
4313 }
4314
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004315 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4316 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004317 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004318 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004319 char *delim;
4320
4321 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4322 * have the server ID betweek p3 and delim, and the original cookie between
4323 * delim+1 and p4. Otherwise, delim==p4 :
4324 *
4325 * Cookie: NAME=SRV~VALUE;
4326 * | || || | |
4327 * | || || | +--> p4
4328 * | || || +--------> delim
4329 * | || |+-----------> p3
4330 * | || +------------> p2
4331 * | |+----------------> p1
4332 * | +-----------------> colon
4333 * +------------------------> cur_ptr
4334 */
4335
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004336 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004337 for (delim = p3; delim < p4; delim++)
4338 if (*delim == COOKIE_DELIM)
4339 break;
4340 }
4341 else
4342 delim = p4;
4343
4344
4345 /* Here, we'll look for the first running server which supports the cookie.
4346 * This allows to share a same cookie between several servers, for example
4347 * to dedicate backup servers to specific servers only.
4348 * However, to prevent clients from sticking to cookie-less backup server
4349 * when they have incidentely learned an empty cookie, we simply ignore
4350 * empty cookies and mark them as invalid.
4351 */
4352 if (delim == p3)
4353 srv = NULL;
4354
4355 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004356 if (srv->cookie && (srv->cklen == delim - p3) &&
4357 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004358 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004359 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004360 txn->flags &= ~TX_CK_MASK;
4361 txn->flags |= TX_CK_VALID;
4362 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004363 t->srv = srv;
4364 break;
4365 } else {
4366 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004367 txn->flags &= ~TX_CK_MASK;
4368 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004369 }
4370 }
4371 srv = srv->next;
4372 }
4373
Willy Tarreau3d300592007-03-18 18:34:41 +01004374 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004376 txn->flags &= ~TX_CK_MASK;
4377 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004378 }
4379
4380 /* depending on the cookie mode, we may have to either :
4381 * - delete the complete cookie if we're in insert+indirect mode, so that
4382 * the server never sees it ;
4383 * - remove the server id from the cookie value, and tag the cookie as an
4384 * application cookie so that it does not get accidentely removed later,
4385 * if we're in cookie prefix mode
4386 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004387 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004388 int delta; /* negative */
4389
4390 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4391 p4 += delta;
4392 cur_end += delta;
4393 cur_next += delta;
4394 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004395 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004396
4397 del_cookie = del_colon = NULL;
4398 app_cookies++; /* protect the header from deletion */
4399 }
4400 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004401 (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 +01004402 del_cookie = p1;
4403 del_colon = colon;
4404 }
4405 } else {
4406 /* now we know that we must keep this cookie since it's
4407 * not ours. But if we wanted to delete our cookie
4408 * earlier, we cannot remove the complete header, but we
4409 * can remove the previous block itself.
4410 */
4411 app_cookies++;
4412
4413 if (del_cookie != NULL) {
4414 int delta; /* negative */
4415
4416 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4417 p4 += delta;
4418 cur_end += delta;
4419 cur_next += delta;
4420 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004421 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004422 del_cookie = del_colon = NULL;
4423 }
4424 }
4425
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004426 if ((t->be->appsession_name != NULL) &&
4427 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004428 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004429
Willy Tarreau58f10d72006-12-04 02:26:12 +01004430 /* Cool... it's the right one */
4431
4432 asession_temp = &local_asession;
4433
Willy Tarreau63963c62007-05-13 21:29:55 +02004434 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004435 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4436 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4437 return;
4438 }
4439
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004440 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4441 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004442 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004443
Willy Tarreau58f10d72006-12-04 02:26:12 +01004444 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004445 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4446 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004447 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004448 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004449 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004450 Alert("Not enough memory process_cli():asession:calloc().\n");
4451 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4452 return;
4453 }
4454
4455 asession_temp->sessid = local_asession.sessid;
4456 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004457 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004458 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459 } else {
4460 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004461 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004463 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004464 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004465 Alert("Found Application Session without matching server.\n");
4466 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004467 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004468 while (srv) {
4469 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004470 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004471 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004472 txn->flags &= ~TX_CK_MASK;
4473 txn->flags |= TX_CK_VALID;
4474 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004475 t->srv = srv;
4476 break;
4477 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004478 txn->flags &= ~TX_CK_MASK;
4479 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004480 }
4481 }
4482 srv = srv->next;
4483 }/* end while(srv) */
4484 }/* end else if server == NULL */
4485
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004486 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004487 asession_temp->request_count++;
4488#if defined(DEBUG_HASH)
4489 Alert("manage_client_side_cookies\n");
4490 appsession_hash_dump(&(t->be->htbl_proxy));
4491#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004492 }/* end if ((t->proxy->appsession_name != NULL) ... */
4493 }
4494
4495 /* we'll have to look for another cookie ... */
4496 p1 = p4;
4497 } /* while (p1 < cur_end) */
4498
4499 /* There's no more cookie on this line.
4500 * We may have marked the last one(s) for deletion.
4501 * We must do this now in two ways :
4502 * - if there is no app cookie, we simply delete the header ;
4503 * - if there are app cookies, we must delete the end of the
4504 * string properly, including the colon/semi-colon before
4505 * the cookie name.
4506 */
4507 if (del_cookie != NULL) {
4508 int delta;
4509 if (app_cookies) {
4510 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4511 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004512 cur_hdr->len += delta;
4513 } else {
4514 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004515
4516 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004517 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4518 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519 cur_hdr->len = 0;
4520 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004521 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004522 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523 }
4524
4525 /* keep the link from this header to next one */
4526 old_idx = cur_idx;
4527 } /* end of cookie processing on this header */
4528}
4529
4530
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531/* Iterate the same filter through all response headers contained in <rtr>.
4532 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4533 */
4534int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4535{
4536 char term;
4537 char *cur_ptr, *cur_end, *cur_next;
4538 int cur_idx, old_idx, last_hdr;
4539 struct http_txn *txn = &t->txn;
4540 struct hdr_idx_elem *cur_hdr;
4541 int len, delta;
4542
4543 last_hdr = 0;
4544
4545 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4546 old_idx = 0;
4547
4548 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004549 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004550 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004551 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 (exp->action == ACT_ALLOW ||
4553 exp->action == ACT_DENY))
4554 return 0;
4555
4556 cur_idx = txn->hdr_idx.v[old_idx].next;
4557 if (!cur_idx)
4558 break;
4559
4560 cur_hdr = &txn->hdr_idx.v[cur_idx];
4561 cur_ptr = cur_next;
4562 cur_end = cur_ptr + cur_hdr->len;
4563 cur_next = cur_end + cur_hdr->cr + 1;
4564
4565 /* Now we have one header between cur_ptr and cur_end,
4566 * and the next header starts at cur_next.
4567 */
4568
4569 /* The annoying part is that pattern matching needs
4570 * that we modify the contents to null-terminate all
4571 * strings before testing them.
4572 */
4573
4574 term = *cur_end;
4575 *cur_end = '\0';
4576
4577 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4578 switch (exp->action) {
4579 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004580 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004581 last_hdr = 1;
4582 break;
4583
4584 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004585 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004586 last_hdr = 1;
4587 break;
4588
4589 case ACT_REPLACE:
4590 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4591 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4592 /* FIXME: if the user adds a newline in the replacement, the
4593 * index will not be recalculated for now, and the new line
4594 * will not be counted as a new header.
4595 */
4596
4597 cur_end += delta;
4598 cur_next += delta;
4599 cur_hdr->len += delta;
4600 txn->rsp.eoh += delta;
4601 break;
4602
4603 case ACT_REMOVE:
4604 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4605 cur_next += delta;
4606
4607 /* FIXME: this should be a separate function */
4608 txn->rsp.eoh += delta;
4609 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4610 txn->hdr_idx.used--;
4611 cur_hdr->len = 0;
4612 cur_end = NULL; /* null-term has been rewritten */
4613 break;
4614
4615 }
4616 }
4617 if (cur_end)
4618 *cur_end = term; /* restore the string terminator */
4619
4620 /* keep the link from this header to next one in case of later
4621 * removal of next header.
4622 */
4623 old_idx = cur_idx;
4624 }
4625 return 0;
4626}
4627
4628
4629/* Apply the filter to the status line in the response buffer <rtr>.
4630 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4631 * or -1 if a replacement resulted in an invalid status line.
4632 */
4633int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4634{
4635 char term;
4636 char *cur_ptr, *cur_end;
4637 int done;
4638 struct http_txn *txn = &t->txn;
4639 int len, delta;
4640
4641
Willy Tarreau3d300592007-03-18 18:34:41 +01004642 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004643 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004644 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004645 (exp->action == ACT_ALLOW ||
4646 exp->action == ACT_DENY))
4647 return 0;
4648 else if (exp->action == ACT_REMOVE)
4649 return 0;
4650
4651 done = 0;
4652
Willy Tarreau9cdde232007-05-02 20:58:19 +02004653 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004654 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4655
4656 /* Now we have the status line between cur_ptr and cur_end */
4657
4658 /* The annoying part is that pattern matching needs
4659 * that we modify the contents to null-terminate all
4660 * strings before testing them.
4661 */
4662
4663 term = *cur_end;
4664 *cur_end = '\0';
4665
4666 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4667 switch (exp->action) {
4668 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004669 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004670 done = 1;
4671 break;
4672
4673 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004674 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004675 done = 1;
4676 break;
4677
4678 case ACT_REPLACE:
4679 *cur_end = term; /* restore the string terminator */
4680 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4681 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4682 /* FIXME: if the user adds a newline in the replacement, the
4683 * index will not be recalculated for now, and the new line
4684 * will not be counted as a new header.
4685 */
4686
4687 txn->rsp.eoh += delta;
4688 cur_end += delta;
4689
Willy Tarreau9cdde232007-05-02 20:58:19 +02004690 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004691 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004692 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004693 cur_ptr, cur_end + 1,
4694 NULL, NULL);
4695 if (unlikely(!cur_end))
4696 return -1;
4697
4698 /* we have a full respnse and we know that we have either a CR
4699 * or an LF at <ptr>.
4700 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004701 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004702 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4703 /* there is no point trying this regex on headers */
4704 return 1;
4705 }
4706 }
4707 *cur_end = term; /* restore the string terminator */
4708 return done;
4709}
4710
4711
4712
4713/*
4714 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4715 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4716 * unparsable response.
4717 */
4718int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4719{
Willy Tarreau3d300592007-03-18 18:34:41 +01004720 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004721 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004722 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004723 int ret;
4724
4725 /*
4726 * The interleaving of transformations and verdicts
4727 * makes it difficult to decide to continue or stop
4728 * the evaluation.
4729 */
4730
Willy Tarreau3d300592007-03-18 18:34:41 +01004731 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4733 exp->action == ACT_PASS)) {
4734 exp = exp->next;
4735 continue;
4736 }
4737
4738 /* Apply the filter to the status line. */
4739 ret = apply_filter_to_sts_line(t, rtr, exp);
4740 if (unlikely(ret < 0))
4741 return -1;
4742
4743 if (likely(ret == 0)) {
4744 /* The filter did not match the response, it can be
4745 * iterated through all headers.
4746 */
4747 apply_filter_to_resp_headers(t, rtr, exp);
4748 }
4749 exp = exp->next;
4750 }
4751 return 0;
4752}
4753
4754
4755
4756/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004757 * Manage server-side cookies. It can impact performance by about 2% so it is
4758 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004759 */
4760void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4761{
4762 struct http_txn *txn = &t->txn;
4763 char *p1, *p2, *p3, *p4;
4764
4765 appsess *asession_temp = NULL;
4766 appsess local_asession;
4767
4768 char *cur_ptr, *cur_end, *cur_next;
4769 int cur_idx, old_idx, delta;
4770
Willy Tarreaua15645d2007-03-18 16:22:39 +01004771 /* Iterate through the headers.
4772 * we start with the start line.
4773 */
4774 old_idx = 0;
4775 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4776
4777 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4778 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004779 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004780
4781 cur_hdr = &txn->hdr_idx.v[cur_idx];
4782 cur_ptr = cur_next;
4783 cur_end = cur_ptr + cur_hdr->len;
4784 cur_next = cur_end + cur_hdr->cr + 1;
4785
4786 /* We have one full header between cur_ptr and cur_end, and the
4787 * next header starts at cur_next. We're only interested in
4788 * "Cookie:" headers.
4789 */
4790
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004791 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4792 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004793 old_idx = cur_idx;
4794 continue;
4795 }
4796
4797 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004798 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799
4800
4801 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004802 if (t->be->cookie_name == NULL &&
4803 t->be->appsession_name == NULL &&
4804 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004805 return;
4806
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004807 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004808
4809 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004810 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4811 break;
4812
4813 /* p1 is at the beginning of the cookie name */
4814 p2 = p1;
4815
4816 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4817 p2++;
4818
4819 if (p2 == cur_end || *p2 == ';') /* next cookie */
4820 break;
4821
4822 p3 = p2 + 1; /* skip the '=' sign */
4823 if (p3 == cur_end)
4824 break;
4825
4826 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004827 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004828 p4++;
4829
4830 /* here, we have the cookie name between p1 and p2,
4831 * and its value between p3 and p4.
4832 * we can process it.
4833 */
4834
4835 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004836 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004837 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004838 (p4 - p1 >= t->be->capture_namelen) &&
4839 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840 int log_len = p4 - p1;
4841
Willy Tarreau086b3b42007-05-13 21:45:51 +02004842 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004843 Alert("HTTP logging : out of memory.\n");
4844 }
4845
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004846 if (log_len > t->be->capture_len)
4847 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004848 memcpy(txn->srv_cookie, p1, log_len);
4849 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850 }
4851
4852 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004853 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4854 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004855 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004856 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004857
4858 /* If the cookie is in insert mode on a known server, we'll delete
4859 * this occurrence because we'll insert another one later.
4860 * We'll delete it too if the "indirect" option is set and we're in
4861 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004862 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4863 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004864 /* this header must be deleted */
4865 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4866 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4867 txn->hdr_idx.used--;
4868 cur_hdr->len = 0;
4869 cur_next += delta;
4870 txn->rsp.eoh += delta;
4871
Willy Tarreau3d300592007-03-18 18:34:41 +01004872 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004873 }
4874 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004875 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004876 /* replace bytes p3->p4 with the cookie name associated
4877 * with this server since we know it.
4878 */
4879 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4880 cur_hdr->len += delta;
4881 cur_next += delta;
4882 txn->rsp.eoh += delta;
4883
Willy Tarreau3d300592007-03-18 18:34:41 +01004884 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004885 }
4886 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004887 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004888 /* insert the cookie name associated with this server
4889 * before existing cookie, and insert a delimitor between them..
4890 */
4891 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4892 cur_hdr->len += delta;
4893 cur_next += delta;
4894 txn->rsp.eoh += delta;
4895
4896 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004897 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004898 }
4899 }
4900 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004901 else if ((t->be->appsession_name != NULL) &&
4902 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903
4904 /* Cool... it's the right one */
4905
4906 size_t server_id_len = strlen(t->srv->id) + 1;
4907 asession_temp = &local_asession;
4908
Willy Tarreau63963c62007-05-13 21:29:55 +02004909 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4911 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4912 return;
4913 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004914 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4915 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004916 asession_temp->serverid = NULL;
4917
4918 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004919 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4920 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004921 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004922 Alert("Not enough Memory process_srv():asession:calloc().\n");
4923 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4924 return;
4925 }
4926 asession_temp->sessid = local_asession.sessid;
4927 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004928 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004929 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4930 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004931 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004932 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004933 }
4934
Willy Tarreaua15645d2007-03-18 16:22:39 +01004935 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004936 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004937 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4938 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4939 return;
4940 }
4941 asession_temp->serverid[0] = '\0';
4942 }
4943
4944 if (asession_temp->serverid[0] == '\0')
4945 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4946
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004947 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004948 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004949#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004950 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004951 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004952#endif
4953 }/* end if ((t->proxy->appsession_name != NULL) ... */
4954 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4955 } /* we're now at the end of the cookie value */
4956
4957 /* keep the link from this header to next one */
4958 old_idx = cur_idx;
4959 } /* end of cookie processing on this header */
4960}
4961
4962
4963
4964/*
4965 * Check if response is cacheable or not. Updates t->flags.
4966 */
4967void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4968{
4969 struct http_txn *txn = &t->txn;
4970 char *p1, *p2;
4971
4972 char *cur_ptr, *cur_end, *cur_next;
4973 int cur_idx;
4974
Willy Tarreau5df51872007-11-25 16:20:08 +01004975 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976 return;
4977
4978 /* Iterate through the headers.
4979 * we start with the start line.
4980 */
4981 cur_idx = 0;
4982 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4983
4984 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4985 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004986 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004987
4988 cur_hdr = &txn->hdr_idx.v[cur_idx];
4989 cur_ptr = cur_next;
4990 cur_end = cur_ptr + cur_hdr->len;
4991 cur_next = cur_end + cur_hdr->cr + 1;
4992
4993 /* We have one full header between cur_ptr and cur_end, and the
4994 * next header starts at cur_next. We're only interested in
4995 * "Cookie:" headers.
4996 */
4997
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004998 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4999 if (val) {
5000 if ((cur_end - (cur_ptr + val) >= 8) &&
5001 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5002 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5003 return;
5004 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005 }
5006
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005007 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5008 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005009 continue;
5010
5011 /* OK, right now we know we have a cache-control header at cur_ptr */
5012
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005013 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005014
5015 if (p1 >= cur_end) /* no more info */
5016 continue;
5017
5018 /* p1 is at the beginning of the value */
5019 p2 = p1;
5020
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005021 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005022 p2++;
5023
5024 /* we have a complete value between p1 and p2 */
5025 if (p2 < cur_end && *p2 == '=') {
5026 /* we have something of the form no-cache="set-cookie" */
5027 if ((cur_end - p1 >= 21) &&
5028 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5029 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005030 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005031 continue;
5032 }
5033
5034 /* OK, so we know that either p2 points to the end of string or to a comma */
5035 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5036 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5037 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5038 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005039 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005040 return;
5041 }
5042
5043 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005044 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005045 continue;
5046 }
5047 }
5048}
5049
5050
Willy Tarreau58f10d72006-12-04 02:26:12 +01005051/*
5052 * Try to retrieve a known appsession in the URI, then the associated server.
5053 * If the server is found, it's assigned to the session.
5054 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005055void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056{
Willy Tarreau3d300592007-03-18 18:34:41 +01005057 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005058 appsess *asession_temp = NULL;
5059 appsess local_asession;
5060 char *request_line;
5061
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005062 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005063 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005064 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005065 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005066 return;
5067
5068 /* skip ';' */
5069 request_line++;
5070
5071 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005072 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005073 return;
5074
5075 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005076 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005077
5078 /* First try if we already have an appsession */
5079 asession_temp = &local_asession;
5080
Willy Tarreau63963c62007-05-13 21:29:55 +02005081 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005082 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5083 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5084 return;
5085 }
5086
5087 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005088 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5089 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005090 asession_temp->serverid = NULL;
5091
5092 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005093 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5094 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005095 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005096 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005097 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098 Alert("Not enough memory process_cli():asession:calloc().\n");
5099 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5100 return;
5101 }
5102 asession_temp->sessid = local_asession.sessid;
5103 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005104 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005105 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005106 }
5107 else {
5108 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005109 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005110 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005111
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005112 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005113 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005114
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005116 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005117 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005118#endif
5119 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005120 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005121 Alert("Found Application Session without matching server.\n");
5122 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005123 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124 while (srv) {
5125 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005126 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005127 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 txn->flags &= ~TX_CK_MASK;
5129 txn->flags |= TX_CK_VALID;
5130 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005131 t->srv = srv;
5132 break;
5133 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005134 txn->flags &= ~TX_CK_MASK;
5135 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005136 }
5137 }
5138 srv = srv->next;
5139 }
5140 }
5141}
5142
5143
Willy Tarreaub2513902006-12-17 14:52:38 +01005144/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005145 * In a GET or HEAD request, check if the requested URI matches the stats uri
5146 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005147 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005148 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005149 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005150 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005151 *
5152 * Returns 1 if the session's state changes, otherwise 0.
5153 */
5154int stats_check_uri_auth(struct session *t, struct proxy *backend)
5155{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005156 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005157 struct uri_auth *uri_auth = backend->uri_auth;
5158 struct user_auth *user;
5159 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005160 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005161
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005162 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005164 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005165 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005166 return 0;
5167
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005168 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005169
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005170 /* the URI is in h */
5171 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005172 return 0;
5173
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005174 h += uri_auth->uri_len;
5175 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5176 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005177 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005178 break;
5179 }
5180 h++;
5181 }
5182
5183 if (uri_auth->refresh) {
5184 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5185 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5186 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005187 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005188 break;
5189 }
5190 h++;
5191 }
5192 }
5193
Willy Tarreau55bb8452007-10-17 18:44:57 +02005194 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5195 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5196 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005197 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005198 break;
5199 }
5200 h++;
5201 }
5202
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005203 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5204
Willy Tarreaub2513902006-12-17 14:52:38 +01005205 /* we are in front of a interceptable URI. Let's check
5206 * if there's an authentication and if it's valid.
5207 */
5208 user = uri_auth->users;
5209 if (!user) {
5210 /* no user auth required, it's OK */
5211 authenticated = 1;
5212 } else {
5213 authenticated = 0;
5214
5215 /* a user list is defined, we have to check.
5216 * skip 21 chars for "Authorization: Basic ".
5217 */
5218
5219 /* FIXME: this should move to an earlier place */
5220 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005221 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5222 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5223 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005224 if (len > 14 &&
5225 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005226 txn->auth_hdr.str = h;
5227 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005228 break;
5229 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005230 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005231 }
5232
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005233 if (txn->auth_hdr.len < 21 ||
5234 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005235 user = NULL;
5236
5237 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005238 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5239 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005240 user->user_pwd, user->user_len)) {
5241 authenticated = 1;
5242 break;
5243 }
5244 user = user->next;
5245 }
5246 }
5247
5248 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005249 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005250
5251 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005252 msg.str = trash;
5253 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005254 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005255 client_retnclose(t, &msg);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005256 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005257 if (!(t->flags & SN_ERR_MASK))
5258 t->flags |= SN_ERR_PRXCOND;
5259 if (!(t->flags & SN_FINST_MASK))
5260 t->flags |= SN_FINST_R;
5261 return 1;
5262 }
5263
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005264 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005265 * data.
5266 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005267 EV_FD_CLR(t->cli_fd, DIR_RD);
5268 buffer_shutr(t->req);
5269 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005270 t->cli_state = CL_STSHUTR;
5271 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005272 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005273 t->data_source = DATA_SRC_STATS;
5274 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005275 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005276 produce_content(t);
5277 return 1;
5278}
5279
5280
Willy Tarreaubaaee002006-06-26 02:48:02 +02005281/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005282 * Print a debug line with a header
5283 */
5284void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5285{
5286 int len, max;
5287 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5288 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5289 max = end - start;
5290 UBOUND(max, sizeof(trash) - len - 1);
5291 len += strlcpy2(trash + len, start, max + 1);
5292 trash[len++] = '\n';
5293 write(1, trash, len);
5294}
5295
5296
Willy Tarreau8797c062007-05-07 00:55:35 +02005297/************************************************************************/
5298/* The code below is dedicated to ACL parsing and matching */
5299/************************************************************************/
5300
5301
5302
5303
5304/* 1. Check on METHOD
5305 * We use the pre-parsed method if it is known, and store its number as an
5306 * integer. If it is unknown, we use the pointer and the length.
5307 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005308static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005309{
5310 int len, meth;
5311
Willy Tarreauae8b7962007-06-09 23:10:04 +02005312 len = strlen(*text);
5313 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005314
5315 pattern->val.i = meth;
5316 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005317 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005318 if (!pattern->ptr.str)
5319 return 0;
5320 pattern->len = len;
5321 }
5322 return 1;
5323}
5324
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005325static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005326acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5327 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005328{
5329 int meth;
5330 struct http_txn *txn = l7;
5331
Willy Tarreaub6866442008-07-14 23:54:42 +02005332 if (!txn)
5333 return 0;
5334
Willy Tarreauc11416f2007-06-17 16:58:38 +02005335 if (txn->req.msg_state != HTTP_MSG_BODY)
5336 return 0;
5337
Willy Tarreau8797c062007-05-07 00:55:35 +02005338 meth = txn->meth;
5339 test->i = meth;
5340 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005341 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5342 /* ensure the indexes are not affected */
5343 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005344 test->len = txn->req.sl.rq.m_l;
5345 test->ptr = txn->req.sol;
5346 }
5347 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5348 return 1;
5349}
5350
5351static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5352{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005353 int icase;
5354
Willy Tarreau8797c062007-05-07 00:55:35 +02005355 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005356 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005357
5358 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005359 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005360
5361 /* Other method, we must compare the strings */
5362 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005363 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005364
5365 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5366 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5367 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005368 return ACL_PAT_FAIL;
5369 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005370}
5371
5372/* 2. Check on Request/Status Version
5373 * We simply compare strings here.
5374 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005375static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005376{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005377 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005378 if (!pattern->ptr.str)
5379 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005380 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005381 return 1;
5382}
5383
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005384static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005385acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5386 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005387{
5388 struct http_txn *txn = l7;
5389 char *ptr;
5390 int len;
5391
Willy Tarreaub6866442008-07-14 23:54:42 +02005392 if (!txn)
5393 return 0;
5394
Willy Tarreauc11416f2007-06-17 16:58:38 +02005395 if (txn->req.msg_state != HTTP_MSG_BODY)
5396 return 0;
5397
Willy Tarreau8797c062007-05-07 00:55:35 +02005398 len = txn->req.sl.rq.v_l;
5399 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5400
5401 while ((len-- > 0) && (*ptr++ != '/'));
5402 if (len <= 0)
5403 return 0;
5404
5405 test->ptr = ptr;
5406 test->len = len;
5407
5408 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5409 return 1;
5410}
5411
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005412static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005413acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5414 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005415{
5416 struct http_txn *txn = l7;
5417 char *ptr;
5418 int len;
5419
Willy Tarreaub6866442008-07-14 23:54:42 +02005420 if (!txn)
5421 return 0;
5422
Willy Tarreauc11416f2007-06-17 16:58:38 +02005423 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5424 return 0;
5425
Willy Tarreau8797c062007-05-07 00:55:35 +02005426 len = txn->rsp.sl.st.v_l;
5427 ptr = txn->rsp.sol;
5428
5429 while ((len-- > 0) && (*ptr++ != '/'));
5430 if (len <= 0)
5431 return 0;
5432
5433 test->ptr = ptr;
5434 test->len = len;
5435
5436 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5437 return 1;
5438}
5439
5440/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005441static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005442acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5443 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005444{
5445 struct http_txn *txn = l7;
5446 char *ptr;
5447 int len;
5448
Willy Tarreaub6866442008-07-14 23:54:42 +02005449 if (!txn)
5450 return 0;
5451
Willy Tarreauc11416f2007-06-17 16:58:38 +02005452 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5453 return 0;
5454
Willy Tarreau8797c062007-05-07 00:55:35 +02005455 len = txn->rsp.sl.st.c_l;
5456 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5457
5458 test->i = __strl2ui(ptr, len);
5459 test->flags = ACL_TEST_F_VOL_1ST;
5460 return 1;
5461}
5462
5463/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005464static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005465acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5466 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005467{
5468 struct http_txn *txn = l7;
5469
Willy Tarreaub6866442008-07-14 23:54:42 +02005470 if (!txn)
5471 return 0;
5472
Willy Tarreauc11416f2007-06-17 16:58:38 +02005473 if (txn->req.msg_state != HTTP_MSG_BODY)
5474 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005475
Willy Tarreauc11416f2007-06-17 16:58:38 +02005476 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5477 /* ensure the indexes are not affected */
5478 return 0;
5479
Willy Tarreau8797c062007-05-07 00:55:35 +02005480 test->len = txn->req.sl.rq.u_l;
5481 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5482
Willy Tarreauf3d25982007-05-08 22:45:09 +02005483 /* we do not need to set READ_ONLY because the data is in a buffer */
5484 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005485 return 1;
5486}
5487
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005488static int
5489acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5490 struct acl_expr *expr, struct acl_test *test)
5491{
5492 struct http_txn *txn = l7;
5493
Willy Tarreaub6866442008-07-14 23:54:42 +02005494 if (!txn)
5495 return 0;
5496
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005497 if (txn->req.msg_state != HTTP_MSG_BODY)
5498 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005499
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005500 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5501 /* ensure the indexes are not affected */
5502 return 0;
5503
5504 /* Parse HTTP request */
5505 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5506 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5507 test->i = AF_INET;
5508
5509 /*
5510 * If we are parsing url in frontend space, we prepare backend stage
5511 * to not parse again the same url ! optimization lazyness...
5512 */
5513 if (px->options & PR_O_HTTP_PROXY)
5514 l4->flags |= SN_ADDR_SET;
5515
5516 test->flags = ACL_TEST_F_READ_ONLY;
5517 return 1;
5518}
5519
5520static int
5521acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5522 struct acl_expr *expr, struct acl_test *test)
5523{
5524 struct http_txn *txn = l7;
5525
Willy Tarreaub6866442008-07-14 23:54:42 +02005526 if (!txn)
5527 return 0;
5528
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005529 if (txn->req.msg_state != HTTP_MSG_BODY)
5530 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005531
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005532 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5533 /* ensure the indexes are not affected */
5534 return 0;
5535
5536 /* Same optimization as url_ip */
5537 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5538 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5539
5540 if (px->options & PR_O_HTTP_PROXY)
5541 l4->flags |= SN_ADDR_SET;
5542
5543 test->flags = ACL_TEST_F_READ_ONLY;
5544 return 1;
5545}
5546
Willy Tarreauc11416f2007-06-17 16:58:38 +02005547/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5548 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5549 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005550static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005551acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005552 struct acl_expr *expr, struct acl_test *test)
5553{
5554 struct http_txn *txn = l7;
5555 struct hdr_idx *idx = &txn->hdr_idx;
5556 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005557
Willy Tarreaub6866442008-07-14 23:54:42 +02005558 if (!txn)
5559 return 0;
5560
Willy Tarreau33a7e692007-06-10 19:45:56 +02005561 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5562 /* search for header from the beginning */
5563 ctx->idx = 0;
5564
Willy Tarreau33a7e692007-06-10 19:45:56 +02005565 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5566 test->flags |= ACL_TEST_F_FETCH_MORE;
5567 test->flags |= ACL_TEST_F_VOL_HDR;
5568 test->len = ctx->vlen;
5569 test->ptr = (char *)ctx->line + ctx->val;
5570 return 1;
5571 }
5572
5573 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5574 test->flags |= ACL_TEST_F_VOL_HDR;
5575 return 0;
5576}
5577
Willy Tarreau33a7e692007-06-10 19:45:56 +02005578static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005579acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5580 struct acl_expr *expr, struct acl_test *test)
5581{
5582 struct http_txn *txn = l7;
5583
Willy Tarreaub6866442008-07-14 23:54:42 +02005584 if (!txn)
5585 return 0;
5586
Willy Tarreauc11416f2007-06-17 16:58:38 +02005587 if (txn->req.msg_state != HTTP_MSG_BODY)
5588 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005589
Willy Tarreauc11416f2007-06-17 16:58:38 +02005590 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5591 /* ensure the indexes are not affected */
5592 return 0;
5593
5594 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5595}
5596
5597static int
5598acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5599 struct acl_expr *expr, struct acl_test *test)
5600{
5601 struct http_txn *txn = l7;
5602
Willy Tarreaub6866442008-07-14 23:54:42 +02005603 if (!txn)
5604 return 0;
5605
Willy Tarreauc11416f2007-06-17 16:58:38 +02005606 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5607 return 0;
5608
5609 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5610}
5611
5612/* 6. Check on HTTP header count. The number of occurrences is returned.
5613 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5614 */
5615static int
5616acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005617 struct acl_expr *expr, struct acl_test *test)
5618{
5619 struct http_txn *txn = l7;
5620 struct hdr_idx *idx = &txn->hdr_idx;
5621 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005622 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005623
Willy Tarreaub6866442008-07-14 23:54:42 +02005624 if (!txn)
5625 return 0;
5626
Willy Tarreau33a7e692007-06-10 19:45:56 +02005627 ctx.idx = 0;
5628 cnt = 0;
5629 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5630 cnt++;
5631
5632 test->i = cnt;
5633 test->flags = ACL_TEST_F_VOL_HDR;
5634 return 1;
5635}
5636
Willy Tarreauc11416f2007-06-17 16:58:38 +02005637static int
5638acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5639 struct acl_expr *expr, struct acl_test *test)
5640{
5641 struct http_txn *txn = l7;
5642
Willy Tarreaub6866442008-07-14 23:54:42 +02005643 if (!txn)
5644 return 0;
5645
Willy Tarreauc11416f2007-06-17 16:58:38 +02005646 if (txn->req.msg_state != HTTP_MSG_BODY)
5647 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005648
Willy Tarreauc11416f2007-06-17 16:58:38 +02005649 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5650 /* ensure the indexes are not affected */
5651 return 0;
5652
5653 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5654}
5655
5656static int
5657acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5658 struct acl_expr *expr, struct acl_test *test)
5659{
5660 struct http_txn *txn = l7;
5661
Willy Tarreaub6866442008-07-14 23:54:42 +02005662 if (!txn)
5663 return 0;
5664
Willy Tarreauc11416f2007-06-17 16:58:38 +02005665 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5666 return 0;
5667
5668 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5669}
5670
Willy Tarreau33a7e692007-06-10 19:45:56 +02005671/* 7. Check on HTTP header's integer value. The integer value is returned.
5672 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005673 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005674 */
5675static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005676acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005677 struct acl_expr *expr, struct acl_test *test)
5678{
5679 struct http_txn *txn = l7;
5680 struct hdr_idx *idx = &txn->hdr_idx;
5681 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005682
Willy Tarreaub6866442008-07-14 23:54:42 +02005683 if (!txn)
5684 return 0;
5685
Willy Tarreau33a7e692007-06-10 19:45:56 +02005686 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5687 /* search for header from the beginning */
5688 ctx->idx = 0;
5689
Willy Tarreau33a7e692007-06-10 19:45:56 +02005690 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5691 test->flags |= ACL_TEST_F_FETCH_MORE;
5692 test->flags |= ACL_TEST_F_VOL_HDR;
5693 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5694 return 1;
5695 }
5696
5697 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5698 test->flags |= ACL_TEST_F_VOL_HDR;
5699 return 0;
5700}
5701
Willy Tarreauc11416f2007-06-17 16:58:38 +02005702static int
5703acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5704 struct acl_expr *expr, struct acl_test *test)
5705{
5706 struct http_txn *txn = l7;
5707
Willy Tarreaub6866442008-07-14 23:54:42 +02005708 if (!txn)
5709 return 0;
5710
Willy Tarreauc11416f2007-06-17 16:58:38 +02005711 if (txn->req.msg_state != HTTP_MSG_BODY)
5712 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005713
Willy Tarreauc11416f2007-06-17 16:58:38 +02005714 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5715 /* ensure the indexes are not affected */
5716 return 0;
5717
5718 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5719}
5720
5721static int
5722acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5723 struct acl_expr *expr, struct acl_test *test)
5724{
5725 struct http_txn *txn = l7;
5726
Willy Tarreaub6866442008-07-14 23:54:42 +02005727 if (!txn)
5728 return 0;
5729
Willy Tarreauc11416f2007-06-17 16:58:38 +02005730 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5731 return 0;
5732
5733 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5734}
5735
Willy Tarreau737b0c12007-06-10 21:28:46 +02005736/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5737 * the first '/' after the possible hostname, and ends before the possible '?'.
5738 */
5739static int
5740acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5741 struct acl_expr *expr, struct acl_test *test)
5742{
5743 struct http_txn *txn = l7;
5744 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005745
Willy Tarreaub6866442008-07-14 23:54:42 +02005746 if (!txn)
5747 return 0;
5748
Willy Tarreauc11416f2007-06-17 16:58:38 +02005749 if (txn->req.msg_state != HTTP_MSG_BODY)
5750 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005751
Willy Tarreauc11416f2007-06-17 16:58:38 +02005752 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5753 /* ensure the indexes are not affected */
5754 return 0;
5755
Willy Tarreau21d2af32008-02-14 20:25:24 +01005756 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5757 ptr = http_get_path(txn);
5758 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005759 return 0;
5760
5761 /* OK, we got the '/' ! */
5762 test->ptr = ptr;
5763
5764 while (ptr < end && *ptr != '?')
5765 ptr++;
5766
5767 test->len = ptr - test->ptr;
5768
5769 /* we do not need to set READ_ONLY because the data is in a buffer */
5770 test->flags = ACL_TEST_F_VOL_1ST;
5771 return 1;
5772}
5773
5774
Willy Tarreau8797c062007-05-07 00:55:35 +02005775
5776/************************************************************************/
5777/* All supported keywords must be declared here. */
5778/************************************************************************/
5779
5780/* Note: must not be declared <const> as its list will be overwritten */
5781static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005782 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5783 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5784 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5785 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005786
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005787 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5788 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5789 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5790 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5791 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5792 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5793 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5794 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5795 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005796
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005797 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5798 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5799 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5800 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5801 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5802 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5803 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5804 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5805 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5806 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005807
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005808 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5809 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5810 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5811 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5812 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5813 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5814 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5815 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5816 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005817
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005818 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5819 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5820 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5821 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5822 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5823 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5824 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005825
Willy Tarreauf3d25982007-05-08 22:45:09 +02005826 { NULL, NULL, NULL, NULL },
5827
5828#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005829 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5830 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5831 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5832 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5833 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5834 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5835 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5836
Willy Tarreau8797c062007-05-07 00:55:35 +02005837 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5838 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5839 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5840 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5841 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5842 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5843 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5844 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5845
5846 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5847 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5848 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5849 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5850 { NULL, NULL, NULL, NULL },
5851#endif
5852}};
5853
5854
5855__attribute__((constructor))
5856static void __http_protocol_init(void)
5857{
5858 acl_register_keywords(&acl_kws);
5859}
5860
5861
Willy Tarreau58f10d72006-12-04 02:26:12 +01005862/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005863 * Local variables:
5864 * c-indent-level: 8
5865 * c-basic-offset: 8
5866 * End:
5867 */