blob: 3dd11ec73860939b180f86311de9548bbb33bc5d [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>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010054#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020055#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/task.h>
57
Willy Tarreau6d1a9882007-01-07 02:03:04 +010058#ifdef CONFIG_HAP_TCPSPLICE
59#include <libtcpsplice.h>
60#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#define DEBUG_PARSE_NO_SPEEDUP
63#undef DEBUG_PARSE_NO_SPEEDUP
64
Willy Tarreau976f1ee2006-12-17 10:06:03 +010065/* This is used to perform a quick jump as an alternative to a break/continue
66 * instruction. The first argument is the label for normal operation, and the
67 * second one is the break/continue instruction in the no_speedup mode.
68 */
69
70#ifdef DEBUG_PARSE_NO_SPEEDUP
71#define QUICK_JUMP(x,y) y
72#else
73#define QUICK_JUMP(x,y) goto x
74#endif
75
Willy Tarreau1c47f852006-07-09 08:22:27 +020076/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010077const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020078 "HTTP/1.0 200 OK\r\n"
79 "Cache-Control: no-cache\r\n"
80 "Connection: close\r\n"
81 "Content-Type: text/html\r\n"
82 "\r\n"
83 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
84
Willy Tarreau0f772532006-12-23 20:51:41 +010085const struct chunk http_200_chunk = {
86 .str = (char *)&HTTP_200,
87 .len = sizeof(HTTP_200)-1
88};
89
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090const char *HTTP_301 =
91 "HTTP/1.0 301 Moved Permantenly\r\n"
92 "Cache-Control: no-cache\r\n"
93 "Connection: close\r\n"
94 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Willy Tarreau0f772532006-12-23 20:51:41 +010096const char *HTTP_302 =
97 "HTTP/1.0 302 Found\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
102/* same as 302 except that the browser MUST retry with the GET method */
103const char *HTTP_303 =
104 "HTTP/1.0 303 See Other\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau0f772532006-12-23 20:51:41 +0100119
120const int http_err_codes[HTTP_ERR_SIZE] = {
121 [HTTP_ERR_400] = 400,
122 [HTTP_ERR_403] = 403,
123 [HTTP_ERR_408] = 408,
124 [HTTP_ERR_500] = 500,
125 [HTTP_ERR_502] = 502,
126 [HTTP_ERR_503] = 503,
127 [HTTP_ERR_504] = 504,
128};
129
Willy Tarreau80587432006-12-24 17:47:20 +0100130static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100132 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
138
139 [HTTP_ERR_403] =
140 "HTTP/1.0 403 Forbidden\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
146
147 [HTTP_ERR_408] =
148 "HTTP/1.0 408 Request Time-out\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
154
155 [HTTP_ERR_500] =
156 "HTTP/1.0 500 Server Error\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
162
163 [HTTP_ERR_502] =
164 "HTTP/1.0 502 Bad Gateway\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
170
171 [HTTP_ERR_503] =
172 "HTTP/1.0 503 Service Unavailable\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
178
179 [HTTP_ERR_504] =
180 "HTTP/1.0 504 Gateway Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
186
187};
188
Willy Tarreau80587432006-12-24 17:47:20 +0100189/* We must put the messages here since GCC cannot initialize consts depending
190 * on strlen().
191 */
192struct chunk http_err_chunks[HTTP_ERR_SIZE];
193
Willy Tarreau42250582007-04-01 01:30:43 +0200194#define FD_SETS_ARE_BITFIELDS
195#ifdef FD_SETS_ARE_BITFIELDS
196/*
197 * This map is used with all the FD_* macros to check whether a particular bit
198 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
199 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
200 * byte should be encoded. Be careful to always pass bytes from 0 to 255
201 * exclusively to the macros.
202 */
203fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
204fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
205
206#else
207#error "Check if your OS uses bitfields for fd_sets"
208#endif
209
Willy Tarreau80587432006-12-24 17:47:20 +0100210void init_proto_http()
211{
Willy Tarreau42250582007-04-01 01:30:43 +0200212 int i;
213 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100214 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200215
Willy Tarreau80587432006-12-24 17:47:20 +0100216 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
217 if (!http_err_msgs[msg]) {
218 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
219 abort();
220 }
221
222 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
223 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
224 }
Willy Tarreau42250582007-04-01 01:30:43 +0200225
226 /* initialize the log header encoding map : '{|}"#' should be encoded with
227 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
228 * URL encoding only requires '"', '#' to be encoded as well as non-
229 * printable characters above.
230 */
231 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
232 memset(url_encode_map, 0, sizeof(url_encode_map));
233 for (i = 0; i < 32; i++) {
234 FD_SET(i, hdr_encode_map);
235 FD_SET(i, url_encode_map);
236 }
237 for (i = 127; i < 256; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241
242 tmp = "\"#{|}";
243 while (*tmp) {
244 FD_SET(*tmp, hdr_encode_map);
245 tmp++;
246 }
247
248 tmp = "\"#";
249 while (*tmp) {
250 FD_SET(*tmp, url_encode_map);
251 tmp++;
252 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200253
254 /* memory allocations */
255 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200256 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100257}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258
Willy Tarreau53b6c742006-12-17 13:37:46 +0100259/*
260 * We have 26 list of methods (1 per first letter), each of which can have
261 * up to 3 entries (2 valid, 1 null).
262 */
263struct http_method_desc {
264 http_meth_t meth;
265 int len;
266 const char text[8];
267};
268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100269const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100270 ['C' - 'A'] = {
271 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
272 },
273 ['D' - 'A'] = {
274 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
275 },
276 ['G' - 'A'] = {
277 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
278 },
279 ['H' - 'A'] = {
280 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
281 },
282 ['P' - 'A'] = {
283 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
284 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
285 },
286 ['T' - 'A'] = {
287 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
288 },
289 /* rest is empty like this :
290 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
291 */
292};
293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296 * RFC2616 for those chars.
297 */
298
299const char http_is_spht[256] = {
300 [' '] = 1, ['\t'] = 1,
301};
302
303const char http_is_crlf[256] = {
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_lws[256] = {
308 [' '] = 1, ['\t'] = 1,
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_sep[256] = {
313 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
314 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
315 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
316 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
317 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
318};
319
320const char http_is_ctl[256] = {
321 [0 ... 31] = 1,
322 [127] = 1,
323};
324
325/*
326 * A token is any ASCII char that is neither a separator nor a CTL char.
327 * Do not overwrite values in assignment since gcc-2.95 will not handle
328 * them correctly. Instead, define every non-CTL char's status.
329 */
330const char http_is_token[256] = {
331 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
332 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
333 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
334 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
335 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
336 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
337 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
338 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
339 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
340 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
341 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
342 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
343 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
344 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
345 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
346 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
347 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
348 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
349 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
350 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
351 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
352 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
353 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
354 ['|'] = 1, ['}'] = 0, ['~'] = 1,
355};
356
357
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100358/*
359 * An http ver_token is any ASCII which can be found in an HTTP version,
360 * which includes 'H', 'T', 'P', '/', '.' and any digit.
361 */
362const char http_is_ver_token[256] = {
363 ['.'] = 1, ['/'] = 1,
364 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
365 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
366 ['H'] = 1, ['P'] = 1, ['T'] = 1,
367};
368
369
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200371static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372#endif
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{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 const char *eol, *sov;
471 int cur_idx;
472
473 if (ctx->idx) {
474 /* We have previously returned a value, let's search
475 * another one on the same line.
476 */
477 cur_idx = ctx->idx;
478 sol = ctx->line;
479 sov = sol + ctx->val + ctx->vlen;
480 eol = sol + idx->v[cur_idx].len;
481
482 if (sov >= eol)
483 /* no more values in this header */
484 goto next_hdr;
485
486 /* values remaining for this header, skip the comma */
487 sov++;
488 while (sov < eol && http_is_lws[(unsigned char)*sov])
489 sov++;
490
491 goto return_hdr;
492 }
493
494 /* first request for this header */
495 sol += hdr_idx_first_pos(idx);
496 cur_idx = hdr_idx_first_idx(idx);
497
498 while (cur_idx) {
499 eol = sol + idx->v[cur_idx].len;
500
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200501 if (len == 0) {
502 /* No argument was passed, we want any header.
503 * To achieve this, we simply build a fake request. */
504 while (sol + len < eol && sol[len] != ':')
505 len++;
506 name = sol;
507 }
508
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 if ((len < eol - sol) &&
510 (sol[len] == ':') &&
511 (strncasecmp(sol, name, len) == 0)) {
512
513 sov = sol + len + 1;
514 while (sov < eol && http_is_lws[(unsigned char)*sov])
515 sov++;
516 return_hdr:
517 ctx->line = sol;
518 ctx->idx = cur_idx;
519 ctx->val = sov - sol;
520
521 eol = find_hdr_value_end(sov, eol);
522 ctx->vlen = eol - sov;
523 return 1;
524 }
525 next_hdr:
526 sol = eol + idx->v[cur_idx].cr + 1;
527 cur_idx = idx->v[cur_idx].next;
528 }
529 return 0;
530}
531
532int http_find_header(const char *name,
533 const char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 return http_find_header2(name, strlen(name), sol, idx, ctx);
537}
538
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100539/* This function handles a server error at the stream interface level. The
540 * stream interface is assumed to be already in a closed state. An optional
541 * message is copied into the input buffer, and an HTTP status code stored.
542 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100543 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100545static void http_server_error(struct session *t, struct stream_interface *si,
546 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100548 buffer_erase(si->ob);
549 buffer_erase(si->ib);
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100550 buffer_write_ena(si->ib);
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 Tarreau2d3d94c2008-11-30 20:20:08 +0100553 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 }
555 if (!(t->flags & SN_ERR_MASK))
556 t->flags |= err;
557 if (!(t->flags & SN_FINST_MASK))
558 t->flags |= finst;
559}
560
Willy Tarreau80587432006-12-24 17:47:20 +0100561/* This function returns the appropriate error location for the given session
562 * and message.
563 */
564
565struct chunk *error_message(struct session *s, int msgnum)
566{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200567 if (s->be->errmsg[msgnum].str)
568 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100569 else if (s->fe->errmsg[msgnum].str)
570 return &s->fe->errmsg[msgnum];
571 else
572 return &http_err_chunks[msgnum];
573}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575/*
576 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
577 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
578 */
579static http_meth_t find_http_meth(const char *str, const int len)
580{
581 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100583
584 m = ((unsigned)*str - 'A');
585
586 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 for (h = http_methods[m]; h->len > 0; h++) {
588 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100590 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 };
593 return HTTP_METH_OTHER;
594 }
595 return HTTP_METH_NONE;
596
597}
598
Willy Tarreau21d2af32008-02-14 20:25:24 +0100599/* Parse the URI from the given transaction (which is assumed to be in request
600 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
601 * It is returned otherwise.
602 */
603static char *
604http_get_path(struct http_txn *txn)
605{
606 char *ptr, *end;
607
608 ptr = txn->req.sol + txn->req.sl.rq.u;
609 end = ptr + txn->req.sl.rq.u_l;
610
611 if (ptr >= end)
612 return NULL;
613
614 /* RFC2616, par. 5.1.2 :
615 * Request-URI = "*" | absuri | abspath | authority
616 */
617
618 if (*ptr == '*')
619 return NULL;
620
621 if (isalpha((unsigned char)*ptr)) {
622 /* this is a scheme as described by RFC3986, par. 3.1 */
623 ptr++;
624 while (ptr < end &&
625 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
626 ptr++;
627 /* skip '://' */
628 if (ptr == end || *ptr++ != ':')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 if (ptr == end || *ptr++ != '/')
633 return NULL;
634 }
635 /* skip [user[:passwd]@]host[:[port]] */
636
637 while (ptr < end && *ptr != '/')
638 ptr++;
639
640 if (ptr == end)
641 return NULL;
642
643 /* OK, we got the '/' ! */
644 return ptr;
645}
646
Willy Tarreauefb453c2008-10-26 20:49:47 +0100647/* Returns a 302 for a redirectable request. This may only be called just after
648 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
649 * left unchanged and will follow normal proxy processing.
650 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100652{
653 struct http_txn *txn;
654 struct chunk rdr;
655 char *path;
656 int len;
657
658 /* 1: create the response header */
659 rdr.len = strlen(HTTP_302);
660 rdr.str = trash;
661 memcpy(rdr.str, HTTP_302, rdr.len);
662
663 /* 2: add the server's prefix */
664 if (rdr.len + s->srv->rdr_len > sizeof(trash))
665 return;
666
667 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
668 rdr.len += s->srv->rdr_len;
669
670 /* 3: add the request URI */
671 txn = &s->txn;
672 path = http_get_path(txn);
673 if (!path)
674 return;
675
676 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
677 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
678 return;
679
680 memcpy(rdr.str + rdr.len, path, len);
681 rdr.len += len;
682 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
683 rdr.len += 4;
684
685 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100686 si->shutr(si);
687 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100688 si->err_type = SI_ET_NONE;
689 si->err_loc = NULL;
690 si->state = SI_ST_CLO;
691
692 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100693 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100694
695 /* FIXME: we should increase a counter of redirects per server and per backend. */
696 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100697 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698}
699
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100700/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701 * that the server side is closed. Note that err_type is actually a
702 * bitmask, where almost only aborts may be cumulated with other
703 * values. We consider that aborted operations are more important
704 * than timeouts or errors due to the fact that nobody else in the
705 * logs might explain incomplete retries. All others should avoid
706 * being cumulated. It should normally not be possible to have multiple
707 * aborts at once, but just in case, the first one in sequence is reported.
708 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100709void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100711 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100712
713 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100714 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
715 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100717 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
718 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100719 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100720 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
721 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100722 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100723 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
724 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100726 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
727 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100728 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100729 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
730 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100731 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100732 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
733 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100734}
735
Willy Tarreau42250582007-04-01 01:30:43 +0200736extern const char sess_term_cond[8];
737extern const char sess_fin_state[8];
738extern const char *monthname[12];
739const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
740const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
741 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
742 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200743struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200744struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100745
Willy Tarreau42250582007-04-01 01:30:43 +0200746/*
747 * send a log for the session when we have enough info about it.
748 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100749 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200751{
752 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
753 struct proxy *fe = s->fe;
754 struct proxy *be = s->be;
755 struct proxy *prx_log;
756 struct http_txn *txn = &s->txn;
757 int tolog;
758 char *uri, *h;
759 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200760 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200761 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200762 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200763 int hdr;
764
765 if (fe->logfac1 < 0 && fe->logfac2 < 0)
766 return;
767 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100768
Willy Tarreau42250582007-04-01 01:30:43 +0200769 if (s->cli_addr.ss_family == AF_INET)
770 inet_ntop(AF_INET,
771 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
772 pn, sizeof(pn));
773 else
774 inet_ntop(AF_INET6,
775 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
776 pn, sizeof(pn));
777
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200778 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200779
780 /* FIXME: let's limit ourselves to frontend logging for now. */
781 tolog = fe->to_log;
782
783 h = tmpline;
784 if (fe->to_log & LW_REQHDR &&
785 txn->req.cap &&
786 (h < tmpline + sizeof(tmpline) - 10)) {
787 *(h++) = ' ';
788 *(h++) = '{';
789 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
790 if (hdr)
791 *(h++) = '|';
792 if (txn->req.cap[hdr] != NULL)
793 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
794 '#', hdr_encode_map, txn->req.cap[hdr]);
795 }
796 *(h++) = '}';
797 }
798
799 if (fe->to_log & LW_RSPHDR &&
800 txn->rsp.cap &&
801 (h < tmpline + sizeof(tmpline) - 7)) {
802 *(h++) = ' ';
803 *(h++) = '{';
804 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
805 if (hdr)
806 *(h++) = '|';
807 if (txn->rsp.cap[hdr] != NULL)
808 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
809 '#', hdr_encode_map, txn->rsp.cap[hdr]);
810 }
811 *(h++) = '}';
812 }
813
814 if (h < tmpline + sizeof(tmpline) - 4) {
815 *(h++) = ' ';
816 *(h++) = '"';
817 uri = txn->uri ? txn->uri : "<BADREQ>";
818 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
819 '#', url_encode_map, uri);
820 *(h++) = '"';
821 }
822 *h = '\0';
823
824 svid = (tolog & LW_SVID) ?
825 (s->data_source != DATA_SRC_STATS) ?
826 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
827
Willy Tarreau70089872008-06-13 21:12:51 +0200828 t_request = -1;
829 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
830 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
831
Willy Tarreau42250582007-04-01 01:30:43 +0200832 send_log(prx_log, LOG_INFO,
833 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200834 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
835 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200836 pn,
837 (s->cli_addr.ss_family == AF_INET) ?
838 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
839 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200840 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200841 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200842 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200843 t_request,
844 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200845 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
846 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
847 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
848 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100849 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200850 txn->cli_cookie ? txn->cli_cookie : "-",
851 txn->srv_cookie ? txn->srv_cookie : "-",
852 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
853 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
854 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
855 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
856 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100857 (s->flags & SN_REDISP)?"+":"",
858 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200859 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
860
861 s->logs.logwait = 0;
862}
863
Willy Tarreau117f59e2007-03-04 18:17:17 +0100864
865/*
866 * Capture headers from message starting at <som> according to header list
867 * <cap_hdr>, and fill the <idx> structure appropriately.
868 */
869void capture_headers(char *som, struct hdr_idx *idx,
870 char **cap, struct cap_hdr *cap_hdr)
871{
872 char *eol, *sol, *col, *sov;
873 int cur_idx;
874 struct cap_hdr *h;
875 int len;
876
877 sol = som + hdr_idx_first_pos(idx);
878 cur_idx = hdr_idx_first_idx(idx);
879
880 while (cur_idx) {
881 eol = sol + idx->v[cur_idx].len;
882
883 col = sol;
884 while (col < eol && *col != ':')
885 col++;
886
887 sov = col + 1;
888 while (sov < eol && http_is_lws[(unsigned char)*sov])
889 sov++;
890
891 for (h = cap_hdr; h; h = h->next) {
892 if ((h->namelen == col - sol) &&
893 (strncasecmp(sol, h->name, h->namelen) == 0)) {
894 if (cap[h->index] == NULL)
895 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200896 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100897
898 if (cap[h->index] == NULL) {
899 Alert("HTTP capture : out of memory.\n");
900 continue;
901 }
902
903 len = eol - sov;
904 if (len > h->len)
905 len = h->len;
906
907 memcpy(cap[h->index], sov, len);
908 cap[h->index][len]=0;
909 }
910 }
911 sol = eol + idx->v[cur_idx].cr + 1;
912 cur_idx = idx->v[cur_idx].next;
913 }
914}
915
916
Willy Tarreau42250582007-04-01 01:30:43 +0200917/* either we find an LF at <ptr> or we jump to <bad>.
918 */
919#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
920
921/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
922 * otherwise to <http_msg_ood> with <state> set to <st>.
923 */
924#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
925 ptr++; \
926 if (likely(ptr < end)) \
927 goto good; \
928 else { \
929 state = (st); \
930 goto http_msg_ood; \
931 } \
932 } while (0)
933
934
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100936 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100937 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
938 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
939 * will give undefined results.
940 * Note that it is upon the caller's responsibility to ensure that ptr < end,
941 * and that msg->sol points to the beginning of the response.
942 * If a complete line is found (which implies that at least one CR or LF is
943 * found before <end>, the updated <ptr> is returned, otherwise NULL is
944 * returned indicating an incomplete line (which does not mean that parts have
945 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
946 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
947 * upon next call.
948 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100950 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
951 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200952 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100953 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100954const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
955 unsigned int state, const char *ptr, const char *end,
956 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100957{
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 switch (state) {
959 http_msg_rpver:
960 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100961 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
963
964 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100965 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
967 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100968 state = HTTP_MSG_ERROR;
969 break;
970
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 http_msg_rpver_sp:
972 case HTTP_MSG_RPVER_SP:
973 if (likely(!HTTP_IS_LWS(*ptr))) {
974 msg->sl.st.c = ptr - msg_buf;
975 goto http_msg_rpcode;
976 }
977 if (likely(HTTP_IS_SPHT(*ptr)))
978 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
979 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100980 state = HTTP_MSG_ERROR;
981 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100982
983 http_msg_rpcode:
984 case HTTP_MSG_RPCODE:
985 if (likely(!HTTP_IS_LWS(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
987
988 if (likely(HTTP_IS_SPHT(*ptr))) {
989 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
991 }
992
993 /* so it's a CR/LF, so there is no reason phrase */
994 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
995 http_msg_rsp_reason:
996 /* FIXME: should we support HTTP responses without any reason phrase ? */
997 msg->sl.st.r = ptr - msg_buf;
998 msg->sl.st.r_l = 0;
999 goto http_msg_rpline_eol;
1000
1001 http_msg_rpcode_sp:
1002 case HTTP_MSG_RPCODE_SP:
1003 if (likely(!HTTP_IS_LWS(*ptr))) {
1004 msg->sl.st.r = ptr - msg_buf;
1005 goto http_msg_rpreason;
1006 }
1007 if (likely(HTTP_IS_SPHT(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1009 /* so it's a CR/LF, so there is no reason phrase */
1010 goto http_msg_rsp_reason;
1011
1012 http_msg_rpreason:
1013 case HTTP_MSG_RPREASON:
1014 if (likely(!HTTP_IS_CRLF(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1016 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1017 http_msg_rpline_eol:
1018 /* We have seen the end of line. Note that we do not
1019 * necessarily have the \n yet, but at least we know that we
1020 * have EITHER \r OR \n, otherwise the response would not be
1021 * complete. We can then record the response length and return
1022 * to the caller which will be able to register it.
1023 */
1024 msg->sl.st.l = ptr - msg->sol;
1025 return ptr;
1026
1027#ifdef DEBUG_FULL
1028 default:
1029 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1030 exit(1);
1031#endif
1032 }
1033
1034 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001035 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 if (ret_state)
1037 *ret_state = state;
1038 if (ret_ptr)
1039 *ret_ptr = (char *)ptr;
1040 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001041}
1042
1043
1044/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001045 * This function parses a request line between <ptr> and <end>, starting with
1046 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1047 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1048 * will give undefined results.
1049 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1050 * and that msg->sol points to the beginning of the request.
1051 * If a complete line is found (which implies that at least one CR or LF is
1052 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1053 * returned indicating an incomplete line (which does not mean that parts have
1054 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1055 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1056 * upon next call.
1057 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001058 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1060 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001061 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001063const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1064 unsigned int state, const char *ptr, const char *end,
1065 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 switch (state) {
1068 http_msg_rqmeth:
1069 case HTTP_MSG_RQMETH:
1070 if (likely(HTTP_IS_TOKEN(*ptr)))
1071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001074 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_CRLF(*ptr))) {
1079 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001080 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri:
1082 msg->sl.rq.u = ptr - msg_buf;
1083 http_msg_req09_uri_e:
1084 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1085 http_msg_req09_ver:
1086 msg->sl.rq.v = ptr - msg_buf;
1087 msg->sl.rq.v_l = 0;
1088 goto http_msg_rqline_eol;
1089 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001090 state = HTTP_MSG_ERROR;
1091 break;
1092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 http_msg_rqmeth_sp:
1094 case HTTP_MSG_RQMETH_SP:
1095 if (likely(!HTTP_IS_LWS(*ptr))) {
1096 msg->sl.rq.u = ptr - msg_buf;
1097 goto http_msg_rquri;
1098 }
1099 if (likely(HTTP_IS_SPHT(*ptr)))
1100 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1101 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1102 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 http_msg_rquri:
1105 case HTTP_MSG_RQURI:
1106 if (likely(!HTTP_IS_LWS(*ptr)))
1107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (likely(HTTP_IS_SPHT(*ptr))) {
1110 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1115 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 http_msg_rquri_sp:
1118 case HTTP_MSG_RQURI_SP:
1119 if (likely(!HTTP_IS_LWS(*ptr))) {
1120 msg->sl.rq.v = ptr - msg_buf;
1121 goto http_msg_rqver;
1122 }
1123 if (likely(HTTP_IS_SPHT(*ptr)))
1124 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1125 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1126 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001127
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001128 http_msg_rqver:
1129 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001130 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001132
1133 if (likely(HTTP_IS_CRLF(*ptr))) {
1134 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1135 http_msg_rqline_eol:
1136 /* We have seen the end of line. Note that we do not
1137 * necessarily have the \n yet, but at least we know that we
1138 * have EITHER \r OR \n, otherwise the request would not be
1139 * complete. We can then record the request length and return
1140 * to the caller which will be able to register it.
1141 */
1142 msg->sl.rq.l = ptr - msg->sol;
1143 return ptr;
1144 }
1145
1146 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150#ifdef DEBUG_FULL
1151 default:
1152 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1153 exit(1);
1154#endif
1155 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001158 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (ret_state)
1160 *ret_state = state;
1161 if (ret_ptr)
1162 *ret_ptr = (char *)ptr;
1163 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
1166
Willy Tarreau8973c702007-01-21 23:58:29 +01001167/*
1168 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001169 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 * when data are missing and recalled at the exact same location with no
1171 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001172 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1173 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001174 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1176{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001177 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001180 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001181 ptr = buf->lr;
1182 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (unlikely(ptr >= end))
1185 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001188 /*
1189 * First, states that are specific to the response only.
1190 * We check them first so that request and headers are
1191 * closer to each other (accessed more often).
1192 */
1193 http_msg_rpbefore:
1194 case HTTP_MSG_RPBEFORE:
1195 if (likely(HTTP_IS_TOKEN(*ptr))) {
1196 if (likely(ptr == buf->data)) {
1197 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001198 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 } else {
1200#if PARSE_PRESERVE_EMPTY_LINES
1201 /* only skip empty leading lines, don't remove them */
1202 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001203 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001204#else
1205 /* Remove empty leading lines, as recommended by
1206 * RFC2616. This takes a lot of time because we
1207 * must move all the buffer backwards, but this
1208 * is rarely needed. The method above will be
1209 * cleaner when we'll be able to start sending
1210 * the request from any place in the buffer.
1211 */
1212 buf->lr = ptr;
1213 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001214 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 msg->sol = buf->data;
1216 ptr = buf->data;
1217 end = buf->r;
1218#endif
1219 }
1220 hdr_idx_init(idx);
1221 state = HTTP_MSG_RPVER;
1222 goto http_msg_rpver;
1223 }
1224
1225 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1226 goto http_msg_invalid;
1227
1228 if (unlikely(*ptr == '\n'))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1231 /* stop here */
1232
1233 http_msg_rpbefore_cr:
1234 case HTTP_MSG_RPBEFORE_CR:
1235 EXPECT_LF_HERE(ptr, http_msg_invalid);
1236 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1237 /* stop here */
1238
1239 http_msg_rpver:
1240 case HTTP_MSG_RPVER:
1241 case HTTP_MSG_RPVER_SP:
1242 case HTTP_MSG_RPCODE:
1243 case HTTP_MSG_RPCODE_SP:
1244 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001245 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001246 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 if (unlikely(!ptr))
1248 return;
1249
1250 /* we have a full response and we know that we have either a CR
1251 * or an LF at <ptr>.
1252 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001253 //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 +01001254 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1255
1256 msg->sol = ptr;
1257 if (likely(*ptr == '\r'))
1258 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1259 goto http_msg_rpline_end;
1260
1261 http_msg_rpline_end:
1262 case HTTP_MSG_RPLINE_END:
1263 /* msg->sol must point to the first of CR or LF. */
1264 EXPECT_LF_HERE(ptr, http_msg_invalid);
1265 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1266 /* stop here */
1267
1268 /*
1269 * Second, states that are specific to the request only
1270 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 http_msg_rqbefore:
1272 case HTTP_MSG_RQBEFORE:
1273 if (likely(HTTP_IS_TOKEN(*ptr))) {
1274 if (likely(ptr == buf->data)) {
1275 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 } else {
1278#if PARSE_PRESERVE_EMPTY_LINES
1279 /* only skip empty leading lines, don't remove them */
1280 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001281 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001282#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 /* Remove empty leading lines, as recommended by
1284 * RFC2616. This takes a lot of time because we
1285 * must move all the buffer backwards, but this
1286 * is rarely needed. The method above will be
1287 * cleaner when we'll be able to start sending
1288 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 buf->lr = ptr;
1291 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001292 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 msg->sol = buf->data;
1294 ptr = buf->data;
1295 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001296#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001298 /* we will need this when keep-alive will be supported
1299 hdr_idx_init(idx);
1300 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 state = HTTP_MSG_RQMETH;
1302 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1306 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (unlikely(*ptr == '\n'))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 http_msg_rqbefore_cr:
1314 case HTTP_MSG_RQBEFORE_CR:
1315 EXPECT_LF_HERE(ptr, http_msg_invalid);
1316 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_rqmeth:
1320 case HTTP_MSG_RQMETH:
1321 case HTTP_MSG_RQMETH_SP:
1322 case HTTP_MSG_RQURI:
1323 case HTTP_MSG_RQURI_SP:
1324 case HTTP_MSG_RQVER:
1325 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001326 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 if (unlikely(!ptr))
1328 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 /* we have a full request and we know that we have either a CR
1331 * or an LF at <ptr>.
1332 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001333 //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 +01001334 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 msg->sol = ptr;
1337 if (likely(*ptr == '\r'))
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 http_msg_rqline_end:
1342 case HTTP_MSG_RQLINE_END:
1343 /* check for HTTP/0.9 request : no version information available.
1344 * msg->sol must point to the first of CR or LF.
1345 */
1346 if (unlikely(msg->sl.rq.v_l == 0))
1347 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001348
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 EXPECT_LF_HERE(ptr, http_msg_invalid);
1350 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 /*
1354 * Common states below
1355 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 http_msg_hdr_first:
1357 case HTTP_MSG_HDR_FIRST:
1358 msg->sol = ptr;
1359 if (likely(!HTTP_IS_CRLF(*ptr))) {
1360 goto http_msg_hdr_name;
1361 }
1362
1363 if (likely(*ptr == '\r'))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1365 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 http_msg_hdr_name:
1368 case HTTP_MSG_HDR_NAME:
1369 /* assumes msg->sol points to the first char */
1370 if (likely(HTTP_IS_TOKEN(*ptr)))
1371 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (likely(*ptr == ':')) {
1374 msg->col = ptr - buf->data;
1375 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1376 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001377
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001378 if (likely(msg->err_pos < -1) || *ptr == '\n')
1379 goto http_msg_invalid;
1380
1381 if (msg->err_pos == -1) /* capture error pointer */
1382 msg->err_pos = ptr - buf->data; /* >= 0 now */
1383
1384 /* and we still accept this non-token character */
1385 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 http_msg_hdr_l1_sp:
1388 case HTTP_MSG_HDR_L1_SP:
1389 /* assumes msg->sol points to the first char and msg->col to the colon */
1390 if (likely(HTTP_IS_SPHT(*ptr)))
1391 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 /* header value can be basically anything except CR/LF */
1394 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 if (likely(!HTTP_IS_CRLF(*ptr))) {
1397 goto http_msg_hdr_val;
1398 }
1399
1400 if (likely(*ptr == '\r'))
1401 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1402 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 http_msg_hdr_l1_lf:
1405 case HTTP_MSG_HDR_L1_LF:
1406 EXPECT_LF_HERE(ptr, http_msg_invalid);
1407 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 http_msg_hdr_l1_lws:
1410 case HTTP_MSG_HDR_L1_LWS:
1411 if (likely(HTTP_IS_SPHT(*ptr))) {
1412 /* replace HT,CR,LF with spaces */
1413 for (; buf->data+msg->sov < ptr; msg->sov++)
1414 buf->data[msg->sov] = ' ';
1415 goto http_msg_hdr_l1_sp;
1416 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001417 /* we had a header consisting only in spaces ! */
1418 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 goto http_msg_complete_header;
1420
1421 http_msg_hdr_val:
1422 case HTTP_MSG_HDR_VAL:
1423 /* assumes msg->sol points to the first char, msg->col to the
1424 * colon, and msg->sov points to the first character of the
1425 * value.
1426 */
1427 if (likely(!HTTP_IS_CRLF(*ptr)))
1428 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 msg->eol = ptr;
1431 /* Note: we could also copy eol into ->eoh so that we have the
1432 * real header end in case it ends with lots of LWS, but is this
1433 * really needed ?
1434 */
1435 if (likely(*ptr == '\r'))
1436 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1437 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 http_msg_hdr_l2_lf:
1440 case HTTP_MSG_HDR_L2_LF:
1441 EXPECT_LF_HERE(ptr, http_msg_invalid);
1442 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 http_msg_hdr_l2_lws:
1445 case HTTP_MSG_HDR_L2_LWS:
1446 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1447 /* LWS: replace HT,CR,LF with spaces */
1448 for (; msg->eol < ptr; msg->eol++)
1449 *msg->eol = ' ';
1450 goto http_msg_hdr_val;
1451 }
1452 http_msg_complete_header:
1453 /*
1454 * It was a new header, so the last one is finished.
1455 * Assumes msg->sol points to the first char, msg->col to the
1456 * colon, msg->sov points to the first character of the value
1457 * and msg->eol to the first CR or LF so we know how the line
1458 * ends. We insert last header into the index.
1459 */
1460 /*
1461 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1462 write(2, msg->sol, msg->eol-msg->sol);
1463 fprintf(stderr,"\n");
1464 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1467 idx, idx->tail) < 0))
1468 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 msg->sol = ptr;
1471 if (likely(!HTTP_IS_CRLF(*ptr))) {
1472 goto http_msg_hdr_name;
1473 }
1474
1475 if (likely(*ptr == '\r'))
1476 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1477 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 http_msg_last_lf:
1480 case HTTP_MSG_LAST_LF:
1481 /* Assumes msg->sol points to the first of either CR or LF */
1482 EXPECT_LF_HERE(ptr, http_msg_invalid);
1483 ptr++;
1484 buf->lr = ptr;
1485 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001486 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 return;
1488#ifdef DEBUG_FULL
1489 default:
1490 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1491 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001492#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 }
1494 http_msg_ood:
1495 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 buf->lr = ptr;
1498 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 http_msg_invalid:
1501 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001502 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001503 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 return;
1505}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001506
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001507/* This function performs all the processing enabled for the current request.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001508 * It returns 1 if the processing can continue on next analysers, or zero if it
1509 * needs more data, encounters an error, or wants to immediately abort the
1510 * request. It relies on buffers flags, and updates s->req->analysers. Its
1511 * behaviour is rather simple:
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001512 * - all enabled analysers are called in turn from the lower to the higher
1513 * bit.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001514 * - the analyser must check for errors and timeouts, and react as expected.
1515 * It does not have to close anything upon error, the caller will.
1516 * - if the analyser does not have enough data, it must return 0without calling
1517 * other ones. It should also probably do a buffer_write_dis() to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001518 * that unprocessed data will not be forwarded. But that probably depends on
Willy Tarreauedcf6682008-11-30 23:15:34 +01001519 * the protocol.
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001520 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreauedcf6682008-11-30 23:15:34 +01001521 * analyser without using buffer_write_dis() (enabled by default).
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001522 * - if an analyser thinks it has no added value anymore staying here, it must
1523 * reset its bit from the analysers flags in order not to be called anymore.
1524 *
1525 * In the future, analysers should be able to indicate that they want to be
1526 * called after XXX bytes have been received (or transfered), and the min of
1527 * all's wishes will be used to ring back (unless a special condition occurs).
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001529int http_process_request(struct session *s, struct buffer *req)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530{
Willy Tarreau59234e92008-11-30 23:51:27 +01001531 /*
1532 * We will parse the partial (or complete) lines.
1533 * We will check the request syntax, and also join multi-line
1534 * headers. An index of all the lines will be elaborated while
1535 * parsing.
1536 *
1537 * For the parsing, we use a 28 states FSM.
1538 *
1539 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001540 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001541 * req->data + req->eoh = end of processed headers / start of current one
1542 * req->data + req->eol = end of current header or line (LF or CRLF)
1543 * req->lr = first non-visited byte
1544 * req->r = end of data
1545 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001546
Willy Tarreau59234e92008-11-30 23:51:27 +01001547 int cur_idx;
1548 struct http_txn *txn = &s->txn;
1549 struct http_msg *msg = &txn->req;
1550 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001551
Willy Tarreau6bf17362009-02-24 10:48:35 +01001552 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1553 now_ms, __FUNCTION__,
1554 s,
1555 req,
1556 req->rex, req->wex,
1557 req->flags,
1558 req->l,
1559 req->analysers);
1560
Willy Tarreau59234e92008-11-30 23:51:27 +01001561 if (likely(req->lr < req->r))
1562 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001563
Willy Tarreau59234e92008-11-30 23:51:27 +01001564 /* 1: we might have to print this header in debug mode */
1565 if (unlikely((global.mode & MODE_DEBUG) &&
1566 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1567 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1568 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau59234e92008-11-30 23:51:27 +01001570 sol = req->data + msg->som;
1571 eol = sol + msg->sl.rq.l;
1572 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001573
Willy Tarreau59234e92008-11-30 23:51:27 +01001574 sol += hdr_idx_first_pos(&txn->hdr_idx);
1575 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau59234e92008-11-30 23:51:27 +01001577 while (cur_idx) {
1578 eol = sol + txn->hdr_idx.v[cur_idx].len;
1579 debug_hdr("clihdr", s, sol, eol);
1580 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1581 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001583 }
1584
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau59234e92008-11-30 23:51:27 +01001586 /*
1587 * Now we quickly check if we have found a full valid request.
1588 * If not so, we check the FD and buffer states before leaving.
1589 * A full request is indicated by the fact that we have seen
1590 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1591 * requests are checked first.
1592 *
1593 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreau59234e92008-11-30 23:51:27 +01001595 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001597 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001599 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1600 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 /* 1: Since we are in header mode, if there's no space
1603 * left for headers, we won't be able to free more
1604 * later, so the session will never terminate. We
1605 * must terminate it now.
1606 */
1607 if (unlikely(req->flags & BF_FULL)) {
1608 /* FIXME: check if URI is set and return Status
1609 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001610 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001611 goto return_bad_req;
1612 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau59234e92008-11-30 23:51:27 +01001614 /* 2: have we encountered a read error ? */
1615 else if (req->flags & BF_READ_ERROR) {
1616 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001617 if (msg->err_pos >= 0)
1618 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001619 msg->msg_state = HTTP_MSG_ERROR;
1620 req->analysers = 0;
1621 s->fe->failed_req++;
1622 if (!(s->flags & SN_ERR_MASK))
1623 s->flags |= SN_ERR_CLICL;
1624 if (!(s->flags & SN_FINST_MASK))
1625 s->flags |= SN_FINST_R;
1626 return 0;
1627 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001628
Willy Tarreau59234e92008-11-30 23:51:27 +01001629 /* 3: has the read timeout expired ? */
1630 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1631 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001632 if (msg->err_pos >= 0)
1633 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 txn->status = 408;
1635 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1636 msg->msg_state = HTTP_MSG_ERROR;
1637 req->analysers = 0;
1638 s->fe->failed_req++;
1639 if (!(s->flags & SN_ERR_MASK))
1640 s->flags |= SN_ERR_CLITO;
1641 if (!(s->flags & SN_FINST_MASK))
1642 s->flags |= SN_FINST_R;
1643 return 0;
1644 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001645
Willy Tarreau59234e92008-11-30 23:51:27 +01001646 /* 4: have we encountered a close ? */
1647 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001648 if (msg->err_pos >= 0)
1649 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001650 txn->status = 400;
1651 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1652 msg->msg_state = HTTP_MSG_ERROR;
1653 req->analysers = 0;
1654 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001655
Willy Tarreau59234e92008-11-30 23:51:27 +01001656 if (!(s->flags & SN_ERR_MASK))
1657 s->flags |= SN_ERR_CLICL;
1658 if (!(s->flags & SN_FINST_MASK))
1659 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001660 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001661 }
1662
Willy Tarreau59234e92008-11-30 23:51:27 +01001663 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001664 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1665
Willy Tarreau59234e92008-11-30 23:51:27 +01001666 /* just set the request timeout once at the beginning of the request */
1667 if (!tick_isset(req->analyse_exp))
1668 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001669
Willy Tarreau59234e92008-11-30 23:51:27 +01001670 /* we're not ready yet */
1671 return 0;
1672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001673
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001674
Willy Tarreau59234e92008-11-30 23:51:27 +01001675 /****************************************************************
1676 * More interesting part now : we know that we have a complete *
1677 * request which at least looks like HTTP. We have an indicator *
1678 * of each header's length, so we can parse them quickly. *
1679 ****************************************************************/
Willy Tarreau9cdde232007-05-02 20:58:19 +02001680
Willy Tarreau4076a152009-04-02 15:18:36 +02001681 if (msg->err_pos >= 0)
1682 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1683
Willy Tarreau59234e92008-11-30 23:51:27 +01001684 req->analysers &= ~AN_REQ_HTTP_HDR;
1685 req->analyse_exp = TICK_ETERNITY;
1686
1687 /* ensure we keep this pointer to the beginning of the message */
1688 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001689
Willy Tarreau59234e92008-11-30 23:51:27 +01001690 /*
1691 * 1: identify the method
1692 */
1693 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1694
1695 /* we can make use of server redirect on GET and HEAD */
1696 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1697 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001698
Willy Tarreau59234e92008-11-30 23:51:27 +01001699 /*
1700 * 2: check if the URI matches the monitor_uri.
1701 * We have to do this for every request which gets in, because
1702 * the monitor-uri is defined by the frontend.
1703 */
1704 if (unlikely((s->fe->monitor_uri_len != 0) &&
1705 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1706 !memcmp(&req->data[msg->sl.rq.u],
1707 s->fe->monitor_uri,
1708 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001709 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001711 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001712 struct acl_cond *cond;
1713 cur_proxy = s->fe;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001714
Willy Tarreau59234e92008-11-30 23:51:27 +01001715 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001716
Willy Tarreau59234e92008-11-30 23:51:27 +01001717 /* Check if we want to fail this monitor request or not */
1718 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1719 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001720
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 ret = acl_pass(ret);
1722 if (cond->pol == ACL_COND_UNLESS)
1723 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001724
Willy Tarreau59234e92008-11-30 23:51:27 +01001725 if (ret) {
1726 /* we fail this request, let's return 503 service unavail */
1727 txn->status = 503;
1728 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1729 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001730 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001732
Willy Tarreau59234e92008-11-30 23:51:27 +01001733 /* nothing to fail, let's reply normaly */
1734 txn->status = 200;
1735 stream_int_retnclose(req->prod, &http_200_chunk);
1736 goto return_prx_cond;
1737 }
1738
1739 /*
1740 * 3: Maybe we have to copy the original REQURI for the logs ?
1741 * Note: we cannot log anymore if the request has been
1742 * classified as invalid.
1743 */
1744 if (unlikely(s->logs.logwait & LW_REQ)) {
1745 /* we have a complete HTTP request that we must log */
1746 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1747 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748
Willy Tarreau59234e92008-11-30 23:51:27 +01001749 if (urilen >= REQURI_LEN)
1750 urilen = REQURI_LEN - 1;
1751 memcpy(txn->uri, &req->data[msg->som], urilen);
1752 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 if (!(s->logs.logwait &= ~LW_REQ))
1755 s->do_log(s);
1756 } else {
1757 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001759 }
Willy Tarreau06619262006-12-17 08:37:22 +01001760
Willy Tarreau59234e92008-11-30 23:51:27 +01001761 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1762 if (unlikely(msg->sl.rq.v_l == 0)) {
1763 int delta;
1764 char *cur_end;
1765 msg->sol = req->data + msg->som;
1766 cur_end = msg->sol + msg->sl.rq.l;
1767 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001768
Willy Tarreau59234e92008-11-30 23:51:27 +01001769 if (msg->sl.rq.u_l == 0) {
1770 /* if no URI was set, add "/" */
1771 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001773 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001774 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001775 /* add HTTP version */
1776 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1777 msg->eoh += delta;
1778 cur_end += delta;
1779 cur_end = (char *)http_parse_reqline(msg, req->data,
1780 HTTP_MSG_RQMETH,
1781 msg->sol, cur_end + 1,
1782 NULL, NULL);
1783 if (unlikely(!cur_end))
1784 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785
Willy Tarreau59234e92008-11-30 23:51:27 +01001786 /* we have a full HTTP/1.0 request now and we know that
1787 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001789 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1790 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
1792
Willy Tarreau59234e92008-11-30 23:51:27 +01001793 /* 5: we may need to capture headers */
1794 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1795 capture_headers(req->data + msg->som, &txn->hdr_idx,
1796 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001797
Willy Tarreau59234e92008-11-30 23:51:27 +01001798 /*
1799 * 6: we will have to evaluate the filters.
1800 * As opposed to version 1.2, now they will be evaluated in the
1801 * filters order and not in the header order. This means that
1802 * each filter has to be validated among all headers.
1803 *
1804 * We can now check whether we want to switch to another
1805 * backend, in which case we will re-check the backend's
1806 * filters and various options. In order to support 3-level
1807 * switching, here's how we should proceed :
1808 *
1809 * a) run be.
1810 * if (switch) then switch ->be to the new backend.
1811 * b) run be if (be != fe).
1812 * There cannot be any switch from there, so ->be cannot be
1813 * changed anymore.
1814 *
1815 * => filters always apply to ->be, then ->be may change.
1816 *
1817 * The response path will be able to apply either ->be, or
1818 * ->be then ->fe filters in order to match the reverse of
1819 * the forward sequence.
1820 */
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001821
Willy Tarreau59234e92008-11-30 23:51:27 +01001822 do {
1823 struct acl_cond *cond;
1824 struct redirect_rule *rule;
1825 struct proxy *rule_set = s->be;
1826 cur_proxy = s->be;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001827
Willy Tarreau59234e92008-11-30 23:51:27 +01001828 /* first check whether we have some ACLs set to redirect this request */
1829 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1830 int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001831
Willy Tarreau59234e92008-11-30 23:51:27 +01001832 ret = acl_pass(ret);
1833 if (rule->cond->pol == ACL_COND_UNLESS)
1834 ret = !ret;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001835
Willy Tarreau59234e92008-11-30 23:51:27 +01001836 if (ret) {
1837 struct chunk rdr = { trash, 0 };
1838 const char *msg_fmt;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001839
Willy Tarreau59234e92008-11-30 23:51:27 +01001840 /* build redirect message */
1841 switch(rule->code) {
1842 case 303:
1843 rdr.len = strlen(HTTP_303);
1844 msg_fmt = HTTP_303;
1845 break;
1846 case 301:
1847 rdr.len = strlen(HTTP_301);
1848 msg_fmt = HTTP_301;
1849 break;
1850 case 302:
1851 default:
1852 rdr.len = strlen(HTTP_302);
1853 msg_fmt = HTTP_302;
1854 break;
1855 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001856
Willy Tarreau59234e92008-11-30 23:51:27 +01001857 if (unlikely(rdr.len > sizeof(trash)))
1858 goto return_bad_req;
1859 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001860
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 switch(rule->type) {
1862 case REDIRECT_TYPE_PREFIX: {
1863 const char *path;
1864 int pathlen;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001865
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 path = http_get_path(txn);
1867 /* build message using path */
1868 if (path) {
1869 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Willy Tarreau79da4692008-11-19 20:03:04 +01001870 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1871 int qs = 0;
1872 while (qs < pathlen) {
1873 if (path[qs] == '?') {
1874 pathlen = qs;
1875 break;
1876 }
1877 qs++;
1878 }
1879 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001880 } else {
1881 path = "/";
1882 pathlen = 1;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001883 }
1884
Willy Tarreau59234e92008-11-30 23:51:27 +01001885 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1886 goto return_bad_req;
1887
Willy Tarreaufe651a52008-11-19 21:15:17 +01001888 /* add prefix. Note that if prefix == "/", we don't want to
1889 * add anything, otherwise it makes it hard for the user to
1890 * configure a self-redirection.
1891 */
1892 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
1893 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1894 rdr.len += rule->rdr_len;
1895 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001896
Willy Tarreau59234e92008-11-30 23:51:27 +01001897 /* add path */
1898 memcpy(rdr.str + rdr.len, path, pathlen);
1899 rdr.len += pathlen;
1900 break;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001901 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001902 case REDIRECT_TYPE_LOCATION:
1903 default:
1904 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1905 goto return_bad_req;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001906
Willy Tarreau59234e92008-11-30 23:51:27 +01001907 /* add location */
1908 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1909 rdr.len += rule->rdr_len;
1910 break;
1911 }
Willy Tarreau11382812008-07-09 16:18:21 +02001912
Willy Tarreau0140f252008-11-19 21:07:09 +01001913 if (rule->cookie_len) {
1914 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
1915 rdr.len += 14;
1916 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
1917 rdr.len += rule->cookie_len;
1918 memcpy(rdr.str + rdr.len, "\r\n", 2);
1919 rdr.len += 2;
1920 }
1921
Willy Tarreau59234e92008-11-30 23:51:27 +01001922 /* add end of headers */
1923 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1924 rdr.len += 4;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001925
Willy Tarreau59234e92008-11-30 23:51:27 +01001926 txn->status = rule->code;
1927 /* let's log the request time */
1928 s->logs.tv_request = now;
1929 stream_int_retnclose(req->prod, &rdr);
1930 goto return_prx_cond;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001931 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001932 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001933
Willy Tarreau59234e92008-11-30 23:51:27 +01001934 /* first check whether we have some ACLs set to block this request */
1935 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1936 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau53b6c742006-12-17 13:37:46 +01001937
Willy Tarreau59234e92008-11-30 23:51:27 +01001938 ret = acl_pass(ret);
1939 if (cond->pol == ACL_COND_UNLESS)
1940 ret = !ret;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001941
Willy Tarreau59234e92008-11-30 23:51:27 +01001942 if (ret) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001943 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001944 /* let's log the request time */
Willy Tarreau59234e92008-11-30 23:51:27 +01001945 s->logs.tv_request = now;
1946 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001947 goto return_prx_cond;
1948 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001949 }
1950
1951 /* try headers filters */
1952 if (rule_set->req_exp != NULL) {
1953 if (apply_filters_to_request(s, req, rule_set->req_exp) < 0)
1954 goto return_bad_req;
1955 }
1956
1957 if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) {
1958 /* to ensure correct connection accounting on
1959 * the backend, we count the connection for the
1960 * one managing the queue.
1961 */
1962 s->be->beconn++;
1963 if (s->be->beconn > s->be->beconn_max)
1964 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01001965 proxy_inc_be_ctr(s->be);
Willy Tarreau59234e92008-11-30 23:51:27 +01001966 s->flags |= SN_BE_ASSIGNED;
1967 }
Willy Tarreau06619262006-12-17 08:37:22 +01001968
Willy Tarreau59234e92008-11-30 23:51:27 +01001969 /* has the request been denied ? */
1970 if (txn->flags & TX_CLDENY) {
1971 /* no need to go further */
1972 txn->status = 403;
1973 /* let's log the request time */
1974 s->logs.tv_request = now;
1975 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1976 goto return_prx_cond;
1977 }
Willy Tarreau06619262006-12-17 08:37:22 +01001978
Willy Tarreau59234e92008-11-30 23:51:27 +01001979 /* We might have to check for "Connection:" */
1980 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1981 !(s->flags & SN_CONN_CLOSED)) {
1982 char *cur_ptr, *cur_end, *cur_next;
1983 int cur_idx, old_idx, delta, val;
1984 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985
Willy Tarreau59234e92008-11-30 23:51:27 +01001986 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1987 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988
Willy Tarreau59234e92008-11-30 23:51:27 +01001989 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1990 cur_hdr = &txn->hdr_idx.v[cur_idx];
1991 cur_ptr = cur_next;
1992 cur_end = cur_ptr + cur_hdr->len;
1993 cur_next = cur_end + cur_hdr->cr + 1;
1994
1995 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1996 if (val) {
1997 /* 3 possibilities :
1998 * - we have already set Connection: close,
1999 * so we remove this line.
2000 * - we have not yet set Connection: close,
2001 * but this line indicates close. We leave
2002 * it untouched and set the flag.
2003 * - we have not yet set Connection: close,
2004 * and this line indicates non-close. We
2005 * replace it.
2006 */
2007 if (s->flags & SN_CONN_CLOSED) {
2008 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2009 txn->req.eoh += delta;
2010 cur_next += delta;
2011 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2012 txn->hdr_idx.used--;
2013 cur_hdr->len = 0;
2014 } else {
2015 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2016 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2017 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002018 cur_next += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01002019 cur_hdr->len += delta;
2020 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002021 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002022 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002023 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002024 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 old_idx = cur_idx;
Willy Tarreau06619262006-12-17 08:37:22 +01002026 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 }
2028 /* add request headers from the rule sets in the same order */
2029 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2030 if (unlikely(http_header_add_tail(req,
2031 &txn->req,
2032 &txn->hdr_idx,
2033 rule_set->req_add[cur_idx])) < 0)
2034 goto return_bad_req;
2035 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002036
Willy Tarreau59234e92008-11-30 23:51:27 +01002037 /* check if stats URI was requested, and if an auth is needed */
2038 if (rule_set->uri_auth != NULL &&
2039 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2040 /* we have to check the URI and auth for this request.
2041 * FIXME!!! that one is rather dangerous, we want to
2042 * make it follow standard rules (eg: clear req->analysers).
2043 */
2044 if (stats_check_uri_auth(s, rule_set)) {
2045 req->analysers = 0;
2046 return 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01002047 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002048 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002049
Willy Tarreau59234e92008-11-30 23:51:27 +01002050 /* now check whether we have some switching rules for this request */
2051 if (!(s->flags & SN_BE_ASSIGNED)) {
2052 struct switching_rule *rule;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002053
Willy Tarreau59234e92008-11-30 23:51:27 +01002054 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2055 int ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002056
Willy Tarreau59234e92008-11-30 23:51:27 +01002057 ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002058
Willy Tarreau59234e92008-11-30 23:51:27 +01002059 ret = acl_pass(ret);
2060 if (rule->cond->pol == ACL_COND_UNLESS)
2061 ret = !ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002062
Willy Tarreau59234e92008-11-30 23:51:27 +01002063 if (ret) {
2064 s->be = rule->be.backend;
2065 s->be->beconn++;
2066 if (s->be->beconn > s->be->beconn_max)
2067 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002068 proxy_inc_be_ctr(s->be);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002069
Willy Tarreau59234e92008-11-30 23:51:27 +01002070 /* assign new parameters to the session from the new backend */
2071 s->rep->rto = s->req->wto = s->be->timeout.server;
2072 s->req->cto = s->be->timeout.connect;
2073 s->conn_retries = s->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02002074 if (s->be->options2 & PR_O2_RSPBUG_OK)
2075 s->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau59234e92008-11-30 23:51:27 +01002076 s->flags |= SN_BE_ASSIGNED;
2077 break;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002078 }
2079 }
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002080 }
2081
Willy Tarreau59234e92008-11-30 23:51:27 +01002082 if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2083 /* No backend was set, but there was a default
2084 * backend set in the frontend, so we use it and
2085 * loop again.
2086 */
2087 s->be = cur_proxy->defbe.be;
2088 s->be->beconn++;
2089 if (s->be->beconn > s->be->beconn_max)
2090 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002091 proxy_inc_be_ctr(s->be);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002092
Willy Tarreau59234e92008-11-30 23:51:27 +01002093 /* assign new parameters to the session from the new backend */
2094 s->rep->rto = s->req->wto = s->be->timeout.server;
2095 s->req->cto = s->be->timeout.connect;
2096 s->conn_retries = s->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02002097 if (s->be->options2 & PR_O2_RSPBUG_OK)
2098 s->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 s->flags |= SN_BE_ASSIGNED;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002100 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 } while (s->be != cur_proxy); /* we loop only if s->be has changed */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002102
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 if (!(s->flags & SN_BE_ASSIGNED)) {
2104 /* To ensure correct connection accounting on
2105 * the backend, we count the connection for the
2106 * one managing the queue.
Willy Tarreau06619262006-12-17 08:37:22 +01002107 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002108 s->be->beconn++;
2109 if (s->be->beconn > s->be->beconn_max)
2110 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002111 proxy_inc_be_ctr(s->be);
Willy Tarreau59234e92008-11-30 23:51:27 +01002112 s->flags |= SN_BE_ASSIGNED;
2113 }
Willy Tarreau06619262006-12-17 08:37:22 +01002114
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 /*
2116 * Right now, we know that we have processed the entire headers
2117 * and that unwanted requests have been filtered out. We can do
2118 * whatever we want with the remaining request. Also, now we
2119 * may have separate values for ->fe, ->be.
2120 */
Willy Tarreau06619262006-12-17 08:37:22 +01002121
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 /*
2123 * If HTTP PROXY is set we simply get remote server address
2124 * parsing incoming request.
2125 */
2126 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2127 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2128 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 /*
2131 * 7: the appsession cookie was looked up very early in 1.2,
2132 * so let's do the same now.
2133 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002134
Willy Tarreau59234e92008-11-30 23:51:27 +01002135 /* It needs to look into the URI */
2136 if (s->be->appsession_name) {
2137 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2138 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002139
Willy Tarreau59234e92008-11-30 23:51:27 +01002140 /*
2141 * 8: Now we can work with the cookies.
2142 * Note that doing so might move headers in the request, but
2143 * the fields will stay coherent and the URI will not move.
2144 * This should only be performed in the backend.
2145 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002146 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2148 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002149
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 /*
2151 * 9: add X-Forwarded-For if either the frontend or the backend
2152 * asks for it.
2153 */
2154 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2155 if (s->cli_addr.ss_family == AF_INET) {
2156 /* Add an X-Forwarded-For header unless the source IP is
2157 * in the 'except' network range.
2158 */
2159 if ((!s->fe->except_mask.s_addr ||
2160 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2161 != s->fe->except_net.s_addr) &&
2162 (!s->be->except_mask.s_addr ||
2163 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2164 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002165 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 unsigned char *pn;
2167 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002168
2169 /* Note: we rely on the backend to get the header name to be used for
2170 * x-forwarded-for, because the header is really meant for the backends.
2171 * However, if the backend did not specify any option, we have to rely
2172 * on the frontend's header name.
2173 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 if (s->be->fwdfor_hdr_len) {
2175 len = s->be->fwdfor_hdr_len;
2176 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002177 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 len = s->fe->fwdfor_hdr_len;
2179 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2180 }
2181 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002182
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002183 if (unlikely(http_header_add_tail2(req, &txn->req,
2184 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002185 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002186 }
2187 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 else if (s->cli_addr.ss_family == AF_INET6) {
2189 /* FIXME: for the sake of completeness, we should also support
2190 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002191 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 int len;
2193 char pn[INET6_ADDRSTRLEN];
2194 inet_ntop(AF_INET6,
2195 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2196 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 /* Note: we rely on the backend to get the header name to be used for
2199 * x-forwarded-for, because the header is really meant for the backends.
2200 * However, if the backend did not specify any option, we have to rely
2201 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002202 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 if (s->be->fwdfor_hdr_len) {
2204 len = s->be->fwdfor_hdr_len;
2205 memcpy(trash, s->be->fwdfor_hdr_name, len);
2206 } else {
2207 len = s->fe->fwdfor_hdr_len;
2208 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002209 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002211
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 if (unlikely(http_header_add_tail2(req, &txn->req,
2213 &txn->hdr_idx, trash, len)) < 0)
2214 goto return_bad_req;
2215 }
2216 }
2217
2218 /*
2219 * 10: add "Connection: close" if needed and not yet set.
2220 * Note that we do not need to add it in case of HTTP/1.0.
2221 */
2222 if (!(s->flags & SN_CONN_CLOSED) &&
2223 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2224 if ((unlikely(msg->sl.rq.v_l != 8) ||
2225 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2226 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2227 "Connection: close", 17)) < 0)
2228 goto return_bad_req;
2229 s->flags |= SN_CONN_CLOSED;
2230 }
2231 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2232 * If not assigned, perhaps we are balancing on url_param, but this is a
2233 * POST; and the parameters are in the body, maybe scan there to find our server.
2234 * (unless headers overflowed the buffer?)
2235 */
2236 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2237 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2238 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2239 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2240 /* are there enough bytes here? total == l || r || rlim ?
2241 * len is unsigned, but eoh is int,
2242 * how many bytes of body have we received?
2243 * eoh is the first empty line of the header
2244 */
2245 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2246 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2247
2248 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2249 * We can't assume responsibility for the server's decision,
2250 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2251 * We also can't change our mind later, about which server to choose, so round robin.
2252 */
2253 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2254 struct hdr_ctx ctx;
2255 ctx.idx = 0;
2256 /* Expect is allowed in 1.1, look for it */
2257 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2258 if (ctx.idx != 0 &&
2259 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2260 /* We can't reliablly stall and wait for data, because of
2261 * .NET clients that don't conform to rfc2616; so, no need for
2262 * the next block to check length expectations.
2263 * We could send 100 status back to the client, but then we need to
2264 * re-write headers, and send the message. And this isn't the right
2265 * place for that action.
2266 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002267 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 goto end_check_maybe_wait_for_body;
2269 }
2270
2271 if (likely(len > s->be->url_param_post_limit)) {
2272 /* nothing to do, we got enough */
2273 } else {
2274 /* limit implies we are supposed to need this many bytes
2275 * to find the parameter. Let's see how many bytes we can wait for.
2276 */
2277 long long hint = len;
2278 struct hdr_ctx ctx;
2279 ctx.idx = 0;
2280 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2281 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2282 buffer_write_dis(req);
2283 req->analysers |= AN_REQ_HTTP_BODY;
2284 }
2285 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002286 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2288 /* now if we have a length, we'll take the hint */
2289 if (ctx.idx) {
2290 /* We have Content-Length */
2291 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2292 hint = 0; /* parse failure, untrusted client */
2293 else {
2294 if (hint > 0)
2295 msg->hdr_content_len = hint;
2296 else
2297 hint = 0; /* bad client, sent negative length */
2298 }
2299 }
2300 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2301 if (s->be->url_param_post_limit < hint)
2302 hint = s->be->url_param_post_limit;
2303 /* now do we really need to buffer more data? */
2304 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002305 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002306 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002307 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002309 }
2310 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 }
2312 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002313
Willy Tarreau59234e92008-11-30 23:51:27 +01002314 /*************************************************************
2315 * OK, that's finished for the headers. We have done what we *
2316 * could. Let's switch to the DATA state. *
2317 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2320 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 /* When a connection is tarpitted, we use the tarpit timeout,
2323 * which may be the same as the connect timeout if unspecified.
2324 * If unset, then set it to zero because we really want it to
2325 * eventually expire. We build the tarpit as an analyser.
2326 */
2327 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002328 buffer_erase(s->req);
2329 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002330 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002331 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 buffer_write_dis(req);
2333 req->analysers |= AN_REQ_HTTP_TARPIT;
2334 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2335 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002336 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /* OK let's go on with the BODY now */
2340 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002341
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002343 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002344 /* we detected a parsing error. We want to archive this request
2345 * in the dedicated proxy area for later troubleshooting.
2346 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002347 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002348 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 txn->req.msg_state = HTTP_MSG_ERROR;
2351 txn->status = 400;
2352 req->analysers = 0;
2353 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2354 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 return_prx_cond:
2357 if (!(s->flags & SN_ERR_MASK))
2358 s->flags |= SN_ERR_PRXCOND;
2359 if (!(s->flags & SN_FINST_MASK))
2360 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002361 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002362}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002363
Willy Tarreau60b85b02008-11-30 23:28:40 +01002364/* This function is an analyser which processes the HTTP tarpit. It always
2365 * returns zero, at the beginning because it prevents any other processing
2366 * from occurring, and at the end because it terminates the request.
2367 */
2368int http_process_tarpit(struct session *s, struct buffer *req)
2369{
2370 struct http_txn *txn = &s->txn;
2371
2372 /* This connection is being tarpitted. The CLIENT side has
2373 * already set the connect expiration date to the right
2374 * timeout. We just have to check that the client is still
2375 * there and that the timeout has not expired.
2376 */
2377 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2378 !tick_is_expired(req->analyse_exp, now_ms))
2379 return 0;
2380
2381 /* We will set the queue timer to the time spent, just for
2382 * logging purposes. We fake a 500 server error, so that the
2383 * attacker will not suspect his connection has been tarpitted.
2384 * It will not cause trouble to the logs because we can exclude
2385 * the tarpitted connections by filtering on the 'PT' status flags.
2386 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002387 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2388
2389 txn->status = 500;
2390 if (req->flags != BF_READ_ERROR)
2391 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2392
2393 req->analysers = 0;
2394 req->analyse_exp = TICK_ETERNITY;
2395
2396 s->fe->failed_req++;
2397 if (!(s->flags & SN_ERR_MASK))
2398 s->flags |= SN_ERR_PRXCOND;
2399 if (!(s->flags & SN_FINST_MASK))
2400 s->flags |= SN_FINST_T;
2401 return 0;
2402}
2403
Willy Tarreaud34af782008-11-30 23:36:37 +01002404/* This function is an analyser which processes the HTTP request body. It looks
2405 * for parameters to be used for the load balancing algorithm (url_param). It
2406 * must only be called after the standard HTTP request processing has occurred,
2407 * because it expects the request to be parsed. It returns zero if it needs to
2408 * read more data, or 1 once it has completed its analysis.
2409 */
2410int http_process_request_body(struct session *s, struct buffer *req)
2411{
2412 struct http_msg *msg = &s->txn.req;
2413 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2414 long long limit = s->be->url_param_post_limit;
2415 struct hdr_ctx ctx;
2416
2417 /* We have to parse the HTTP request body to find any required data.
2418 * "balance url_param check_post" should have been the only way to get
2419 * into this. We were brought here after HTTP header analysis, so all
2420 * related structures are ready.
2421 */
2422
2423 ctx.idx = 0;
2424
2425 /* now if we have a length, we'll take the hint */
2426 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2427 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2428 unsigned int chunk = 0;
2429 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2430 char c = msg->sol[body];
2431 if (ishex(c)) {
2432 unsigned int hex = toupper(c) - '0';
2433 if (hex > 9)
2434 hex -= 'A' - '9' - 1;
2435 chunk = (chunk << 4) | hex;
2436 } else
2437 break;
2438 body++;
2439 }
2440 if (body + 2 >= req->l) /* we want CRLF too */
2441 goto http_body_end; /* end of buffer? data missing! */
2442
2443 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2444 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2445
2446 body += 2; // skip CRLF
2447
2448 /* if we support more then one chunk here, we have to do it again when assigning server
2449 * 1. how much entity data do we have? new var
2450 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2451 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2452 */
2453
2454 if (chunk < limit)
2455 limit = chunk; /* only reading one chunk */
2456 } else {
2457 if (msg->hdr_content_len < limit)
2458 limit = msg->hdr_content_len;
2459 }
2460
2461 http_body_end:
2462 /* we leave once we know we have nothing left to do. This means that we have
2463 * enough bytes, or that we know we'll not get any more (buffer full, read
2464 * buffer closed).
2465 */
2466 if (req->l - body >= limit || /* enough bytes! */
2467 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2468 tick_is_expired(req->analyse_exp, now_ms)) {
2469 /* The situation will not evolve, so let's give up on the analysis. */
2470 s->logs.tv_request = now; /* update the request timer to reflect full request */
2471 req->analysers &= ~AN_REQ_HTTP_BODY;
2472 req->analyse_exp = TICK_ETERNITY;
2473 return 1;
2474 }
2475 else {
2476 /* Not enough data. We'll re-use the http-request
2477 * timeout here. Ideally, we should set the timeout
2478 * relative to the accept() date. We just set the
2479 * request timeout once at the beginning of the
2480 * request.
2481 */
2482 buffer_write_dis(req);
2483 if (!tick_isset(req->analyse_exp))
2484 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2485 return 0;
2486 }
2487}
2488
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002489/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002490 * It normally returns zero, but may return 1 if it absolutely needs to be
2491 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002492 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002493 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002494 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002495int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002496{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002497 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002498 struct buffer *req = t->req;
2499 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002500
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002501 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002502 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002503 t,
2504 rep,
2505 rep->rex, rep->wex,
2506 rep->flags,
2507 rep->l,
2508 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002509
Willy Tarreau2df28e82008-08-17 15:20:19 +02002510 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002511 /*
2512 * Now parse the partial (or complete) lines.
2513 * We will check the response syntax, and also join multi-line
2514 * headers. An index of all the lines will be elaborated while
2515 * parsing.
2516 *
2517 * For the parsing, we use a 28 states FSM.
2518 *
2519 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002520 * rep->data + rep->som = beginning of response
2521 * rep->data + rep->eoh = end of processed headers / start of current one
2522 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002523 * rep->lr = first non-visited byte
2524 * rep->r = end of data
2525 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002526
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002527 int cur_idx;
2528 struct http_msg *msg = &txn->rsp;
2529 struct proxy *cur_proxy;
2530
2531 if (likely(rep->lr < rep->r))
2532 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2533
2534 /* 1: we might have to print this header in debug mode */
2535 if (unlikely((global.mode & MODE_DEBUG) &&
2536 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2537 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2538 char *eol, *sol;
2539
2540 sol = rep->data + msg->som;
2541 eol = sol + msg->sl.rq.l;
2542 debug_hdr("srvrep", t, sol, eol);
2543
2544 sol += hdr_idx_first_pos(&txn->hdr_idx);
2545 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2546
2547 while (cur_idx) {
2548 eol = sol + txn->hdr_idx.v[cur_idx].len;
2549 debug_hdr("srvhdr", t, sol, eol);
2550 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2551 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002552 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002553 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002554
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002555 /*
2556 * Now we quickly check if we have found a full valid response.
2557 * If not so, we check the FD and buffer states before leaving.
2558 * A full response is indicated by the fact that we have seen
2559 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2560 * responses are checked first.
2561 *
2562 * Depending on whether the client is still there or not, we
2563 * may send an error response back or not. Note that normally
2564 * we should only check for HTTP status there, and check I/O
2565 * errors somewhere else.
2566 */
2567
2568 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002569 /* Invalid response */
2570 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002571 /* we detected a parsing error. We want to archive this response
2572 * in the dedicated proxy area for later troubleshooting.
2573 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002574 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002575 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2576 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2577
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002578 buffer_shutr_now(rep);
2579 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002580 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002581 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002582 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002583 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002584 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002585 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002586 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002587 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002588 if (!(t->flags & SN_FINST_MASK))
2589 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002590
Willy Tarreaudafde432008-08-17 01:00:46 +02002591 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002593 /* too large response does not fit in buffer. */
2594 else if (rep->flags & BF_FULL) {
2595 goto hdr_response_bad;
2596 }
2597 /* read error */
2598 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002599 if (msg->err_pos >= 0)
2600 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002601 buffer_shutr_now(rep);
2602 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002603 if (t->srv)
2604 t->srv->failed_resp++;
2605 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002606 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002607 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002608 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002609 if (!(t->flags & SN_ERR_MASK))
2610 t->flags |= SN_ERR_SRVCL;
2611 if (!(t->flags & SN_FINST_MASK))
2612 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002613 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002614 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002615 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002616 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002617 if (msg->err_pos >= 0)
2618 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002619 buffer_shutr_now(rep);
2620 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002621 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002622 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002623 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002624 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002625 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002626 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002627 if (!(t->flags & SN_ERR_MASK))
2628 t->flags |= SN_ERR_SRVTO;
2629 if (!(t->flags & SN_FINST_MASK))
2630 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002631 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002632 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002633 /* close from server */
2634 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002635 if (msg->err_pos >= 0)
2636 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002637 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002638 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002639 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002640 t->be->failed_resp++;
2641 rep->analysers = 0;
2642 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002643 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002644 if (!(t->flags & SN_ERR_MASK))
2645 t->flags |= SN_ERR_SRVCL;
2646 if (!(t->flags & SN_FINST_MASK))
2647 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002648 return 0;
2649 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002650 /* write error to client (we don't send any message then) */
2651 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002652 if (msg->err_pos >= 0)
2653 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002654 buffer_shutr_now(rep);
2655 t->be->failed_resp++;
2656 rep->analysers = 0;
2657 if (!(t->flags & SN_ERR_MASK))
2658 t->flags |= SN_ERR_CLICL;
2659 if (!(t->flags & SN_FINST_MASK))
2660 t->flags |= SN_FINST_H;
2661 return 0;
2662 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002663 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002664 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002665 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002666
Willy Tarreau21d2af32008-02-14 20:25:24 +01002667
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002668 /*****************************************************************
2669 * More interesting part now : we know that we have a complete *
2670 * response which at least looks like HTTP. We have an indicator *
2671 * of each header's length, so we can parse them quickly. *
2672 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002673
Willy Tarreau4076a152009-04-02 15:18:36 +02002674 if (msg->err_pos >= 0)
2675 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2676
Willy Tarreau2df28e82008-08-17 15:20:19 +02002677 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002678
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002679 /* ensure we keep this pointer to the beginning of the message */
2680 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002682 /*
2683 * 1: get the status code and check for cacheability.
2684 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002685
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002686 t->logs.logwait &= ~LW_RESP;
2687 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002688
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002689 switch (txn->status) {
2690 case 200:
2691 case 203:
2692 case 206:
2693 case 300:
2694 case 301:
2695 case 410:
2696 /* RFC2616 @13.4:
2697 * "A response received with a status code of
2698 * 200, 203, 206, 300, 301 or 410 MAY be stored
2699 * by a cache (...) unless a cache-control
2700 * directive prohibits caching."
2701 *
2702 * RFC2616 @9.5: POST method :
2703 * "Responses to this method are not cacheable,
2704 * unless the response includes appropriate
2705 * Cache-Control or Expires header fields."
2706 */
2707 if (likely(txn->meth != HTTP_METH_POST) &&
2708 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2709 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2710 break;
2711 default:
2712 break;
2713 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002714
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002715 /*
2716 * 2: we may need to capture headers
2717 */
2718 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2719 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2720 txn->rsp.cap, t->fe->rsp_cap);
2721
2722 /*
2723 * 3: we will have to evaluate the filters.
2724 * As opposed to version 1.2, now they will be evaluated in the
2725 * filters order and not in the header order. This means that
2726 * each filter has to be validated among all headers.
2727 *
2728 * Filters are tried with ->be first, then with ->fe if it is
2729 * different from ->be.
2730 */
2731
2732 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2733
2734 cur_proxy = t->be;
2735 while (1) {
2736 struct proxy *rule_set = cur_proxy;
2737
2738 /* try headers filters */
2739 if (rule_set->rsp_exp != NULL) {
2740 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2741 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01002742 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002743 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002744 cur_proxy->failed_resp++;
2745 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002746 buffer_shutr_now(rep);
2747 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002748 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002749 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002750 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002751 if (!(t->flags & SN_ERR_MASK))
2752 t->flags |= SN_ERR_PRXCOND;
2753 if (!(t->flags & SN_FINST_MASK))
2754 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002755 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002756 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002757 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002758
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002759 /* has the response been denied ? */
2760 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01002761 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002762 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 cur_proxy->denied_resp++;
2764 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002765 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002767 /* We might have to check for "Connection:" */
2768 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2769 !(t->flags & SN_CONN_CLOSED)) {
2770 char *cur_ptr, *cur_end, *cur_next;
2771 int cur_idx, old_idx, delta, val;
2772 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002773
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002774 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2775 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002776
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002777 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2778 cur_hdr = &txn->hdr_idx.v[cur_idx];
2779 cur_ptr = cur_next;
2780 cur_end = cur_ptr + cur_hdr->len;
2781 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002782
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002783 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2784 if (val) {
2785 /* 3 possibilities :
2786 * - we have already set Connection: close,
2787 * so we remove this line.
2788 * - we have not yet set Connection: close,
2789 * but this line indicates close. We leave
2790 * it untouched and set the flag.
2791 * - we have not yet set Connection: close,
2792 * and this line indicates non-close. We
2793 * replace it.
2794 */
2795 if (t->flags & SN_CONN_CLOSED) {
2796 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2797 txn->rsp.eoh += delta;
2798 cur_next += delta;
2799 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2800 txn->hdr_idx.used--;
2801 cur_hdr->len = 0;
2802 } else {
2803 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2804 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2805 "close", 5);
2806 cur_next += delta;
2807 cur_hdr->len += delta;
2808 txn->rsp.eoh += delta;
2809 }
2810 t->flags |= SN_CONN_CLOSED;
2811 }
2812 }
2813 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002814 }
2815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002817 /* add response headers from the rule sets in the same order */
2818 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2819 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2820 rule_set->rsp_add[cur_idx])) < 0)
2821 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002822 }
2823
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002824 /* check whether we're already working on the frontend */
2825 if (cur_proxy == t->fe)
2826 break;
2827 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002828 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002829
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002830 /*
2831 * 4: check for server cookie.
2832 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002833 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002834 || (t->be->options & PR_O_CHK_CACHE))
2835 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002836
Willy Tarreaubaaee002006-06-26 02:48:02 +02002837
Willy Tarreaua15645d2007-03-18 16:22:39 +01002838 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002839 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002840 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002841 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2842 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002844 /*
2845 * 6: add server cookie in the response if needed
2846 */
2847 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2848 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2849 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002850
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002851 /* the server is known, it's not the one the client requested, we have to
2852 * insert a set-cookie here, except if we want to insert only on POST
2853 * requests and this one isn't. Note that servers which don't have cookies
2854 * (eg: some backup servers) will return a full cookie removal request.
2855 */
2856 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2857 t->be->cookie_name,
2858 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002860 if (t->be->cookie_domain)
2861 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2864 trash, len)) < 0)
2865 goto return_bad_resp;
2866 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002867
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002868 /* Here, we will tell an eventual cache on the client side that we don't
2869 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2870 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2871 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2872 */
2873 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002875 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2876
2877 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2878 "Cache-control: private", 22)) < 0)
2879 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880 }
2881 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002882
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002884 /*
2885 * 7: check if result will be cacheable with a cookie.
2886 * We'll block the response if security checks have caught
2887 * nasty things such as a cacheable cookie.
2888 */
2889 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2890 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2891 (t->be->options & PR_O_CHK_CACHE)) {
2892
2893 /* we're in presence of a cacheable response containing
2894 * a set-cookie header. We'll block it as requested by
2895 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002896 */
Willy Tarreau8365f932009-03-15 23:11:49 +01002897 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002898 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002899 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002900
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002901 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2902 t->be->id, t->srv?t->srv->id:"<dispatch>");
2903 send_log(t->be, LOG_ALERT,
2904 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2905 t->be->id, t->srv?t->srv->id:"<dispatch>");
2906 goto return_srv_prx_502;
2907 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908
2909 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002910 * 8: add "Connection: close" if needed and not yet set.
2911 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002912 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002913 if (!(t->flags & SN_CONN_CLOSED) &&
2914 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2915 if ((unlikely(msg->sl.st.v_l != 8) ||
2916 unlikely(req->data[msg->som + 7] != '0')) &&
2917 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2918 "Connection: close", 17)) < 0)
2919 goto return_bad_resp;
2920 t->flags |= SN_CONN_CLOSED;
2921 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002922
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 /*************************************************************
2924 * OK, that's finished for the headers. We have done what we *
2925 * could. Let's switch to the DATA state. *
2926 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927
Willy Tarreaue393fe22008-08-16 22:18:07 +02002928 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002929 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002930
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931#ifdef CONFIG_HAP_TCPSPLICE
2932 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2933 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002934 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 }
2936#endif
2937 /* if the user wants to log as soon as possible, without counting
2938 * bytes from the server, then this is the right moment. We have
2939 * to temporarily assign bytes_out to log what we currently have.
2940 */
2941 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2942 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2943 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002944 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002945 t->logs.bytes_out = 0;
2946 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 /* Note: we must not try to cheat by jumping directly to DATA,
2949 * otherwise we would not let the client side wake up.
2950 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002951
Willy Tarreaudafde432008-08-17 01:00:46 +02002952 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002954
Willy Tarreau2df28e82008-08-17 15:20:19 +02002955 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2956 * probably reduce one day's debugging session.
2957 */
2958#ifdef DEBUG_DEV
2959 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
2960 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2961 __FILE__, __LINE__, rep->analysers);
2962 ABORT_NOW();
2963 }
2964#endif
2965 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002966 return 0;
2967}
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969/*
2970 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02002971 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02002972 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
2973 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01002974 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01002976void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977{
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01002979 buffer_stop_hijack(rep);
2980 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981 }
2982 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002983 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01002984 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02002985 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01002986 return;
Willy Tarreau91861262007-10-17 17:06:05 +02002987 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002988 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989
Willy Tarreau91861262007-10-17 17:06:05 +02002990 /* unknown data source or internal error */
2991 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01002992 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02002993 if (!(s->flags & SN_ERR_MASK))
2994 s->flags |= SN_ERR_PRXCOND;
2995 if (!(s->flags & SN_FINST_MASK))
2996 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01002997 buffer_stop_hijack(rep);
2998 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999}
3000
3001
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003002/* Iterate the same filter through all request headers.
3003 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003004 * Since it can manage the switch to another backend, it updates the per-proxy
3005 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003006 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003007int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003008{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003009 char term;
3010 char *cur_ptr, *cur_end, *cur_next;
3011 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003012 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003013 struct hdr_idx_elem *cur_hdr;
3014 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003015
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003016 last_hdr = 0;
3017
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003018 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003019 old_idx = 0;
3020
3021 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003022 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003023 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003024 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003025 (exp->action == ACT_ALLOW ||
3026 exp->action == ACT_DENY ||
3027 exp->action == ACT_TARPIT))
3028 return 0;
3029
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003030 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003031 if (!cur_idx)
3032 break;
3033
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003034 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003035 cur_ptr = cur_next;
3036 cur_end = cur_ptr + cur_hdr->len;
3037 cur_next = cur_end + cur_hdr->cr + 1;
3038
3039 /* Now we have one header between cur_ptr and cur_end,
3040 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003041 */
3042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003043 /* The annoying part is that pattern matching needs
3044 * that we modify the contents to null-terminate all
3045 * strings before testing them.
3046 */
3047
3048 term = *cur_end;
3049 *cur_end = '\0';
3050
3051 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3052 switch (exp->action) {
3053 case ACT_SETBE:
3054 /* It is not possible to jump a second time.
3055 * FIXME: should we return an HTTP/500 here so that
3056 * the admin knows there's a problem ?
3057 */
3058 if (t->be != t->fe)
3059 break;
3060
3061 /* Swithing Proxy */
3062 t->be = (struct proxy *) exp->replace;
3063
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003064 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003065 * because we have associated req_cap and rsp_cap to the
3066 * frontend, and the beconn will be updated later.
3067 */
3068
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003069 t->rep->rto = t->req->wto = t->be->timeout.server;
3070 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003071 t->conn_retries = t->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02003072 if (t->be->options2 & PR_O2_RSPBUG_OK)
3073 t->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003074 last_hdr = 1;
3075 break;
3076
3077 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003078 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003079 last_hdr = 1;
3080 break;
3081
3082 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003083 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003084 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003085 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003086 break;
3087
3088 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003089 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003090 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003091 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003092 break;
3093
3094 case ACT_REPLACE:
3095 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3096 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3097 /* FIXME: if the user adds a newline in the replacement, the
3098 * index will not be recalculated for now, and the new line
3099 * will not be counted as a new header.
3100 */
3101
3102 cur_end += delta;
3103 cur_next += delta;
3104 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003105 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003106 break;
3107
3108 case ACT_REMOVE:
3109 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3110 cur_next += delta;
3111
3112 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003113 txn->req.eoh += delta;
3114 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3115 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003116 cur_hdr->len = 0;
3117 cur_end = NULL; /* null-term has been rewritten */
3118 break;
3119
3120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003121 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003122 if (cur_end)
3123 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003125 /* keep the link from this header to next one in case of later
3126 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003127 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003128 old_idx = cur_idx;
3129 }
3130 return 0;
3131}
3132
3133
3134/* Apply the filter to the request line.
3135 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3136 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003137 * Since it can manage the switch to another backend, it updates the per-proxy
3138 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003139 */
3140int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3141{
3142 char term;
3143 char *cur_ptr, *cur_end;
3144 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003145 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003146 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003147
Willy Tarreau58f10d72006-12-04 02:26:12 +01003148
Willy Tarreau3d300592007-03-18 18:34:41 +01003149 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003150 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003151 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003152 (exp->action == ACT_ALLOW ||
3153 exp->action == ACT_DENY ||
3154 exp->action == ACT_TARPIT))
3155 return 0;
3156 else if (exp->action == ACT_REMOVE)
3157 return 0;
3158
3159 done = 0;
3160
Willy Tarreau9cdde232007-05-02 20:58:19 +02003161 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003162 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003163
3164 /* Now we have the request line between cur_ptr and cur_end */
3165
3166 /* The annoying part is that pattern matching needs
3167 * that we modify the contents to null-terminate all
3168 * strings before testing them.
3169 */
3170
3171 term = *cur_end;
3172 *cur_end = '\0';
3173
3174 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3175 switch (exp->action) {
3176 case ACT_SETBE:
3177 /* It is not possible to jump a second time.
3178 * FIXME: should we return an HTTP/500 here so that
3179 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003180 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003181 if (t->be != t->fe)
3182 break;
3183
3184 /* Swithing Proxy */
3185 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003187 /* right now, the backend switch is not too much complicated
3188 * because we have associated req_cap and rsp_cap to the
3189 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003190 */
3191
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003192 t->rep->rto = t->req->wto = t->be->timeout.server;
3193 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003194 t->conn_retries = t->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02003195 if (t->be->options2 & PR_O2_RSPBUG_OK)
3196 t->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003197 done = 1;
3198 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003199
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003200 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003201 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003202 done = 1;
3203 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003204
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003205 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003206 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003207 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003208 done = 1;
3209 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003210
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003211 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003212 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003213 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003214 done = 1;
3215 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003216
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003217 case ACT_REPLACE:
3218 *cur_end = term; /* restore the string terminator */
3219 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3220 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3221 /* FIXME: if the user adds a newline in the replacement, the
3222 * index will not be recalculated for now, and the new line
3223 * will not be counted as a new header.
3224 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003225
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003226 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003227 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003228
Willy Tarreau9cdde232007-05-02 20:58:19 +02003229 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003230 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003231 HTTP_MSG_RQMETH,
3232 cur_ptr, cur_end + 1,
3233 NULL, NULL);
3234 if (unlikely(!cur_end))
3235 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003237 /* we have a full request and we know that we have either a CR
3238 * or an LF at <ptr>.
3239 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003240 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3241 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003242 /* there is no point trying this regex on headers */
3243 return 1;
3244 }
3245 }
3246 *cur_end = term; /* restore the string terminator */
3247 return done;
3248}
Willy Tarreau97de6242006-12-27 17:18:38 +01003249
Willy Tarreau58f10d72006-12-04 02:26:12 +01003250
Willy Tarreau58f10d72006-12-04 02:26:12 +01003251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003252/*
3253 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3254 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003255 * unparsable request. Since it can manage the switch to another backend, it
3256 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003257 */
3258int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3259{
Willy Tarreau3d300592007-03-18 18:34:41 +01003260 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003261 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003262 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003263 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003265 /*
3266 * The interleaving of transformations and verdicts
3267 * makes it difficult to decide to continue or stop
3268 * the evaluation.
3269 */
3270
Willy Tarreau3d300592007-03-18 18:34:41 +01003271 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003272 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3273 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3274 exp = exp->next;
3275 continue;
3276 }
3277
3278 /* Apply the filter to the request line. */
3279 ret = apply_filter_to_req_line(t, req, exp);
3280 if (unlikely(ret < 0))
3281 return -1;
3282
3283 if (likely(ret == 0)) {
3284 /* The filter did not match the request, it can be
3285 * iterated through all headers.
3286 */
3287 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003288 }
3289 exp = exp->next;
3290 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003291 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003292}
3293
3294
Willy Tarreaua15645d2007-03-18 16:22:39 +01003295
Willy Tarreau58f10d72006-12-04 02:26:12 +01003296/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003297 * Manage client-side cookie. It can impact performance by about 2% so it is
3298 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003299 */
3300void manage_client_side_cookies(struct session *t, struct buffer *req)
3301{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003302 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003303 char *p1, *p2, *p3, *p4;
3304 char *del_colon, *del_cookie, *colon;
3305 int app_cookies;
3306
3307 appsess *asession_temp = NULL;
3308 appsess local_asession;
3309
3310 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003311 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003312
Willy Tarreau2a324282006-12-05 00:05:46 +01003313 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003314 * we start with the start line.
3315 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003316 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003317 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003318
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003319 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003320 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003321 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003322
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003323 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003324 cur_ptr = cur_next;
3325 cur_end = cur_ptr + cur_hdr->len;
3326 cur_next = cur_end + cur_hdr->cr + 1;
3327
3328 /* We have one full header between cur_ptr and cur_end, and the
3329 * next header starts at cur_next. We're only interested in
3330 * "Cookie:" headers.
3331 */
3332
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003333 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3334 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003335 old_idx = cur_idx;
3336 continue;
3337 }
3338
3339 /* Now look for cookies. Conforming to RFC2109, we have to support
3340 * attributes whose name begin with a '$', and associate them with
3341 * the right cookie, if we want to delete this cookie.
3342 * So there are 3 cases for each cookie read :
3343 * 1) it's a special attribute, beginning with a '$' : ignore it.
3344 * 2) it's a server id cookie that we *MAY* want to delete : save
3345 * some pointers on it (last semi-colon, beginning of cookie...)
3346 * 3) it's an application cookie : we *MAY* have to delete a previous
3347 * "special" cookie.
3348 * At the end of loop, if a "special" cookie remains, we may have to
3349 * remove it. If no application cookie persists in the header, we
3350 * *MUST* delete it
3351 */
3352
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003353 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003354
Willy Tarreau58f10d72006-12-04 02:26:12 +01003355 /* del_cookie == NULL => nothing to be deleted */
3356 del_colon = del_cookie = NULL;
3357 app_cookies = 0;
3358
3359 while (p1 < cur_end) {
3360 /* skip spaces and colons, but keep an eye on these ones */
3361 while (p1 < cur_end) {
3362 if (*p1 == ';' || *p1 == ',')
3363 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003364 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003365 break;
3366 p1++;
3367 }
3368
3369 if (p1 == cur_end)
3370 break;
3371
3372 /* p1 is at the beginning of the cookie name */
3373 p2 = p1;
3374 while (p2 < cur_end && *p2 != '=')
3375 p2++;
3376
3377 if (p2 == cur_end)
3378 break;
3379
3380 p3 = p2 + 1; /* skips the '=' sign */
3381 if (p3 == cur_end)
3382 break;
3383
3384 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003385 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003386 p4++;
3387
3388 /* here, we have the cookie name between p1 and p2,
3389 * and its value between p3 and p4.
3390 * we can process it :
3391 *
3392 * Cookie: NAME=VALUE;
3393 * | || || |
3394 * | || || +--> p4
3395 * | || |+-------> p3
3396 * | || +--------> p2
3397 * | |+------------> p1
3398 * | +-------------> colon
3399 * +--------------------> cur_ptr
3400 */
3401
3402 if (*p1 == '$') {
3403 /* skip this one */
3404 }
3405 else {
3406 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003407 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003408 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003409 (p4 - p1 >= t->fe->capture_namelen) &&
3410 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003411 int log_len = p4 - p1;
3412
Willy Tarreau086b3b42007-05-13 21:45:51 +02003413 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003414 Alert("HTTP logging : out of memory.\n");
3415 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003416 if (log_len > t->fe->capture_len)
3417 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003418 memcpy(txn->cli_cookie, p1, log_len);
3419 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003420 }
3421 }
3422
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003423 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3424 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003425 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003426 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003427 char *delim;
3428
3429 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3430 * have the server ID betweek p3 and delim, and the original cookie between
3431 * delim+1 and p4. Otherwise, delim==p4 :
3432 *
3433 * Cookie: NAME=SRV~VALUE;
3434 * | || || | |
3435 * | || || | +--> p4
3436 * | || || +--------> delim
3437 * | || |+-----------> p3
3438 * | || +------------> p2
3439 * | |+----------------> p1
3440 * | +-----------------> colon
3441 * +------------------------> cur_ptr
3442 */
3443
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003444 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003445 for (delim = p3; delim < p4; delim++)
3446 if (*delim == COOKIE_DELIM)
3447 break;
3448 }
3449 else
3450 delim = p4;
3451
3452
3453 /* Here, we'll look for the first running server which supports the cookie.
3454 * This allows to share a same cookie between several servers, for example
3455 * to dedicate backup servers to specific servers only.
3456 * However, to prevent clients from sticking to cookie-less backup server
3457 * when they have incidentely learned an empty cookie, we simply ignore
3458 * empty cookies and mark them as invalid.
3459 */
3460 if (delim == p3)
3461 srv = NULL;
3462
3463 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003464 if (srv->cookie && (srv->cklen == delim - p3) &&
3465 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003466 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003467 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003468 txn->flags &= ~TX_CK_MASK;
3469 txn->flags |= TX_CK_VALID;
3470 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003471 t->srv = srv;
3472 break;
3473 } else {
3474 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003475 txn->flags &= ~TX_CK_MASK;
3476 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003477 }
3478 }
3479 srv = srv->next;
3480 }
3481
Willy Tarreau3d300592007-03-18 18:34:41 +01003482 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003483 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003484 txn->flags &= ~TX_CK_MASK;
3485 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003486 }
3487
3488 /* depending on the cookie mode, we may have to either :
3489 * - delete the complete cookie if we're in insert+indirect mode, so that
3490 * the server never sees it ;
3491 * - remove the server id from the cookie value, and tag the cookie as an
3492 * application cookie so that it does not get accidentely removed later,
3493 * if we're in cookie prefix mode
3494 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003495 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003496 int delta; /* negative */
3497
3498 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3499 p4 += delta;
3500 cur_end += delta;
3501 cur_next += delta;
3502 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003503 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003504
3505 del_cookie = del_colon = NULL;
3506 app_cookies++; /* protect the header from deletion */
3507 }
3508 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003509 (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 +01003510 del_cookie = p1;
3511 del_colon = colon;
3512 }
3513 } else {
3514 /* now we know that we must keep this cookie since it's
3515 * not ours. But if we wanted to delete our cookie
3516 * earlier, we cannot remove the complete header, but we
3517 * can remove the previous block itself.
3518 */
3519 app_cookies++;
3520
3521 if (del_cookie != NULL) {
3522 int delta; /* negative */
3523
3524 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3525 p4 += delta;
3526 cur_end += delta;
3527 cur_next += delta;
3528 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003529 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003530 del_cookie = del_colon = NULL;
3531 }
3532 }
3533
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003534 if ((t->be->appsession_name != NULL) &&
3535 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003536 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003537
Willy Tarreau58f10d72006-12-04 02:26:12 +01003538 /* Cool... it's the right one */
3539
3540 asession_temp = &local_asession;
3541
Willy Tarreau63963c62007-05-13 21:29:55 +02003542 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003543 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3544 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3545 return;
3546 }
3547
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003548 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3549 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003550 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003551
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003553 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3554 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003555 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003556 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003557 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003558 Alert("Not enough memory process_cli():asession:calloc().\n");
3559 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3560 return;
3561 }
3562
3563 asession_temp->sessid = local_asession.sessid;
3564 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003565 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003566 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003567 } else {
3568 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003569 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003571 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003572 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003573 Alert("Found Application Session without matching server.\n");
3574 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003575 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003576 while (srv) {
3577 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003578 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003579 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003580 txn->flags &= ~TX_CK_MASK;
3581 txn->flags |= TX_CK_VALID;
3582 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003583 t->srv = srv;
3584 break;
3585 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003586 txn->flags &= ~TX_CK_MASK;
3587 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588 }
3589 }
3590 srv = srv->next;
3591 }/* end while(srv) */
3592 }/* end else if server == NULL */
3593
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003594 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003595 asession_temp->request_count++;
3596#if defined(DEBUG_HASH)
3597 Alert("manage_client_side_cookies\n");
3598 appsession_hash_dump(&(t->be->htbl_proxy));
3599#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003600 }/* end if ((t->proxy->appsession_name != NULL) ... */
3601 }
3602
3603 /* we'll have to look for another cookie ... */
3604 p1 = p4;
3605 } /* while (p1 < cur_end) */
3606
3607 /* There's no more cookie on this line.
3608 * We may have marked the last one(s) for deletion.
3609 * We must do this now in two ways :
3610 * - if there is no app cookie, we simply delete the header ;
3611 * - if there are app cookies, we must delete the end of the
3612 * string properly, including the colon/semi-colon before
3613 * the cookie name.
3614 */
3615 if (del_cookie != NULL) {
3616 int delta;
3617 if (app_cookies) {
3618 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3619 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003620 cur_hdr->len += delta;
3621 } else {
3622 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003623
3624 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003625 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3626 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003627 cur_hdr->len = 0;
3628 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003629 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003630 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003631 }
3632
3633 /* keep the link from this header to next one */
3634 old_idx = cur_idx;
3635 } /* end of cookie processing on this header */
3636}
3637
3638
Willy Tarreaua15645d2007-03-18 16:22:39 +01003639/* Iterate the same filter through all response headers contained in <rtr>.
3640 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3641 */
3642int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3643{
3644 char term;
3645 char *cur_ptr, *cur_end, *cur_next;
3646 int cur_idx, old_idx, last_hdr;
3647 struct http_txn *txn = &t->txn;
3648 struct hdr_idx_elem *cur_hdr;
3649 int len, delta;
3650
3651 last_hdr = 0;
3652
3653 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3654 old_idx = 0;
3655
3656 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003657 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003658 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003659 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003660 (exp->action == ACT_ALLOW ||
3661 exp->action == ACT_DENY))
3662 return 0;
3663
3664 cur_idx = txn->hdr_idx.v[old_idx].next;
3665 if (!cur_idx)
3666 break;
3667
3668 cur_hdr = &txn->hdr_idx.v[cur_idx];
3669 cur_ptr = cur_next;
3670 cur_end = cur_ptr + cur_hdr->len;
3671 cur_next = cur_end + cur_hdr->cr + 1;
3672
3673 /* Now we have one header between cur_ptr and cur_end,
3674 * and the next header starts at cur_next.
3675 */
3676
3677 /* The annoying part is that pattern matching needs
3678 * that we modify the contents to null-terminate all
3679 * strings before testing them.
3680 */
3681
3682 term = *cur_end;
3683 *cur_end = '\0';
3684
3685 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3686 switch (exp->action) {
3687 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003688 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003689 last_hdr = 1;
3690 break;
3691
3692 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003693 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003694 last_hdr = 1;
3695 break;
3696
3697 case ACT_REPLACE:
3698 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3699 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3700 /* FIXME: if the user adds a newline in the replacement, the
3701 * index will not be recalculated for now, and the new line
3702 * will not be counted as a new header.
3703 */
3704
3705 cur_end += delta;
3706 cur_next += delta;
3707 cur_hdr->len += delta;
3708 txn->rsp.eoh += delta;
3709 break;
3710
3711 case ACT_REMOVE:
3712 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3713 cur_next += delta;
3714
3715 /* FIXME: this should be a separate function */
3716 txn->rsp.eoh += delta;
3717 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3718 txn->hdr_idx.used--;
3719 cur_hdr->len = 0;
3720 cur_end = NULL; /* null-term has been rewritten */
3721 break;
3722
3723 }
3724 }
3725 if (cur_end)
3726 *cur_end = term; /* restore the string terminator */
3727
3728 /* keep the link from this header to next one in case of later
3729 * removal of next header.
3730 */
3731 old_idx = cur_idx;
3732 }
3733 return 0;
3734}
3735
3736
3737/* Apply the filter to the status line in the response buffer <rtr>.
3738 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3739 * or -1 if a replacement resulted in an invalid status line.
3740 */
3741int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3742{
3743 char term;
3744 char *cur_ptr, *cur_end;
3745 int done;
3746 struct http_txn *txn = &t->txn;
3747 int len, delta;
3748
3749
Willy Tarreau3d300592007-03-18 18:34:41 +01003750 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003751 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003752 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003753 (exp->action == ACT_ALLOW ||
3754 exp->action == ACT_DENY))
3755 return 0;
3756 else if (exp->action == ACT_REMOVE)
3757 return 0;
3758
3759 done = 0;
3760
Willy Tarreau9cdde232007-05-02 20:58:19 +02003761 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003762 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3763
3764 /* Now we have the status line between cur_ptr and cur_end */
3765
3766 /* The annoying part is that pattern matching needs
3767 * that we modify the contents to null-terminate all
3768 * strings before testing them.
3769 */
3770
3771 term = *cur_end;
3772 *cur_end = '\0';
3773
3774 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3775 switch (exp->action) {
3776 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003777 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003778 done = 1;
3779 break;
3780
3781 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003782 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003783 done = 1;
3784 break;
3785
3786 case ACT_REPLACE:
3787 *cur_end = term; /* restore the string terminator */
3788 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3789 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3790 /* FIXME: if the user adds a newline in the replacement, the
3791 * index will not be recalculated for now, and the new line
3792 * will not be counted as a new header.
3793 */
3794
3795 txn->rsp.eoh += delta;
3796 cur_end += delta;
3797
Willy Tarreau9cdde232007-05-02 20:58:19 +02003798 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003799 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003800 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003801 cur_ptr, cur_end + 1,
3802 NULL, NULL);
3803 if (unlikely(!cur_end))
3804 return -1;
3805
3806 /* we have a full respnse and we know that we have either a CR
3807 * or an LF at <ptr>.
3808 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003809 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003810 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3811 /* there is no point trying this regex on headers */
3812 return 1;
3813 }
3814 }
3815 *cur_end = term; /* restore the string terminator */
3816 return done;
3817}
3818
3819
3820
3821/*
3822 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3823 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3824 * unparsable response.
3825 */
3826int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3827{
Willy Tarreau3d300592007-03-18 18:34:41 +01003828 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003829 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003830 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003831 int ret;
3832
3833 /*
3834 * The interleaving of transformations and verdicts
3835 * makes it difficult to decide to continue or stop
3836 * the evaluation.
3837 */
3838
Willy Tarreau3d300592007-03-18 18:34:41 +01003839 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003840 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3841 exp->action == ACT_PASS)) {
3842 exp = exp->next;
3843 continue;
3844 }
3845
3846 /* Apply the filter to the status line. */
3847 ret = apply_filter_to_sts_line(t, rtr, exp);
3848 if (unlikely(ret < 0))
3849 return -1;
3850
3851 if (likely(ret == 0)) {
3852 /* The filter did not match the response, it can be
3853 * iterated through all headers.
3854 */
3855 apply_filter_to_resp_headers(t, rtr, exp);
3856 }
3857 exp = exp->next;
3858 }
3859 return 0;
3860}
3861
3862
3863
3864/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003865 * Manage server-side cookies. It can impact performance by about 2% so it is
3866 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003867 */
3868void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3869{
3870 struct http_txn *txn = &t->txn;
3871 char *p1, *p2, *p3, *p4;
3872
3873 appsess *asession_temp = NULL;
3874 appsess local_asession;
3875
3876 char *cur_ptr, *cur_end, *cur_next;
3877 int cur_idx, old_idx, delta;
3878
Willy Tarreaua15645d2007-03-18 16:22:39 +01003879 /* Iterate through the headers.
3880 * we start with the start line.
3881 */
3882 old_idx = 0;
3883 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3884
3885 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3886 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003887 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003888
3889 cur_hdr = &txn->hdr_idx.v[cur_idx];
3890 cur_ptr = cur_next;
3891 cur_end = cur_ptr + cur_hdr->len;
3892 cur_next = cur_end + cur_hdr->cr + 1;
3893
3894 /* We have one full header between cur_ptr and cur_end, and the
3895 * next header starts at cur_next. We're only interested in
3896 * "Cookie:" headers.
3897 */
3898
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003899 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3900 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003901 old_idx = cur_idx;
3902 continue;
3903 }
3904
3905 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003906 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003907
3908
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003909 /* maybe we only wanted to see if there was a set-cookie. Note that
3910 * the cookie capture is declared in the fronend.
3911 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003912 if (t->be->cookie_name == NULL &&
3913 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003914 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003915 return;
3916
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003917 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003918
3919 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003920 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3921 break;
3922
3923 /* p1 is at the beginning of the cookie name */
3924 p2 = p1;
3925
3926 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3927 p2++;
3928
3929 if (p2 == cur_end || *p2 == ';') /* next cookie */
3930 break;
3931
3932 p3 = p2 + 1; /* skip the '=' sign */
3933 if (p3 == cur_end)
3934 break;
3935
3936 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003937 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003938 p4++;
3939
3940 /* here, we have the cookie name between p1 and p2,
3941 * and its value between p3 and p4.
3942 * we can process it.
3943 */
3944
3945 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003946 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003947 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003948 (p4 - p1 >= t->fe->capture_namelen) &&
3949 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003950 int log_len = p4 - p1;
3951
Willy Tarreau086b3b42007-05-13 21:45:51 +02003952 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003953 Alert("HTTP logging : out of memory.\n");
3954 }
3955
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003956 if (log_len > t->fe->capture_len)
3957 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003958 memcpy(txn->srv_cookie, p1, log_len);
3959 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003960 }
3961
3962 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003963 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3964 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003965 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003966 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003967
3968 /* If the cookie is in insert mode on a known server, we'll delete
3969 * this occurrence because we'll insert another one later.
3970 * We'll delete it too if the "indirect" option is set and we're in
3971 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003972 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
3973 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003974 /* this header must be deleted */
3975 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3976 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3977 txn->hdr_idx.used--;
3978 cur_hdr->len = 0;
3979 cur_next += delta;
3980 txn->rsp.eoh += delta;
3981
Willy Tarreau3d300592007-03-18 18:34:41 +01003982 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003983 }
3984 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003985 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003986 /* replace bytes p3->p4 with the cookie name associated
3987 * with this server since we know it.
3988 */
3989 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
3990 cur_hdr->len += delta;
3991 cur_next += delta;
3992 txn->rsp.eoh += delta;
3993
Willy Tarreau3d300592007-03-18 18:34:41 +01003994 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003995 }
3996 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003997 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003998 /* insert the cookie name associated with this server
3999 * before existing cookie, and insert a delimitor between them..
4000 */
4001 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4002 cur_hdr->len += delta;
4003 cur_next += delta;
4004 txn->rsp.eoh += delta;
4005
4006 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004007 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004008 }
4009 }
4010 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004011 else if ((t->be->appsession_name != NULL) &&
4012 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004013
4014 /* Cool... it's the right one */
4015
4016 size_t server_id_len = strlen(t->srv->id) + 1;
4017 asession_temp = &local_asession;
4018
Willy Tarreau63963c62007-05-13 21:29:55 +02004019 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004020 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4021 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4022 return;
4023 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004024 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4025 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004026 asession_temp->serverid = NULL;
4027
4028 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004029 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4030 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004031 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004032 Alert("Not enough Memory process_srv():asession:calloc().\n");
4033 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4034 return;
4035 }
4036 asession_temp->sessid = local_asession.sessid;
4037 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004038 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004039 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4040 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004041 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004042 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004043 }
4044
Willy Tarreaua15645d2007-03-18 16:22:39 +01004045 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004046 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004047 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4048 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4049 return;
4050 }
4051 asession_temp->serverid[0] = '\0';
4052 }
4053
4054 if (asession_temp->serverid[0] == '\0')
4055 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4056
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004057 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004058 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004059#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004060 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004061 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004062#endif
4063 }/* end if ((t->proxy->appsession_name != NULL) ... */
4064 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4065 } /* we're now at the end of the cookie value */
4066
4067 /* keep the link from this header to next one */
4068 old_idx = cur_idx;
4069 } /* end of cookie processing on this header */
4070}
4071
4072
4073
4074/*
4075 * Check if response is cacheable or not. Updates t->flags.
4076 */
4077void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4078{
4079 struct http_txn *txn = &t->txn;
4080 char *p1, *p2;
4081
4082 char *cur_ptr, *cur_end, *cur_next;
4083 int cur_idx;
4084
Willy Tarreau5df51872007-11-25 16:20:08 +01004085 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004086 return;
4087
4088 /* Iterate through the headers.
4089 * we start with the start line.
4090 */
4091 cur_idx = 0;
4092 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4093
4094 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4095 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004096 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004097
4098 cur_hdr = &txn->hdr_idx.v[cur_idx];
4099 cur_ptr = cur_next;
4100 cur_end = cur_ptr + cur_hdr->len;
4101 cur_next = cur_end + cur_hdr->cr + 1;
4102
4103 /* We have one full header between cur_ptr and cur_end, and the
4104 * next header starts at cur_next. We're only interested in
4105 * "Cookie:" headers.
4106 */
4107
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004108 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4109 if (val) {
4110 if ((cur_end - (cur_ptr + val) >= 8) &&
4111 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4112 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4113 return;
4114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004115 }
4116
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004117 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4118 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004119 continue;
4120
4121 /* OK, right now we know we have a cache-control header at cur_ptr */
4122
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004123 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004124
4125 if (p1 >= cur_end) /* no more info */
4126 continue;
4127
4128 /* p1 is at the beginning of the value */
4129 p2 = p1;
4130
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004131 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004132 p2++;
4133
4134 /* we have a complete value between p1 and p2 */
4135 if (p2 < cur_end && *p2 == '=') {
4136 /* we have something of the form no-cache="set-cookie" */
4137 if ((cur_end - p1 >= 21) &&
4138 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4139 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004140 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004141 continue;
4142 }
4143
4144 /* OK, so we know that either p2 points to the end of string or to a comma */
4145 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4146 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4147 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4148 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004149 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004150 return;
4151 }
4152
4153 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004154 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004155 continue;
4156 }
4157 }
4158}
4159
4160
Willy Tarreau58f10d72006-12-04 02:26:12 +01004161/*
4162 * Try to retrieve a known appsession in the URI, then the associated server.
4163 * If the server is found, it's assigned to the session.
4164 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004165void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004166{
Willy Tarreau3d300592007-03-18 18:34:41 +01004167 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168 appsess *asession_temp = NULL;
4169 appsess local_asession;
4170 char *request_line;
4171
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004172 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004173 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004174 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004175 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004176 return;
4177
4178 /* skip ';' */
4179 request_line++;
4180
4181 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004182 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004183 return;
4184
4185 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004186 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004187
4188 /* First try if we already have an appsession */
4189 asession_temp = &local_asession;
4190
Willy Tarreau63963c62007-05-13 21:29:55 +02004191 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004192 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4193 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4194 return;
4195 }
4196
4197 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004198 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4199 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004200 asession_temp->serverid = NULL;
4201
4202 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004203 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4204 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004205 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004207 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 Alert("Not enough memory process_cli():asession:calloc().\n");
4209 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4210 return;
4211 }
4212 asession_temp->sessid = local_asession.sessid;
4213 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004214 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004215 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216 }
4217 else {
4218 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004219 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004220 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004221
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004222 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004223 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004224
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004226 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004227 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004228#endif
4229 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004230 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231 Alert("Found Application Session without matching server.\n");
4232 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004233 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004234 while (srv) {
4235 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004236 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004237 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004238 txn->flags &= ~TX_CK_MASK;
4239 txn->flags |= TX_CK_VALID;
4240 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004241 t->srv = srv;
4242 break;
4243 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004244 txn->flags &= ~TX_CK_MASK;
4245 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 }
4247 }
4248 srv = srv->next;
4249 }
4250 }
4251}
4252
4253
Willy Tarreaub2513902006-12-17 14:52:38 +01004254/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004255 * In a GET or HEAD request, check if the requested URI matches the stats uri
4256 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004257 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004258 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004259 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004260 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004261 *
4262 * Returns 1 if the session's state changes, otherwise 0.
4263 */
4264int stats_check_uri_auth(struct session *t, struct proxy *backend)
4265{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004266 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004267 struct uri_auth *uri_auth = backend->uri_auth;
4268 struct user_auth *user;
4269 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004270 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004271
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004272 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004274 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004275 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004276 return 0;
4277
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004278 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004279
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004280 /* the URI is in h */
4281 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004282 return 0;
4283
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004284 h += uri_auth->uri_len;
4285 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4286 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004287 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004288 break;
4289 }
4290 h++;
4291 }
4292
4293 if (uri_auth->refresh) {
4294 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4295 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4296 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004297 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004298 break;
4299 }
4300 h++;
4301 }
4302 }
4303
Willy Tarreau55bb8452007-10-17 18:44:57 +02004304 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4305 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4306 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004307 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004308 break;
4309 }
4310 h++;
4311 }
4312
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004313 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4314
Willy Tarreaub2513902006-12-17 14:52:38 +01004315 /* we are in front of a interceptable URI. Let's check
4316 * if there's an authentication and if it's valid.
4317 */
4318 user = uri_auth->users;
4319 if (!user) {
4320 /* no user auth required, it's OK */
4321 authenticated = 1;
4322 } else {
4323 authenticated = 0;
4324
4325 /* a user list is defined, we have to check.
4326 * skip 21 chars for "Authorization: Basic ".
4327 */
4328
4329 /* FIXME: this should move to an earlier place */
4330 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004331 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4332 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4333 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004334 if (len > 14 &&
4335 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004336 txn->auth_hdr.str = h;
4337 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004338 break;
4339 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004340 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004341 }
4342
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004343 if (txn->auth_hdr.len < 21 ||
4344 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004345 user = NULL;
4346
4347 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004348 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4349 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004350 user->user_pwd, user->user_len)) {
4351 authenticated = 1;
4352 break;
4353 }
4354 user = user->next;
4355 }
4356 }
4357
4358 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004359 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004360
4361 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004362 msg.str = trash;
4363 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004364 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004365 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004366 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004367 if (!(t->flags & SN_ERR_MASK))
4368 t->flags |= SN_ERR_PRXCOND;
4369 if (!(t->flags & SN_FINST_MASK))
4370 t->flags |= SN_FINST_R;
4371 return 1;
4372 }
4373
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004374 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004375 * data.
4376 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004377 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004378 buffer_shutw_now(t->req);
4379 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004380 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004381 t->data_source = DATA_SRC_STATS;
4382 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004383 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004384 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004385 return 1;
4386}
4387
Willy Tarreau4076a152009-04-02 15:18:36 +02004388/*
4389 * Capture a bad request or response and archive it in the proxy's structure.
4390 */
4391void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4392 struct buffer *buf, struct http_msg *msg,
4393 struct proxy *other_end)
4394{
4395 int maxlen = MIN(buf->r - buf->data + msg->som, sizeof(es->buf));
4396
4397 memcpy(es->buf, buf->data + msg->som, maxlen);
4398 if (msg->err_pos >= 0)
4399 es->pos = msg->err_pos + msg->som;
4400 else
4401 es->pos = buf->lr - buf->data + msg->som;
4402 es->len = buf->r - buf->data + msg->som;
4403 es->when = date; // user-visible date
4404 es->sid = s->uniq_id;
4405 es->srv = s->srv;
4406 es->oe = other_end;
4407 es->src = s->cli_addr;
4408}
Willy Tarreaub2513902006-12-17 14:52:38 +01004409
Willy Tarreaubaaee002006-06-26 02:48:02 +02004410/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411 * Print a debug line with a header
4412 */
4413void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4414{
4415 int len, max;
4416 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004417 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004418 max = end - start;
4419 UBOUND(max, sizeof(trash) - len - 1);
4420 len += strlcpy2(trash + len, start, max + 1);
4421 trash[len++] = '\n';
4422 write(1, trash, len);
4423}
4424
4425
Willy Tarreau8797c062007-05-07 00:55:35 +02004426/************************************************************************/
4427/* The code below is dedicated to ACL parsing and matching */
4428/************************************************************************/
4429
4430
4431
4432
4433/* 1. Check on METHOD
4434 * We use the pre-parsed method if it is known, and store its number as an
4435 * integer. If it is unknown, we use the pointer and the length.
4436 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004437static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004438{
4439 int len, meth;
4440
Willy Tarreauae8b7962007-06-09 23:10:04 +02004441 len = strlen(*text);
4442 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004443
4444 pattern->val.i = meth;
4445 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004446 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004447 if (!pattern->ptr.str)
4448 return 0;
4449 pattern->len = len;
4450 }
4451 return 1;
4452}
4453
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004454static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004455acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4456 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004457{
4458 int meth;
4459 struct http_txn *txn = l7;
4460
Willy Tarreaub6866442008-07-14 23:54:42 +02004461 if (!txn)
4462 return 0;
4463
Willy Tarreauc11416f2007-06-17 16:58:38 +02004464 if (txn->req.msg_state != HTTP_MSG_BODY)
4465 return 0;
4466
Willy Tarreau8797c062007-05-07 00:55:35 +02004467 meth = txn->meth;
4468 test->i = meth;
4469 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004470 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4471 /* ensure the indexes are not affected */
4472 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004473 test->len = txn->req.sl.rq.m_l;
4474 test->ptr = txn->req.sol;
4475 }
4476 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4477 return 1;
4478}
4479
4480static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4481{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004482 int icase;
4483
Willy Tarreau8797c062007-05-07 00:55:35 +02004484 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004485 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004486
4487 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004488 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004489
4490 /* Other method, we must compare the strings */
4491 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004492 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004493
4494 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4495 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4496 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004497 return ACL_PAT_FAIL;
4498 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004499}
4500
4501/* 2. Check on Request/Status Version
4502 * We simply compare strings here.
4503 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004504static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004505{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004506 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004507 if (!pattern->ptr.str)
4508 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004509 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004510 return 1;
4511}
4512
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004513static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004514acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4515 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004516{
4517 struct http_txn *txn = l7;
4518 char *ptr;
4519 int len;
4520
Willy Tarreaub6866442008-07-14 23:54:42 +02004521 if (!txn)
4522 return 0;
4523
Willy Tarreauc11416f2007-06-17 16:58:38 +02004524 if (txn->req.msg_state != HTTP_MSG_BODY)
4525 return 0;
4526
Willy Tarreau8797c062007-05-07 00:55:35 +02004527 len = txn->req.sl.rq.v_l;
4528 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4529
4530 while ((len-- > 0) && (*ptr++ != '/'));
4531 if (len <= 0)
4532 return 0;
4533
4534 test->ptr = ptr;
4535 test->len = len;
4536
4537 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4538 return 1;
4539}
4540
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004541static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004542acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4543 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004544{
4545 struct http_txn *txn = l7;
4546 char *ptr;
4547 int len;
4548
Willy Tarreaub6866442008-07-14 23:54:42 +02004549 if (!txn)
4550 return 0;
4551
Willy Tarreauc11416f2007-06-17 16:58:38 +02004552 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4553 return 0;
4554
Willy Tarreau8797c062007-05-07 00:55:35 +02004555 len = txn->rsp.sl.st.v_l;
4556 ptr = txn->rsp.sol;
4557
4558 while ((len-- > 0) && (*ptr++ != '/'));
4559 if (len <= 0)
4560 return 0;
4561
4562 test->ptr = ptr;
4563 test->len = len;
4564
4565 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4566 return 1;
4567}
4568
4569/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004570static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004571acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4572 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004573{
4574 struct http_txn *txn = l7;
4575 char *ptr;
4576 int len;
4577
Willy Tarreaub6866442008-07-14 23:54:42 +02004578 if (!txn)
4579 return 0;
4580
Willy Tarreauc11416f2007-06-17 16:58:38 +02004581 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4582 return 0;
4583
Willy Tarreau8797c062007-05-07 00:55:35 +02004584 len = txn->rsp.sl.st.c_l;
4585 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4586
4587 test->i = __strl2ui(ptr, len);
4588 test->flags = ACL_TEST_F_VOL_1ST;
4589 return 1;
4590}
4591
4592/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004593static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004594acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4595 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004596{
4597 struct http_txn *txn = l7;
4598
Willy Tarreaub6866442008-07-14 23:54:42 +02004599 if (!txn)
4600 return 0;
4601
Willy Tarreauc11416f2007-06-17 16:58:38 +02004602 if (txn->req.msg_state != HTTP_MSG_BODY)
4603 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004604
Willy Tarreauc11416f2007-06-17 16:58:38 +02004605 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4606 /* ensure the indexes are not affected */
4607 return 0;
4608
Willy Tarreau8797c062007-05-07 00:55:35 +02004609 test->len = txn->req.sl.rq.u_l;
4610 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4611
Willy Tarreauf3d25982007-05-08 22:45:09 +02004612 /* we do not need to set READ_ONLY because the data is in a buffer */
4613 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004614 return 1;
4615}
4616
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004617static int
4618acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4619 struct acl_expr *expr, struct acl_test *test)
4620{
4621 struct http_txn *txn = l7;
4622
Willy Tarreaub6866442008-07-14 23:54:42 +02004623 if (!txn)
4624 return 0;
4625
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004626 if (txn->req.msg_state != HTTP_MSG_BODY)
4627 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004628
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004629 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4630 /* ensure the indexes are not affected */
4631 return 0;
4632
4633 /* Parse HTTP request */
4634 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4635 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4636 test->i = AF_INET;
4637
4638 /*
4639 * If we are parsing url in frontend space, we prepare backend stage
4640 * to not parse again the same url ! optimization lazyness...
4641 */
4642 if (px->options & PR_O_HTTP_PROXY)
4643 l4->flags |= SN_ADDR_SET;
4644
4645 test->flags = ACL_TEST_F_READ_ONLY;
4646 return 1;
4647}
4648
4649static int
4650acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4651 struct acl_expr *expr, struct acl_test *test)
4652{
4653 struct http_txn *txn = l7;
4654
Willy Tarreaub6866442008-07-14 23:54:42 +02004655 if (!txn)
4656 return 0;
4657
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004658 if (txn->req.msg_state != HTTP_MSG_BODY)
4659 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004660
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004661 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4662 /* ensure the indexes are not affected */
4663 return 0;
4664
4665 /* Same optimization as url_ip */
4666 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4667 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4668
4669 if (px->options & PR_O_HTTP_PROXY)
4670 l4->flags |= SN_ADDR_SET;
4671
4672 test->flags = ACL_TEST_F_READ_ONLY;
4673 return 1;
4674}
4675
Willy Tarreauc11416f2007-06-17 16:58:38 +02004676/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4677 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4678 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004679static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004680acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004681 struct acl_expr *expr, struct acl_test *test)
4682{
4683 struct http_txn *txn = l7;
4684 struct hdr_idx *idx = &txn->hdr_idx;
4685 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004686
Willy Tarreaub6866442008-07-14 23:54:42 +02004687 if (!txn)
4688 return 0;
4689
Willy Tarreau33a7e692007-06-10 19:45:56 +02004690 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4691 /* search for header from the beginning */
4692 ctx->idx = 0;
4693
Willy Tarreau33a7e692007-06-10 19:45:56 +02004694 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4695 test->flags |= ACL_TEST_F_FETCH_MORE;
4696 test->flags |= ACL_TEST_F_VOL_HDR;
4697 test->len = ctx->vlen;
4698 test->ptr = (char *)ctx->line + ctx->val;
4699 return 1;
4700 }
4701
4702 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4703 test->flags |= ACL_TEST_F_VOL_HDR;
4704 return 0;
4705}
4706
Willy Tarreau33a7e692007-06-10 19:45:56 +02004707static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004708acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4709 struct acl_expr *expr, struct acl_test *test)
4710{
4711 struct http_txn *txn = l7;
4712
Willy Tarreaub6866442008-07-14 23:54:42 +02004713 if (!txn)
4714 return 0;
4715
Willy Tarreauc11416f2007-06-17 16:58:38 +02004716 if (txn->req.msg_state != HTTP_MSG_BODY)
4717 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004718
Willy Tarreauc11416f2007-06-17 16:58:38 +02004719 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4720 /* ensure the indexes are not affected */
4721 return 0;
4722
4723 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4724}
4725
4726static int
4727acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4728 struct acl_expr *expr, struct acl_test *test)
4729{
4730 struct http_txn *txn = l7;
4731
Willy Tarreaub6866442008-07-14 23:54:42 +02004732 if (!txn)
4733 return 0;
4734
Willy Tarreauc11416f2007-06-17 16:58:38 +02004735 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4736 return 0;
4737
4738 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4739}
4740
4741/* 6. Check on HTTP header count. The number of occurrences is returned.
4742 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4743 */
4744static int
4745acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004746 struct acl_expr *expr, struct acl_test *test)
4747{
4748 struct http_txn *txn = l7;
4749 struct hdr_idx *idx = &txn->hdr_idx;
4750 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004751 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004752
Willy Tarreaub6866442008-07-14 23:54:42 +02004753 if (!txn)
4754 return 0;
4755
Willy Tarreau33a7e692007-06-10 19:45:56 +02004756 ctx.idx = 0;
4757 cnt = 0;
4758 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4759 cnt++;
4760
4761 test->i = cnt;
4762 test->flags = ACL_TEST_F_VOL_HDR;
4763 return 1;
4764}
4765
Willy Tarreauc11416f2007-06-17 16:58:38 +02004766static int
4767acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4768 struct acl_expr *expr, struct acl_test *test)
4769{
4770 struct http_txn *txn = l7;
4771
Willy Tarreaub6866442008-07-14 23:54:42 +02004772 if (!txn)
4773 return 0;
4774
Willy Tarreauc11416f2007-06-17 16:58:38 +02004775 if (txn->req.msg_state != HTTP_MSG_BODY)
4776 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004777
Willy Tarreauc11416f2007-06-17 16:58:38 +02004778 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4779 /* ensure the indexes are not affected */
4780 return 0;
4781
4782 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4783}
4784
4785static int
4786acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4787 struct acl_expr *expr, struct acl_test *test)
4788{
4789 struct http_txn *txn = l7;
4790
Willy Tarreaub6866442008-07-14 23:54:42 +02004791 if (!txn)
4792 return 0;
4793
Willy Tarreauc11416f2007-06-17 16:58:38 +02004794 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4795 return 0;
4796
4797 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4798}
4799
Willy Tarreau33a7e692007-06-10 19:45:56 +02004800/* 7. Check on HTTP header's integer value. The integer value is returned.
4801 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004802 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004803 */
4804static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004805acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004806 struct acl_expr *expr, struct acl_test *test)
4807{
4808 struct http_txn *txn = l7;
4809 struct hdr_idx *idx = &txn->hdr_idx;
4810 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004811
Willy Tarreaub6866442008-07-14 23:54:42 +02004812 if (!txn)
4813 return 0;
4814
Willy Tarreau33a7e692007-06-10 19:45:56 +02004815 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4816 /* search for header from the beginning */
4817 ctx->idx = 0;
4818
Willy Tarreau33a7e692007-06-10 19:45:56 +02004819 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4820 test->flags |= ACL_TEST_F_FETCH_MORE;
4821 test->flags |= ACL_TEST_F_VOL_HDR;
4822 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4823 return 1;
4824 }
4825
4826 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4827 test->flags |= ACL_TEST_F_VOL_HDR;
4828 return 0;
4829}
4830
Willy Tarreauc11416f2007-06-17 16:58:38 +02004831static int
4832acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4833 struct acl_expr *expr, struct acl_test *test)
4834{
4835 struct http_txn *txn = l7;
4836
Willy Tarreaub6866442008-07-14 23:54:42 +02004837 if (!txn)
4838 return 0;
4839
Willy Tarreauc11416f2007-06-17 16:58:38 +02004840 if (txn->req.msg_state != HTTP_MSG_BODY)
4841 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004842
Willy Tarreauc11416f2007-06-17 16:58:38 +02004843 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4844 /* ensure the indexes are not affected */
4845 return 0;
4846
4847 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4848}
4849
4850static int
4851acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4852 struct acl_expr *expr, struct acl_test *test)
4853{
4854 struct http_txn *txn = l7;
4855
Willy Tarreaub6866442008-07-14 23:54:42 +02004856 if (!txn)
4857 return 0;
4858
Willy Tarreauc11416f2007-06-17 16:58:38 +02004859 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4860 return 0;
4861
4862 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4863}
4864
Willy Tarreau737b0c12007-06-10 21:28:46 +02004865/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4866 * the first '/' after the possible hostname, and ends before the possible '?'.
4867 */
4868static int
4869acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4870 struct acl_expr *expr, struct acl_test *test)
4871{
4872 struct http_txn *txn = l7;
4873 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004874
Willy Tarreaub6866442008-07-14 23:54:42 +02004875 if (!txn)
4876 return 0;
4877
Willy Tarreauc11416f2007-06-17 16:58:38 +02004878 if (txn->req.msg_state != HTTP_MSG_BODY)
4879 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004880
Willy Tarreauc11416f2007-06-17 16:58:38 +02004881 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4882 /* ensure the indexes are not affected */
4883 return 0;
4884
Willy Tarreau21d2af32008-02-14 20:25:24 +01004885 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4886 ptr = http_get_path(txn);
4887 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004888 return 0;
4889
4890 /* OK, we got the '/' ! */
4891 test->ptr = ptr;
4892
4893 while (ptr < end && *ptr != '?')
4894 ptr++;
4895
4896 test->len = ptr - test->ptr;
4897
4898 /* we do not need to set READ_ONLY because the data is in a buffer */
4899 test->flags = ACL_TEST_F_VOL_1ST;
4900 return 1;
4901}
4902
4903
Willy Tarreau8797c062007-05-07 00:55:35 +02004904
4905/************************************************************************/
4906/* All supported keywords must be declared here. */
4907/************************************************************************/
4908
4909/* Note: must not be declared <const> as its list will be overwritten */
4910static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004911 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4912 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4913 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4914 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004915
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004916 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4917 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4918 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4919 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4920 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4921 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4922 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4923 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4924 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004925
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004926 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4927 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4928 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4929 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4930 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4931 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4932 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4933 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4934 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4935 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004936
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004937 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4938 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4939 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4940 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4941 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4942 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4943 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4944 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4945 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004946
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004947 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4948 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4949 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4950 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4951 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4952 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4953 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004954
Willy Tarreauf3d25982007-05-08 22:45:09 +02004955 { NULL, NULL, NULL, NULL },
4956
4957#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02004958 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
4959 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
4960 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
4961 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
4962 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
4963 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
4964 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
4965
Willy Tarreau8797c062007-05-07 00:55:35 +02004966 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
4967 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
4968 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
4969 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
4970 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
4971 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
4972 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
4973 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
4974
4975 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
4976 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
4977 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
4978 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
4979 { NULL, NULL, NULL, NULL },
4980#endif
4981}};
4982
4983
4984__attribute__((constructor))
4985static void __http_protocol_init(void)
4986{
4987 acl_register_keywords(&acl_kws);
4988}
4989
4990
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004992 * Local variables:
4993 * c-indent-level: 8
4994 * c-basic-offset: 8
4995 * End:
4996 */