blob: 6ffc14e48ce11a8d114a09f78e58bfb5aeabf800 [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>
Maik Broemme2850cb42009-04-17 18:53:21 +020044#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010053#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010055#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020056#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/task.h>
58
Willy Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#define DEBUG_PARSE_NO_SPEEDUP
64#undef DEBUG_PARSE_NO_SPEEDUP
65
Willy Tarreau976f1ee2006-12-17 10:06:03 +010066/* This is used to perform a quick jump as an alternative to a break/continue
67 * instruction. The first argument is the label for normal operation, and the
68 * second one is the break/continue instruction in the no_speedup mode.
69 */
70
71#ifdef DEBUG_PARSE_NO_SPEEDUP
72#define QUICK_JUMP(x,y) y
73#else
74#define QUICK_JUMP(x,y) goto x
75#endif
76
Willy Tarreau1c47f852006-07-09 08:22:27 +020077/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020079 "HTTP/1.0 200 OK\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Connection: close\r\n"
82 "Content-Type: text/html\r\n"
83 "\r\n"
84 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const struct chunk http_200_chunk = {
87 .str = (char *)&HTTP_200,
88 .len = sizeof(HTTP_200)-1
89};
90
Willy Tarreaub463dfb2008-06-07 23:08:56 +020091const char *HTTP_301 =
92 "HTTP/1.0 301 Moved Permantenly\r\n"
93 "Cache-Control: no-cache\r\n"
94 "Connection: close\r\n"
95 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Willy Tarreau0f772532006-12-23 20:51:41 +010097const char *HTTP_302 =
98 "HTTP/1.0 302 Found\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Connection: close\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 302 except that the browser MUST retry with the GET method */
104const char *HTTP_303 =
105 "HTTP/1.0 303 See Other\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Connection: close\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau0f772532006-12-23 20:51:41 +0100120
121const int http_err_codes[HTTP_ERR_SIZE] = {
122 [HTTP_ERR_400] = 400,
123 [HTTP_ERR_403] = 403,
124 [HTTP_ERR_408] = 408,
125 [HTTP_ERR_500] = 500,
126 [HTTP_ERR_502] = 502,
127 [HTTP_ERR_503] = 503,
128 [HTTP_ERR_504] = 504,
129};
130
Willy Tarreau80587432006-12-24 17:47:20 +0100131static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100133 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
139
140 [HTTP_ERR_403] =
141 "HTTP/1.0 403 Forbidden\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
147
148 [HTTP_ERR_408] =
149 "HTTP/1.0 408 Request Time-out\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
155
156 [HTTP_ERR_500] =
157 "HTTP/1.0 500 Server Error\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
163
164 [HTTP_ERR_502] =
165 "HTTP/1.0 502 Bad Gateway\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
171
172 [HTTP_ERR_503] =
173 "HTTP/1.0 503 Service Unavailable\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
179
180 [HTTP_ERR_504] =
181 "HTTP/1.0 504 Gateway Time-out\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
187
188};
189
Willy Tarreau80587432006-12-24 17:47:20 +0100190/* We must put the messages here since GCC cannot initialize consts depending
191 * on strlen().
192 */
193struct chunk http_err_chunks[HTTP_ERR_SIZE];
194
Willy Tarreau42250582007-04-01 01:30:43 +0200195#define FD_SETS_ARE_BITFIELDS
196#ifdef FD_SETS_ARE_BITFIELDS
197/*
198 * This map is used with all the FD_* macros to check whether a particular bit
199 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
200 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
201 * byte should be encoded. Be careful to always pass bytes from 0 to 255
202 * exclusively to the macros.
203 */
204fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
205fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
206
207#else
208#error "Check if your OS uses bitfields for fd_sets"
209#endif
210
Willy Tarreau80587432006-12-24 17:47:20 +0100211void init_proto_http()
212{
Willy Tarreau42250582007-04-01 01:30:43 +0200213 int i;
214 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100215 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200216
Willy Tarreau80587432006-12-24 17:47:20 +0100217 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
218 if (!http_err_msgs[msg]) {
219 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
220 abort();
221 }
222
223 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
224 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
225 }
Willy Tarreau42250582007-04-01 01:30:43 +0200226
227 /* initialize the log header encoding map : '{|}"#' should be encoded with
228 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
229 * URL encoding only requires '"', '#' to be encoded as well as non-
230 * printable characters above.
231 */
232 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
233 memset(url_encode_map, 0, sizeof(url_encode_map));
234 for (i = 0; i < 32; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238 for (i = 127; i < 256; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242
243 tmp = "\"#{|}";
244 while (*tmp) {
245 FD_SET(*tmp, hdr_encode_map);
246 tmp++;
247 }
248
249 tmp = "\"#";
250 while (*tmp) {
251 FD_SET(*tmp, url_encode_map);
252 tmp++;
253 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200254
255 /* memory allocations */
256 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200257 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100258}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259
Willy Tarreau53b6c742006-12-17 13:37:46 +0100260/*
261 * We have 26 list of methods (1 per first letter), each of which can have
262 * up to 3 entries (2 valid, 1 null).
263 */
264struct http_method_desc {
265 http_meth_t meth;
266 int len;
267 const char text[8];
268};
269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100270const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100271 ['C' - 'A'] = {
272 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
273 },
274 ['D' - 'A'] = {
275 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
276 },
277 ['G' - 'A'] = {
278 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
279 },
280 ['H' - 'A'] = {
281 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
282 },
283 ['P' - 'A'] = {
284 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
285 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
286 },
287 ['T' - 'A'] = {
288 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
289 },
290 /* rest is empty like this :
291 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
292 */
293};
294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100295/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200296 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100297 * RFC2616 for those chars.
298 */
299
300const char http_is_spht[256] = {
301 [' '] = 1, ['\t'] = 1,
302};
303
304const char http_is_crlf[256] = {
305 ['\r'] = 1, ['\n'] = 1,
306};
307
308const char http_is_lws[256] = {
309 [' '] = 1, ['\t'] = 1,
310 ['\r'] = 1, ['\n'] = 1,
311};
312
313const char http_is_sep[256] = {
314 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
315 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
316 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
317 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
318 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
319};
320
321const char http_is_ctl[256] = {
322 [0 ... 31] = 1,
323 [127] = 1,
324};
325
326/*
327 * A token is any ASCII char that is neither a separator nor a CTL char.
328 * Do not overwrite values in assignment since gcc-2.95 will not handle
329 * them correctly. Instead, define every non-CTL char's status.
330 */
331const char http_is_token[256] = {
332 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
333 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
334 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
335 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
336 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
337 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
338 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
339 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
340 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
341 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
342 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
343 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
344 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
345 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
346 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
347 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
348 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
349 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
350 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
351 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
352 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
353 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
354 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
355 ['|'] = 1, ['}'] = 0, ['~'] = 1,
356};
357
358
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100359/*
360 * An http ver_token is any ASCII which can be found in an HTTP version,
361 * which includes 'H', 'T', 'P', '/', '.' and any digit.
362 */
363const char http_is_ver_token[256] = {
364 ['.'] = 1, ['/'] = 1,
365 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
366 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
367 ['H'] = 1, ['P'] = 1, ['T'] = 1,
368};
369
370
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200372static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373#endif
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
376 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
377 * CRLF. Text length is measured first, so it cannot be NULL.
378 * The header is also automatically added to the index <hdr_idx>, and the end
379 * of headers is automatically adjusted. The number of bytes added is returned
380 * on success, otherwise <0 is returned indicating an error.
381 */
382int http_header_add_tail(struct buffer *b, struct http_msg *msg,
383 struct hdr_idx *hdr_idx, const char *text)
384{
385 int bytes, len;
386
387 len = strlen(text);
388 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
389 if (!bytes)
390 return -1;
391 msg->eoh += bytes;
392 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
393}
394
395/*
396 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
397 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
398 * the buffer is only opened and the space reserved, but nothing is copied.
399 * The header is also automatically added to the index <hdr_idx>, and the end
400 * of headers is automatically adjusted. The number of bytes added is returned
401 * on success, otherwise <0 is returned indicating an error.
402 */
403int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
404 struct hdr_idx *hdr_idx, const char *text, int len)
405{
406 int bytes;
407
408 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
409 if (!bytes)
410 return -1;
411 msg->eoh += bytes;
412 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
413}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414
415/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100416 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
417 * If so, returns the position of the first non-space character relative to
418 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
419 * to return a pointer to the place after the first space. Returns 0 if the
420 * header name does not match. Checks are case-insensitive.
421 */
422int http_header_match2(const char *hdr, const char *end,
423 const char *name, int len)
424{
425 const char *val;
426
427 if (hdr + len >= end)
428 return 0;
429 if (hdr[len] != ':')
430 return 0;
431 if (strncasecmp(hdr, name, len) != 0)
432 return 0;
433 val = hdr + len + 1;
434 while (val < end && HTTP_IS_SPHT(*val))
435 val++;
436 if ((val >= end) && (len + 2 <= end - hdr))
437 return len + 2; /* we may replace starting from second space */
438 return val - hdr;
439}
440
Willy Tarreau33a7e692007-06-10 19:45:56 +0200441/* Find the end of the header value contained between <s> and <e>.
442 * See RFC2616, par 2.2 for more information. Note that it requires
443 * a valid header to return a valid result.
444 */
445const char *find_hdr_value_end(const char *s, const char *e)
446{
447 int quoted, qdpair;
448
449 quoted = qdpair = 0;
450 for (; s < e; s++) {
451 if (qdpair) qdpair = 0;
452 else if (quoted && *s == '\\') qdpair = 1;
453 else if (quoted && *s == '"') quoted = 0;
454 else if (*s == '"') quoted = 1;
455 else if (*s == ',') return s;
456 }
457 return s;
458}
459
460/* Find the first or next occurrence of header <name> in message buffer <sol>
461 * using headers index <idx>, and return it in the <ctx> structure. This
462 * structure holds everything necessary to use the header and find next
463 * occurrence. If its <idx> member is 0, the header is searched from the
464 * beginning. Otherwise, the next occurrence is returned. The function returns
465 * 1 when it finds a value, and 0 when there is no more.
466 */
467int http_find_header2(const char *name, int len,
468 const char *sol, struct hdr_idx *idx,
469 struct hdr_ctx *ctx)
470{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100540/* This function handles a server error at the stream interface level. The
541 * stream interface is assumed to be already in a closed state. An optional
542 * message is copied into the input buffer, and an HTTP status code stored.
543 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100544 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100546static void http_server_error(struct session *t, struct stream_interface *si,
547 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100549 buffer_erase(si->ob);
550 buffer_erase(si->ib);
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100551 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100552 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100553 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100554 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreauefb453c2008-10-26 20:49:47 +0100648/* Returns a 302 for a redirectable request. This may only be called just after
649 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
650 * left unchanged and will follow normal proxy processing.
651 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100652void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100653{
654 struct http_txn *txn;
655 struct chunk rdr;
656 char *path;
657 int len;
658
659 /* 1: create the response header */
660 rdr.len = strlen(HTTP_302);
661 rdr.str = trash;
662 memcpy(rdr.str, HTTP_302, rdr.len);
663
664 /* 2: add the server's prefix */
665 if (rdr.len + s->srv->rdr_len > sizeof(trash))
666 return;
667
668 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
669 rdr.len += s->srv->rdr_len;
670
671 /* 3: add the request URI */
672 txn = &s->txn;
673 path = http_get_path(txn);
674 if (!path)
675 return;
676
677 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
678 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
679 return;
680
681 memcpy(rdr.str + rdr.len, path, len);
682 rdr.len += len;
683 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
684 rdr.len += 4;
685
686 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100687 si->shutr(si);
688 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689 si->err_type = SI_ET_NONE;
690 si->err_loc = NULL;
691 si->state = SI_ST_CLO;
692
693 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100694 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695
696 /* FIXME: we should increase a counter of redirects per server and per backend. */
697 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100698 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699}
700
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100701/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100702 * that the server side is closed. Note that err_type is actually a
703 * bitmask, where almost only aborts may be cumulated with other
704 * values. We consider that aborted operations are more important
705 * than timeouts or errors due to the fact that nobody else in the
706 * logs might explain incomplete retries. All others should avoid
707 * being cumulated. It should normally not be possible to have multiple
708 * aborts at once, but just in case, the first one in sequence is reported.
709 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100710void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100712 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713
714 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
716 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100718 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
719 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100720 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100721 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
722 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100723 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100724 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
725 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100726 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
728 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100730 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
731 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
734 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735}
736
Willy Tarreau42250582007-04-01 01:30:43 +0200737extern const char sess_term_cond[8];
738extern const char sess_fin_state[8];
739extern const char *monthname[12];
740const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
741const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
742 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
743 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200744struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200745struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100746
Willy Tarreau42250582007-04-01 01:30:43 +0200747/*
748 * send a log for the session when we have enough info about it.
749 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100750 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200752{
753 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
754 struct proxy *fe = s->fe;
755 struct proxy *be = s->be;
756 struct proxy *prx_log;
757 struct http_txn *txn = &s->txn;
758 int tolog;
759 char *uri, *h;
760 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200761 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200762 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200763 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200764 int hdr;
765
766 if (fe->logfac1 < 0 && fe->logfac2 < 0)
767 return;
768 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100769
Willy Tarreau42250582007-04-01 01:30:43 +0200770 if (s->cli_addr.ss_family == AF_INET)
771 inet_ntop(AF_INET,
772 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
773 pn, sizeof(pn));
774 else
775 inet_ntop(AF_INET6,
776 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
777 pn, sizeof(pn));
778
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200779 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200780
781 /* FIXME: let's limit ourselves to frontend logging for now. */
782 tolog = fe->to_log;
783
784 h = tmpline;
785 if (fe->to_log & LW_REQHDR &&
786 txn->req.cap &&
787 (h < tmpline + sizeof(tmpline) - 10)) {
788 *(h++) = ' ';
789 *(h++) = '{';
790 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
791 if (hdr)
792 *(h++) = '|';
793 if (txn->req.cap[hdr] != NULL)
794 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
795 '#', hdr_encode_map, txn->req.cap[hdr]);
796 }
797 *(h++) = '}';
798 }
799
800 if (fe->to_log & LW_RSPHDR &&
801 txn->rsp.cap &&
802 (h < tmpline + sizeof(tmpline) - 7)) {
803 *(h++) = ' ';
804 *(h++) = '{';
805 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
806 if (hdr)
807 *(h++) = '|';
808 if (txn->rsp.cap[hdr] != NULL)
809 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
810 '#', hdr_encode_map, txn->rsp.cap[hdr]);
811 }
812 *(h++) = '}';
813 }
814
815 if (h < tmpline + sizeof(tmpline) - 4) {
816 *(h++) = ' ';
817 *(h++) = '"';
818 uri = txn->uri ? txn->uri : "<BADREQ>";
819 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
820 '#', url_encode_map, uri);
821 *(h++) = '"';
822 }
823 *h = '\0';
824
825 svid = (tolog & LW_SVID) ?
826 (s->data_source != DATA_SRC_STATS) ?
827 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
828
Willy Tarreau70089872008-06-13 21:12:51 +0200829 t_request = -1;
830 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
831 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
832
Willy Tarreau42250582007-04-01 01:30:43 +0200833 send_log(prx_log, LOG_INFO,
834 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200835 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
836 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200837 pn,
838 (s->cli_addr.ss_family == AF_INET) ?
839 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
840 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200841 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200842 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200843 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200844 t_request,
845 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200846 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
847 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
848 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
849 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100850 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200851 txn->cli_cookie ? txn->cli_cookie : "-",
852 txn->srv_cookie ? txn->srv_cookie : "-",
853 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
854 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
855 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
856 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
857 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100858 (s->flags & SN_REDISP)?"+":"",
859 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200860 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
861
862 s->logs.logwait = 0;
863}
864
Willy Tarreau117f59e2007-03-04 18:17:17 +0100865
866/*
867 * Capture headers from message starting at <som> according to header list
868 * <cap_hdr>, and fill the <idx> structure appropriately.
869 */
870void capture_headers(char *som, struct hdr_idx *idx,
871 char **cap, struct cap_hdr *cap_hdr)
872{
873 char *eol, *sol, *col, *sov;
874 int cur_idx;
875 struct cap_hdr *h;
876 int len;
877
878 sol = som + hdr_idx_first_pos(idx);
879 cur_idx = hdr_idx_first_idx(idx);
880
881 while (cur_idx) {
882 eol = sol + idx->v[cur_idx].len;
883
884 col = sol;
885 while (col < eol && *col != ':')
886 col++;
887
888 sov = col + 1;
889 while (sov < eol && http_is_lws[(unsigned char)*sov])
890 sov++;
891
892 for (h = cap_hdr; h; h = h->next) {
893 if ((h->namelen == col - sol) &&
894 (strncasecmp(sol, h->name, h->namelen) == 0)) {
895 if (cap[h->index] == NULL)
896 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200897 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100898
899 if (cap[h->index] == NULL) {
900 Alert("HTTP capture : out of memory.\n");
901 continue;
902 }
903
904 len = eol - sov;
905 if (len > h->len)
906 len = h->len;
907
908 memcpy(cap[h->index], sov, len);
909 cap[h->index][len]=0;
910 }
911 }
912 sol = eol + idx->v[cur_idx].cr + 1;
913 cur_idx = idx->v[cur_idx].next;
914 }
915}
916
917
Willy Tarreau42250582007-04-01 01:30:43 +0200918/* either we find an LF at <ptr> or we jump to <bad>.
919 */
920#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
921
922/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
923 * otherwise to <http_msg_ood> with <state> set to <st>.
924 */
925#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
926 ptr++; \
927 if (likely(ptr < end)) \
928 goto good; \
929 else { \
930 state = (st); \
931 goto http_msg_ood; \
932 } \
933 } while (0)
934
935
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100937 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100938 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
939 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
940 * will give undefined results.
941 * Note that it is upon the caller's responsibility to ensure that ptr < end,
942 * and that msg->sol points to the beginning of the response.
943 * If a complete line is found (which implies that at least one CR or LF is
944 * found before <end>, the updated <ptr> is returned, otherwise NULL is
945 * returned indicating an incomplete line (which does not mean that parts have
946 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
947 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
948 * upon next call.
949 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
952 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200953 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100955const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
956 unsigned int state, const char *ptr, const char *end,
957 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100958{
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 switch (state) {
960 http_msg_rpver:
961 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100962 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
964
965 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100966 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
968 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100969 state = HTTP_MSG_ERROR;
970 break;
971
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 http_msg_rpver_sp:
973 case HTTP_MSG_RPVER_SP:
974 if (likely(!HTTP_IS_LWS(*ptr))) {
975 msg->sl.st.c = ptr - msg_buf;
976 goto http_msg_rpcode;
977 }
978 if (likely(HTTP_IS_SPHT(*ptr)))
979 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
980 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100981 state = HTTP_MSG_ERROR;
982 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100983
984 http_msg_rpcode:
985 case HTTP_MSG_RPCODE:
986 if (likely(!HTTP_IS_LWS(*ptr)))
987 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
988
989 if (likely(HTTP_IS_SPHT(*ptr))) {
990 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
991 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
992 }
993
994 /* so it's a CR/LF, so there is no reason phrase */
995 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
996 http_msg_rsp_reason:
997 /* FIXME: should we support HTTP responses without any reason phrase ? */
998 msg->sl.st.r = ptr - msg_buf;
999 msg->sl.st.r_l = 0;
1000 goto http_msg_rpline_eol;
1001
1002 http_msg_rpcode_sp:
1003 case HTTP_MSG_RPCODE_SP:
1004 if (likely(!HTTP_IS_LWS(*ptr))) {
1005 msg->sl.st.r = ptr - msg_buf;
1006 goto http_msg_rpreason;
1007 }
1008 if (likely(HTTP_IS_SPHT(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1010 /* so it's a CR/LF, so there is no reason phrase */
1011 goto http_msg_rsp_reason;
1012
1013 http_msg_rpreason:
1014 case HTTP_MSG_RPREASON:
1015 if (likely(!HTTP_IS_CRLF(*ptr)))
1016 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1017 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1018 http_msg_rpline_eol:
1019 /* We have seen the end of line. Note that we do not
1020 * necessarily have the \n yet, but at least we know that we
1021 * have EITHER \r OR \n, otherwise the response would not be
1022 * complete. We can then record the response length and return
1023 * to the caller which will be able to register it.
1024 */
1025 msg->sl.st.l = ptr - msg->sol;
1026 return ptr;
1027
1028#ifdef DEBUG_FULL
1029 default:
1030 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1031 exit(1);
1032#endif
1033 }
1034
1035 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001036 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001037 if (ret_state)
1038 *ret_state = state;
1039 if (ret_ptr)
1040 *ret_ptr = (char *)ptr;
1041 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001042}
1043
1044
1045/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001046 * This function parses a request line between <ptr> and <end>, starting with
1047 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1048 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1049 * will give undefined results.
1050 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1051 * and that msg->sol points to the beginning of the request.
1052 * If a complete line is found (which implies that at least one CR or LF is
1053 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1054 * returned indicating an incomplete line (which does not mean that parts have
1055 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1056 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1057 * upon next call.
1058 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1061 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001062 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001063 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001064const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1065 unsigned int state, const char *ptr, const char *end,
1066 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 switch (state) {
1069 http_msg_rqmeth:
1070 case HTTP_MSG_RQMETH:
1071 if (likely(HTTP_IS_TOKEN(*ptr)))
1072 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001075 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_CRLF(*ptr))) {
1080 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001081 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 http_msg_req09_uri:
1083 msg->sl.rq.u = ptr - msg_buf;
1084 http_msg_req09_uri_e:
1085 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1086 http_msg_req09_ver:
1087 msg->sl.rq.v = ptr - msg_buf;
1088 msg->sl.rq.v_l = 0;
1089 goto http_msg_rqline_eol;
1090 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001091 state = HTTP_MSG_ERROR;
1092 break;
1093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 http_msg_rqmeth_sp:
1095 case HTTP_MSG_RQMETH_SP:
1096 if (likely(!HTTP_IS_LWS(*ptr))) {
1097 msg->sl.rq.u = ptr - msg_buf;
1098 goto http_msg_rquri;
1099 }
1100 if (likely(HTTP_IS_SPHT(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1102 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1103 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 http_msg_rquri:
1106 case HTTP_MSG_RQURI:
1107 if (likely(!HTTP_IS_LWS(*ptr)))
1108 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 if (likely(HTTP_IS_SPHT(*ptr))) {
1111 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1116 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118 http_msg_rquri_sp:
1119 case HTTP_MSG_RQURI_SP:
1120 if (likely(!HTTP_IS_LWS(*ptr))) {
1121 msg->sl.rq.v = ptr - msg_buf;
1122 goto http_msg_rqver;
1123 }
1124 if (likely(HTTP_IS_SPHT(*ptr)))
1125 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1126 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1127 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 http_msg_rqver:
1130 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001131 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001133
1134 if (likely(HTTP_IS_CRLF(*ptr))) {
1135 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1136 http_msg_rqline_eol:
1137 /* We have seen the end of line. Note that we do not
1138 * necessarily have the \n yet, but at least we know that we
1139 * have EITHER \r OR \n, otherwise the request would not be
1140 * complete. We can then record the request length and return
1141 * to the caller which will be able to register it.
1142 */
1143 msg->sl.rq.l = ptr - msg->sol;
1144 return ptr;
1145 }
1146
1147 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001148 state = HTTP_MSG_ERROR;
1149 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151#ifdef DEBUG_FULL
1152 default:
1153 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1154 exit(1);
1155#endif
1156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001159 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 if (ret_state)
1161 *ret_state = state;
1162 if (ret_ptr)
1163 *ret_ptr = (char *)ptr;
1164 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001166
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168/*
1169 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001170 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 * when data are missing and recalled at the exact same location with no
1172 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001173 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1174 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001175 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1177{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001178 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001181 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 ptr = buf->lr;
1183 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (unlikely(ptr >= end))
1186 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 /*
1190 * First, states that are specific to the response only.
1191 * We check them first so that request and headers are
1192 * closer to each other (accessed more often).
1193 */
1194 http_msg_rpbefore:
1195 case HTTP_MSG_RPBEFORE:
1196 if (likely(HTTP_IS_TOKEN(*ptr))) {
1197 if (likely(ptr == buf->data)) {
1198 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001199 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 } else {
1201#if PARSE_PRESERVE_EMPTY_LINES
1202 /* only skip empty leading lines, don't remove them */
1203 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001204 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001205#else
1206 /* Remove empty leading lines, as recommended by
1207 * RFC2616. This takes a lot of time because we
1208 * must move all the buffer backwards, but this
1209 * is rarely needed. The method above will be
1210 * cleaner when we'll be able to start sending
1211 * the request from any place in the buffer.
1212 */
1213 buf->lr = ptr;
1214 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001215 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 msg->sol = buf->data;
1217 ptr = buf->data;
1218 end = buf->r;
1219#endif
1220 }
1221 hdr_idx_init(idx);
1222 state = HTTP_MSG_RPVER;
1223 goto http_msg_rpver;
1224 }
1225
1226 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1227 goto http_msg_invalid;
1228
1229 if (unlikely(*ptr == '\n'))
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1231 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1232 /* stop here */
1233
1234 http_msg_rpbefore_cr:
1235 case HTTP_MSG_RPBEFORE_CR:
1236 EXPECT_LF_HERE(ptr, http_msg_invalid);
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1238 /* stop here */
1239
1240 http_msg_rpver:
1241 case HTTP_MSG_RPVER:
1242 case HTTP_MSG_RPVER_SP:
1243 case HTTP_MSG_RPCODE:
1244 case HTTP_MSG_RPCODE_SP:
1245 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001246 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001247 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 if (unlikely(!ptr))
1249 return;
1250
1251 /* we have a full response and we know that we have either a CR
1252 * or an LF at <ptr>.
1253 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001254 //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 +01001255 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1256
1257 msg->sol = ptr;
1258 if (likely(*ptr == '\r'))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1260 goto http_msg_rpline_end;
1261
1262 http_msg_rpline_end:
1263 case HTTP_MSG_RPLINE_END:
1264 /* msg->sol must point to the first of CR or LF. */
1265 EXPECT_LF_HERE(ptr, http_msg_invalid);
1266 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1267 /* stop here */
1268
1269 /*
1270 * Second, states that are specific to the request only
1271 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 http_msg_rqbefore:
1273 case HTTP_MSG_RQBEFORE:
1274 if (likely(HTTP_IS_TOKEN(*ptr))) {
1275 if (likely(ptr == buf->data)) {
1276 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001277 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 } else {
1279#if PARSE_PRESERVE_EMPTY_LINES
1280 /* only skip empty leading lines, don't remove them */
1281 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001283#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 /* Remove empty leading lines, as recommended by
1285 * RFC2616. This takes a lot of time because we
1286 * must move all the buffer backwards, but this
1287 * is rarely needed. The method above will be
1288 * cleaner when we'll be able to start sending
1289 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001290 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 buf->lr = ptr;
1292 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001293 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001294 msg->sol = buf->data;
1295 ptr = buf->data;
1296 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001297#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001299 /* we will need this when keep-alive will be supported
1300 hdr_idx_init(idx);
1301 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 state = HTTP_MSG_RQMETH;
1303 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1307 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 if (unlikely(*ptr == '\n'))
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_rqbefore_cr:
1315 case HTTP_MSG_RQBEFORE_CR:
1316 EXPECT_LF_HERE(ptr, http_msg_invalid);
1317 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 http_msg_rqmeth:
1321 case HTTP_MSG_RQMETH:
1322 case HTTP_MSG_RQMETH_SP:
1323 case HTTP_MSG_RQURI:
1324 case HTTP_MSG_RQURI_SP:
1325 case HTTP_MSG_RQVER:
1326 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001327 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (unlikely(!ptr))
1329 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 /* we have a full request and we know that we have either a CR
1332 * or an LF at <ptr>.
1333 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001334 //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 +01001335 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 msg->sol = ptr;
1338 if (likely(*ptr == '\r'))
1339 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 http_msg_rqline_end:
1343 case HTTP_MSG_RQLINE_END:
1344 /* check for HTTP/0.9 request : no version information available.
1345 * msg->sol must point to the first of CR or LF.
1346 */
1347 if (unlikely(msg->sl.rq.v_l == 0))
1348 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 EXPECT_LF_HERE(ptr, http_msg_invalid);
1351 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001353
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 /*
1355 * Common states below
1356 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 http_msg_hdr_first:
1358 case HTTP_MSG_HDR_FIRST:
1359 msg->sol = ptr;
1360 if (likely(!HTTP_IS_CRLF(*ptr))) {
1361 goto http_msg_hdr_name;
1362 }
1363
1364 if (likely(*ptr == '\r'))
1365 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1366 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 http_msg_hdr_name:
1369 case HTTP_MSG_HDR_NAME:
1370 /* assumes msg->sol points to the first char */
1371 if (likely(HTTP_IS_TOKEN(*ptr)))
1372 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 if (likely(*ptr == ':')) {
1375 msg->col = ptr - buf->data;
1376 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001379 if (likely(msg->err_pos < -1) || *ptr == '\n')
1380 goto http_msg_invalid;
1381
1382 if (msg->err_pos == -1) /* capture error pointer */
1383 msg->err_pos = ptr - buf->data; /* >= 0 now */
1384
1385 /* and we still accept this non-token character */
1386 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_hdr_l1_sp:
1389 case HTTP_MSG_HDR_L1_SP:
1390 /* assumes msg->sol points to the first char and msg->col to the colon */
1391 if (likely(HTTP_IS_SPHT(*ptr)))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 /* header value can be basically anything except CR/LF */
1395 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (likely(!HTTP_IS_CRLF(*ptr))) {
1398 goto http_msg_hdr_val;
1399 }
1400
1401 if (likely(*ptr == '\r'))
1402 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1403 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 http_msg_hdr_l1_lf:
1406 case HTTP_MSG_HDR_L1_LF:
1407 EXPECT_LF_HERE(ptr, http_msg_invalid);
1408 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 http_msg_hdr_l1_lws:
1411 case HTTP_MSG_HDR_L1_LWS:
1412 if (likely(HTTP_IS_SPHT(*ptr))) {
1413 /* replace HT,CR,LF with spaces */
1414 for (; buf->data+msg->sov < ptr; msg->sov++)
1415 buf->data[msg->sov] = ' ';
1416 goto http_msg_hdr_l1_sp;
1417 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001418 /* we had a header consisting only in spaces ! */
1419 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 goto http_msg_complete_header;
1421
1422 http_msg_hdr_val:
1423 case HTTP_MSG_HDR_VAL:
1424 /* assumes msg->sol points to the first char, msg->col to the
1425 * colon, and msg->sov points to the first character of the
1426 * value.
1427 */
1428 if (likely(!HTTP_IS_CRLF(*ptr)))
1429 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 msg->eol = ptr;
1432 /* Note: we could also copy eol into ->eoh so that we have the
1433 * real header end in case it ends with lots of LWS, but is this
1434 * really needed ?
1435 */
1436 if (likely(*ptr == '\r'))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1438 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 http_msg_hdr_l2_lf:
1441 case HTTP_MSG_HDR_L2_LF:
1442 EXPECT_LF_HERE(ptr, http_msg_invalid);
1443 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 http_msg_hdr_l2_lws:
1446 case HTTP_MSG_HDR_L2_LWS:
1447 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1448 /* LWS: replace HT,CR,LF with spaces */
1449 for (; msg->eol < ptr; msg->eol++)
1450 *msg->eol = ' ';
1451 goto http_msg_hdr_val;
1452 }
1453 http_msg_complete_header:
1454 /*
1455 * It was a new header, so the last one is finished.
1456 * Assumes msg->sol points to the first char, msg->col to the
1457 * colon, msg->sov points to the first character of the value
1458 * and msg->eol to the first CR or LF so we know how the line
1459 * ends. We insert last header into the index.
1460 */
1461 /*
1462 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1463 write(2, msg->sol, msg->eol-msg->sol);
1464 fprintf(stderr,"\n");
1465 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1468 idx, idx->tail) < 0))
1469 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 msg->sol = ptr;
1472 if (likely(!HTTP_IS_CRLF(*ptr))) {
1473 goto http_msg_hdr_name;
1474 }
1475
1476 if (likely(*ptr == '\r'))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1478 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 http_msg_last_lf:
1481 case HTTP_MSG_LAST_LF:
1482 /* Assumes msg->sol points to the first of either CR or LF */
1483 EXPECT_LF_HERE(ptr, http_msg_invalid);
1484 ptr++;
1485 buf->lr = ptr;
1486 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 return;
1489#ifdef DEBUG_FULL
1490 default:
1491 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1492 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001493#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 }
1495 http_msg_ood:
1496 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001497 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 buf->lr = ptr;
1499 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 http_msg_invalid:
1502 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001503 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001504 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 return;
1506}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001507
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001508/* This function performs all the processing enabled for the current request.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001509 * It returns 1 if the processing can continue on next analysers, or zero if it
1510 * needs more data, encounters an error, or wants to immediately abort the
1511 * request. It relies on buffers flags, and updates s->req->analysers. Its
1512 * behaviour is rather simple:
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001513 * - all enabled analysers are called in turn from the lower to the higher
1514 * bit.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001515 * - the analyser must check for errors and timeouts, and react as expected.
1516 * It does not have to close anything upon error, the caller will.
1517 * - if the analyser does not have enough data, it must return 0without calling
1518 * other ones. It should also probably do a buffer_write_dis() to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001519 * that unprocessed data will not be forwarded. But that probably depends on
Willy Tarreauedcf6682008-11-30 23:15:34 +01001520 * the protocol.
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001521 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreauedcf6682008-11-30 23:15:34 +01001522 * analyser without using buffer_write_dis() (enabled by default).
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001523 * - if an analyser thinks it has no added value anymore staying here, it must
1524 * reset its bit from the analysers flags in order not to be called anymore.
1525 *
1526 * In the future, analysers should be able to indicate that they want to be
1527 * called after XXX bytes have been received (or transfered), and the min of
1528 * all's wishes will be used to ring back (unless a special condition occurs).
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001530int http_process_request(struct session *s, struct buffer *req)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531{
Willy Tarreau59234e92008-11-30 23:51:27 +01001532 /*
1533 * We will parse the partial (or complete) lines.
1534 * We will check the request syntax, and also join multi-line
1535 * headers. An index of all the lines will be elaborated while
1536 * parsing.
1537 *
1538 * For the parsing, we use a 28 states FSM.
1539 *
1540 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001541 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001542 * req->data + req->eoh = end of processed headers / start of current one
1543 * req->data + req->eol = end of current header or line (LF or CRLF)
1544 * req->lr = first non-visited byte
1545 * req->r = end of data
1546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau59234e92008-11-30 23:51:27 +01001548 int cur_idx;
1549 struct http_txn *txn = &s->txn;
1550 struct http_msg *msg = &txn->req;
1551 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau6bf17362009-02-24 10:48:35 +01001553 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1554 now_ms, __FUNCTION__,
1555 s,
1556 req,
1557 req->rex, req->wex,
1558 req->flags,
1559 req->l,
1560 req->analysers);
1561
Willy Tarreau59234e92008-11-30 23:51:27 +01001562 if (likely(req->lr < req->r))
1563 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001564
Willy Tarreau59234e92008-11-30 23:51:27 +01001565 /* 1: we might have to print this header in debug mode */
1566 if (unlikely((global.mode & MODE_DEBUG) &&
1567 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1568 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1569 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau59234e92008-11-30 23:51:27 +01001571 sol = req->data + msg->som;
1572 eol = sol + msg->sl.rq.l;
1573 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001574
Willy Tarreau59234e92008-11-30 23:51:27 +01001575 sol += hdr_idx_first_pos(&txn->hdr_idx);
1576 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau59234e92008-11-30 23:51:27 +01001578 while (cur_idx) {
1579 eol = sol + txn->hdr_idx.v[cur_idx].len;
1580 debug_hdr("clihdr", s, sol, eol);
1581 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1582 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001584 }
1585
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 /*
1588 * Now we quickly check if we have found a full valid request.
1589 * If not so, we check the FD and buffer states before leaving.
1590 * A full request is indicated by the fact that we have seen
1591 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1592 * requests are checked first.
1593 *
1594 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau59234e92008-11-30 23:51:27 +01001596 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001597 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001598 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001600 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1601 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau59234e92008-11-30 23:51:27 +01001603 /* 1: Since we are in header mode, if there's no space
1604 * left for headers, we won't be able to free more
1605 * later, so the session will never terminate. We
1606 * must terminate it now.
1607 */
1608 if (unlikely(req->flags & BF_FULL)) {
1609 /* FIXME: check if URI is set and return Status
1610 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001611 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001612 goto return_bad_req;
1613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001614
Willy Tarreau59234e92008-11-30 23:51:27 +01001615 /* 2: have we encountered a read error ? */
1616 else if (req->flags & BF_READ_ERROR) {
1617 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001618 if (msg->err_pos >= 0)
1619 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001620 msg->msg_state = HTTP_MSG_ERROR;
1621 req->analysers = 0;
1622 s->fe->failed_req++;
1623 if (!(s->flags & SN_ERR_MASK))
1624 s->flags |= SN_ERR_CLICL;
1625 if (!(s->flags & SN_FINST_MASK))
1626 s->flags |= SN_FINST_R;
1627 return 0;
1628 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001629
Willy Tarreau59234e92008-11-30 23:51:27 +01001630 /* 3: has the read timeout expired ? */
1631 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1632 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001633 if (msg->err_pos >= 0)
1634 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001635 txn->status = 408;
1636 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1637 msg->msg_state = HTTP_MSG_ERROR;
1638 req->analysers = 0;
1639 s->fe->failed_req++;
1640 if (!(s->flags & SN_ERR_MASK))
1641 s->flags |= SN_ERR_CLITO;
1642 if (!(s->flags & SN_FINST_MASK))
1643 s->flags |= SN_FINST_R;
1644 return 0;
1645 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001646
Willy Tarreau59234e92008-11-30 23:51:27 +01001647 /* 4: have we encountered a close ? */
1648 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001649 if (msg->err_pos >= 0)
1650 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001651 txn->status = 400;
1652 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1653 msg->msg_state = HTTP_MSG_ERROR;
1654 req->analysers = 0;
1655 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001656
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 if (!(s->flags & SN_ERR_MASK))
1658 s->flags |= SN_ERR_CLICL;
1659 if (!(s->flags & SN_FINST_MASK))
1660 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001661 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001662 }
1663
Willy Tarreau59234e92008-11-30 23:51:27 +01001664 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001665 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1666
Willy Tarreau59234e92008-11-30 23:51:27 +01001667 /* just set the request timeout once at the beginning of the request */
1668 if (!tick_isset(req->analyse_exp))
1669 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 /* we're not ready yet */
1672 return 0;
1673 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001674
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001675
Willy Tarreau59234e92008-11-30 23:51:27 +01001676 /****************************************************************
1677 * More interesting part now : we know that we have a complete *
1678 * request which at least looks like HTTP. We have an indicator *
1679 * of each header's length, so we can parse them quickly. *
1680 ****************************************************************/
Willy Tarreau9cdde232007-05-02 20:58:19 +02001681
Willy Tarreau4076a152009-04-02 15:18:36 +02001682 if (msg->err_pos >= 0)
1683 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1684
Willy Tarreau59234e92008-11-30 23:51:27 +01001685 req->analysers &= ~AN_REQ_HTTP_HDR;
1686 req->analyse_exp = TICK_ETERNITY;
1687
1688 /* ensure we keep this pointer to the beginning of the message */
1689 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 /*
1692 * 1: identify the method
1693 */
1694 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1695
1696 /* we can make use of server redirect on GET and HEAD */
1697 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1698 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001699
Willy Tarreau59234e92008-11-30 23:51:27 +01001700 /*
1701 * 2: check if the URI matches the monitor_uri.
1702 * We have to do this for every request which gets in, because
1703 * the monitor-uri is defined by the frontend.
1704 */
1705 if (unlikely((s->fe->monitor_uri_len != 0) &&
1706 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1707 !memcmp(&req->data[msg->sl.rq.u],
1708 s->fe->monitor_uri,
1709 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001712 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001713 struct acl_cond *cond;
1714 cur_proxy = s->fe;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001715
Willy Tarreau59234e92008-11-30 23:51:27 +01001716 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001717
Willy Tarreau59234e92008-11-30 23:51:27 +01001718 /* Check if we want to fail this monitor request or not */
1719 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1720 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001721
Willy Tarreau59234e92008-11-30 23:51:27 +01001722 ret = acl_pass(ret);
1723 if (cond->pol == ACL_COND_UNLESS)
1724 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001725
Willy Tarreau59234e92008-11-30 23:51:27 +01001726 if (ret) {
1727 /* we fail this request, let's return 503 service unavail */
1728 txn->status = 503;
1729 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1730 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001731 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001733
Willy Tarreau59234e92008-11-30 23:51:27 +01001734 /* nothing to fail, let's reply normaly */
1735 txn->status = 200;
1736 stream_int_retnclose(req->prod, &http_200_chunk);
1737 goto return_prx_cond;
1738 }
1739
1740 /*
1741 * 3: Maybe we have to copy the original REQURI for the logs ?
1742 * Note: we cannot log anymore if the request has been
1743 * classified as invalid.
1744 */
1745 if (unlikely(s->logs.logwait & LW_REQ)) {
1746 /* we have a complete HTTP request that we must log */
1747 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1748 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749
Willy Tarreau59234e92008-11-30 23:51:27 +01001750 if (urilen >= REQURI_LEN)
1751 urilen = REQURI_LEN - 1;
1752 memcpy(txn->uri, &req->data[msg->som], urilen);
1753 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754
Willy Tarreau59234e92008-11-30 23:51:27 +01001755 if (!(s->logs.logwait &= ~LW_REQ))
1756 s->do_log(s);
1757 } else {
1758 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001760 }
Willy Tarreau06619262006-12-17 08:37:22 +01001761
Willy Tarreau59234e92008-11-30 23:51:27 +01001762 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1763 if (unlikely(msg->sl.rq.v_l == 0)) {
1764 int delta;
1765 char *cur_end;
1766 msg->sol = req->data + msg->som;
1767 cur_end = msg->sol + msg->sl.rq.l;
1768 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001769
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 if (msg->sl.rq.u_l == 0) {
1771 /* if no URI was set, add "/" */
1772 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001776 /* add HTTP version */
1777 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1778 msg->eoh += delta;
1779 cur_end += delta;
1780 cur_end = (char *)http_parse_reqline(msg, req->data,
1781 HTTP_MSG_RQMETH,
1782 msg->sol, cur_end + 1,
1783 NULL, NULL);
1784 if (unlikely(!cur_end))
1785 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 /* we have a full HTTP/1.0 request now and we know that
1788 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001790 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1791 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
1793
Willy Tarreau59234e92008-11-30 23:51:27 +01001794 /* 5: we may need to capture headers */
1795 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1796 capture_headers(req->data + msg->som, &txn->hdr_idx,
1797 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001798
Willy Tarreau59234e92008-11-30 23:51:27 +01001799 /*
1800 * 6: we will have to evaluate the filters.
1801 * As opposed to version 1.2, now they will be evaluated in the
1802 * filters order and not in the header order. This means that
1803 * each filter has to be validated among all headers.
1804 *
1805 * We can now check whether we want to switch to another
1806 * backend, in which case we will re-check the backend's
1807 * filters and various options. In order to support 3-level
1808 * switching, here's how we should proceed :
1809 *
1810 * a) run be.
1811 * if (switch) then switch ->be to the new backend.
1812 * b) run be if (be != fe).
1813 * There cannot be any switch from there, so ->be cannot be
1814 * changed anymore.
1815 *
1816 * => filters always apply to ->be, then ->be may change.
1817 *
1818 * The response path will be able to apply either ->be, or
1819 * ->be then ->fe filters in order to match the reverse of
1820 * the forward sequence.
1821 */
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001822
Willy Tarreau59234e92008-11-30 23:51:27 +01001823 do {
1824 struct acl_cond *cond;
1825 struct redirect_rule *rule;
1826 struct proxy *rule_set = s->be;
1827 cur_proxy = s->be;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001828
Willy Tarreau59234e92008-11-30 23:51:27 +01001829 /* first check whether we have some ACLs set to redirect this request */
1830 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1831 int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001832
Willy Tarreau59234e92008-11-30 23:51:27 +01001833 ret = acl_pass(ret);
1834 if (rule->cond->pol == ACL_COND_UNLESS)
1835 ret = !ret;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001836
Willy Tarreau59234e92008-11-30 23:51:27 +01001837 if (ret) {
1838 struct chunk rdr = { trash, 0 };
1839 const char *msg_fmt;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001840
Willy Tarreau59234e92008-11-30 23:51:27 +01001841 /* build redirect message */
1842 switch(rule->code) {
1843 case 303:
1844 rdr.len = strlen(HTTP_303);
1845 msg_fmt = HTTP_303;
1846 break;
1847 case 301:
1848 rdr.len = strlen(HTTP_301);
1849 msg_fmt = HTTP_301;
1850 break;
1851 case 302:
1852 default:
1853 rdr.len = strlen(HTTP_302);
1854 msg_fmt = HTTP_302;
1855 break;
1856 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001857
Willy Tarreau59234e92008-11-30 23:51:27 +01001858 if (unlikely(rdr.len > sizeof(trash)))
1859 goto return_bad_req;
1860 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001861
Willy Tarreau59234e92008-11-30 23:51:27 +01001862 switch(rule->type) {
1863 case REDIRECT_TYPE_PREFIX: {
1864 const char *path;
1865 int pathlen;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001866
Willy Tarreau59234e92008-11-30 23:51:27 +01001867 path = http_get_path(txn);
1868 /* build message using path */
1869 if (path) {
1870 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Willy Tarreau79da4692008-11-19 20:03:04 +01001871 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1872 int qs = 0;
1873 while (qs < pathlen) {
1874 if (path[qs] == '?') {
1875 pathlen = qs;
1876 break;
1877 }
1878 qs++;
1879 }
1880 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001881 } else {
1882 path = "/";
1883 pathlen = 1;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001884 }
1885
Willy Tarreau59234e92008-11-30 23:51:27 +01001886 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1887 goto return_bad_req;
1888
Willy Tarreaufe651a52008-11-19 21:15:17 +01001889 /* add prefix. Note that if prefix == "/", we don't want to
1890 * add anything, otherwise it makes it hard for the user to
1891 * configure a self-redirection.
1892 */
1893 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
1894 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1895 rdr.len += rule->rdr_len;
1896 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001897
Willy Tarreau59234e92008-11-30 23:51:27 +01001898 /* add path */
1899 memcpy(rdr.str + rdr.len, path, pathlen);
1900 rdr.len += pathlen;
1901 break;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001902 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001903 case REDIRECT_TYPE_LOCATION:
1904 default:
1905 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1906 goto return_bad_req;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001907
Willy Tarreau59234e92008-11-30 23:51:27 +01001908 /* add location */
1909 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1910 rdr.len += rule->rdr_len;
1911 break;
1912 }
Willy Tarreau11382812008-07-09 16:18:21 +02001913
Willy Tarreau0140f252008-11-19 21:07:09 +01001914 if (rule->cookie_len) {
1915 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
1916 rdr.len += 14;
1917 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
1918 rdr.len += rule->cookie_len;
1919 memcpy(rdr.str + rdr.len, "\r\n", 2);
1920 rdr.len += 2;
1921 }
1922
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 /* add end of headers */
1924 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1925 rdr.len += 4;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001926
Willy Tarreau59234e92008-11-30 23:51:27 +01001927 txn->status = rule->code;
1928 /* let's log the request time */
1929 s->logs.tv_request = now;
1930 stream_int_retnclose(req->prod, &rdr);
1931 goto return_prx_cond;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001932 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001934
Willy Tarreau59234e92008-11-30 23:51:27 +01001935 /* first check whether we have some ACLs set to block this request */
1936 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1937 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau53b6c742006-12-17 13:37:46 +01001938
Willy Tarreau59234e92008-11-30 23:51:27 +01001939 ret = acl_pass(ret);
1940 if (cond->pol == ACL_COND_UNLESS)
1941 ret = !ret;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001942
Willy Tarreau59234e92008-11-30 23:51:27 +01001943 if (ret) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001944 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001945 /* let's log the request time */
Willy Tarreau59234e92008-11-30 23:51:27 +01001946 s->logs.tv_request = now;
1947 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001948 goto return_prx_cond;
1949 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 }
1951
1952 /* try headers filters */
1953 if (rule_set->req_exp != NULL) {
1954 if (apply_filters_to_request(s, req, rule_set->req_exp) < 0)
1955 goto return_bad_req;
1956 }
1957
1958 if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) {
1959 /* to ensure correct connection accounting on
1960 * the backend, we count the connection for the
1961 * one managing the queue.
1962 */
1963 s->be->beconn++;
1964 if (s->be->beconn > s->be->beconn_max)
1965 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01001966 proxy_inc_be_ctr(s->be);
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 s->flags |= SN_BE_ASSIGNED;
1968 }
Willy Tarreau06619262006-12-17 08:37:22 +01001969
Willy Tarreau59234e92008-11-30 23:51:27 +01001970 /* has the request been denied ? */
1971 if (txn->flags & TX_CLDENY) {
1972 /* no need to go further */
1973 txn->status = 403;
1974 /* let's log the request time */
1975 s->logs.tv_request = now;
1976 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1977 goto return_prx_cond;
1978 }
Willy Tarreau06619262006-12-17 08:37:22 +01001979
Willy Tarreau59234e92008-11-30 23:51:27 +01001980 /* We might have to check for "Connection:" */
1981 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1982 !(s->flags & SN_CONN_CLOSED)) {
1983 char *cur_ptr, *cur_end, *cur_next;
1984 int cur_idx, old_idx, delta, val;
1985 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001986
Willy Tarreau59234e92008-11-30 23:51:27 +01001987 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1988 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989
Willy Tarreau59234e92008-11-30 23:51:27 +01001990 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1991 cur_hdr = &txn->hdr_idx.v[cur_idx];
1992 cur_ptr = cur_next;
1993 cur_end = cur_ptr + cur_hdr->len;
1994 cur_next = cur_end + cur_hdr->cr + 1;
1995
1996 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1997 if (val) {
1998 /* 3 possibilities :
1999 * - we have already set Connection: close,
2000 * so we remove this line.
2001 * - we have not yet set Connection: close,
2002 * but this line indicates close. We leave
2003 * it untouched and set the flag.
2004 * - we have not yet set Connection: close,
2005 * and this line indicates non-close. We
2006 * replace it.
2007 */
2008 if (s->flags & SN_CONN_CLOSED) {
2009 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2010 txn->req.eoh += delta;
2011 cur_next += delta;
2012 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2013 txn->hdr_idx.used--;
2014 cur_hdr->len = 0;
2015 } else {
2016 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2017 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2018 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002019 cur_next += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01002020 cur_hdr->len += delta;
2021 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002022 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002024 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002026 old_idx = cur_idx;
Willy Tarreau06619262006-12-17 08:37:22 +01002027 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002028 }
2029 /* add request headers from the rule sets in the same order */
2030 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2031 if (unlikely(http_header_add_tail(req,
2032 &txn->req,
2033 &txn->hdr_idx,
2034 rule_set->req_add[cur_idx])) < 0)
2035 goto return_bad_req;
2036 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002037
Willy Tarreau59234e92008-11-30 23:51:27 +01002038 /* check if stats URI was requested, and if an auth is needed */
2039 if (rule_set->uri_auth != NULL &&
2040 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2041 /* we have to check the URI and auth for this request.
2042 * FIXME!!! that one is rather dangerous, we want to
2043 * make it follow standard rules (eg: clear req->analysers).
2044 */
2045 if (stats_check_uri_auth(s, rule_set)) {
2046 req->analysers = 0;
2047 return 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01002048 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002049 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002050
Willy Tarreau59234e92008-11-30 23:51:27 +01002051 /* now check whether we have some switching rules for this request */
2052 if (!(s->flags & SN_BE_ASSIGNED)) {
2053 struct switching_rule *rule;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002054
Willy Tarreau59234e92008-11-30 23:51:27 +01002055 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2056 int ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002057
Willy Tarreau59234e92008-11-30 23:51:27 +01002058 ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002059
Willy Tarreau59234e92008-11-30 23:51:27 +01002060 ret = acl_pass(ret);
2061 if (rule->cond->pol == ACL_COND_UNLESS)
2062 ret = !ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002063
Willy Tarreau59234e92008-11-30 23:51:27 +01002064 if (ret) {
2065 s->be = rule->be.backend;
2066 s->be->beconn++;
2067 if (s->be->beconn > s->be->beconn_max)
2068 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002069 proxy_inc_be_ctr(s->be);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002070
Willy Tarreau59234e92008-11-30 23:51:27 +01002071 /* assign new parameters to the session from the new backend */
2072 s->rep->rto = s->req->wto = s->be->timeout.server;
2073 s->req->cto = s->be->timeout.connect;
2074 s->conn_retries = s->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02002075 if (s->be->options2 & PR_O2_RSPBUG_OK)
2076 s->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau59234e92008-11-30 23:51:27 +01002077 s->flags |= SN_BE_ASSIGNED;
2078 break;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002079 }
2080 }
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002081 }
2082
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2084 /* No backend was set, but there was a default
2085 * backend set in the frontend, so we use it and
2086 * loop again.
2087 */
2088 s->be = cur_proxy->defbe.be;
2089 s->be->beconn++;
2090 if (s->be->beconn > s->be->beconn_max)
2091 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002092 proxy_inc_be_ctr(s->be);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002093
Willy Tarreau59234e92008-11-30 23:51:27 +01002094 /* assign new parameters to the session from the new backend */
2095 s->rep->rto = s->req->wto = s->be->timeout.server;
2096 s->req->cto = s->be->timeout.connect;
2097 s->conn_retries = s->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02002098 if (s->be->options2 & PR_O2_RSPBUG_OK)
2099 s->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 s->flags |= SN_BE_ASSIGNED;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002101 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 } while (s->be != cur_proxy); /* we loop only if s->be has changed */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 if (!(s->flags & SN_BE_ASSIGNED)) {
2105 /* To ensure correct connection accounting on
2106 * the backend, we count the connection for the
2107 * one managing the queue.
Willy Tarreau06619262006-12-17 08:37:22 +01002108 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002109 s->be->beconn++;
2110 if (s->be->beconn > s->be->beconn_max)
2111 s->be->beconn_max = s->be->beconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +01002112 proxy_inc_be_ctr(s->be);
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 s->flags |= SN_BE_ASSIGNED;
2114 }
Willy Tarreau06619262006-12-17 08:37:22 +01002115
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 /*
2117 * Right now, we know that we have processed the entire headers
2118 * and that unwanted requests have been filtered out. We can do
2119 * whatever we want with the remaining request. Also, now we
2120 * may have separate values for ->fe, ->be.
2121 */
Willy Tarreau06619262006-12-17 08:37:22 +01002122
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 /*
2124 * If HTTP PROXY is set we simply get remote server address
2125 * parsing incoming request.
2126 */
2127 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2128 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2129 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002130
Willy Tarreau59234e92008-11-30 23:51:27 +01002131 /*
2132 * 7: the appsession cookie was looked up very early in 1.2,
2133 * so let's do the same now.
2134 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002135
Willy Tarreau59234e92008-11-30 23:51:27 +01002136 /* It needs to look into the URI */
2137 if (s->be->appsession_name) {
2138 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2139 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 /*
2142 * 8: Now we can work with the cookies.
2143 * Note that doing so might move headers in the request, but
2144 * the fields will stay coherent and the URI will not move.
2145 * This should only be performed in the backend.
2146 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002147 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002148 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2149 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002150
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 /*
2152 * 9: add X-Forwarded-For if either the frontend or the backend
2153 * asks for it.
2154 */
2155 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2156 if (s->cli_addr.ss_family == AF_INET) {
2157 /* Add an X-Forwarded-For header unless the source IP is
2158 * in the 'except' network range.
2159 */
2160 if ((!s->fe->except_mask.s_addr ||
2161 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2162 != s->fe->except_net.s_addr) &&
2163 (!s->be->except_mask.s_addr ||
2164 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2165 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002166 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 unsigned char *pn;
2168 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002169
2170 /* Note: we rely on the backend to get the header name to be used for
2171 * x-forwarded-for, because the header is really meant for the backends.
2172 * However, if the backend did not specify any option, we have to rely
2173 * on the frontend's header name.
2174 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 if (s->be->fwdfor_hdr_len) {
2176 len = s->be->fwdfor_hdr_len;
2177 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002178 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002179 len = s->fe->fwdfor_hdr_len;
2180 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2181 }
2182 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002183
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002184 if (unlikely(http_header_add_tail2(req, &txn->req,
2185 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002186 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002187 }
2188 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 else if (s->cli_addr.ss_family == AF_INET6) {
2190 /* FIXME: for the sake of completeness, we should also support
2191 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002192 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 int len;
2194 char pn[INET6_ADDRSTRLEN];
2195 inet_ntop(AF_INET6,
2196 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2197 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002198
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 /* Note: we rely on the backend to get the header name to be used for
2200 * x-forwarded-for, because the header is really meant for the backends.
2201 * However, if the backend did not specify any option, we have to rely
2202 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002203 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 if (s->be->fwdfor_hdr_len) {
2205 len = s->be->fwdfor_hdr_len;
2206 memcpy(trash, s->be->fwdfor_hdr_name, len);
2207 } else {
2208 len = s->fe->fwdfor_hdr_len;
2209 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002210 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002212
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 if (unlikely(http_header_add_tail2(req, &txn->req,
2214 &txn->hdr_idx, trash, len)) < 0)
2215 goto return_bad_req;
2216 }
2217 }
2218
2219 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002220 * 10: add X-Original-To if either the frontend or the backend
2221 * asks for it.
2222 */
2223 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2224
2225 /* FIXME: don't know if IPv6 can handle that case too. */
2226 if (s->cli_addr.ss_family == AF_INET) {
2227 /* Add an X-Original-To header unless the destination IP is
2228 * in the 'except' network range.
2229 */
2230 if (!(s->flags & SN_FRT_ADDR_SET))
2231 get_frt_addr(s);
2232
2233 if ((!s->fe->except_mask_to.s_addr ||
2234 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2235 != s->fe->except_to.s_addr) &&
2236 (!s->be->except_mask_to.s_addr ||
2237 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2238 != s->be->except_to.s_addr)) {
2239 int len;
2240 unsigned char *pn;
2241 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2242
2243 /* Note: we rely on the backend to get the header name to be used for
2244 * x-original-to, because the header is really meant for the backends.
2245 * However, if the backend did not specify any option, we have to rely
2246 * on the frontend's header name.
2247 */
2248 if (s->be->orgto_hdr_len) {
2249 len = s->be->orgto_hdr_len;
2250 memcpy(trash, s->be->orgto_hdr_name, len);
2251 } else {
2252 len = s->fe->orgto_hdr_len;
2253 memcpy(trash, s->fe->orgto_hdr_name, len);
2254 }
2255 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2256
2257 if (unlikely(http_header_add_tail2(req, &txn->req,
2258 &txn->hdr_idx, trash, len)) < 0)
2259 goto return_bad_req;
2260 }
2261 }
2262 }
2263
2264 /*
2265 * 11: add "Connection: close" if needed and not yet set.
Willy Tarreau59234e92008-11-30 23:51:27 +01002266 * Note that we do not need to add it in case of HTTP/1.0.
2267 */
2268 if (!(s->flags & SN_CONN_CLOSED) &&
2269 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2270 if ((unlikely(msg->sl.rq.v_l != 8) ||
2271 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2272 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2273 "Connection: close", 17)) < 0)
2274 goto return_bad_req;
2275 s->flags |= SN_CONN_CLOSED;
2276 }
2277 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2278 * If not assigned, perhaps we are balancing on url_param, but this is a
2279 * POST; and the parameters are in the body, maybe scan there to find our server.
2280 * (unless headers overflowed the buffer?)
2281 */
2282 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2283 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2284 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2285 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2286 /* are there enough bytes here? total == l || r || rlim ?
2287 * len is unsigned, but eoh is int,
2288 * how many bytes of body have we received?
2289 * eoh is the first empty line of the header
2290 */
2291 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2292 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2293
2294 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2295 * We can't assume responsibility for the server's decision,
2296 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2297 * We also can't change our mind later, about which server to choose, so round robin.
2298 */
2299 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2300 struct hdr_ctx ctx;
2301 ctx.idx = 0;
2302 /* Expect is allowed in 1.1, look for it */
2303 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2304 if (ctx.idx != 0 &&
2305 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2306 /* We can't reliablly stall and wait for data, because of
2307 * .NET clients that don't conform to rfc2616; so, no need for
2308 * the next block to check length expectations.
2309 * We could send 100 status back to the client, but then we need to
2310 * re-write headers, and send the message. And this isn't the right
2311 * place for that action.
2312 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002313 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002314 goto end_check_maybe_wait_for_body;
2315 }
2316
2317 if (likely(len > s->be->url_param_post_limit)) {
2318 /* nothing to do, we got enough */
2319 } else {
2320 /* limit implies we are supposed to need this many bytes
2321 * to find the parameter. Let's see how many bytes we can wait for.
2322 */
2323 long long hint = len;
2324 struct hdr_ctx ctx;
2325 ctx.idx = 0;
2326 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2327 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2328 buffer_write_dis(req);
2329 req->analysers |= AN_REQ_HTTP_BODY;
2330 }
2331 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002332 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2334 /* now if we have a length, we'll take the hint */
2335 if (ctx.idx) {
2336 /* We have Content-Length */
2337 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2338 hint = 0; /* parse failure, untrusted client */
2339 else {
2340 if (hint > 0)
2341 msg->hdr_content_len = hint;
2342 else
2343 hint = 0; /* bad client, sent negative length */
2344 }
2345 }
2346 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2347 if (s->be->url_param_post_limit < hint)
2348 hint = s->be->url_param_post_limit;
2349 /* now do we really need to buffer more data? */
2350 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002351 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002352 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002353 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002355 }
2356 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 }
2358 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 /*************************************************************
2361 * OK, that's finished for the headers. We have done what we *
2362 * could. Let's switch to the DATA state. *
2363 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2366 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 /* When a connection is tarpitted, we use the tarpit timeout,
2369 * which may be the same as the connect timeout if unspecified.
2370 * If unset, then set it to zero because we really want it to
2371 * eventually expire. We build the tarpit as an analyser.
2372 */
2373 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002374 buffer_erase(s->req);
2375 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002377 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 buffer_write_dis(req);
2379 req->analysers |= AN_REQ_HTTP_TARPIT;
2380 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2381 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002382 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 /* OK let's go on with the BODY now */
2386 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002389 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002390 /* we detected a parsing error. We want to archive this request
2391 * in the dedicated proxy area for later troubleshooting.
2392 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002393 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002394 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002395
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 txn->req.msg_state = HTTP_MSG_ERROR;
2397 txn->status = 400;
2398 req->analysers = 0;
2399 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2400 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 return_prx_cond:
2403 if (!(s->flags & SN_ERR_MASK))
2404 s->flags |= SN_ERR_PRXCOND;
2405 if (!(s->flags & SN_FINST_MASK))
2406 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002407 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002408}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002409
Willy Tarreau60b85b02008-11-30 23:28:40 +01002410/* This function is an analyser which processes the HTTP tarpit. It always
2411 * returns zero, at the beginning because it prevents any other processing
2412 * from occurring, and at the end because it terminates the request.
2413 */
2414int http_process_tarpit(struct session *s, struct buffer *req)
2415{
2416 struct http_txn *txn = &s->txn;
2417
2418 /* This connection is being tarpitted. The CLIENT side has
2419 * already set the connect expiration date to the right
2420 * timeout. We just have to check that the client is still
2421 * there and that the timeout has not expired.
2422 */
2423 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2424 !tick_is_expired(req->analyse_exp, now_ms))
2425 return 0;
2426
2427 /* We will set the queue timer to the time spent, just for
2428 * logging purposes. We fake a 500 server error, so that the
2429 * attacker will not suspect his connection has been tarpitted.
2430 * It will not cause trouble to the logs because we can exclude
2431 * the tarpitted connections by filtering on the 'PT' status flags.
2432 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002433 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2434
2435 txn->status = 500;
2436 if (req->flags != BF_READ_ERROR)
2437 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2438
2439 req->analysers = 0;
2440 req->analyse_exp = TICK_ETERNITY;
2441
2442 s->fe->failed_req++;
2443 if (!(s->flags & SN_ERR_MASK))
2444 s->flags |= SN_ERR_PRXCOND;
2445 if (!(s->flags & SN_FINST_MASK))
2446 s->flags |= SN_FINST_T;
2447 return 0;
2448}
2449
Willy Tarreaud34af782008-11-30 23:36:37 +01002450/* This function is an analyser which processes the HTTP request body. It looks
2451 * for parameters to be used for the load balancing algorithm (url_param). It
2452 * must only be called after the standard HTTP request processing has occurred,
2453 * because it expects the request to be parsed. It returns zero if it needs to
2454 * read more data, or 1 once it has completed its analysis.
2455 */
2456int http_process_request_body(struct session *s, struct buffer *req)
2457{
2458 struct http_msg *msg = &s->txn.req;
2459 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2460 long long limit = s->be->url_param_post_limit;
2461 struct hdr_ctx ctx;
2462
2463 /* We have to parse the HTTP request body to find any required data.
2464 * "balance url_param check_post" should have been the only way to get
2465 * into this. We were brought here after HTTP header analysis, so all
2466 * related structures are ready.
2467 */
2468
2469 ctx.idx = 0;
2470
2471 /* now if we have a length, we'll take the hint */
2472 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2473 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2474 unsigned int chunk = 0;
2475 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2476 char c = msg->sol[body];
2477 if (ishex(c)) {
2478 unsigned int hex = toupper(c) - '0';
2479 if (hex > 9)
2480 hex -= 'A' - '9' - 1;
2481 chunk = (chunk << 4) | hex;
2482 } else
2483 break;
2484 body++;
2485 }
2486 if (body + 2 >= req->l) /* we want CRLF too */
2487 goto http_body_end; /* end of buffer? data missing! */
2488
2489 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2490 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2491
2492 body += 2; // skip CRLF
2493
2494 /* if we support more then one chunk here, we have to do it again when assigning server
2495 * 1. how much entity data do we have? new var
2496 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2497 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2498 */
2499
2500 if (chunk < limit)
2501 limit = chunk; /* only reading one chunk */
2502 } else {
2503 if (msg->hdr_content_len < limit)
2504 limit = msg->hdr_content_len;
2505 }
2506
2507 http_body_end:
2508 /* we leave once we know we have nothing left to do. This means that we have
2509 * enough bytes, or that we know we'll not get any more (buffer full, read
2510 * buffer closed).
2511 */
2512 if (req->l - body >= limit || /* enough bytes! */
2513 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2514 tick_is_expired(req->analyse_exp, now_ms)) {
2515 /* The situation will not evolve, so let's give up on the analysis. */
2516 s->logs.tv_request = now; /* update the request timer to reflect full request */
2517 req->analysers &= ~AN_REQ_HTTP_BODY;
2518 req->analyse_exp = TICK_ETERNITY;
2519 return 1;
2520 }
2521 else {
2522 /* Not enough data. We'll re-use the http-request
2523 * timeout here. Ideally, we should set the timeout
2524 * relative to the accept() date. We just set the
2525 * request timeout once at the beginning of the
2526 * request.
2527 */
2528 buffer_write_dis(req);
2529 if (!tick_isset(req->analyse_exp))
2530 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2531 return 0;
2532 }
2533}
2534
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002535/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002536 * It normally returns zero, but may return 1 if it absolutely needs to be
2537 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002538 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002539 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002540 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002541int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002542{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002543 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002544 struct buffer *req = t->req;
2545 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002546
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002547 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 +02002548 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002549 t,
2550 rep,
2551 rep->rex, rep->wex,
2552 rep->flags,
2553 rep->l,
2554 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002555
Willy Tarreau2df28e82008-08-17 15:20:19 +02002556 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002557 /*
2558 * Now parse the partial (or complete) lines.
2559 * We will check the response syntax, and also join multi-line
2560 * headers. An index of all the lines will be elaborated while
2561 * parsing.
2562 *
2563 * For the parsing, we use a 28 states FSM.
2564 *
2565 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002566 * rep->data + rep->som = beginning of response
2567 * rep->data + rep->eoh = end of processed headers / start of current one
2568 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002569 * rep->lr = first non-visited byte
2570 * rep->r = end of data
2571 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002572
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002573 int cur_idx;
2574 struct http_msg *msg = &txn->rsp;
2575 struct proxy *cur_proxy;
2576
2577 if (likely(rep->lr < rep->r))
2578 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2579
2580 /* 1: we might have to print this header in debug mode */
2581 if (unlikely((global.mode & MODE_DEBUG) &&
2582 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2583 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2584 char *eol, *sol;
2585
2586 sol = rep->data + msg->som;
2587 eol = sol + msg->sl.rq.l;
2588 debug_hdr("srvrep", t, sol, eol);
2589
2590 sol += hdr_idx_first_pos(&txn->hdr_idx);
2591 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2592
2593 while (cur_idx) {
2594 eol = sol + txn->hdr_idx.v[cur_idx].len;
2595 debug_hdr("srvhdr", t, sol, eol);
2596 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2597 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002598 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002600
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002601 /*
2602 * Now we quickly check if we have found a full valid response.
2603 * If not so, we check the FD and buffer states before leaving.
2604 * A full response is indicated by the fact that we have seen
2605 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2606 * responses are checked first.
2607 *
2608 * Depending on whether the client is still there or not, we
2609 * may send an error response back or not. Note that normally
2610 * we should only check for HTTP status there, and check I/O
2611 * errors somewhere else.
2612 */
2613
2614 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002615 /* Invalid response */
2616 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002617 /* we detected a parsing error. We want to archive this response
2618 * in the dedicated proxy area for later troubleshooting.
2619 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002620 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002621 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2622 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2623
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002624 buffer_shutr_now(rep);
2625 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002626 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002627 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002628 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002629 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002630 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002631 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002632 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002633 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002634 if (!(t->flags & SN_FINST_MASK))
2635 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002636
Willy Tarreaudafde432008-08-17 01:00:46 +02002637 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002638 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002639 /* too large response does not fit in buffer. */
2640 else if (rep->flags & BF_FULL) {
2641 goto hdr_response_bad;
2642 }
2643 /* read error */
2644 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002645 if (msg->err_pos >= 0)
2646 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002647 buffer_shutr_now(rep);
2648 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002649 if (t->srv)
2650 t->srv->failed_resp++;
2651 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002652 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002653 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002654 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002655 if (!(t->flags & SN_ERR_MASK))
2656 t->flags |= SN_ERR_SRVCL;
2657 if (!(t->flags & SN_FINST_MASK))
2658 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002659 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002661 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002662 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002663 if (msg->err_pos >= 0)
2664 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002665 buffer_shutr_now(rep);
2666 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002667 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002668 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002669 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002670 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002671 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002672 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002673 if (!(t->flags & SN_ERR_MASK))
2674 t->flags |= SN_ERR_SRVTO;
2675 if (!(t->flags & SN_FINST_MASK))
2676 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002677 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002679 /* close from server */
2680 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002681 if (msg->err_pos >= 0)
2682 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002683 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002684 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002685 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002686 t->be->failed_resp++;
2687 rep->analysers = 0;
2688 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002689 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002690 if (!(t->flags & SN_ERR_MASK))
2691 t->flags |= SN_ERR_SRVCL;
2692 if (!(t->flags & SN_FINST_MASK))
2693 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002694 return 0;
2695 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002696 /* write error to client (we don't send any message then) */
2697 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002698 if (msg->err_pos >= 0)
2699 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002700 buffer_shutr_now(rep);
2701 t->be->failed_resp++;
2702 rep->analysers = 0;
2703 if (!(t->flags & SN_ERR_MASK))
2704 t->flags |= SN_ERR_CLICL;
2705 if (!(t->flags & SN_FINST_MASK))
2706 t->flags |= SN_FINST_H;
2707 return 0;
2708 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002709 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002710 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002711 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002712
Willy Tarreau21d2af32008-02-14 20:25:24 +01002713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002714 /*****************************************************************
2715 * More interesting part now : we know that we have a complete *
2716 * response which at least looks like HTTP. We have an indicator *
2717 * of each header's length, so we can parse them quickly. *
2718 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002719
Willy Tarreau4076a152009-04-02 15:18:36 +02002720 if (msg->err_pos >= 0)
2721 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2722
Willy Tarreau2df28e82008-08-17 15:20:19 +02002723 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002724
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002725 /* ensure we keep this pointer to the beginning of the message */
2726 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002727
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002728 /*
2729 * 1: get the status code and check for cacheability.
2730 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002731
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002732 t->logs.logwait &= ~LW_RESP;
2733 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002734
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002735 switch (txn->status) {
2736 case 200:
2737 case 203:
2738 case 206:
2739 case 300:
2740 case 301:
2741 case 410:
2742 /* RFC2616 @13.4:
2743 * "A response received with a status code of
2744 * 200, 203, 206, 300, 301 or 410 MAY be stored
2745 * by a cache (...) unless a cache-control
2746 * directive prohibits caching."
2747 *
2748 * RFC2616 @9.5: POST method :
2749 * "Responses to this method are not cacheable,
2750 * unless the response includes appropriate
2751 * Cache-Control or Expires header fields."
2752 */
2753 if (likely(txn->meth != HTTP_METH_POST) &&
2754 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2755 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2756 break;
2757 default:
2758 break;
2759 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002760
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002761 /*
2762 * 2: we may need to capture headers
2763 */
2764 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2765 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2766 txn->rsp.cap, t->fe->rsp_cap);
2767
2768 /*
2769 * 3: we will have to evaluate the filters.
2770 * As opposed to version 1.2, now they will be evaluated in the
2771 * filters order and not in the header order. This means that
2772 * each filter has to be validated among all headers.
2773 *
2774 * Filters are tried with ->be first, then with ->fe if it is
2775 * different from ->be.
2776 */
2777
2778 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2779
2780 cur_proxy = t->be;
2781 while (1) {
2782 struct proxy *rule_set = cur_proxy;
2783
2784 /* try headers filters */
2785 if (rule_set->rsp_exp != NULL) {
2786 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2787 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01002788 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002789 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002790 cur_proxy->failed_resp++;
2791 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002792 buffer_shutr_now(rep);
2793 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002794 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002795 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002796 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002797 if (!(t->flags & SN_ERR_MASK))
2798 t->flags |= SN_ERR_PRXCOND;
2799 if (!(t->flags & SN_FINST_MASK))
2800 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002801 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002802 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002803 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002804
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 /* has the response been denied ? */
2806 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01002807 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002808 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002809 cur_proxy->denied_resp++;
2810 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002812
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002813 /* We might have to check for "Connection:" */
2814 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2815 !(t->flags & SN_CONN_CLOSED)) {
2816 char *cur_ptr, *cur_end, *cur_next;
2817 int cur_idx, old_idx, delta, val;
2818 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002820 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2821 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002822
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002823 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2824 cur_hdr = &txn->hdr_idx.v[cur_idx];
2825 cur_ptr = cur_next;
2826 cur_end = cur_ptr + cur_hdr->len;
2827 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002828
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002829 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2830 if (val) {
2831 /* 3 possibilities :
2832 * - we have already set Connection: close,
2833 * so we remove this line.
2834 * - we have not yet set Connection: close,
2835 * but this line indicates close. We leave
2836 * it untouched and set the flag.
2837 * - we have not yet set Connection: close,
2838 * and this line indicates non-close. We
2839 * replace it.
2840 */
2841 if (t->flags & SN_CONN_CLOSED) {
2842 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2843 txn->rsp.eoh += delta;
2844 cur_next += delta;
2845 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2846 txn->hdr_idx.used--;
2847 cur_hdr->len = 0;
2848 } else {
2849 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2850 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2851 "close", 5);
2852 cur_next += delta;
2853 cur_hdr->len += delta;
2854 txn->rsp.eoh += delta;
2855 }
2856 t->flags |= SN_CONN_CLOSED;
2857 }
2858 }
2859 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002860 }
2861 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 /* add response headers from the rule sets in the same order */
2864 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2865 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2866 rule_set->rsp_add[cur_idx])) < 0)
2867 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002868 }
2869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002870 /* check whether we're already working on the frontend */
2871 if (cur_proxy == t->fe)
2872 break;
2873 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002876 /*
2877 * 4: check for server cookie.
2878 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002879 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002880 || (t->be->options & PR_O_CHK_CACHE))
2881 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002882
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
Willy Tarreaua15645d2007-03-18 16:22:39 +01002884 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002886 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002887 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2888 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002890 /*
2891 * 6: add server cookie in the response if needed
2892 */
2893 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2894 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2895 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002897 /* the server is known, it's not the one the client requested, we have to
2898 * insert a set-cookie here, except if we want to insert only on POST
2899 * requests and this one isn't. Note that servers which don't have cookies
2900 * (eg: some backup servers) will return a full cookie removal request.
2901 */
2902 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2903 t->be->cookie_name,
2904 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 if (t->be->cookie_domain)
2907 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002908
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002909 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2910 trash, len)) < 0)
2911 goto return_bad_resp;
2912 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002914 /* Here, we will tell an eventual cache on the client side that we don't
2915 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2916 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2917 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2918 */
2919 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002921 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2922
2923 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2924 "Cache-control: private", 22)) < 0)
2925 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 }
2927 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928
Willy Tarreaubaaee002006-06-26 02:48:02 +02002929
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002930 /*
2931 * 7: check if result will be cacheable with a cookie.
2932 * We'll block the response if security checks have caught
2933 * nasty things such as a cacheable cookie.
2934 */
2935 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2936 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2937 (t->be->options & PR_O_CHK_CACHE)) {
2938
2939 /* we're in presence of a cacheable response containing
2940 * a set-cookie header. We'll block it as requested by
2941 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 */
Willy Tarreau8365f932009-03-15 23:11:49 +01002943 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002944 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002945 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002947 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2948 t->be->id, t->srv?t->srv->id:"<dispatch>");
2949 send_log(t->be, LOG_ALERT,
2950 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2951 t->be->id, t->srv?t->srv->id:"<dispatch>");
2952 goto return_srv_prx_502;
2953 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002954
2955 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002956 * 8: add "Connection: close" if needed and not yet set.
2957 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002958 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 if (!(t->flags & SN_CONN_CLOSED) &&
2960 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2961 if ((unlikely(msg->sl.st.v_l != 8) ||
2962 unlikely(req->data[msg->som + 7] != '0')) &&
2963 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2964 "Connection: close", 17)) < 0)
2965 goto return_bad_resp;
2966 t->flags |= SN_CONN_CLOSED;
2967 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002969 /*************************************************************
2970 * OK, that's finished for the headers. We have done what we *
2971 * could. Let's switch to the DATA state. *
2972 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973
Willy Tarreaue393fe22008-08-16 22:18:07 +02002974 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002975 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977#ifdef CONFIG_HAP_TCPSPLICE
2978 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2979 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002980 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002981 }
2982#endif
2983 /* if the user wants to log as soon as possible, without counting
2984 * bytes from the server, then this is the right moment. We have
2985 * to temporarily assign bytes_out to log what we currently have.
2986 */
2987 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2988 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2989 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002990 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002991 t->logs.bytes_out = 0;
2992 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002993
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002994 /* Note: we must not try to cheat by jumping directly to DATA,
2995 * otherwise we would not let the client side wake up.
2996 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002997
Willy Tarreaudafde432008-08-17 01:00:46 +02002998 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002999 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003000
Willy Tarreau2df28e82008-08-17 15:20:19 +02003001 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3002 * probably reduce one day's debugging session.
3003 */
3004#ifdef DEBUG_DEV
3005 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3006 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3007 __FILE__, __LINE__, rep->analysers);
3008 ABORT_NOW();
3009 }
3010#endif
3011 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003012 return 0;
3013}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003014
Willy Tarreaubaaee002006-06-26 02:48:02 +02003015/*
3016 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003017 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003018 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3019 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01003020 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003022void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003023{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003025 buffer_stop_hijack(rep);
3026 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003027 }
3028 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003029 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003030 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003031 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003032 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003033 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003034 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003035
Willy Tarreau91861262007-10-17 17:06:05 +02003036 /* unknown data source or internal error */
3037 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003038 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02003039 if (!(s->flags & SN_ERR_MASK))
3040 s->flags |= SN_ERR_PRXCOND;
3041 if (!(s->flags & SN_FINST_MASK))
3042 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003043 buffer_stop_hijack(rep);
3044 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045}
3046
3047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003048/* Iterate the same filter through all request headers.
3049 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003050 * Since it can manage the switch to another backend, it updates the per-proxy
3051 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003052 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003053int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003054{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003055 char term;
3056 char *cur_ptr, *cur_end, *cur_next;
3057 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003058 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003059 struct hdr_idx_elem *cur_hdr;
3060 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003062 last_hdr = 0;
3063
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003064 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003065 old_idx = 0;
3066
3067 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003068 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003069 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003070 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003071 (exp->action == ACT_ALLOW ||
3072 exp->action == ACT_DENY ||
3073 exp->action == ACT_TARPIT))
3074 return 0;
3075
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003076 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003077 if (!cur_idx)
3078 break;
3079
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003080 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003081 cur_ptr = cur_next;
3082 cur_end = cur_ptr + cur_hdr->len;
3083 cur_next = cur_end + cur_hdr->cr + 1;
3084
3085 /* Now we have one header between cur_ptr and cur_end,
3086 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003087 */
3088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003089 /* The annoying part is that pattern matching needs
3090 * that we modify the contents to null-terminate all
3091 * strings before testing them.
3092 */
3093
3094 term = *cur_end;
3095 *cur_end = '\0';
3096
3097 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3098 switch (exp->action) {
3099 case ACT_SETBE:
3100 /* It is not possible to jump a second time.
3101 * FIXME: should we return an HTTP/500 here so that
3102 * the admin knows there's a problem ?
3103 */
3104 if (t->be != t->fe)
3105 break;
3106
3107 /* Swithing Proxy */
3108 t->be = (struct proxy *) exp->replace;
3109
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003110 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003111 * because we have associated req_cap and rsp_cap to the
3112 * frontend, and the beconn will be updated later.
3113 */
3114
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003115 t->rep->rto = t->req->wto = t->be->timeout.server;
3116 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003117 t->conn_retries = t->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02003118 if (t->be->options2 & PR_O2_RSPBUG_OK)
3119 t->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003120 last_hdr = 1;
3121 break;
3122
3123 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003124 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003125 last_hdr = 1;
3126 break;
3127
3128 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003129 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003130 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003131 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003132 break;
3133
3134 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003135 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003136 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003137 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003138 break;
3139
3140 case ACT_REPLACE:
3141 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3142 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3143 /* FIXME: if the user adds a newline in the replacement, the
3144 * index will not be recalculated for now, and the new line
3145 * will not be counted as a new header.
3146 */
3147
3148 cur_end += delta;
3149 cur_next += delta;
3150 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003151 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003152 break;
3153
3154 case ACT_REMOVE:
3155 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3156 cur_next += delta;
3157
3158 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003159 txn->req.eoh += delta;
3160 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3161 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003162 cur_hdr->len = 0;
3163 cur_end = NULL; /* null-term has been rewritten */
3164 break;
3165
3166 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003167 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003168 if (cur_end)
3169 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003171 /* keep the link from this header to next one in case of later
3172 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003173 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003174 old_idx = cur_idx;
3175 }
3176 return 0;
3177}
3178
3179
3180/* Apply the filter to the request line.
3181 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3182 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003183 * Since it can manage the switch to another backend, it updates the per-proxy
3184 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003185 */
3186int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3187{
3188 char term;
3189 char *cur_ptr, *cur_end;
3190 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003191 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003192 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003193
Willy Tarreau58f10d72006-12-04 02:26:12 +01003194
Willy Tarreau3d300592007-03-18 18:34:41 +01003195 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003196 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003197 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003198 (exp->action == ACT_ALLOW ||
3199 exp->action == ACT_DENY ||
3200 exp->action == ACT_TARPIT))
3201 return 0;
3202 else if (exp->action == ACT_REMOVE)
3203 return 0;
3204
3205 done = 0;
3206
Willy Tarreau9cdde232007-05-02 20:58:19 +02003207 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003208 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003209
3210 /* Now we have the request line between cur_ptr and cur_end */
3211
3212 /* The annoying part is that pattern matching needs
3213 * that we modify the contents to null-terminate all
3214 * strings before testing them.
3215 */
3216
3217 term = *cur_end;
3218 *cur_end = '\0';
3219
3220 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3221 switch (exp->action) {
3222 case ACT_SETBE:
3223 /* It is not possible to jump a second time.
3224 * FIXME: should we return an HTTP/500 here so that
3225 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003226 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003227 if (t->be != t->fe)
3228 break;
3229
3230 /* Swithing Proxy */
3231 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003233 /* right now, the backend switch is not too much complicated
3234 * because we have associated req_cap and rsp_cap to the
3235 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003236 */
3237
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003238 t->rep->rto = t->req->wto = t->be->timeout.server;
3239 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003240 t->conn_retries = t->be->conn_retries;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02003241 if (t->be->options2 & PR_O2_RSPBUG_OK)
3242 t->txn.rsp.err_pos = -1; /* let buggy responses pass */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003243 done = 1;
3244 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003246 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003247 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003248 done = 1;
3249 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003250
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003251 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003252 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003253 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003254 done = 1;
3255 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003256
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003257 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003258 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003259 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003260 done = 1;
3261 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003263 case ACT_REPLACE:
3264 *cur_end = term; /* restore the string terminator */
3265 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3266 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3267 /* FIXME: if the user adds a newline in the replacement, the
3268 * index will not be recalculated for now, and the new line
3269 * will not be counted as a new header.
3270 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003271
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003272 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003273 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003274
Willy Tarreau9cdde232007-05-02 20:58:19 +02003275 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003276 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003277 HTTP_MSG_RQMETH,
3278 cur_ptr, cur_end + 1,
3279 NULL, NULL);
3280 if (unlikely(!cur_end))
3281 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003283 /* we have a full request and we know that we have either a CR
3284 * or an LF at <ptr>.
3285 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003286 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3287 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003288 /* there is no point trying this regex on headers */
3289 return 1;
3290 }
3291 }
3292 *cur_end = term; /* restore the string terminator */
3293 return done;
3294}
Willy Tarreau97de6242006-12-27 17:18:38 +01003295
Willy Tarreau58f10d72006-12-04 02:26:12 +01003296
Willy Tarreau58f10d72006-12-04 02:26:12 +01003297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003298/*
3299 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3300 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003301 * unparsable request. Since it can manage the switch to another backend, it
3302 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003303 */
3304int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3305{
Willy Tarreau3d300592007-03-18 18:34:41 +01003306 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003307 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003308 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003309 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003311 /*
3312 * The interleaving of transformations and verdicts
3313 * makes it difficult to decide to continue or stop
3314 * the evaluation.
3315 */
3316
Willy Tarreau3d300592007-03-18 18:34:41 +01003317 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003318 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3319 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3320 exp = exp->next;
3321 continue;
3322 }
3323
3324 /* Apply the filter to the request line. */
3325 ret = apply_filter_to_req_line(t, req, exp);
3326 if (unlikely(ret < 0))
3327 return -1;
3328
3329 if (likely(ret == 0)) {
3330 /* The filter did not match the request, it can be
3331 * iterated through all headers.
3332 */
3333 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003334 }
3335 exp = exp->next;
3336 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003337 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003338}
3339
3340
Willy Tarreaua15645d2007-03-18 16:22:39 +01003341
Willy Tarreau58f10d72006-12-04 02:26:12 +01003342/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003343 * Manage client-side cookie. It can impact performance by about 2% so it is
3344 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003345 */
3346void manage_client_side_cookies(struct session *t, struct buffer *req)
3347{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003348 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003349 char *p1, *p2, *p3, *p4;
3350 char *del_colon, *del_cookie, *colon;
3351 int app_cookies;
3352
3353 appsess *asession_temp = NULL;
3354 appsess local_asession;
3355
3356 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003357 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003358
Willy Tarreau2a324282006-12-05 00:05:46 +01003359 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003360 * we start with the start line.
3361 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003362 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003363 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003364
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003365 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003366 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003367 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003368
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003369 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003370 cur_ptr = cur_next;
3371 cur_end = cur_ptr + cur_hdr->len;
3372 cur_next = cur_end + cur_hdr->cr + 1;
3373
3374 /* We have one full header between cur_ptr and cur_end, and the
3375 * next header starts at cur_next. We're only interested in
3376 * "Cookie:" headers.
3377 */
3378
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003379 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3380 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003381 old_idx = cur_idx;
3382 continue;
3383 }
3384
3385 /* Now look for cookies. Conforming to RFC2109, we have to support
3386 * attributes whose name begin with a '$', and associate them with
3387 * the right cookie, if we want to delete this cookie.
3388 * So there are 3 cases for each cookie read :
3389 * 1) it's a special attribute, beginning with a '$' : ignore it.
3390 * 2) it's a server id cookie that we *MAY* want to delete : save
3391 * some pointers on it (last semi-colon, beginning of cookie...)
3392 * 3) it's an application cookie : we *MAY* have to delete a previous
3393 * "special" cookie.
3394 * At the end of loop, if a "special" cookie remains, we may have to
3395 * remove it. If no application cookie persists in the header, we
3396 * *MUST* delete it
3397 */
3398
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003399 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003400
Willy Tarreau58f10d72006-12-04 02:26:12 +01003401 /* del_cookie == NULL => nothing to be deleted */
3402 del_colon = del_cookie = NULL;
3403 app_cookies = 0;
3404
3405 while (p1 < cur_end) {
3406 /* skip spaces and colons, but keep an eye on these ones */
3407 while (p1 < cur_end) {
3408 if (*p1 == ';' || *p1 == ',')
3409 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003410 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003411 break;
3412 p1++;
3413 }
3414
3415 if (p1 == cur_end)
3416 break;
3417
3418 /* p1 is at the beginning of the cookie name */
3419 p2 = p1;
3420 while (p2 < cur_end && *p2 != '=')
3421 p2++;
3422
3423 if (p2 == cur_end)
3424 break;
3425
3426 p3 = p2 + 1; /* skips the '=' sign */
3427 if (p3 == cur_end)
3428 break;
3429
3430 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003431 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003432 p4++;
3433
3434 /* here, we have the cookie name between p1 and p2,
3435 * and its value between p3 and p4.
3436 * we can process it :
3437 *
3438 * Cookie: NAME=VALUE;
3439 * | || || |
3440 * | || || +--> p4
3441 * | || |+-------> p3
3442 * | || +--------> p2
3443 * | |+------------> p1
3444 * | +-------------> colon
3445 * +--------------------> cur_ptr
3446 */
3447
3448 if (*p1 == '$') {
3449 /* skip this one */
3450 }
3451 else {
3452 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003453 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003454 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003455 (p4 - p1 >= t->fe->capture_namelen) &&
3456 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003457 int log_len = p4 - p1;
3458
Willy Tarreau086b3b42007-05-13 21:45:51 +02003459 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003460 Alert("HTTP logging : out of memory.\n");
3461 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003462 if (log_len > t->fe->capture_len)
3463 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003464 memcpy(txn->cli_cookie, p1, log_len);
3465 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003466 }
3467 }
3468
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003469 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3470 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003471 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003472 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003473 char *delim;
3474
3475 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3476 * have the server ID betweek p3 and delim, and the original cookie between
3477 * delim+1 and p4. Otherwise, delim==p4 :
3478 *
3479 * Cookie: NAME=SRV~VALUE;
3480 * | || || | |
3481 * | || || | +--> p4
3482 * | || || +--------> delim
3483 * | || |+-----------> p3
3484 * | || +------------> p2
3485 * | |+----------------> p1
3486 * | +-----------------> colon
3487 * +------------------------> cur_ptr
3488 */
3489
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003490 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003491 for (delim = p3; delim < p4; delim++)
3492 if (*delim == COOKIE_DELIM)
3493 break;
3494 }
3495 else
3496 delim = p4;
3497
3498
3499 /* Here, we'll look for the first running server which supports the cookie.
3500 * This allows to share a same cookie between several servers, for example
3501 * to dedicate backup servers to specific servers only.
3502 * However, to prevent clients from sticking to cookie-less backup server
3503 * when they have incidentely learned an empty cookie, we simply ignore
3504 * empty cookies and mark them as invalid.
3505 */
3506 if (delim == p3)
3507 srv = NULL;
3508
3509 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003510 if (srv->cookie && (srv->cklen == delim - p3) &&
3511 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003512 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003513 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003514 txn->flags &= ~TX_CK_MASK;
3515 txn->flags |= TX_CK_VALID;
3516 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003517 t->srv = srv;
3518 break;
3519 } else {
3520 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003521 txn->flags &= ~TX_CK_MASK;
3522 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003523 }
3524 }
3525 srv = srv->next;
3526 }
3527
Willy Tarreau3d300592007-03-18 18:34:41 +01003528 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003529 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003530 txn->flags &= ~TX_CK_MASK;
3531 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003532 }
3533
3534 /* depending on the cookie mode, we may have to either :
3535 * - delete the complete cookie if we're in insert+indirect mode, so that
3536 * the server never sees it ;
3537 * - remove the server id from the cookie value, and tag the cookie as an
3538 * application cookie so that it does not get accidentely removed later,
3539 * if we're in cookie prefix mode
3540 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003541 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003542 int delta; /* negative */
3543
3544 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3545 p4 += delta;
3546 cur_end += delta;
3547 cur_next += delta;
3548 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003549 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003550
3551 del_cookie = del_colon = NULL;
3552 app_cookies++; /* protect the header from deletion */
3553 }
3554 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003555 (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 +01003556 del_cookie = p1;
3557 del_colon = colon;
3558 }
3559 } else {
3560 /* now we know that we must keep this cookie since it's
3561 * not ours. But if we wanted to delete our cookie
3562 * earlier, we cannot remove the complete header, but we
3563 * can remove the previous block itself.
3564 */
3565 app_cookies++;
3566
3567 if (del_cookie != NULL) {
3568 int delta; /* negative */
3569
3570 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3571 p4 += delta;
3572 cur_end += delta;
3573 cur_next += delta;
3574 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003575 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003576 del_cookie = del_colon = NULL;
3577 }
3578 }
3579
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003580 if ((t->be->appsession_name != NULL) &&
3581 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003582 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003583
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584 /* Cool... it's the right one */
3585
3586 asession_temp = &local_asession;
3587
Willy Tarreau63963c62007-05-13 21:29:55 +02003588 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003589 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3590 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3591 return;
3592 }
3593
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003594 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3595 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003596 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003597
Willy Tarreau58f10d72006-12-04 02:26:12 +01003598 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003599 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3600 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003601 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003603 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003604 Alert("Not enough memory process_cli():asession:calloc().\n");
3605 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3606 return;
3607 }
3608
3609 asession_temp->sessid = local_asession.sessid;
3610 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003611 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003612 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003613 } else {
3614 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003615 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003617 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003618 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003619 Alert("Found Application Session without matching server.\n");
3620 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003621 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003622 while (srv) {
3623 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003624 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003625 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003626 txn->flags &= ~TX_CK_MASK;
3627 txn->flags |= TX_CK_VALID;
3628 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003629 t->srv = srv;
3630 break;
3631 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003632 txn->flags &= ~TX_CK_MASK;
3633 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003634 }
3635 }
3636 srv = srv->next;
3637 }/* end while(srv) */
3638 }/* end else if server == NULL */
3639
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003640 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003641 asession_temp->request_count++;
3642#if defined(DEBUG_HASH)
3643 Alert("manage_client_side_cookies\n");
3644 appsession_hash_dump(&(t->be->htbl_proxy));
3645#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003646 }/* end if ((t->proxy->appsession_name != NULL) ... */
3647 }
3648
3649 /* we'll have to look for another cookie ... */
3650 p1 = p4;
3651 } /* while (p1 < cur_end) */
3652
3653 /* There's no more cookie on this line.
3654 * We may have marked the last one(s) for deletion.
3655 * We must do this now in two ways :
3656 * - if there is no app cookie, we simply delete the header ;
3657 * - if there are app cookies, we must delete the end of the
3658 * string properly, including the colon/semi-colon before
3659 * the cookie name.
3660 */
3661 if (del_cookie != NULL) {
3662 int delta;
3663 if (app_cookies) {
3664 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3665 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003666 cur_hdr->len += delta;
3667 } else {
3668 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003669
3670 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003671 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3672 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003673 cur_hdr->len = 0;
3674 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003675 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003676 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003677 }
3678
3679 /* keep the link from this header to next one */
3680 old_idx = cur_idx;
3681 } /* end of cookie processing on this header */
3682}
3683
3684
Willy Tarreaua15645d2007-03-18 16:22:39 +01003685/* Iterate the same filter through all response headers contained in <rtr>.
3686 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3687 */
3688int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3689{
3690 char term;
3691 char *cur_ptr, *cur_end, *cur_next;
3692 int cur_idx, old_idx, last_hdr;
3693 struct http_txn *txn = &t->txn;
3694 struct hdr_idx_elem *cur_hdr;
3695 int len, delta;
3696
3697 last_hdr = 0;
3698
3699 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3700 old_idx = 0;
3701
3702 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003703 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003704 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003705 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003706 (exp->action == ACT_ALLOW ||
3707 exp->action == ACT_DENY))
3708 return 0;
3709
3710 cur_idx = txn->hdr_idx.v[old_idx].next;
3711 if (!cur_idx)
3712 break;
3713
3714 cur_hdr = &txn->hdr_idx.v[cur_idx];
3715 cur_ptr = cur_next;
3716 cur_end = cur_ptr + cur_hdr->len;
3717 cur_next = cur_end + cur_hdr->cr + 1;
3718
3719 /* Now we have one header between cur_ptr and cur_end,
3720 * and the next header starts at cur_next.
3721 */
3722
3723 /* The annoying part is that pattern matching needs
3724 * that we modify the contents to null-terminate all
3725 * strings before testing them.
3726 */
3727
3728 term = *cur_end;
3729 *cur_end = '\0';
3730
3731 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3732 switch (exp->action) {
3733 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003734 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003735 last_hdr = 1;
3736 break;
3737
3738 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003739 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003740 last_hdr = 1;
3741 break;
3742
3743 case ACT_REPLACE:
3744 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3745 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3746 /* FIXME: if the user adds a newline in the replacement, the
3747 * index will not be recalculated for now, and the new line
3748 * will not be counted as a new header.
3749 */
3750
3751 cur_end += delta;
3752 cur_next += delta;
3753 cur_hdr->len += delta;
3754 txn->rsp.eoh += delta;
3755 break;
3756
3757 case ACT_REMOVE:
3758 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3759 cur_next += delta;
3760
3761 /* FIXME: this should be a separate function */
3762 txn->rsp.eoh += delta;
3763 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3764 txn->hdr_idx.used--;
3765 cur_hdr->len = 0;
3766 cur_end = NULL; /* null-term has been rewritten */
3767 break;
3768
3769 }
3770 }
3771 if (cur_end)
3772 *cur_end = term; /* restore the string terminator */
3773
3774 /* keep the link from this header to next one in case of later
3775 * removal of next header.
3776 */
3777 old_idx = cur_idx;
3778 }
3779 return 0;
3780}
3781
3782
3783/* Apply the filter to the status line in the response buffer <rtr>.
3784 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3785 * or -1 if a replacement resulted in an invalid status line.
3786 */
3787int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3788{
3789 char term;
3790 char *cur_ptr, *cur_end;
3791 int done;
3792 struct http_txn *txn = &t->txn;
3793 int len, delta;
3794
3795
Willy Tarreau3d300592007-03-18 18:34:41 +01003796 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003797 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003798 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003799 (exp->action == ACT_ALLOW ||
3800 exp->action == ACT_DENY))
3801 return 0;
3802 else if (exp->action == ACT_REMOVE)
3803 return 0;
3804
3805 done = 0;
3806
Willy Tarreau9cdde232007-05-02 20:58:19 +02003807 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003808 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3809
3810 /* Now we have the status line between cur_ptr and cur_end */
3811
3812 /* The annoying part is that pattern matching needs
3813 * that we modify the contents to null-terminate all
3814 * strings before testing them.
3815 */
3816
3817 term = *cur_end;
3818 *cur_end = '\0';
3819
3820 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3821 switch (exp->action) {
3822 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003823 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003824 done = 1;
3825 break;
3826
3827 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003828 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003829 done = 1;
3830 break;
3831
3832 case ACT_REPLACE:
3833 *cur_end = term; /* restore the string terminator */
3834 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3835 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3836 /* FIXME: if the user adds a newline in the replacement, the
3837 * index will not be recalculated for now, and the new line
3838 * will not be counted as a new header.
3839 */
3840
3841 txn->rsp.eoh += delta;
3842 cur_end += delta;
3843
Willy Tarreau9cdde232007-05-02 20:58:19 +02003844 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003845 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003846 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003847 cur_ptr, cur_end + 1,
3848 NULL, NULL);
3849 if (unlikely(!cur_end))
3850 return -1;
3851
3852 /* we have a full respnse and we know that we have either a CR
3853 * or an LF at <ptr>.
3854 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003855 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003856 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3857 /* there is no point trying this regex on headers */
3858 return 1;
3859 }
3860 }
3861 *cur_end = term; /* restore the string terminator */
3862 return done;
3863}
3864
3865
3866
3867/*
3868 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3869 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3870 * unparsable response.
3871 */
3872int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3873{
Willy Tarreau3d300592007-03-18 18:34:41 +01003874 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003875 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003876 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003877 int ret;
3878
3879 /*
3880 * The interleaving of transformations and verdicts
3881 * makes it difficult to decide to continue or stop
3882 * the evaluation.
3883 */
3884
Willy Tarreau3d300592007-03-18 18:34:41 +01003885 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003886 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3887 exp->action == ACT_PASS)) {
3888 exp = exp->next;
3889 continue;
3890 }
3891
3892 /* Apply the filter to the status line. */
3893 ret = apply_filter_to_sts_line(t, rtr, exp);
3894 if (unlikely(ret < 0))
3895 return -1;
3896
3897 if (likely(ret == 0)) {
3898 /* The filter did not match the response, it can be
3899 * iterated through all headers.
3900 */
3901 apply_filter_to_resp_headers(t, rtr, exp);
3902 }
3903 exp = exp->next;
3904 }
3905 return 0;
3906}
3907
3908
3909
3910/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003911 * Manage server-side cookies. It can impact performance by about 2% so it is
3912 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003913 */
3914void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3915{
3916 struct http_txn *txn = &t->txn;
3917 char *p1, *p2, *p3, *p4;
3918
3919 appsess *asession_temp = NULL;
3920 appsess local_asession;
3921
3922 char *cur_ptr, *cur_end, *cur_next;
3923 int cur_idx, old_idx, delta;
3924
Willy Tarreaua15645d2007-03-18 16:22:39 +01003925 /* Iterate through the headers.
3926 * we start with the start line.
3927 */
3928 old_idx = 0;
3929 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3930
3931 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3932 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003933 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003934
3935 cur_hdr = &txn->hdr_idx.v[cur_idx];
3936 cur_ptr = cur_next;
3937 cur_end = cur_ptr + cur_hdr->len;
3938 cur_next = cur_end + cur_hdr->cr + 1;
3939
3940 /* We have one full header between cur_ptr and cur_end, and the
3941 * next header starts at cur_next. We're only interested in
3942 * "Cookie:" headers.
3943 */
3944
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003945 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3946 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003947 old_idx = cur_idx;
3948 continue;
3949 }
3950
3951 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003952 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003953
3954
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003955 /* maybe we only wanted to see if there was a set-cookie. Note that
3956 * the cookie capture is declared in the fronend.
3957 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003958 if (t->be->cookie_name == NULL &&
3959 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003960 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003961 return;
3962
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003963 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003964
3965 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003966 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3967 break;
3968
3969 /* p1 is at the beginning of the cookie name */
3970 p2 = p1;
3971
3972 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3973 p2++;
3974
3975 if (p2 == cur_end || *p2 == ';') /* next cookie */
3976 break;
3977
3978 p3 = p2 + 1; /* skip the '=' sign */
3979 if (p3 == cur_end)
3980 break;
3981
3982 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003983 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003984 p4++;
3985
3986 /* here, we have the cookie name between p1 and p2,
3987 * and its value between p3 and p4.
3988 * we can process it.
3989 */
3990
3991 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003992 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003993 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003994 (p4 - p1 >= t->fe->capture_namelen) &&
3995 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003996 int log_len = p4 - p1;
3997
Willy Tarreau086b3b42007-05-13 21:45:51 +02003998 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003999 Alert("HTTP logging : out of memory.\n");
4000 }
4001
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004002 if (log_len > t->fe->capture_len)
4003 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004004 memcpy(txn->srv_cookie, p1, log_len);
4005 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004006 }
4007
4008 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004009 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4010 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004011 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004012 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004013
4014 /* If the cookie is in insert mode on a known server, we'll delete
4015 * this occurrence because we'll insert another one later.
4016 * We'll delete it too if the "indirect" option is set and we're in
4017 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004018 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4019 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004020 /* this header must be deleted */
4021 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4022 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4023 txn->hdr_idx.used--;
4024 cur_hdr->len = 0;
4025 cur_next += delta;
4026 txn->rsp.eoh += delta;
4027
Willy Tarreau3d300592007-03-18 18:34:41 +01004028 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004029 }
4030 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004031 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004032 /* replace bytes p3->p4 with the cookie name associated
4033 * with this server since we know it.
4034 */
4035 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4036 cur_hdr->len += delta;
4037 cur_next += delta;
4038 txn->rsp.eoh += delta;
4039
Willy Tarreau3d300592007-03-18 18:34:41 +01004040 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004041 }
4042 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004043 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004044 /* insert the cookie name associated with this server
4045 * before existing cookie, and insert a delimitor between them..
4046 */
4047 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4048 cur_hdr->len += delta;
4049 cur_next += delta;
4050 txn->rsp.eoh += delta;
4051
4052 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004053 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004054 }
4055 }
4056 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004057 else if ((t->be->appsession_name != NULL) &&
4058 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004059
4060 /* Cool... it's the right one */
4061
4062 size_t server_id_len = strlen(t->srv->id) + 1;
4063 asession_temp = &local_asession;
4064
Willy Tarreau63963c62007-05-13 21:29:55 +02004065 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004066 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4067 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4068 return;
4069 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004070 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4071 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004072 asession_temp->serverid = NULL;
4073
4074 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004075 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4076 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004077 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004078 Alert("Not enough Memory process_srv():asession:calloc().\n");
4079 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4080 return;
4081 }
4082 asession_temp->sessid = local_asession.sessid;
4083 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004084 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004085 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4086 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004087 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004088 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004089 }
4090
Willy Tarreaua15645d2007-03-18 16:22:39 +01004091 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004092 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004093 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4094 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4095 return;
4096 }
4097 asession_temp->serverid[0] = '\0';
4098 }
4099
4100 if (asession_temp->serverid[0] == '\0')
4101 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4102
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004103 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004104 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004105#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004106 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004107 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108#endif
4109 }/* end if ((t->proxy->appsession_name != NULL) ... */
4110 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4111 } /* we're now at the end of the cookie value */
4112
4113 /* keep the link from this header to next one */
4114 old_idx = cur_idx;
4115 } /* end of cookie processing on this header */
4116}
4117
4118
4119
4120/*
4121 * Check if response is cacheable or not. Updates t->flags.
4122 */
4123void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4124{
4125 struct http_txn *txn = &t->txn;
4126 char *p1, *p2;
4127
4128 char *cur_ptr, *cur_end, *cur_next;
4129 int cur_idx;
4130
Willy Tarreau5df51872007-11-25 16:20:08 +01004131 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004132 return;
4133
4134 /* Iterate through the headers.
4135 * we start with the start line.
4136 */
4137 cur_idx = 0;
4138 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4139
4140 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4141 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004142 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004143
4144 cur_hdr = &txn->hdr_idx.v[cur_idx];
4145 cur_ptr = cur_next;
4146 cur_end = cur_ptr + cur_hdr->len;
4147 cur_next = cur_end + cur_hdr->cr + 1;
4148
4149 /* We have one full header between cur_ptr and cur_end, and the
4150 * next header starts at cur_next. We're only interested in
4151 * "Cookie:" headers.
4152 */
4153
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004154 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4155 if (val) {
4156 if ((cur_end - (cur_ptr + val) >= 8) &&
4157 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4158 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4159 return;
4160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004161 }
4162
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004163 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4164 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004165 continue;
4166
4167 /* OK, right now we know we have a cache-control header at cur_ptr */
4168
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004169 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004170
4171 if (p1 >= cur_end) /* no more info */
4172 continue;
4173
4174 /* p1 is at the beginning of the value */
4175 p2 = p1;
4176
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004177 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004178 p2++;
4179
4180 /* we have a complete value between p1 and p2 */
4181 if (p2 < cur_end && *p2 == '=') {
4182 /* we have something of the form no-cache="set-cookie" */
4183 if ((cur_end - p1 >= 21) &&
4184 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4185 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004186 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004187 continue;
4188 }
4189
4190 /* OK, so we know that either p2 points to the end of string or to a comma */
4191 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4192 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4193 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4194 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004195 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004196 return;
4197 }
4198
4199 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004200 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004201 continue;
4202 }
4203 }
4204}
4205
4206
Willy Tarreau58f10d72006-12-04 02:26:12 +01004207/*
4208 * Try to retrieve a known appsession in the URI, then the associated server.
4209 * If the server is found, it's assigned to the session.
4210 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004211void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004212{
Willy Tarreau3d300592007-03-18 18:34:41 +01004213 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214 appsess *asession_temp = NULL;
4215 appsess local_asession;
4216 char *request_line;
4217
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004218 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004219 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004220 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004221 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 return;
4223
4224 /* skip ';' */
4225 request_line++;
4226
4227 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004228 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004229 return;
4230
4231 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004232 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004233
4234 /* First try if we already have an appsession */
4235 asession_temp = &local_asession;
4236
Willy Tarreau63963c62007-05-13 21:29:55 +02004237 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4239 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4240 return;
4241 }
4242
4243 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004244 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4245 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 asession_temp->serverid = NULL;
4247
4248 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004249 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4250 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004251 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004253 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004254 Alert("Not enough memory process_cli():asession:calloc().\n");
4255 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4256 return;
4257 }
4258 asession_temp->sessid = local_asession.sessid;
4259 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004260 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004261 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004262 }
4263 else {
4264 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004265 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004266 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004267
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004268 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004270
Willy Tarreau58f10d72006-12-04 02:26:12 +01004271#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004272 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004273 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004274#endif
4275 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004276 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 Alert("Found Application Session without matching server.\n");
4278 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004279 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004280 while (srv) {
4281 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004282 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004283 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004284 txn->flags &= ~TX_CK_MASK;
4285 txn->flags |= TX_CK_VALID;
4286 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004287 t->srv = srv;
4288 break;
4289 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004290 txn->flags &= ~TX_CK_MASK;
4291 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004292 }
4293 }
4294 srv = srv->next;
4295 }
4296 }
4297}
4298
4299
Willy Tarreaub2513902006-12-17 14:52:38 +01004300/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004301 * In a GET or HEAD request, check if the requested URI matches the stats uri
4302 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004303 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004304 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004305 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004306 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004307 *
4308 * Returns 1 if the session's state changes, otherwise 0.
4309 */
4310int stats_check_uri_auth(struct session *t, struct proxy *backend)
4311{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004312 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004313 struct uri_auth *uri_auth = backend->uri_auth;
4314 struct user_auth *user;
4315 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004316 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004317
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004318 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004320 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004321 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004322 return 0;
4323
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004324 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004325
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004326 /* the URI is in h */
4327 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004328 return 0;
4329
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004330 h += uri_auth->uri_len;
4331 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4332 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004333 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004334 break;
4335 }
4336 h++;
4337 }
4338
4339 if (uri_auth->refresh) {
4340 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4341 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4342 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004343 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004344 break;
4345 }
4346 h++;
4347 }
4348 }
4349
Willy Tarreau55bb8452007-10-17 18:44:57 +02004350 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4351 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4352 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004353 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004354 break;
4355 }
4356 h++;
4357 }
4358
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004359 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4360
Willy Tarreaub2513902006-12-17 14:52:38 +01004361 /* we are in front of a interceptable URI. Let's check
4362 * if there's an authentication and if it's valid.
4363 */
4364 user = uri_auth->users;
4365 if (!user) {
4366 /* no user auth required, it's OK */
4367 authenticated = 1;
4368 } else {
4369 authenticated = 0;
4370
4371 /* a user list is defined, we have to check.
4372 * skip 21 chars for "Authorization: Basic ".
4373 */
4374
4375 /* FIXME: this should move to an earlier place */
4376 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004377 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4378 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4379 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004380 if (len > 14 &&
4381 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004382 txn->auth_hdr.str = h;
4383 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004384 break;
4385 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004386 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004387 }
4388
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004389 if (txn->auth_hdr.len < 21 ||
4390 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004391 user = NULL;
4392
4393 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004394 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4395 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004396 user->user_pwd, user->user_len)) {
4397 authenticated = 1;
4398 break;
4399 }
4400 user = user->next;
4401 }
4402 }
4403
4404 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004405 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004406
4407 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004408 msg.str = trash;
4409 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004410 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004411 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004412 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004413 if (!(t->flags & SN_ERR_MASK))
4414 t->flags |= SN_ERR_PRXCOND;
4415 if (!(t->flags & SN_FINST_MASK))
4416 t->flags |= SN_FINST_R;
4417 return 1;
4418 }
4419
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004420 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004421 * data.
4422 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004423 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004424 buffer_shutw_now(t->req);
4425 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004426 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004427 t->data_source = DATA_SRC_STATS;
4428 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004429 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004430 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004431 return 1;
4432}
4433
Willy Tarreau4076a152009-04-02 15:18:36 +02004434/*
4435 * Capture a bad request or response and archive it in the proxy's structure.
4436 */
4437void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4438 struct buffer *buf, struct http_msg *msg,
4439 struct proxy *other_end)
4440{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004441 es->len = buf->r - (buf->data + msg->som);
4442 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004443 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004444 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004445 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004446 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004447 es->when = date; // user-visible date
4448 es->sid = s->uniq_id;
4449 es->srv = s->srv;
4450 es->oe = other_end;
4451 es->src = s->cli_addr;
4452}
Willy Tarreaub2513902006-12-17 14:52:38 +01004453
Willy Tarreaubaaee002006-06-26 02:48:02 +02004454/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004455 * Print a debug line with a header
4456 */
4457void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4458{
4459 int len, max;
4460 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004461 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 max = end - start;
4463 UBOUND(max, sizeof(trash) - len - 1);
4464 len += strlcpy2(trash + len, start, max + 1);
4465 trash[len++] = '\n';
4466 write(1, trash, len);
4467}
4468
4469
Willy Tarreau8797c062007-05-07 00:55:35 +02004470/************************************************************************/
4471/* The code below is dedicated to ACL parsing and matching */
4472/************************************************************************/
4473
4474
4475
4476
4477/* 1. Check on METHOD
4478 * We use the pre-parsed method if it is known, and store its number as an
4479 * integer. If it is unknown, we use the pointer and the length.
4480 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004481static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004482{
4483 int len, meth;
4484
Willy Tarreauae8b7962007-06-09 23:10:04 +02004485 len = strlen(*text);
4486 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004487
4488 pattern->val.i = meth;
4489 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004490 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004491 if (!pattern->ptr.str)
4492 return 0;
4493 pattern->len = len;
4494 }
4495 return 1;
4496}
4497
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004498static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004499acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4500 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004501{
4502 int meth;
4503 struct http_txn *txn = l7;
4504
Willy Tarreaub6866442008-07-14 23:54:42 +02004505 if (!txn)
4506 return 0;
4507
Willy Tarreauc11416f2007-06-17 16:58:38 +02004508 if (txn->req.msg_state != HTTP_MSG_BODY)
4509 return 0;
4510
Willy Tarreau8797c062007-05-07 00:55:35 +02004511 meth = txn->meth;
4512 test->i = meth;
4513 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004514 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4515 /* ensure the indexes are not affected */
4516 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004517 test->len = txn->req.sl.rq.m_l;
4518 test->ptr = txn->req.sol;
4519 }
4520 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4521 return 1;
4522}
4523
4524static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4525{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004526 int icase;
4527
Willy Tarreau8797c062007-05-07 00:55:35 +02004528 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004529 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004530
4531 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004532 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004533
4534 /* Other method, we must compare the strings */
4535 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004536 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004537
4538 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4539 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4540 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004541 return ACL_PAT_FAIL;
4542 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004543}
4544
4545/* 2. Check on Request/Status Version
4546 * We simply compare strings here.
4547 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004548static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004549{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004550 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004551 if (!pattern->ptr.str)
4552 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004553 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004554 return 1;
4555}
4556
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004557static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004558acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4559 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004560{
4561 struct http_txn *txn = l7;
4562 char *ptr;
4563 int len;
4564
Willy Tarreaub6866442008-07-14 23:54:42 +02004565 if (!txn)
4566 return 0;
4567
Willy Tarreauc11416f2007-06-17 16:58:38 +02004568 if (txn->req.msg_state != HTTP_MSG_BODY)
4569 return 0;
4570
Willy Tarreau8797c062007-05-07 00:55:35 +02004571 len = txn->req.sl.rq.v_l;
4572 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4573
4574 while ((len-- > 0) && (*ptr++ != '/'));
4575 if (len <= 0)
4576 return 0;
4577
4578 test->ptr = ptr;
4579 test->len = len;
4580
4581 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4582 return 1;
4583}
4584
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004585static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004586acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4587 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004588{
4589 struct http_txn *txn = l7;
4590 char *ptr;
4591 int len;
4592
Willy Tarreaub6866442008-07-14 23:54:42 +02004593 if (!txn)
4594 return 0;
4595
Willy Tarreauc11416f2007-06-17 16:58:38 +02004596 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4597 return 0;
4598
Willy Tarreau8797c062007-05-07 00:55:35 +02004599 len = txn->rsp.sl.st.v_l;
4600 ptr = txn->rsp.sol;
4601
4602 while ((len-- > 0) && (*ptr++ != '/'));
4603 if (len <= 0)
4604 return 0;
4605
4606 test->ptr = ptr;
4607 test->len = len;
4608
4609 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4610 return 1;
4611}
4612
4613/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004614static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004615acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4616 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004617{
4618 struct http_txn *txn = l7;
4619 char *ptr;
4620 int len;
4621
Willy Tarreaub6866442008-07-14 23:54:42 +02004622 if (!txn)
4623 return 0;
4624
Willy Tarreauc11416f2007-06-17 16:58:38 +02004625 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4626 return 0;
4627
Willy Tarreau8797c062007-05-07 00:55:35 +02004628 len = txn->rsp.sl.st.c_l;
4629 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4630
4631 test->i = __strl2ui(ptr, len);
4632 test->flags = ACL_TEST_F_VOL_1ST;
4633 return 1;
4634}
4635
4636/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004637static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004638acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4639 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004640{
4641 struct http_txn *txn = l7;
4642
Willy Tarreaub6866442008-07-14 23:54:42 +02004643 if (!txn)
4644 return 0;
4645
Willy Tarreauc11416f2007-06-17 16:58:38 +02004646 if (txn->req.msg_state != HTTP_MSG_BODY)
4647 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004648
Willy Tarreauc11416f2007-06-17 16:58:38 +02004649 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4650 /* ensure the indexes are not affected */
4651 return 0;
4652
Willy Tarreau8797c062007-05-07 00:55:35 +02004653 test->len = txn->req.sl.rq.u_l;
4654 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4655
Willy Tarreauf3d25982007-05-08 22:45:09 +02004656 /* we do not need to set READ_ONLY because the data is in a buffer */
4657 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004658 return 1;
4659}
4660
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004661static int
4662acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4663 struct acl_expr *expr, struct acl_test *test)
4664{
4665 struct http_txn *txn = l7;
4666
Willy Tarreaub6866442008-07-14 23:54:42 +02004667 if (!txn)
4668 return 0;
4669
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004670 if (txn->req.msg_state != HTTP_MSG_BODY)
4671 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004672
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004673 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4674 /* ensure the indexes are not affected */
4675 return 0;
4676
4677 /* Parse HTTP request */
4678 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4679 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4680 test->i = AF_INET;
4681
4682 /*
4683 * If we are parsing url in frontend space, we prepare backend stage
4684 * to not parse again the same url ! optimization lazyness...
4685 */
4686 if (px->options & PR_O_HTTP_PROXY)
4687 l4->flags |= SN_ADDR_SET;
4688
4689 test->flags = ACL_TEST_F_READ_ONLY;
4690 return 1;
4691}
4692
4693static int
4694acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4695 struct acl_expr *expr, struct acl_test *test)
4696{
4697 struct http_txn *txn = l7;
4698
Willy Tarreaub6866442008-07-14 23:54:42 +02004699 if (!txn)
4700 return 0;
4701
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004702 if (txn->req.msg_state != HTTP_MSG_BODY)
4703 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004704
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004705 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4706 /* ensure the indexes are not affected */
4707 return 0;
4708
4709 /* Same optimization as url_ip */
4710 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4711 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4712
4713 if (px->options & PR_O_HTTP_PROXY)
4714 l4->flags |= SN_ADDR_SET;
4715
4716 test->flags = ACL_TEST_F_READ_ONLY;
4717 return 1;
4718}
4719
Willy Tarreauc11416f2007-06-17 16:58:38 +02004720/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4721 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4722 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004723static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004724acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004725 struct acl_expr *expr, struct acl_test *test)
4726{
4727 struct http_txn *txn = l7;
4728 struct hdr_idx *idx = &txn->hdr_idx;
4729 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004730
Willy Tarreaub6866442008-07-14 23:54:42 +02004731 if (!txn)
4732 return 0;
4733
Willy Tarreau33a7e692007-06-10 19:45:56 +02004734 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4735 /* search for header from the beginning */
4736 ctx->idx = 0;
4737
Willy Tarreau33a7e692007-06-10 19:45:56 +02004738 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4739 test->flags |= ACL_TEST_F_FETCH_MORE;
4740 test->flags |= ACL_TEST_F_VOL_HDR;
4741 test->len = ctx->vlen;
4742 test->ptr = (char *)ctx->line + ctx->val;
4743 return 1;
4744 }
4745
4746 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4747 test->flags |= ACL_TEST_F_VOL_HDR;
4748 return 0;
4749}
4750
Willy Tarreau33a7e692007-06-10 19:45:56 +02004751static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004752acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4753 struct acl_expr *expr, struct acl_test *test)
4754{
4755 struct http_txn *txn = l7;
4756
Willy Tarreaub6866442008-07-14 23:54:42 +02004757 if (!txn)
4758 return 0;
4759
Willy Tarreauc11416f2007-06-17 16:58:38 +02004760 if (txn->req.msg_state != HTTP_MSG_BODY)
4761 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004762
Willy Tarreauc11416f2007-06-17 16:58:38 +02004763 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4764 /* ensure the indexes are not affected */
4765 return 0;
4766
4767 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4768}
4769
4770static int
4771acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4772 struct acl_expr *expr, struct acl_test *test)
4773{
4774 struct http_txn *txn = l7;
4775
Willy Tarreaub6866442008-07-14 23:54:42 +02004776 if (!txn)
4777 return 0;
4778
Willy Tarreauc11416f2007-06-17 16:58:38 +02004779 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4780 return 0;
4781
4782 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4783}
4784
4785/* 6. Check on HTTP header count. The number of occurrences is returned.
4786 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4787 */
4788static int
4789acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004790 struct acl_expr *expr, struct acl_test *test)
4791{
4792 struct http_txn *txn = l7;
4793 struct hdr_idx *idx = &txn->hdr_idx;
4794 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004795 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004796
Willy Tarreaub6866442008-07-14 23:54:42 +02004797 if (!txn)
4798 return 0;
4799
Willy Tarreau33a7e692007-06-10 19:45:56 +02004800 ctx.idx = 0;
4801 cnt = 0;
4802 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4803 cnt++;
4804
4805 test->i = cnt;
4806 test->flags = ACL_TEST_F_VOL_HDR;
4807 return 1;
4808}
4809
Willy Tarreauc11416f2007-06-17 16:58:38 +02004810static int
4811acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4812 struct acl_expr *expr, struct acl_test *test)
4813{
4814 struct http_txn *txn = l7;
4815
Willy Tarreaub6866442008-07-14 23:54:42 +02004816 if (!txn)
4817 return 0;
4818
Willy Tarreauc11416f2007-06-17 16:58:38 +02004819 if (txn->req.msg_state != HTTP_MSG_BODY)
4820 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004821
Willy Tarreauc11416f2007-06-17 16:58:38 +02004822 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4823 /* ensure the indexes are not affected */
4824 return 0;
4825
4826 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4827}
4828
4829static int
4830acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4831 struct acl_expr *expr, struct acl_test *test)
4832{
4833 struct http_txn *txn = l7;
4834
Willy Tarreaub6866442008-07-14 23:54:42 +02004835 if (!txn)
4836 return 0;
4837
Willy Tarreauc11416f2007-06-17 16:58:38 +02004838 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4839 return 0;
4840
4841 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4842}
4843
Willy Tarreau33a7e692007-06-10 19:45:56 +02004844/* 7. Check on HTTP header's integer value. The integer value is returned.
4845 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004846 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004847 */
4848static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004849acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004850 struct acl_expr *expr, struct acl_test *test)
4851{
4852 struct http_txn *txn = l7;
4853 struct hdr_idx *idx = &txn->hdr_idx;
4854 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004855
Willy Tarreaub6866442008-07-14 23:54:42 +02004856 if (!txn)
4857 return 0;
4858
Willy Tarreau33a7e692007-06-10 19:45:56 +02004859 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4860 /* search for header from the beginning */
4861 ctx->idx = 0;
4862
Willy Tarreau33a7e692007-06-10 19:45:56 +02004863 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4864 test->flags |= ACL_TEST_F_FETCH_MORE;
4865 test->flags |= ACL_TEST_F_VOL_HDR;
4866 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4867 return 1;
4868 }
4869
4870 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4871 test->flags |= ACL_TEST_F_VOL_HDR;
4872 return 0;
4873}
4874
Willy Tarreauc11416f2007-06-17 16:58:38 +02004875static int
4876acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4877 struct acl_expr *expr, struct acl_test *test)
4878{
4879 struct http_txn *txn = l7;
4880
Willy Tarreaub6866442008-07-14 23:54:42 +02004881 if (!txn)
4882 return 0;
4883
Willy Tarreauc11416f2007-06-17 16:58:38 +02004884 if (txn->req.msg_state != HTTP_MSG_BODY)
4885 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004886
Willy Tarreauc11416f2007-06-17 16:58:38 +02004887 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4888 /* ensure the indexes are not affected */
4889 return 0;
4890
4891 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4892}
4893
4894static int
4895acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4896 struct acl_expr *expr, struct acl_test *test)
4897{
4898 struct http_txn *txn = l7;
4899
Willy Tarreaub6866442008-07-14 23:54:42 +02004900 if (!txn)
4901 return 0;
4902
Willy Tarreauc11416f2007-06-17 16:58:38 +02004903 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4904 return 0;
4905
4906 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4907}
4908
Willy Tarreau737b0c12007-06-10 21:28:46 +02004909/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4910 * the first '/' after the possible hostname, and ends before the possible '?'.
4911 */
4912static int
4913acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4914 struct acl_expr *expr, struct acl_test *test)
4915{
4916 struct http_txn *txn = l7;
4917 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004918
Willy Tarreaub6866442008-07-14 23:54:42 +02004919 if (!txn)
4920 return 0;
4921
Willy Tarreauc11416f2007-06-17 16:58:38 +02004922 if (txn->req.msg_state != HTTP_MSG_BODY)
4923 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004924
Willy Tarreauc11416f2007-06-17 16:58:38 +02004925 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4926 /* ensure the indexes are not affected */
4927 return 0;
4928
Willy Tarreau21d2af32008-02-14 20:25:24 +01004929 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4930 ptr = http_get_path(txn);
4931 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004932 return 0;
4933
4934 /* OK, we got the '/' ! */
4935 test->ptr = ptr;
4936
4937 while (ptr < end && *ptr != '?')
4938 ptr++;
4939
4940 test->len = ptr - test->ptr;
4941
4942 /* we do not need to set READ_ONLY because the data is in a buffer */
4943 test->flags = ACL_TEST_F_VOL_1ST;
4944 return 1;
4945}
4946
4947
Willy Tarreau8797c062007-05-07 00:55:35 +02004948
4949/************************************************************************/
4950/* All supported keywords must be declared here. */
4951/************************************************************************/
4952
4953/* Note: must not be declared <const> as its list will be overwritten */
4954static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004955 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4956 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4957 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4958 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004959
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004960 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4961 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4962 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4963 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4964 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4965 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4966 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4967 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4968 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004969
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004970 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4971 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4972 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4973 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4974 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4975 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4976 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4977 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4978 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4979 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004980
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004981 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4982 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4983 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4984 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4985 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4986 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4987 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4988 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4989 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004990
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004991 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4992 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4993 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4994 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4995 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4996 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4997 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004998
Willy Tarreauf3d25982007-05-08 22:45:09 +02004999 { NULL, NULL, NULL, NULL },
5000
5001#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005002 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5003 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5004 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5005 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5006 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5007 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5008 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5009
Willy Tarreau8797c062007-05-07 00:55:35 +02005010 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5011 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5012 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5013 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5014 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5015 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5016 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5017 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5018
5019 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5020 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5021 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5022 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5023 { NULL, NULL, NULL, NULL },
5024#endif
5025}};
5026
5027
5028__attribute__((constructor))
5029static void __http_protocol_init(void)
5030{
5031 acl_register_keywords(&acl_kws);
5032}
5033
5034
Willy Tarreau58f10d72006-12-04 02:26:12 +01005035/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005036 * Local variables:
5037 * c-indent-level: 8
5038 * c-basic-offset: 8
5039 * End:
5040 */