blob: 99350cfec38a5a594fbacba165c1a339f7552b2f [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
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002599 /* Invalid response */
2600 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2601 hdr_response_bad:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002602 buffer_shutr_done(rep);
2603 buffer_shutw_done(req);
2604 fd_delete(t->srv_fd);
2605 if (t->srv) {
2606 t->srv->cur_sess--;
2607 t->srv->failed_resp++;
2608 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002609 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002610 t->be->failed_resp++;
2611 t->srv_state = SV_STCLOSE;
2612 t->analysis &= ~AN_RTR_ANY;
2613 rep->flags |= BF_MAY_FORWARD;
2614 txn->status = 502;
2615 client_return(t, error_message(t, HTTP_ERR_502));
2616 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002617 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002618 if (!(t->flags & SN_FINST_MASK))
2619 t->flags |= SN_FINST_H;
2620 /* We used to have a free connection slot. Since we'll never use it,
2621 * we have to inform the server that it may be used by another session.
2622 */
2623 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2624 process_srv_queue(t->srv);
2625
2626 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002627 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002628
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002629 /* write error to client, read error or close from server */
2630 if (req->flags & BF_WRITE_ERROR ||
2631 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002632 buffer_shutr_done(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002633 buffer_shutw_done(req);
2634 fd_delete(t->srv_fd);
2635 if (t->srv) {
2636 t->srv->cur_sess--;
2637 t->srv->failed_resp++;
2638 sess_change_server(t, NULL);
2639 }
2640 t->be->failed_resp++;
2641 t->srv_state = SV_STCLOSE;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002642 t->analysis &= ~AN_RTR_ANY;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002643 rep->flags |= BF_MAY_FORWARD;
2644 txn->status = 502;
2645 client_return(t, error_message(t, HTTP_ERR_502));
2646 if (!(t->flags & SN_ERR_MASK))
2647 t->flags |= SN_ERR_SRVCL;
2648 if (!(t->flags & SN_FINST_MASK))
2649 t->flags |= SN_FINST_H;
2650 /* We used to have a free connection slot. Since we'll never use it,
2651 * we have to inform the server that it may be used by another session.
2652 */
2653 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2654 process_srv_queue(t->srv);
2655
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002656 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002657 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002658
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002659 /* too large response does not fit in buffer. */
2660 else if (rep->l >= rep->rlim - rep->data)
2661 goto hdr_response_bad;
2662
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002663 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002664 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002665 buffer_shutr_done(rep);
2666 buffer_shutw_done(req);
2667 fd_delete(t->srv_fd);
2668 if (t->srv) {
2669 t->srv->cur_sess--;
2670 t->srv->failed_resp++;
2671 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002672 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002673 t->be->failed_resp++;
2674 t->srv_state = SV_STCLOSE;
2675 t->analysis &= ~AN_RTR_ANY;
2676 rep->flags |= BF_MAY_FORWARD;
2677 txn->status = 504;
2678 client_return(t, error_message(t, HTTP_ERR_504));
2679 if (!(t->flags & SN_ERR_MASK))
2680 t->flags |= SN_ERR_SRVTO;
2681 if (!(t->flags & SN_FINST_MASK))
2682 t->flags |= SN_FINST_H;
2683 /* We used to have a free connection slot. Since we'll never use it,
2684 * we have to inform the server that it may be used by another session.
2685 */
2686 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2687 process_srv_queue(t->srv);
2688 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002690
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002691 ///* last client read and buffer empty */
2692 //else if (unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) {
2693 // EV_FD_CLR(t->srv_fd, DIR_WR);
2694 // buffer_shutw_done(req);
2695 //
2696 // /* We must ensure that the read part is still
2697 // * alive when switching to shutw */
2698 // EV_FD_SET(t->srv_fd, DIR_RD);
2699 // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
2700 //
2701 // shutdown(t->srv_fd, SHUT_WR);
2702 // t->srv_state = SV_STSHUTW;
2703 // t->analysis &= ~AN_RTR_ANY;
2704 // return 1;
2705 //}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002706
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002707 /* write timeout */
2708 /* FIXME!!! here, we don't want to switch to SHUTW if the
2709 * client shuts read too early, because we may still have
2710 * some work to do on the headers.
2711 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002712 //else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
2713 // tick_is_expired(req->wex, now_ms))) {
2714 // EV_FD_CLR(t->srv_fd, DIR_WR);
2715 // buffer_shutw_done(req);
2716 // shutdown(t->srv_fd, SHUT_WR);
2717 // /* We must ensure that the read part is still alive
2718 // * when switching to shutw */
2719 // EV_FD_SET(t->srv_fd, DIR_RD);
2720 // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
2721 //
2722 // t->srv_state = SV_STSHUTW;
2723 // t->analysis &= ~AN_RTR_ANY;
2724 // if (!(t->flags & SN_ERR_MASK))
2725 // t->flags |= SN_ERR_SRVTO;
2726 // if (!(t->flags & SN_FINST_MASK))
2727 // t->flags |= SN_FINST_H;
2728 // return 1;
2729 //}
Willy Tarreaub8750a82006-09-03 09:56:00 +02002730
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002731 /*
2732 * And now the non-error cases.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002733 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002734
2735 /* Data remaining in the request buffer.
2736 * This happens during the first pass here, and during
2737 * long posts.
2738 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002739 //else if (likely(req->l)) {
2740 // if (!tick_isset(req->wex)) {
2741 // EV_FD_COND_S(t->srv_fd, DIR_WR);
2742 // /* restart writing */
2743 // req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
2744 // if (tick_isset(req->wex)) {
2745 // /* FIXME: to prevent the server from expiring read timeouts during writes,
2746 // * we refresh it. */
2747 // rep->rex = req->wex;
2748 // }
2749 // }
2750 //}
2751 //
2752 ///* nothing left in the request buffer */
2753 //else {
2754 // if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2755 // /* stop writing */
2756 // req->wex = TICK_ETERNITY;
2757 // }
2758 //}
2759 //
2760 ///* return 0 if nothing changed */
2761 //return !(t->analysis & AN_RTR_ANY);
2762 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002764
Willy Tarreau21d2af32008-02-14 20:25:24 +01002765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002766 /*****************************************************************
2767 * More interesting part now : we know that we have a complete *
2768 * response which at least looks like HTTP. We have an indicator *
2769 * of each header's length, so we can parse them quickly. *
2770 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002771
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002772 /* ensure we keep this pointer to the beginning of the message */
2773 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002775 /*
2776 * 1: get the status code and check for cacheability.
2777 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002778
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002779 t->logs.logwait &= ~LW_RESP;
2780 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002781
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002782 switch (txn->status) {
2783 case 200:
2784 case 203:
2785 case 206:
2786 case 300:
2787 case 301:
2788 case 410:
2789 /* RFC2616 @13.4:
2790 * "A response received with a status code of
2791 * 200, 203, 206, 300, 301 or 410 MAY be stored
2792 * by a cache (...) unless a cache-control
2793 * directive prohibits caching."
2794 *
2795 * RFC2616 @9.5: POST method :
2796 * "Responses to this method are not cacheable,
2797 * unless the response includes appropriate
2798 * Cache-Control or Expires header fields."
2799 */
2800 if (likely(txn->meth != HTTP_METH_POST) &&
2801 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2802 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2803 break;
2804 default:
2805 break;
2806 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002807
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002808 /*
2809 * 2: we may need to capture headers
2810 */
2811 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2812 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2813 txn->rsp.cap, t->fe->rsp_cap);
2814
2815 /*
2816 * 3: we will have to evaluate the filters.
2817 * As opposed to version 1.2, now they will be evaluated in the
2818 * filters order and not in the header order. This means that
2819 * each filter has to be validated among all headers.
2820 *
2821 * Filters are tried with ->be first, then with ->fe if it is
2822 * different from ->be.
2823 */
2824
2825 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2826
2827 cur_proxy = t->be;
2828 while (1) {
2829 struct proxy *rule_set = cur_proxy;
2830
2831 /* try headers filters */
2832 if (rule_set->rsp_exp != NULL) {
2833 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2834 return_bad_resp:
2835 if (t->srv) {
2836 t->srv->cur_sess--;
2837 t->srv->failed_resp++;
2838 sess_change_server(t, NULL);
2839 }
2840 cur_proxy->failed_resp++;
2841 return_srv_prx_502:
2842 buffer_shutr_done(rep);
2843 buffer_shutw_done(req);
2844 fd_delete(t->srv_fd);
2845 t->srv_state = SV_STCLOSE;
2846 t->analysis &= ~AN_RTR_ANY;
2847 rep->flags |= BF_MAY_FORWARD;
2848 txn->status = 502;
2849 client_return(t, error_message(t, HTTP_ERR_502));
2850 if (!(t->flags & SN_ERR_MASK))
2851 t->flags |= SN_ERR_PRXCOND;
2852 if (!(t->flags & SN_FINST_MASK))
2853 t->flags |= SN_FINST_H;
2854 /* We used to have a free connection slot. Since we'll never use it,
2855 * we have to inform the server that it may be used by another session.
2856 */
2857 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2858 process_srv_queue(t->srv);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002859 return 1;
2860 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 /* has the response been denied ? */
2864 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002865 if (t->srv) {
2866 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002867 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002868 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002869 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002870 cur_proxy->denied_resp++;
2871 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002873
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002874 /* We might have to check for "Connection:" */
2875 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2876 !(t->flags & SN_CONN_CLOSED)) {
2877 char *cur_ptr, *cur_end, *cur_next;
2878 int cur_idx, old_idx, delta, val;
2879 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002880
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002881 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2882 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002884 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2885 cur_hdr = &txn->hdr_idx.v[cur_idx];
2886 cur_ptr = cur_next;
2887 cur_end = cur_ptr + cur_hdr->len;
2888 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002890 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2891 if (val) {
2892 /* 3 possibilities :
2893 * - we have already set Connection: close,
2894 * so we remove this line.
2895 * - we have not yet set Connection: close,
2896 * but this line indicates close. We leave
2897 * it untouched and set the flag.
2898 * - we have not yet set Connection: close,
2899 * and this line indicates non-close. We
2900 * replace it.
2901 */
2902 if (t->flags & SN_CONN_CLOSED) {
2903 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2904 txn->rsp.eoh += delta;
2905 cur_next += delta;
2906 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2907 txn->hdr_idx.used--;
2908 cur_hdr->len = 0;
2909 } else {
2910 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2911 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2912 "close", 5);
2913 cur_next += delta;
2914 cur_hdr->len += delta;
2915 txn->rsp.eoh += delta;
2916 }
2917 t->flags |= SN_CONN_CLOSED;
2918 }
2919 }
2920 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002921 }
2922 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002923
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002924 /* add response headers from the rule sets in the same order */
2925 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2926 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2927 rule_set->rsp_add[cur_idx])) < 0)
2928 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002929 }
2930
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 /* check whether we're already working on the frontend */
2932 if (cur_proxy == t->fe)
2933 break;
2934 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002935 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937 /*
2938 * 4: check for server cookie.
2939 */
2940 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2941 || (t->be->options & PR_O_CHK_CACHE))
2942 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002943
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944
Willy Tarreaua15645d2007-03-18 16:22:39 +01002945 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002946 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2949 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002950
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002951 /*
2952 * 6: add server cookie in the response if needed
2953 */
2954 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2955 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2956 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002957
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002958 /* the server is known, it's not the one the client requested, we have to
2959 * insert a set-cookie here, except if we want to insert only on POST
2960 * requests and this one isn't. Note that servers which don't have cookies
2961 * (eg: some backup servers) will return a full cookie removal request.
2962 */
2963 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2964 t->be->cookie_name,
2965 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002966
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002967 if (t->be->cookie_domain)
2968 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002970 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2971 trash, len)) < 0)
2972 goto return_bad_resp;
2973 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002975 /* Here, we will tell an eventual cache on the client side that we don't
2976 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2977 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2978 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2979 */
2980 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002982 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2983
2984 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2985 "Cache-control: private", 22)) < 0)
2986 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002987 }
2988 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989
Willy Tarreaubaaee002006-06-26 02:48:02 +02002990
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002991 /*
2992 * 7: check if result will be cacheable with a cookie.
2993 * We'll block the response if security checks have caught
2994 * nasty things such as a cacheable cookie.
2995 */
2996 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2997 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2998 (t->be->options & PR_O_CHK_CACHE)) {
2999
3000 /* we're in presence of a cacheable response containing
3001 * a set-cookie header. We'll block it as requested by
3002 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003003 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003004 if (t->srv) {
3005 t->srv->cur_sess--;
3006 t->srv->failed_secu++;
3007 sess_change_server(t, NULL);
3008 }
3009 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003010
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003011 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3012 t->be->id, t->srv?t->srv->id:"<dispatch>");
3013 send_log(t->be, LOG_ALERT,
3014 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3015 t->be->id, t->srv?t->srv->id:"<dispatch>");
3016 goto return_srv_prx_502;
3017 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003018
3019 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 * 8: add "Connection: close" if needed and not yet set.
3021 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003022 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003023 if (!(t->flags & SN_CONN_CLOSED) &&
3024 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3025 if ((unlikely(msg->sl.st.v_l != 8) ||
3026 unlikely(req->data[msg->som + 7] != '0')) &&
3027 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3028 "Connection: close", 17)) < 0)
3029 goto return_bad_resp;
3030 t->flags |= SN_CONN_CLOSED;
3031 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003032
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /*************************************************************
3035 * OK, that's finished for the headers. We have done what we *
3036 * could. Let's switch to the DATA state. *
3037 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003039 t->srv_state = SV_STDATA;
3040 t->analysis &= ~AN_RTR_ANY;
3041 rep->flags |= BF_MAY_FORWARD;
3042 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
3043 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003044
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003045 /* client connection already closed or option 'forceclose' required :
3046 * we close the server's outgoing connection right now.
3047 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003048 //if ((req->l == 0) &&
3049 // (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) {
3050 // EV_FD_CLR(t->srv_fd, DIR_WR);
3051 // buffer_shutw_done(req);
3052 //
3053 // /* We must ensure that the read part is still alive when switching
3054 // * to shutw */
3055 // EV_FD_SET(t->srv_fd, DIR_RD);
3056 // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3057 //
3058 // shutdown(t->srv_fd, SHUT_WR);
3059 // t->srv_state = SV_STSHUTW;
3060 // t->analysis &= ~AN_RTR_ANY;
3061 //}
Willy Tarreau6468d922008-08-03 19:15:35 +02003062
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003063#ifdef CONFIG_HAP_TCPSPLICE
3064 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3065 /* TCP splicing supported by both FE and BE */
3066 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3067 }
3068#endif
3069 /* if the user wants to log as soon as possible, without counting
3070 * bytes from the server, then this is the right moment. We have
3071 * to temporarily assign bytes_out to log what we currently have.
3072 */
3073 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3074 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3075 t->logs.bytes_out = txn->rsp.eoh;
3076 if (t->fe->to_log & LW_REQ)
3077 http_sess_log(t);
3078 else
3079 tcp_sess_log(t);
3080 t->logs.bytes_out = 0;
3081 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003082
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003083 /* Note: we must not try to cheat by jumping directly to DATA,
3084 * otherwise we would not let the client side wake up.
3085 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 return 1;
3088 }
3089 return 0;
3090}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003091
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003092/*
3093 * manages the client FSM and its socket. BTW, it also tries to handle the
3094 * cookie. It returns 1 if a state has changed (and a resync may be needed),
3095 * 0 else.
3096 */
3097int process_cli(struct session *t)
3098{
3099 int s = t->srv_state;
3100 int c = t->cli_state;
3101 struct buffer *req = t->req;
3102 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003103
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003104 DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003105 now_ms,
3106 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3107 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
3108 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003109 req->flags, rep->flags,
3110 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003111
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003112 /* if no analysis remains, it's time to forward the connection */
3113 if (!(t->analysis & AN_REQ_ANY) && !(req->flags & (BF_MAY_CONNECT|BF_MAY_FORWARD)))
3114 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003115
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003116 if (c == CL_STDATA || c == CL_STSHUTR || c == CL_STSHUTW) {
3117 /* read or write error */
3118 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
3119 buffer_shutr_done(req);
3120 buffer_shutw_done(rep);
3121 fd_delete(t->cli_fd);
3122 t->cli_state = CL_STCLOSE;
3123 if (!(t->flags & SN_ERR_MASK))
3124 t->flags |= SN_ERR_CLICL;
3125 if (!(t->flags & SN_FINST_MASK)) {
3126 if (t->analysis & AN_REQ_ANY)
3127 t->flags |= SN_FINST_R;
3128 else if (t->pend_pos)
3129 t->flags |= SN_FINST_Q;
3130 else if (s == SV_STCONN)
3131 t->flags |= SN_FINST_C;
3132 else
3133 t->flags |= SN_FINST_D;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003134 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003136 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003137 /* last read, or end of server write */
3138 else if (!(req->flags & BF_SHUTR_STATUS) && /* already done */
3139 req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
3140 buffer_shutr_done(req);
3141 if (!(rep->flags & BF_SHUTW_STATUS)) {
3142 EV_FD_CLR(t->cli_fd, DIR_RD);
3143 t->cli_state = CL_STSHUTR;
3144 } else {
3145 /* output was already closed */
3146 fd_delete(t->cli_fd);
3147 t->cli_state = CL_STCLOSE;
3148 }
3149 return 1;
3150 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003151 /* last server read and buffer empty : we only check them when we're
3152 * allowed to forward the data.
3153 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003154 else if (!(rep->flags & BF_SHUTW_STATUS) && /* already done */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003155 rep->l == 0 && rep->flags & BF_MAY_FORWARD &&
3156 rep->flags & BF_SHUTR_STATUS && !(t->flags & SN_SELF_GEN)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003157 buffer_shutw_done(rep);
3158 if (!(req->flags & BF_SHUTR_STATUS)) {
3159 EV_FD_CLR(t->cli_fd, DIR_WR);
3160 shutdown(t->cli_fd, SHUT_WR);
3161 /* We must ensure that the read part is still alive when switching to shutw */
3162 /* FIXME: is this still true ? */
3163 EV_FD_SET(t->cli_fd, DIR_RD);
3164 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3165 t->cli_state = CL_STSHUTW;
3166 } else {
3167 fd_delete(t->cli_fd);
3168 t->cli_state = CL_STCLOSE;
3169 }
3170 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003171 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003172 /* read timeout */
3173 else if (tick_is_expired(req->rex, now_ms)) {
3174 buffer_shutr_done(req);
3175 req->flags |= BF_READ_TIMEOUT;
3176 if (!(rep->flags & BF_SHUTW_STATUS)) {
3177 EV_FD_CLR(t->cli_fd, DIR_RD);
3178 t->cli_state = CL_STSHUTR;
3179 } else {
3180 /* output was already closed */
3181 fd_delete(t->cli_fd);
3182 t->cli_state = CL_STCLOSE;
3183 }
3184 if (!(t->flags & SN_ERR_MASK))
3185 t->flags |= SN_ERR_CLITO;
3186 if (!(t->flags & SN_FINST_MASK)) {
3187 if (t->analysis & AN_REQ_ANY)
3188 t->flags |= SN_FINST_R;
3189 else if (t->pend_pos)
3190 t->flags |= SN_FINST_Q;
3191 else if (s == SV_STCONN)
3192 t->flags |= SN_FINST_C;
3193 else
3194 t->flags |= SN_FINST_D;
3195 }
3196 return 1;
3197 }
3198 /* write timeout */
3199 else if (tick_is_expired(rep->wex, now_ms)) {
3200 buffer_shutw_done(rep);
3201 rep->flags |= BF_WRITE_TIMEOUT;
3202 if (!(req->flags & BF_SHUTR_STATUS)) {
3203 EV_FD_CLR(t->cli_fd, DIR_WR);
3204 shutdown(t->cli_fd, SHUT_WR);
3205 /* We must ensure that the read part is still alive when switching to shutw */
3206 /* FIXME: is this still true ? */
3207 EV_FD_SET(t->cli_fd, DIR_RD);
3208 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3209 t->cli_state = CL_STSHUTW;
3210 } else {
3211 fd_delete(t->cli_fd);
3212 t->cli_state = CL_STCLOSE;
3213 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003214
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003215 if (!(t->flags & SN_ERR_MASK))
3216 t->flags |= SN_ERR_CLITO;
3217 if (!(t->flags & SN_FINST_MASK)) {
3218 if (t->analysis & AN_REQ_ANY)
3219 t->flags |= SN_FINST_R;
3220 else if (t->pend_pos)
3221 t->flags |= SN_FINST_Q;
3222 else if (s == SV_STCONN)
3223 t->flags |= SN_FINST_C;
3224 else
3225 t->flags |= SN_FINST_D;
3226 }
3227 return 1;
3228 }
3229
3230 /* manage read timeout */
3231 if (!(req->flags & BF_SHUTR_STATUS)) {
3232 if (req->l >= req->rlim - req->data) {
3233 /* no room to read more data */
3234 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3235 /* stop reading until we get some space */
3236 req->rex = TICK_ETERNITY;
3237 }
3238 } else {
3239 EV_FD_COND_S(t->cli_fd, DIR_RD);
3240 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3241 }
3242 }
3243
3244 /* manage write timeout */
3245 if (!(rep->flags & BF_SHUTW_STATUS)) {
3246 /* first, we may have to produce data (eg: stats).
3247 * right now, this is limited to the SHUTR state.
3248 */
3249 if (req->flags & BF_SHUTR_STATUS && t->flags & SN_SELF_GEN) {
3250 produce_content(t);
3251 if (rep->l == 0) {
3252 buffer_shutw_done(rep);
3253 fd_delete(t->cli_fd);
3254 t->cli_state = CL_STCLOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255 return 1;
3256 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003257 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003258
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003259 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3260 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
3261 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3262 /* stop writing */
3263 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003264 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003265 } else {
3266 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003267 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003268 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 /* restart writing */
3270 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3271 if (!(req->flags & BF_SHUTR_STATUS) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3272 /* FIXME: to prevent the client from expiring read timeouts during writes,
3273 * we refresh it, except if it was already infinite. */
3274 req->rex = rep->wex;
3275 }
3276 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003277 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003278 }
3279 return 0; /* other cases change nothing */
3280 }
3281 else { /* CL_STCLOSE: nothing to do */
3282 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3283 int len;
3284 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);
3285 write(1, trash, len);
3286 }
3287 return 0;
3288 }
3289 return 0;
3290}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003293/*
3294 * manages the server FSM and its socket. It returns 1 if a state has changed
3295 * (and a resync may be needed), 0 else.
3296 */
3297int process_srv(struct session *t)
3298{
3299 int s = t->srv_state;
3300 struct http_txn *txn = &t->txn;
3301 struct buffer *req = t->req;
3302 struct buffer *rep = t->rep;
3303 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003305 DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003306 now_ms,
3307 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3308 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
3309 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003310 req->flags, rep->flags,
3311 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003312
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003313 if (s == SV_STIDLE) {
3314 if ((rep->flags & BF_SHUTW_STATUS) ||
3315 ((req->flags & BF_SHUTR_STATUS) &&
3316 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3317 req->cex = TICK_ETERNITY;
3318 if (t->pend_pos)
3319 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3320 /* note that this must not return any error because it would be able to
3321 * overwrite the client_retnclose() output.
3322 */
3323 if (txn->flags & TX_CLTARPIT)
3324 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3325 else
3326 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3327
3328 return 1;
3329 }
3330 else if (req->flags & BF_MAY_CONNECT) {
3331 /* the client allows the server to connect */
3332 if (txn->flags & TX_CLTARPIT) {
3333 /* This connection is being tarpitted. The CLIENT side has
3334 * already set the connect expiration date to the right
3335 * timeout. We just have to check that it has not expired.
3336 */
3337 if (!tick_is_expired(req->cex, now_ms))
3338 return 0;
3339
3340 /* We will set the queue timer to the time spent, just for
3341 * logging purposes. We fake a 500 server error, so that the
3342 * attacker will not suspect his connection has been tarpitted.
3343 * It will not cause trouble to the logs because we can exclude
3344 * the tarpitted connections by filtering on the 'PT' status flags.
3345 */
3346 req->cex = TICK_ETERNITY;
3347 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3348 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3349 500, error_message(t, HTTP_ERR_500));
3350 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003351 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003353 /* Right now, we will need to create a connection to the server.
3354 * We might already have tried, and got a connection pending, in
3355 * which case we will not do anything till it's pending. It's up
3356 * to any other session to release it and wake us up again.
3357 */
3358 if (t->pend_pos) {
3359 if (!tick_is_expired(req->cex, now_ms)) {
3360 return 0;
3361 } else {
3362 /* we've been waiting too long here */
3363 req->cex = TICK_ETERNITY;
3364 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3365 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3366 503, error_message(t, HTTP_ERR_503));
3367 if (t->srv)
3368 t->srv->failed_conns++;
3369 t->be->failed_conns++;
3370 return 1;
3371 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003372 }
3373
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003374 do {
3375 /* first, get a connection */
3376 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3377 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003378
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003379 if (srv_redispatch_connect(t))
3380 return t->srv_state != SV_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003381
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003382 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3383 /* Server supporting redirection and it is possible.
3384 * Invalid requests are reported as such. It concerns all
3385 * the largest ones.
3386 */
3387 struct chunk rdr;
3388 char *path;
3389 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003390
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003391 /* 1: create the response header */
3392 rdr.len = strlen(HTTP_302);
3393 rdr.str = trash;
3394 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003395
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003396 /* 2: add the server's prefix */
3397 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3398 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003399
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003400 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3401 rdr.len += t->srv->rdr_len;
3402
3403 /* 3: add the request URI */
3404 path = http_get_path(txn);
3405 if (!path)
3406 goto cancel_redir;
3407 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3408 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3409 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003410
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003411 memcpy(rdr.str + rdr.len, path, len);
3412 rdr.len += len;
3413 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3414 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003415
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003416 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3417 /* FIXME: we should increase a counter of redirects per server and per backend. */
3418 if (t->srv)
3419 t->srv->cum_sess++;
3420 return 1;
3421 cancel_redir:
3422 txn->status = 400;
3423 t->fe->failed_req++;
3424 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3425 400, error_message(t, HTTP_ERR_400));
3426 return 1;
3427 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003428
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003429 /* try to (re-)connect to the server, and fail if we expire the
3430 * number of retries.
3431 */
3432 if (srv_retryable_connect(t)) {
3433 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3434 return t->srv_state != SV_STIDLE;
3435 }
3436 } while (1);
3437 }
3438 }
3439 else if (s == SV_STCONN) { /* connection in progress */
3440 if ((rep->flags & BF_SHUTW_STATUS) ||
3441 ((req->flags & BF_SHUTR_STATUS) &&
3442 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
3443 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3444 req->cex = TICK_ETERNITY;
3445 if (!(t->flags & SN_CONN_TAR)) {
3446 /* if we are in turn-around, we have already closed the FD */
3447 fd_delete(t->srv_fd);
3448 if (t->srv) {
3449 t->srv->cur_sess--;
3450 sess_change_server(t, NULL);
3451 }
3452 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003453
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003454 /* note that this must not return any error because it would be able to
3455 * overwrite the client_retnclose() output.
3456 */
3457 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3458 return 1;
3459 }
3460 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
3461 return 0; /* nothing changed */
3462 }
3463 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3464 /* timeout, asynchronous connect error or first write error */
3465 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003466
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 if (t->flags & SN_CONN_TAR) {
3468 /* We are doing a turn-around waiting for a new connection attempt. */
3469 if (!tick_is_expired(req->cex, now_ms))
3470 return 0;
3471 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003472 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003473 else {
3474 fd_delete(t->srv_fd);
3475 if (t->srv) {
3476 t->srv->cur_sess--;
3477 sess_change_server(t, NULL);
3478 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003479
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003480 if (!(req->flags & BF_WRITE_STATUS))
3481 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3482 else
3483 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003484
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003485 /* ensure that we have enough retries left */
3486 if (srv_count_retry_down(t, conn_err))
3487 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003489 if (req->flags & BF_WRITE_ERROR) {
3490 /* we encountered an immediate connection error, and we
3491 * will have to retry connecting to the same server, most
3492 * likely leading to the same result. To avoid this, we
3493 * fake a connection timeout to retry after a turn-around
3494 * time of 1 second. We will wait in the previous if block.
3495 */
3496 t->flags |= SN_CONN_TAR;
3497 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
3498 return 0;
3499 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003500 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003501
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003502 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3503 /* We're on our last chance, and the REDISP option was specified.
3504 * We will ignore cookie and force to balance or use the dispatcher.
3505 */
3506 /* let's try to offer this slot to anybody */
3507 if (may_dequeue_tasks(t->srv, t->be))
3508 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003510 /* it's left to the dispatcher to choose a server */
3511 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3512 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003514 /* first, get a connection */
3515 if (srv_redispatch_connect(t))
3516 return t->srv_state != SV_STCONN;
3517 } else {
3518 if (t->srv)
3519 t->srv->retries++;
3520 t->be->retries++;
3521 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003522
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003523 do {
3524 /* Now we will try to either reconnect to the same server or
3525 * connect to another server. If the connection gets queued
3526 * because all servers are saturated, then we will go back to
3527 * the SV_STIDLE state.
3528 */
3529 if (srv_retryable_connect(t)) {
3530 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3531 return t->srv_state != SV_STCONN;
3532 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003533
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003534 /* we need to redispatch the connection to another server */
3535 if (srv_redispatch_connect(t))
3536 return t->srv_state != SV_STCONN;
3537 } while (1);
3538 }
3539 else { /* no error or write 0 */
3540 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003541
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003542 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
3543 if (req->l == 0) /* nothing to write */ {
3544 EV_FD_CLR(t->srv_fd, DIR_WR);
3545 req->wex = TICK_ETERNITY;
3546 } else /* need the right to write */ {
3547 EV_FD_SET(t->srv_fd, DIR_WR);
3548 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3549 if (tick_isset(req->wex)) {
3550 /* FIXME: to prevent the server from expiring read timeouts during writes,
3551 * we refresh it. */
3552 rep->rex = req->wex;
3553 }
3554 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003555
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003556 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3557 EV_FD_SET(t->srv_fd, DIR_RD);
3558 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3559 t->srv_state = SV_STDATA;
3560 rep->flags |= BF_MAY_FORWARD;
3561 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003562
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003563 /* if the user wants to log as soon as possible, without counting
3564 bytes from the server, then this is the right moment. */
3565 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3566 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3567 tcp_sess_log(t);
3568 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003569#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003570 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3571 /* TCP splicing supported by both FE and BE */
3572 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3573 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003574#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003575 }
3576 else {
3577 t->srv_state = SV_STDATA;
3578 t->analysis |= AN_RTR_HTTP_HDR;
3579 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
3580 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3581 /* reset hdr_idx which was already initialized by the request.
3582 * right now, the http parser does it.
3583 * hdr_idx_init(&t->txn.hdr_idx);
3584 */
3585 }
3586 req->cex = TICK_ETERNITY;
3587 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003589 }
3590 else if (s == SV_STDATA) {
3591 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003592 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003593 buffer_shutr_done(rep);
3594 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003595 fd_delete(t->srv_fd);
3596 if (t->srv) {
3597 t->srv->cur_sess--;
3598 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003599 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003600 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003601 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003602 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003603 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003604 if (!(t->flags & SN_ERR_MASK))
3605 t->flags |= SN_ERR_SRVCL;
3606 if (!(t->flags & SN_FINST_MASK))
3607 t->flags |= SN_FINST_D;
3608 /* We used to have a free connection slot. Since we'll never use it,
3609 * we have to inform the server that it may be used by another session.
3610 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003611 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003612 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003613
3614 return 1;
3615 }
3616 /* last read, or end of client write */
Willy Tarreau6468d922008-08-03 19:15:35 +02003617 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003618 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003619 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003620 t->srv_state = SV_STSHUTR;
3621 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3622 return 1;
3623 }
3624 /* end of client read and no more data to send */
Willy Tarreau6468d922008-08-03 19:15:35 +02003625 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003626 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003627 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003628 shutdown(t->srv_fd, SHUT_WR);
3629 /* We must ensure that the read part is still alive when switching
3630 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003631 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003632 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633
3634 t->srv_state = SV_STSHUTW;
3635 return 1;
3636 }
3637 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003638 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003639 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003640 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003641 t->srv_state = SV_STSHUTR;
3642 if (!(t->flags & SN_ERR_MASK))
3643 t->flags |= SN_ERR_SRVTO;
3644 if (!(t->flags & SN_FINST_MASK))
3645 t->flags |= SN_FINST_D;
3646 return 1;
3647 }
3648 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003649 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003650 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003651 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003652 shutdown(t->srv_fd, SHUT_WR);
3653 /* We must ensure that the read part is still alive when switching
3654 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003655 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003656 rep->cex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003657 t->srv_state = SV_STSHUTW;
3658 if (!(t->flags & SN_ERR_MASK))
3659 t->flags |= SN_ERR_SRVTO;
3660 if (!(t->flags & SN_FINST_MASK))
3661 t->flags |= SN_FINST_D;
3662 return 1;
3663 }
3664
3665 /* recompute request time-outs */
3666 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003667 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3668 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003669 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003670 }
3671 }
3672 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003673 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003674 if (!tick_isset(req->wex)) {
Willy Tarreau66319382007-04-08 17:17:37 +02003675 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003676 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003677 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003678 /* FIXME: to prevent the server from expiring read timeouts during writes,
3679 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003680 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003681 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682 }
3683 }
3684
3685 /* recompute response time-outs */
3686 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003687 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003688 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003689 }
3690 }
3691 else {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003692 EV_FD_COND_S(t->srv_fd, DIR_RD);
3693 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003694 }
3695
3696 return 0; /* other cases change nothing */
3697 }
3698 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003699 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003700 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003701 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003702 fd_delete(t->srv_fd);
3703 if (t->srv) {
3704 t->srv->cur_sess--;
3705 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003706 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003707 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003708 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003709 //close(t->srv_fd);
3710 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003711 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003712 if (!(t->flags & SN_ERR_MASK))
3713 t->flags |= SN_ERR_SRVCL;
3714 if (!(t->flags & SN_FINST_MASK))
3715 t->flags |= SN_FINST_D;
3716 /* We used to have a free connection slot. Since we'll never use it,
3717 * we have to inform the server that it may be used by another session.
3718 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003719 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003720 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003721
3722 return 1;
3723 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003724 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003725 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003726 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003728 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003729 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003730 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003731 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003732 //close(t->srv_fd);
3733 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003734 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003735 /* We used to have a free connection slot. Since we'll never use it,
3736 * we have to inform the server that it may be used by another session.
3737 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003738 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003739 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003740
3741 return 1;
3742 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003743 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003744 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003745 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003746 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003747 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003748 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003749 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003750 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003751 //close(t->srv_fd);
3752 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003753 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003754 if (!(t->flags & SN_ERR_MASK))
3755 t->flags |= SN_ERR_SRVTO;
3756 if (!(t->flags & SN_FINST_MASK))
3757 t->flags |= SN_FINST_D;
3758 /* We used to have a free connection slot. Since we'll never use it,
3759 * we have to inform the server that it may be used by another session.
3760 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003761 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003762 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003763
3764 return 1;
3765 }
3766 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003767 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3768 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003769 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003770 }
3771 }
3772 else { /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003773 if (!tick_isset(req->wex)) {
3774 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003775 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003776 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003777 }
3778 }
3779 return 0;
3780 }
3781 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003782 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003783 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003784 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785 fd_delete(t->srv_fd);
3786 if (t->srv) {
3787 t->srv->cur_sess--;
3788 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003789 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003790 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003791 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003792 //close(t->srv_fd);
3793 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003794 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003795 if (!(t->flags & SN_ERR_MASK))
3796 t->flags |= SN_ERR_SRVCL;
3797 if (!(t->flags & SN_FINST_MASK))
3798 t->flags |= SN_FINST_D;
3799 /* We used to have a free connection slot. Since we'll never use it,
3800 * we have to inform the server that it may be used by another session.
3801 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003802 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003803 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003804
3805 return 1;
3806 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003807 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003808 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003809 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003810 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003811 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003812 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003813 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003814 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003815 //close(t->srv_fd);
3816 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003817 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003818 /* We used to have a free connection slot. Since we'll never use it,
3819 * we have to inform the server that it may be used by another session.
3820 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003821 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003822 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003823
3824 return 1;
3825 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003826 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003827 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003828 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003829 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003830 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003831 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003832 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003833 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003834 //close(t->srv_fd);
3835 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003836 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003837 if (!(t->flags & SN_ERR_MASK))
3838 t->flags |= SN_ERR_SRVTO;
3839 if (!(t->flags & SN_FINST_MASK))
3840 t->flags |= SN_FINST_D;
3841 /* We used to have a free connection slot. Since we'll never use it,
3842 * we have to inform the server that it may be used by another session.
3843 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003844 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003845 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003846
3847 return 1;
3848 }
3849 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003850 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003851 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852 }
3853 }
3854 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003855 if (!tick_isset(rep->rex)) {
3856 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003857 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003858 }
3859 }
3860 return 0;
3861 }
3862 else { /* SV_STCLOSE : nothing to do */
3863 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3864 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003865 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003866 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003867 write(1, trash, len);
3868 }
3869 return 0;
3870 }
3871 return 0;
3872}
3873
3874
3875/*
3876 * Produces data for the session <s> depending on its source. Expects to be
3877 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3878 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3879 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003880 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreaubaaee002006-06-26 02:48:02 +02003881 * if it changes the session state from CL_STSHUTR, otherwise 0.
3882 */
3883int produce_content(struct session *s)
3884{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003885 if (s->data_source == DATA_SRC_NONE) {
3886 s->flags &= ~SN_SELF_GEN;
3887 return 1;
3888 }
3889 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003890 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003891 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003892 if (ret >= 0)
3893 return ret;
3894 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003896
Willy Tarreau91861262007-10-17 17:06:05 +02003897 /* unknown data source or internal error */
3898 s->txn.status = 500;
3899 client_retnclose(s, error_message(s, HTTP_ERR_500));
3900 if (!(s->flags & SN_ERR_MASK))
3901 s->flags |= SN_ERR_PRXCOND;
3902 if (!(s->flags & SN_FINST_MASK))
3903 s->flags |= SN_FINST_R;
3904 s->flags &= ~SN_SELF_GEN;
3905 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003906}
3907
3908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003909/* Iterate the same filter through all request headers.
3910 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003911 * Since it can manage the switch to another backend, it updates the per-proxy
3912 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003913 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003914int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003915{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003916 char term;
3917 char *cur_ptr, *cur_end, *cur_next;
3918 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003919 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003920 struct hdr_idx_elem *cur_hdr;
3921 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003923 last_hdr = 0;
3924
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003925 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003926 old_idx = 0;
3927
3928 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003929 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003930 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003931 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003932 (exp->action == ACT_ALLOW ||
3933 exp->action == ACT_DENY ||
3934 exp->action == ACT_TARPIT))
3935 return 0;
3936
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003937 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003938 if (!cur_idx)
3939 break;
3940
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003941 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003942 cur_ptr = cur_next;
3943 cur_end = cur_ptr + cur_hdr->len;
3944 cur_next = cur_end + cur_hdr->cr + 1;
3945
3946 /* Now we have one header between cur_ptr and cur_end,
3947 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003948 */
3949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003950 /* The annoying part is that pattern matching needs
3951 * that we modify the contents to null-terminate all
3952 * strings before testing them.
3953 */
3954
3955 term = *cur_end;
3956 *cur_end = '\0';
3957
3958 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3959 switch (exp->action) {
3960 case ACT_SETBE:
3961 /* It is not possible to jump a second time.
3962 * FIXME: should we return an HTTP/500 here so that
3963 * the admin knows there's a problem ?
3964 */
3965 if (t->be != t->fe)
3966 break;
3967
3968 /* Swithing Proxy */
3969 t->be = (struct proxy *) exp->replace;
3970
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003971 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003972 * because we have associated req_cap and rsp_cap to the
3973 * frontend, and the beconn will be updated later.
3974 */
3975
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003976 t->rep->rto = t->req->wto = t->be->timeout.server;
3977 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003978 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003979 last_hdr = 1;
3980 break;
3981
3982 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003983 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003984 last_hdr = 1;
3985 break;
3986
3987 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003988 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003989 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003990 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991 break;
3992
3993 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003994 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003995 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003996 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003997 break;
3998
3999 case ACT_REPLACE:
4000 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4001 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4002 /* FIXME: if the user adds a newline in the replacement, the
4003 * index will not be recalculated for now, and the new line
4004 * will not be counted as a new header.
4005 */
4006
4007 cur_end += delta;
4008 cur_next += delta;
4009 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004010 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004011 break;
4012
4013 case ACT_REMOVE:
4014 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4015 cur_next += delta;
4016
4017 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004018 txn->req.eoh += delta;
4019 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4020 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004021 cur_hdr->len = 0;
4022 cur_end = NULL; /* null-term has been rewritten */
4023 break;
4024
4025 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004026 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004027 if (cur_end)
4028 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004030 /* keep the link from this header to next one in case of later
4031 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004032 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 old_idx = cur_idx;
4034 }
4035 return 0;
4036}
4037
4038
4039/* Apply the filter to the request line.
4040 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4041 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004042 * Since it can manage the switch to another backend, it updates the per-proxy
4043 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004044 */
4045int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4046{
4047 char term;
4048 char *cur_ptr, *cur_end;
4049 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004050 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004051 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052
Willy Tarreau58f10d72006-12-04 02:26:12 +01004053
Willy Tarreau3d300592007-03-18 18:34:41 +01004054 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004055 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004056 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004057 (exp->action == ACT_ALLOW ||
4058 exp->action == ACT_DENY ||
4059 exp->action == ACT_TARPIT))
4060 return 0;
4061 else if (exp->action == ACT_REMOVE)
4062 return 0;
4063
4064 done = 0;
4065
Willy Tarreau9cdde232007-05-02 20:58:19 +02004066 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004067 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004068
4069 /* Now we have the request line between cur_ptr and cur_end */
4070
4071 /* The annoying part is that pattern matching needs
4072 * that we modify the contents to null-terminate all
4073 * strings before testing them.
4074 */
4075
4076 term = *cur_end;
4077 *cur_end = '\0';
4078
4079 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4080 switch (exp->action) {
4081 case ACT_SETBE:
4082 /* It is not possible to jump a second time.
4083 * FIXME: should we return an HTTP/500 here so that
4084 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004085 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004086 if (t->be != t->fe)
4087 break;
4088
4089 /* Swithing Proxy */
4090 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004092 /* right now, the backend switch is not too much complicated
4093 * because we have associated req_cap and rsp_cap to the
4094 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004095 */
4096
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004097 t->rep->rto = t->req->wto = t->be->timeout.server;
4098 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004099 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004100 done = 1;
4101 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004103 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004104 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004105 done = 1;
4106 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004108 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004109 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004110 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004111 done = 1;
4112 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004114 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004115 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004116 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004117 done = 1;
4118 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004120 case ACT_REPLACE:
4121 *cur_end = term; /* restore the string terminator */
4122 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4123 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4124 /* FIXME: if the user adds a newline in the replacement, the
4125 * index will not be recalculated for now, and the new line
4126 * will not be counted as a new header.
4127 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004128
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004129 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004130 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004131
Willy Tarreau9cdde232007-05-02 20:58:19 +02004132 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004133 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 HTTP_MSG_RQMETH,
4135 cur_ptr, cur_end + 1,
4136 NULL, NULL);
4137 if (unlikely(!cur_end))
4138 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004140 /* we have a full request and we know that we have either a CR
4141 * or an LF at <ptr>.
4142 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004143 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4144 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004145 /* there is no point trying this regex on headers */
4146 return 1;
4147 }
4148 }
4149 *cur_end = term; /* restore the string terminator */
4150 return done;
4151}
Willy Tarreau97de6242006-12-27 17:18:38 +01004152
Willy Tarreau58f10d72006-12-04 02:26:12 +01004153
Willy Tarreau58f10d72006-12-04 02:26:12 +01004154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004155/*
4156 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4157 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004158 * unparsable request. Since it can manage the switch to another backend, it
4159 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004160 */
4161int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4162{
Willy Tarreau3d300592007-03-18 18:34:41 +01004163 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004165 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004166 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004168 /*
4169 * The interleaving of transformations and verdicts
4170 * makes it difficult to decide to continue or stop
4171 * the evaluation.
4172 */
4173
Willy Tarreau3d300592007-03-18 18:34:41 +01004174 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004175 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4176 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4177 exp = exp->next;
4178 continue;
4179 }
4180
4181 /* Apply the filter to the request line. */
4182 ret = apply_filter_to_req_line(t, req, exp);
4183 if (unlikely(ret < 0))
4184 return -1;
4185
4186 if (likely(ret == 0)) {
4187 /* The filter did not match the request, it can be
4188 * iterated through all headers.
4189 */
4190 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004191 }
4192 exp = exp->next;
4193 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004194 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004195}
4196
4197
Willy Tarreaua15645d2007-03-18 16:22:39 +01004198
Willy Tarreau58f10d72006-12-04 02:26:12 +01004199/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004200 * Manage client-side cookie. It can impact performance by about 2% so it is
4201 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004202 */
4203void manage_client_side_cookies(struct session *t, struct buffer *req)
4204{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004205 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 char *p1, *p2, *p3, *p4;
4207 char *del_colon, *del_cookie, *colon;
4208 int app_cookies;
4209
4210 appsess *asession_temp = NULL;
4211 appsess local_asession;
4212
4213 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004214 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004215
Willy Tarreau2a324282006-12-05 00:05:46 +01004216 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004217 * we start with the start line.
4218 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004219 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004220 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004223 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004224 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004226 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 cur_ptr = cur_next;
4228 cur_end = cur_ptr + cur_hdr->len;
4229 cur_next = cur_end + cur_hdr->cr + 1;
4230
4231 /* We have one full header between cur_ptr and cur_end, and the
4232 * next header starts at cur_next. We're only interested in
4233 * "Cookie:" headers.
4234 */
4235
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004236 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4237 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 old_idx = cur_idx;
4239 continue;
4240 }
4241
4242 /* Now look for cookies. Conforming to RFC2109, we have to support
4243 * attributes whose name begin with a '$', and associate them with
4244 * the right cookie, if we want to delete this cookie.
4245 * So there are 3 cases for each cookie read :
4246 * 1) it's a special attribute, beginning with a '$' : ignore it.
4247 * 2) it's a server id cookie that we *MAY* want to delete : save
4248 * some pointers on it (last semi-colon, beginning of cookie...)
4249 * 3) it's an application cookie : we *MAY* have to delete a previous
4250 * "special" cookie.
4251 * At the end of loop, if a "special" cookie remains, we may have to
4252 * remove it. If no application cookie persists in the header, we
4253 * *MUST* delete it
4254 */
4255
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004256 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257
Willy Tarreau58f10d72006-12-04 02:26:12 +01004258 /* del_cookie == NULL => nothing to be deleted */
4259 del_colon = del_cookie = NULL;
4260 app_cookies = 0;
4261
4262 while (p1 < cur_end) {
4263 /* skip spaces and colons, but keep an eye on these ones */
4264 while (p1 < cur_end) {
4265 if (*p1 == ';' || *p1 == ',')
4266 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004267 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004268 break;
4269 p1++;
4270 }
4271
4272 if (p1 == cur_end)
4273 break;
4274
4275 /* p1 is at the beginning of the cookie name */
4276 p2 = p1;
4277 while (p2 < cur_end && *p2 != '=')
4278 p2++;
4279
4280 if (p2 == cur_end)
4281 break;
4282
4283 p3 = p2 + 1; /* skips the '=' sign */
4284 if (p3 == cur_end)
4285 break;
4286
4287 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004288 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 p4++;
4290
4291 /* here, we have the cookie name between p1 and p2,
4292 * and its value between p3 and p4.
4293 * we can process it :
4294 *
4295 * Cookie: NAME=VALUE;
4296 * | || || |
4297 * | || || +--> p4
4298 * | || |+-------> p3
4299 * | || +--------> p2
4300 * | |+------------> p1
4301 * | +-------------> colon
4302 * +--------------------> cur_ptr
4303 */
4304
4305 if (*p1 == '$') {
4306 /* skip this one */
4307 }
4308 else {
4309 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004310 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004311 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004312 (p4 - p1 >= t->fe->capture_namelen) &&
4313 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004314 int log_len = p4 - p1;
4315
Willy Tarreau086b3b42007-05-13 21:45:51 +02004316 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004317 Alert("HTTP logging : out of memory.\n");
4318 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004319 if (log_len > t->fe->capture_len)
4320 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004321 memcpy(txn->cli_cookie, p1, log_len);
4322 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004323 }
4324 }
4325
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004326 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4327 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004328 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004329 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004330 char *delim;
4331
4332 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4333 * have the server ID betweek p3 and delim, and the original cookie between
4334 * delim+1 and p4. Otherwise, delim==p4 :
4335 *
4336 * Cookie: NAME=SRV~VALUE;
4337 * | || || | |
4338 * | || || | +--> p4
4339 * | || || +--------> delim
4340 * | || |+-----------> p3
4341 * | || +------------> p2
4342 * | |+----------------> p1
4343 * | +-----------------> colon
4344 * +------------------------> cur_ptr
4345 */
4346
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004347 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004348 for (delim = p3; delim < p4; delim++)
4349 if (*delim == COOKIE_DELIM)
4350 break;
4351 }
4352 else
4353 delim = p4;
4354
4355
4356 /* Here, we'll look for the first running server which supports the cookie.
4357 * This allows to share a same cookie between several servers, for example
4358 * to dedicate backup servers to specific servers only.
4359 * However, to prevent clients from sticking to cookie-less backup server
4360 * when they have incidentely learned an empty cookie, we simply ignore
4361 * empty cookies and mark them as invalid.
4362 */
4363 if (delim == p3)
4364 srv = NULL;
4365
4366 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004367 if (srv->cookie && (srv->cklen == delim - p3) &&
4368 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004369 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004370 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004371 txn->flags &= ~TX_CK_MASK;
4372 txn->flags |= TX_CK_VALID;
4373 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004374 t->srv = srv;
4375 break;
4376 } else {
4377 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004378 txn->flags &= ~TX_CK_MASK;
4379 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004380 }
4381 }
4382 srv = srv->next;
4383 }
4384
Willy Tarreau3d300592007-03-18 18:34:41 +01004385 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004386 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004387 txn->flags &= ~TX_CK_MASK;
4388 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004389 }
4390
4391 /* depending on the cookie mode, we may have to either :
4392 * - delete the complete cookie if we're in insert+indirect mode, so that
4393 * the server never sees it ;
4394 * - remove the server id from the cookie value, and tag the cookie as an
4395 * application cookie so that it does not get accidentely removed later,
4396 * if we're in cookie prefix mode
4397 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 int delta; /* negative */
4400
4401 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4402 p4 += delta;
4403 cur_end += delta;
4404 cur_next += delta;
4405 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004406 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004407
4408 del_cookie = del_colon = NULL;
4409 app_cookies++; /* protect the header from deletion */
4410 }
4411 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004412 (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 +01004413 del_cookie = p1;
4414 del_colon = colon;
4415 }
4416 } else {
4417 /* now we know that we must keep this cookie since it's
4418 * not ours. But if we wanted to delete our cookie
4419 * earlier, we cannot remove the complete header, but we
4420 * can remove the previous block itself.
4421 */
4422 app_cookies++;
4423
4424 if (del_cookie != NULL) {
4425 int delta; /* negative */
4426
4427 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4428 p4 += delta;
4429 cur_end += delta;
4430 cur_next += delta;
4431 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004432 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004433 del_cookie = del_colon = NULL;
4434 }
4435 }
4436
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004437 if ((t->be->appsession_name != NULL) &&
4438 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004439 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004440
Willy Tarreau58f10d72006-12-04 02:26:12 +01004441 /* Cool... it's the right one */
4442
4443 asession_temp = &local_asession;
4444
Willy Tarreau63963c62007-05-13 21:29:55 +02004445 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004446 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4447 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4448 return;
4449 }
4450
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004451 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4452 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004453 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004454
Willy Tarreau58f10d72006-12-04 02:26:12 +01004455 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004456 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4457 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004458 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004460 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004461 Alert("Not enough memory process_cli():asession:calloc().\n");
4462 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4463 return;
4464 }
4465
4466 asession_temp->sessid = local_asession.sessid;
4467 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004468 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004469 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004470 } else {
4471 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004472 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004473 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004474 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004475 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004476 Alert("Found Application Session without matching server.\n");
4477 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004478 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004479 while (srv) {
4480 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004481 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004482 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004483 txn->flags &= ~TX_CK_MASK;
4484 txn->flags |= TX_CK_VALID;
4485 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004486 t->srv = srv;
4487 break;
4488 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004489 txn->flags &= ~TX_CK_MASK;
4490 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004491 }
4492 }
4493 srv = srv->next;
4494 }/* end while(srv) */
4495 }/* end else if server == NULL */
4496
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004497 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004498 asession_temp->request_count++;
4499#if defined(DEBUG_HASH)
4500 Alert("manage_client_side_cookies\n");
4501 appsession_hash_dump(&(t->be->htbl_proxy));
4502#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503 }/* end if ((t->proxy->appsession_name != NULL) ... */
4504 }
4505
4506 /* we'll have to look for another cookie ... */
4507 p1 = p4;
4508 } /* while (p1 < cur_end) */
4509
4510 /* There's no more cookie on this line.
4511 * We may have marked the last one(s) for deletion.
4512 * We must do this now in two ways :
4513 * - if there is no app cookie, we simply delete the header ;
4514 * - if there are app cookies, we must delete the end of the
4515 * string properly, including the colon/semi-colon before
4516 * the cookie name.
4517 */
4518 if (del_cookie != NULL) {
4519 int delta;
4520 if (app_cookies) {
4521 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4522 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523 cur_hdr->len += delta;
4524 } else {
4525 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004526
4527 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004528 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4529 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004530 cur_hdr->len = 0;
4531 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004532 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004533 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004534 }
4535
4536 /* keep the link from this header to next one */
4537 old_idx = cur_idx;
4538 } /* end of cookie processing on this header */
4539}
4540
4541
Willy Tarreaua15645d2007-03-18 16:22:39 +01004542/* Iterate the same filter through all response headers contained in <rtr>.
4543 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4544 */
4545int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4546{
4547 char term;
4548 char *cur_ptr, *cur_end, *cur_next;
4549 int cur_idx, old_idx, last_hdr;
4550 struct http_txn *txn = &t->txn;
4551 struct hdr_idx_elem *cur_hdr;
4552 int len, delta;
4553
4554 last_hdr = 0;
4555
4556 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4557 old_idx = 0;
4558
4559 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004560 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004561 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004562 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004563 (exp->action == ACT_ALLOW ||
4564 exp->action == ACT_DENY))
4565 return 0;
4566
4567 cur_idx = txn->hdr_idx.v[old_idx].next;
4568 if (!cur_idx)
4569 break;
4570
4571 cur_hdr = &txn->hdr_idx.v[cur_idx];
4572 cur_ptr = cur_next;
4573 cur_end = cur_ptr + cur_hdr->len;
4574 cur_next = cur_end + cur_hdr->cr + 1;
4575
4576 /* Now we have one header between cur_ptr and cur_end,
4577 * and the next header starts at cur_next.
4578 */
4579
4580 /* The annoying part is that pattern matching needs
4581 * that we modify the contents to null-terminate all
4582 * strings before testing them.
4583 */
4584
4585 term = *cur_end;
4586 *cur_end = '\0';
4587
4588 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4589 switch (exp->action) {
4590 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004591 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 last_hdr = 1;
4593 break;
4594
4595 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004596 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004597 last_hdr = 1;
4598 break;
4599
4600 case ACT_REPLACE:
4601 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4602 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4603 /* FIXME: if the user adds a newline in the replacement, the
4604 * index will not be recalculated for now, and the new line
4605 * will not be counted as a new header.
4606 */
4607
4608 cur_end += delta;
4609 cur_next += delta;
4610 cur_hdr->len += delta;
4611 txn->rsp.eoh += delta;
4612 break;
4613
4614 case ACT_REMOVE:
4615 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4616 cur_next += delta;
4617
4618 /* FIXME: this should be a separate function */
4619 txn->rsp.eoh += delta;
4620 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4621 txn->hdr_idx.used--;
4622 cur_hdr->len = 0;
4623 cur_end = NULL; /* null-term has been rewritten */
4624 break;
4625
4626 }
4627 }
4628 if (cur_end)
4629 *cur_end = term; /* restore the string terminator */
4630
4631 /* keep the link from this header to next one in case of later
4632 * removal of next header.
4633 */
4634 old_idx = cur_idx;
4635 }
4636 return 0;
4637}
4638
4639
4640/* Apply the filter to the status line in the response buffer <rtr>.
4641 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4642 * or -1 if a replacement resulted in an invalid status line.
4643 */
4644int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4645{
4646 char term;
4647 char *cur_ptr, *cur_end;
4648 int done;
4649 struct http_txn *txn = &t->txn;
4650 int len, delta;
4651
4652
Willy Tarreau3d300592007-03-18 18:34:41 +01004653 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004654 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004655 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004656 (exp->action == ACT_ALLOW ||
4657 exp->action == ACT_DENY))
4658 return 0;
4659 else if (exp->action == ACT_REMOVE)
4660 return 0;
4661
4662 done = 0;
4663
Willy Tarreau9cdde232007-05-02 20:58:19 +02004664 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004665 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4666
4667 /* Now we have the status line between cur_ptr and cur_end */
4668
4669 /* The annoying part is that pattern matching needs
4670 * that we modify the contents to null-terminate all
4671 * strings before testing them.
4672 */
4673
4674 term = *cur_end;
4675 *cur_end = '\0';
4676
4677 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4678 switch (exp->action) {
4679 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004680 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681 done = 1;
4682 break;
4683
4684 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004685 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004686 done = 1;
4687 break;
4688
4689 case ACT_REPLACE:
4690 *cur_end = term; /* restore the string terminator */
4691 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4692 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4693 /* FIXME: if the user adds a newline in the replacement, the
4694 * index will not be recalculated for now, and the new line
4695 * will not be counted as a new header.
4696 */
4697
4698 txn->rsp.eoh += delta;
4699 cur_end += delta;
4700
Willy Tarreau9cdde232007-05-02 20:58:19 +02004701 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004702 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004703 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004704 cur_ptr, cur_end + 1,
4705 NULL, NULL);
4706 if (unlikely(!cur_end))
4707 return -1;
4708
4709 /* we have a full respnse and we know that we have either a CR
4710 * or an LF at <ptr>.
4711 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004712 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004713 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4714 /* there is no point trying this regex on headers */
4715 return 1;
4716 }
4717 }
4718 *cur_end = term; /* restore the string terminator */
4719 return done;
4720}
4721
4722
4723
4724/*
4725 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4726 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4727 * unparsable response.
4728 */
4729int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4730{
Willy Tarreau3d300592007-03-18 18:34:41 +01004731 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004733 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 int ret;
4735
4736 /*
4737 * The interleaving of transformations and verdicts
4738 * makes it difficult to decide to continue or stop
4739 * the evaluation.
4740 */
4741
Willy Tarreau3d300592007-03-18 18:34:41 +01004742 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004743 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4744 exp->action == ACT_PASS)) {
4745 exp = exp->next;
4746 continue;
4747 }
4748
4749 /* Apply the filter to the status line. */
4750 ret = apply_filter_to_sts_line(t, rtr, exp);
4751 if (unlikely(ret < 0))
4752 return -1;
4753
4754 if (likely(ret == 0)) {
4755 /* The filter did not match the response, it can be
4756 * iterated through all headers.
4757 */
4758 apply_filter_to_resp_headers(t, rtr, exp);
4759 }
4760 exp = exp->next;
4761 }
4762 return 0;
4763}
4764
4765
4766
4767/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004768 * Manage server-side cookies. It can impact performance by about 2% so it is
4769 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004770 */
4771void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4772{
4773 struct http_txn *txn = &t->txn;
4774 char *p1, *p2, *p3, *p4;
4775
4776 appsess *asession_temp = NULL;
4777 appsess local_asession;
4778
4779 char *cur_ptr, *cur_end, *cur_next;
4780 int cur_idx, old_idx, delta;
4781
Willy Tarreaua15645d2007-03-18 16:22:39 +01004782 /* Iterate through the headers.
4783 * we start with the start line.
4784 */
4785 old_idx = 0;
4786 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4787
4788 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4789 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004790 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004791
4792 cur_hdr = &txn->hdr_idx.v[cur_idx];
4793 cur_ptr = cur_next;
4794 cur_end = cur_ptr + cur_hdr->len;
4795 cur_next = cur_end + cur_hdr->cr + 1;
4796
4797 /* We have one full header between cur_ptr and cur_end, and the
4798 * next header starts at cur_next. We're only interested in
4799 * "Cookie:" headers.
4800 */
4801
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004802 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4803 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004804 old_idx = cur_idx;
4805 continue;
4806 }
4807
4808 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004809 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004810
4811
4812 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004813 if (t->be->cookie_name == NULL &&
4814 t->be->appsession_name == NULL &&
4815 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816 return;
4817
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004818 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004819
4820 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004821 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4822 break;
4823
4824 /* p1 is at the beginning of the cookie name */
4825 p2 = p1;
4826
4827 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4828 p2++;
4829
4830 if (p2 == cur_end || *p2 == ';') /* next cookie */
4831 break;
4832
4833 p3 = p2 + 1; /* skip the '=' sign */
4834 if (p3 == cur_end)
4835 break;
4836
4837 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004838 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839 p4++;
4840
4841 /* here, we have the cookie name between p1 and p2,
4842 * and its value between p3 and p4.
4843 * we can process it.
4844 */
4845
4846 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004847 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004848 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004849 (p4 - p1 >= t->be->capture_namelen) &&
4850 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851 int log_len = p4 - p1;
4852
Willy Tarreau086b3b42007-05-13 21:45:51 +02004853 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004854 Alert("HTTP logging : out of memory.\n");
4855 }
4856
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004857 if (log_len > t->be->capture_len)
4858 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004859 memcpy(txn->srv_cookie, p1, log_len);
4860 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004861 }
4862
4863 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004864 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4865 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004866 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004867 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868
4869 /* If the cookie is in insert mode on a known server, we'll delete
4870 * this occurrence because we'll insert another one later.
4871 * We'll delete it too if the "indirect" option is set and we're in
4872 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004873 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4874 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 /* this header must be deleted */
4876 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4877 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4878 txn->hdr_idx.used--;
4879 cur_hdr->len = 0;
4880 cur_next += delta;
4881 txn->rsp.eoh += delta;
4882
Willy Tarreau3d300592007-03-18 18:34:41 +01004883 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004884 }
4885 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004886 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004887 /* replace bytes p3->p4 with the cookie name associated
4888 * with this server since we know it.
4889 */
4890 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4891 cur_hdr->len += delta;
4892 cur_next += delta;
4893 txn->rsp.eoh += delta;
4894
Willy Tarreau3d300592007-03-18 18:34:41 +01004895 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004896 }
4897 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004898 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899 /* insert the cookie name associated with this server
4900 * before existing cookie, and insert a delimitor between them..
4901 */
4902 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4903 cur_hdr->len += delta;
4904 cur_next += delta;
4905 txn->rsp.eoh += delta;
4906
4907 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004908 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004909 }
4910 }
4911 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004912 else if ((t->be->appsession_name != NULL) &&
4913 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004914
4915 /* Cool... it's the right one */
4916
4917 size_t server_id_len = strlen(t->srv->id) + 1;
4918 asession_temp = &local_asession;
4919
Willy Tarreau63963c62007-05-13 21:29:55 +02004920 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004921 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4922 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4923 return;
4924 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004925 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4926 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927 asession_temp->serverid = NULL;
4928
4929 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004930 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4931 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004932 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004933 Alert("Not enough Memory process_srv():asession:calloc().\n");
4934 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4935 return;
4936 }
4937 asession_temp->sessid = local_asession.sessid;
4938 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004939 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004940 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4941 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004943 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004944 }
4945
Willy Tarreaua15645d2007-03-18 16:22:39 +01004946 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004947 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4949 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4950 return;
4951 }
4952 asession_temp->serverid[0] = '\0';
4953 }
4954
4955 if (asession_temp->serverid[0] == '\0')
4956 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4957
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004958 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004959 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004960#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004961 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004962 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004963#endif
4964 }/* end if ((t->proxy->appsession_name != NULL) ... */
4965 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4966 } /* we're now at the end of the cookie value */
4967
4968 /* keep the link from this header to next one */
4969 old_idx = cur_idx;
4970 } /* end of cookie processing on this header */
4971}
4972
4973
4974
4975/*
4976 * Check if response is cacheable or not. Updates t->flags.
4977 */
4978void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4979{
4980 struct http_txn *txn = &t->txn;
4981 char *p1, *p2;
4982
4983 char *cur_ptr, *cur_end, *cur_next;
4984 int cur_idx;
4985
Willy Tarreau5df51872007-11-25 16:20:08 +01004986 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004987 return;
4988
4989 /* Iterate through the headers.
4990 * we start with the start line.
4991 */
4992 cur_idx = 0;
4993 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4994
4995 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4996 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004997 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998
4999 cur_hdr = &txn->hdr_idx.v[cur_idx];
5000 cur_ptr = cur_next;
5001 cur_end = cur_ptr + cur_hdr->len;
5002 cur_next = cur_end + cur_hdr->cr + 1;
5003
5004 /* We have one full header between cur_ptr and cur_end, and the
5005 * next header starts at cur_next. We're only interested in
5006 * "Cookie:" headers.
5007 */
5008
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005009 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5010 if (val) {
5011 if ((cur_end - (cur_ptr + val) >= 8) &&
5012 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5013 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5014 return;
5015 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005016 }
5017
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005018 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5019 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005020 continue;
5021
5022 /* OK, right now we know we have a cache-control header at cur_ptr */
5023
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005024 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005025
5026 if (p1 >= cur_end) /* no more info */
5027 continue;
5028
5029 /* p1 is at the beginning of the value */
5030 p2 = p1;
5031
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005032 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005033 p2++;
5034
5035 /* we have a complete value between p1 and p2 */
5036 if (p2 < cur_end && *p2 == '=') {
5037 /* we have something of the form no-cache="set-cookie" */
5038 if ((cur_end - p1 >= 21) &&
5039 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5040 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005041 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042 continue;
5043 }
5044
5045 /* OK, so we know that either p2 points to the end of string or to a comma */
5046 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5047 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5048 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5049 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005050 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005051 return;
5052 }
5053
5054 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005055 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005056 continue;
5057 }
5058 }
5059}
5060
5061
Willy Tarreau58f10d72006-12-04 02:26:12 +01005062/*
5063 * Try to retrieve a known appsession in the URI, then the associated server.
5064 * If the server is found, it's assigned to the session.
5065 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005066void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005067{
Willy Tarreau3d300592007-03-18 18:34:41 +01005068 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005069 appsess *asession_temp = NULL;
5070 appsess local_asession;
5071 char *request_line;
5072
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005073 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005074 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005075 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005076 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005077 return;
5078
5079 /* skip ';' */
5080 request_line++;
5081
5082 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005083 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005084 return;
5085
5086 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005087 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088
5089 /* First try if we already have an appsession */
5090 asession_temp = &local_asession;
5091
Willy Tarreau63963c62007-05-13 21:29:55 +02005092 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005093 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5094 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5095 return;
5096 }
5097
5098 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005099 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5100 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005101 asession_temp->serverid = NULL;
5102
5103 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005104 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5105 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005106 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005107 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005108 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005109 Alert("Not enough memory process_cli():asession:calloc().\n");
5110 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5111 return;
5112 }
5113 asession_temp->sessid = local_asession.sessid;
5114 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005115 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005116 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117 }
5118 else {
5119 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005120 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005121 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005122
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005123 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005125
Willy Tarreau58f10d72006-12-04 02:26:12 +01005126#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005127 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005128 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005129#endif
5130 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005131 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005132 Alert("Found Application Session without matching server.\n");
5133 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005134 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005135 while (srv) {
5136 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005137 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005139 txn->flags &= ~TX_CK_MASK;
5140 txn->flags |= TX_CK_VALID;
5141 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005142 t->srv = srv;
5143 break;
5144 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005145 txn->flags &= ~TX_CK_MASK;
5146 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147 }
5148 }
5149 srv = srv->next;
5150 }
5151 }
5152}
5153
5154
Willy Tarreaub2513902006-12-17 14:52:38 +01005155/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005156 * In a GET or HEAD request, check if the requested URI matches the stats uri
5157 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005158 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005159 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005160 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005161 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005162 *
5163 * Returns 1 if the session's state changes, otherwise 0.
5164 */
5165int stats_check_uri_auth(struct session *t, struct proxy *backend)
5166{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005167 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005168 struct uri_auth *uri_auth = backend->uri_auth;
5169 struct user_auth *user;
5170 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005171 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005172
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005173 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005175 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005176 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005177 return 0;
5178
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005179 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005180
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005181 /* the URI is in h */
5182 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005183 return 0;
5184
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005185 h += uri_auth->uri_len;
5186 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5187 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005188 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005189 break;
5190 }
5191 h++;
5192 }
5193
5194 if (uri_auth->refresh) {
5195 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5196 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5197 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005198 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005199 break;
5200 }
5201 h++;
5202 }
5203 }
5204
Willy Tarreau55bb8452007-10-17 18:44:57 +02005205 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5206 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5207 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005208 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005209 break;
5210 }
5211 h++;
5212 }
5213
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005214 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5215
Willy Tarreaub2513902006-12-17 14:52:38 +01005216 /* we are in front of a interceptable URI. Let's check
5217 * if there's an authentication and if it's valid.
5218 */
5219 user = uri_auth->users;
5220 if (!user) {
5221 /* no user auth required, it's OK */
5222 authenticated = 1;
5223 } else {
5224 authenticated = 0;
5225
5226 /* a user list is defined, we have to check.
5227 * skip 21 chars for "Authorization: Basic ".
5228 */
5229
5230 /* FIXME: this should move to an earlier place */
5231 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005232 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5233 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5234 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005235 if (len > 14 &&
5236 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005237 txn->auth_hdr.str = h;
5238 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005239 break;
5240 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005241 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005242 }
5243
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005244 if (txn->auth_hdr.len < 21 ||
5245 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005246 user = NULL;
5247
5248 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005249 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5250 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005251 user->user_pwd, user->user_len)) {
5252 authenticated = 1;
5253 break;
5254 }
5255 user = user->next;
5256 }
5257 }
5258
5259 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005260 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005261
5262 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005263 msg.str = trash;
5264 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005265 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005266 client_retnclose(t, &msg);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005267 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005268 if (!(t->flags & SN_ERR_MASK))
5269 t->flags |= SN_ERR_PRXCOND;
5270 if (!(t->flags & SN_FINST_MASK))
5271 t->flags |= SN_FINST_R;
5272 return 1;
5273 }
5274
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005275 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005276 * data.
5277 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005278 EV_FD_CLR(t->cli_fd, DIR_RD);
5279 buffer_shutr(t->req);
5280 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005281 t->cli_state = CL_STSHUTR;
5282 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005283 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005284 t->data_source = DATA_SRC_STATS;
5285 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005286 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005287 produce_content(t);
5288 return 1;
5289}
5290
5291
Willy Tarreaubaaee002006-06-26 02:48:02 +02005292/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293 * Print a debug line with a header
5294 */
5295void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5296{
5297 int len, max;
5298 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5299 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5300 max = end - start;
5301 UBOUND(max, sizeof(trash) - len - 1);
5302 len += strlcpy2(trash + len, start, max + 1);
5303 trash[len++] = '\n';
5304 write(1, trash, len);
5305}
5306
5307
Willy Tarreau8797c062007-05-07 00:55:35 +02005308/************************************************************************/
5309/* The code below is dedicated to ACL parsing and matching */
5310/************************************************************************/
5311
5312
5313
5314
5315/* 1. Check on METHOD
5316 * We use the pre-parsed method if it is known, and store its number as an
5317 * integer. If it is unknown, we use the pointer and the length.
5318 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005319static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005320{
5321 int len, meth;
5322
Willy Tarreauae8b7962007-06-09 23:10:04 +02005323 len = strlen(*text);
5324 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005325
5326 pattern->val.i = meth;
5327 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005328 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005329 if (!pattern->ptr.str)
5330 return 0;
5331 pattern->len = len;
5332 }
5333 return 1;
5334}
5335
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005336static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005337acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5338 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005339{
5340 int meth;
5341 struct http_txn *txn = l7;
5342
Willy Tarreaub6866442008-07-14 23:54:42 +02005343 if (!txn)
5344 return 0;
5345
Willy Tarreauc11416f2007-06-17 16:58:38 +02005346 if (txn->req.msg_state != HTTP_MSG_BODY)
5347 return 0;
5348
Willy Tarreau8797c062007-05-07 00:55:35 +02005349 meth = txn->meth;
5350 test->i = meth;
5351 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005352 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5353 /* ensure the indexes are not affected */
5354 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005355 test->len = txn->req.sl.rq.m_l;
5356 test->ptr = txn->req.sol;
5357 }
5358 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5359 return 1;
5360}
5361
5362static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5363{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005364 int icase;
5365
Willy Tarreau8797c062007-05-07 00:55:35 +02005366 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005367 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005368
5369 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005370 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005371
5372 /* Other method, we must compare the strings */
5373 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005374 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005375
5376 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5377 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5378 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005379 return ACL_PAT_FAIL;
5380 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005381}
5382
5383/* 2. Check on Request/Status Version
5384 * We simply compare strings here.
5385 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005386static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005387{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005388 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005389 if (!pattern->ptr.str)
5390 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005391 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005392 return 1;
5393}
5394
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005395static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005396acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5397 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005398{
5399 struct http_txn *txn = l7;
5400 char *ptr;
5401 int len;
5402
Willy Tarreaub6866442008-07-14 23:54:42 +02005403 if (!txn)
5404 return 0;
5405
Willy Tarreauc11416f2007-06-17 16:58:38 +02005406 if (txn->req.msg_state != HTTP_MSG_BODY)
5407 return 0;
5408
Willy Tarreau8797c062007-05-07 00:55:35 +02005409 len = txn->req.sl.rq.v_l;
5410 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5411
5412 while ((len-- > 0) && (*ptr++ != '/'));
5413 if (len <= 0)
5414 return 0;
5415
5416 test->ptr = ptr;
5417 test->len = len;
5418
5419 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5420 return 1;
5421}
5422
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005423static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005424acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5425 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005426{
5427 struct http_txn *txn = l7;
5428 char *ptr;
5429 int len;
5430
Willy Tarreaub6866442008-07-14 23:54:42 +02005431 if (!txn)
5432 return 0;
5433
Willy Tarreauc11416f2007-06-17 16:58:38 +02005434 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5435 return 0;
5436
Willy Tarreau8797c062007-05-07 00:55:35 +02005437 len = txn->rsp.sl.st.v_l;
5438 ptr = txn->rsp.sol;
5439
5440 while ((len-- > 0) && (*ptr++ != '/'));
5441 if (len <= 0)
5442 return 0;
5443
5444 test->ptr = ptr;
5445 test->len = len;
5446
5447 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5448 return 1;
5449}
5450
5451/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005452static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005453acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5454 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005455{
5456 struct http_txn *txn = l7;
5457 char *ptr;
5458 int len;
5459
Willy Tarreaub6866442008-07-14 23:54:42 +02005460 if (!txn)
5461 return 0;
5462
Willy Tarreauc11416f2007-06-17 16:58:38 +02005463 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5464 return 0;
5465
Willy Tarreau8797c062007-05-07 00:55:35 +02005466 len = txn->rsp.sl.st.c_l;
5467 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5468
5469 test->i = __strl2ui(ptr, len);
5470 test->flags = ACL_TEST_F_VOL_1ST;
5471 return 1;
5472}
5473
5474/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005475static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005476acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5477 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005478{
5479 struct http_txn *txn = l7;
5480
Willy Tarreaub6866442008-07-14 23:54:42 +02005481 if (!txn)
5482 return 0;
5483
Willy Tarreauc11416f2007-06-17 16:58:38 +02005484 if (txn->req.msg_state != HTTP_MSG_BODY)
5485 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005486
Willy Tarreauc11416f2007-06-17 16:58:38 +02005487 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5488 /* ensure the indexes are not affected */
5489 return 0;
5490
Willy Tarreau8797c062007-05-07 00:55:35 +02005491 test->len = txn->req.sl.rq.u_l;
5492 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5493
Willy Tarreauf3d25982007-05-08 22:45:09 +02005494 /* we do not need to set READ_ONLY because the data is in a buffer */
5495 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005496 return 1;
5497}
5498
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005499static int
5500acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5501 struct acl_expr *expr, struct acl_test *test)
5502{
5503 struct http_txn *txn = l7;
5504
Willy Tarreaub6866442008-07-14 23:54:42 +02005505 if (!txn)
5506 return 0;
5507
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005508 if (txn->req.msg_state != HTTP_MSG_BODY)
5509 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005510
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005511 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5512 /* ensure the indexes are not affected */
5513 return 0;
5514
5515 /* Parse HTTP request */
5516 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5517 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5518 test->i = AF_INET;
5519
5520 /*
5521 * If we are parsing url in frontend space, we prepare backend stage
5522 * to not parse again the same url ! optimization lazyness...
5523 */
5524 if (px->options & PR_O_HTTP_PROXY)
5525 l4->flags |= SN_ADDR_SET;
5526
5527 test->flags = ACL_TEST_F_READ_ONLY;
5528 return 1;
5529}
5530
5531static int
5532acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5533 struct acl_expr *expr, struct acl_test *test)
5534{
5535 struct http_txn *txn = l7;
5536
Willy Tarreaub6866442008-07-14 23:54:42 +02005537 if (!txn)
5538 return 0;
5539
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005540 if (txn->req.msg_state != HTTP_MSG_BODY)
5541 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005542
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005543 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5544 /* ensure the indexes are not affected */
5545 return 0;
5546
5547 /* Same optimization as url_ip */
5548 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5549 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5550
5551 if (px->options & PR_O_HTTP_PROXY)
5552 l4->flags |= SN_ADDR_SET;
5553
5554 test->flags = ACL_TEST_F_READ_ONLY;
5555 return 1;
5556}
5557
Willy Tarreauc11416f2007-06-17 16:58:38 +02005558/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5559 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5560 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005561static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005562acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005563 struct acl_expr *expr, struct acl_test *test)
5564{
5565 struct http_txn *txn = l7;
5566 struct hdr_idx *idx = &txn->hdr_idx;
5567 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005568
Willy Tarreaub6866442008-07-14 23:54:42 +02005569 if (!txn)
5570 return 0;
5571
Willy Tarreau33a7e692007-06-10 19:45:56 +02005572 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5573 /* search for header from the beginning */
5574 ctx->idx = 0;
5575
Willy Tarreau33a7e692007-06-10 19:45:56 +02005576 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5577 test->flags |= ACL_TEST_F_FETCH_MORE;
5578 test->flags |= ACL_TEST_F_VOL_HDR;
5579 test->len = ctx->vlen;
5580 test->ptr = (char *)ctx->line + ctx->val;
5581 return 1;
5582 }
5583
5584 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5585 test->flags |= ACL_TEST_F_VOL_HDR;
5586 return 0;
5587}
5588
Willy Tarreau33a7e692007-06-10 19:45:56 +02005589static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005590acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5591 struct acl_expr *expr, struct acl_test *test)
5592{
5593 struct http_txn *txn = l7;
5594
Willy Tarreaub6866442008-07-14 23:54:42 +02005595 if (!txn)
5596 return 0;
5597
Willy Tarreauc11416f2007-06-17 16:58:38 +02005598 if (txn->req.msg_state != HTTP_MSG_BODY)
5599 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005600
Willy Tarreauc11416f2007-06-17 16:58:38 +02005601 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5602 /* ensure the indexes are not affected */
5603 return 0;
5604
5605 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5606}
5607
5608static int
5609acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5610 struct acl_expr *expr, struct acl_test *test)
5611{
5612 struct http_txn *txn = l7;
5613
Willy Tarreaub6866442008-07-14 23:54:42 +02005614 if (!txn)
5615 return 0;
5616
Willy Tarreauc11416f2007-06-17 16:58:38 +02005617 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5618 return 0;
5619
5620 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5621}
5622
5623/* 6. Check on HTTP header count. The number of occurrences is returned.
5624 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5625 */
5626static int
5627acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005628 struct acl_expr *expr, struct acl_test *test)
5629{
5630 struct http_txn *txn = l7;
5631 struct hdr_idx *idx = &txn->hdr_idx;
5632 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005633 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005634
Willy Tarreaub6866442008-07-14 23:54:42 +02005635 if (!txn)
5636 return 0;
5637
Willy Tarreau33a7e692007-06-10 19:45:56 +02005638 ctx.idx = 0;
5639 cnt = 0;
5640 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5641 cnt++;
5642
5643 test->i = cnt;
5644 test->flags = ACL_TEST_F_VOL_HDR;
5645 return 1;
5646}
5647
Willy Tarreauc11416f2007-06-17 16:58:38 +02005648static int
5649acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5650 struct acl_expr *expr, struct acl_test *test)
5651{
5652 struct http_txn *txn = l7;
5653
Willy Tarreaub6866442008-07-14 23:54:42 +02005654 if (!txn)
5655 return 0;
5656
Willy Tarreauc11416f2007-06-17 16:58:38 +02005657 if (txn->req.msg_state != HTTP_MSG_BODY)
5658 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005659
Willy Tarreauc11416f2007-06-17 16:58:38 +02005660 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5661 /* ensure the indexes are not affected */
5662 return 0;
5663
5664 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5665}
5666
5667static int
5668acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5669 struct acl_expr *expr, struct acl_test *test)
5670{
5671 struct http_txn *txn = l7;
5672
Willy Tarreaub6866442008-07-14 23:54:42 +02005673 if (!txn)
5674 return 0;
5675
Willy Tarreauc11416f2007-06-17 16:58:38 +02005676 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5677 return 0;
5678
5679 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5680}
5681
Willy Tarreau33a7e692007-06-10 19:45:56 +02005682/* 7. Check on HTTP header's integer value. The integer value is returned.
5683 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005684 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005685 */
5686static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005687acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005688 struct acl_expr *expr, struct acl_test *test)
5689{
5690 struct http_txn *txn = l7;
5691 struct hdr_idx *idx = &txn->hdr_idx;
5692 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005693
Willy Tarreaub6866442008-07-14 23:54:42 +02005694 if (!txn)
5695 return 0;
5696
Willy Tarreau33a7e692007-06-10 19:45:56 +02005697 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5698 /* search for header from the beginning */
5699 ctx->idx = 0;
5700
Willy Tarreau33a7e692007-06-10 19:45:56 +02005701 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5702 test->flags |= ACL_TEST_F_FETCH_MORE;
5703 test->flags |= ACL_TEST_F_VOL_HDR;
5704 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5705 return 1;
5706 }
5707
5708 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5709 test->flags |= ACL_TEST_F_VOL_HDR;
5710 return 0;
5711}
5712
Willy Tarreauc11416f2007-06-17 16:58:38 +02005713static int
5714acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5715 struct acl_expr *expr, struct acl_test *test)
5716{
5717 struct http_txn *txn = l7;
5718
Willy Tarreaub6866442008-07-14 23:54:42 +02005719 if (!txn)
5720 return 0;
5721
Willy Tarreauc11416f2007-06-17 16:58:38 +02005722 if (txn->req.msg_state != HTTP_MSG_BODY)
5723 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005724
Willy Tarreauc11416f2007-06-17 16:58:38 +02005725 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5726 /* ensure the indexes are not affected */
5727 return 0;
5728
5729 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5730}
5731
5732static int
5733acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5734 struct acl_expr *expr, struct acl_test *test)
5735{
5736 struct http_txn *txn = l7;
5737
Willy Tarreaub6866442008-07-14 23:54:42 +02005738 if (!txn)
5739 return 0;
5740
Willy Tarreauc11416f2007-06-17 16:58:38 +02005741 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5742 return 0;
5743
5744 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5745}
5746
Willy Tarreau737b0c12007-06-10 21:28:46 +02005747/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5748 * the first '/' after the possible hostname, and ends before the possible '?'.
5749 */
5750static int
5751acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5752 struct acl_expr *expr, struct acl_test *test)
5753{
5754 struct http_txn *txn = l7;
5755 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005756
Willy Tarreaub6866442008-07-14 23:54:42 +02005757 if (!txn)
5758 return 0;
5759
Willy Tarreauc11416f2007-06-17 16:58:38 +02005760 if (txn->req.msg_state != HTTP_MSG_BODY)
5761 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005762
Willy Tarreauc11416f2007-06-17 16:58:38 +02005763 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5764 /* ensure the indexes are not affected */
5765 return 0;
5766
Willy Tarreau21d2af32008-02-14 20:25:24 +01005767 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5768 ptr = http_get_path(txn);
5769 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005770 return 0;
5771
5772 /* OK, we got the '/' ! */
5773 test->ptr = ptr;
5774
5775 while (ptr < end && *ptr != '?')
5776 ptr++;
5777
5778 test->len = ptr - test->ptr;
5779
5780 /* we do not need to set READ_ONLY because the data is in a buffer */
5781 test->flags = ACL_TEST_F_VOL_1ST;
5782 return 1;
5783}
5784
5785
Willy Tarreau8797c062007-05-07 00:55:35 +02005786
5787/************************************************************************/
5788/* All supported keywords must be declared here. */
5789/************************************************************************/
5790
5791/* Note: must not be declared <const> as its list will be overwritten */
5792static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005793 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5794 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5795 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5796 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005797
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005798 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5799 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5800 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5801 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5802 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5803 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5804 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5805 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5806 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005807
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005808 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5809 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5810 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5811 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5812 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5813 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5814 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5815 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5816 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5817 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005818
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005819 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5820 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5821 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5822 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5823 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5824 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5825 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5826 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5827 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005828
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005829 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5830 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5831 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5832 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5833 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5834 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5835 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005836
Willy Tarreauf3d25982007-05-08 22:45:09 +02005837 { NULL, NULL, NULL, NULL },
5838
5839#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005840 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5841 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5842 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5843 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5844 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5845 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5846 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5847
Willy Tarreau8797c062007-05-07 00:55:35 +02005848 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5849 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5850 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5851 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5852 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5853 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5854 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5855 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5856
5857 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5858 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5859 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5860 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5861 { NULL, NULL, NULL, NULL },
5862#endif
5863}};
5864
5865
5866__attribute__((constructor))
5867static void __http_protocol_init(void)
5868{
5869 acl_register_keywords(&acl_kws);
5870}
5871
5872
Willy Tarreau58f10d72006-12-04 02:26:12 +01005873/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005874 * Local variables:
5875 * c-indent-level: 8
5876 * c-basic-offset: 8
5877 * End:
5878 */