blob: 64e93d3eed5a229cd9660be41e47ed3b0a60d756 [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 Tarreau1c47f852006-07-09 08:22:27 +020063/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010064const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020065 "HTTP/1.0 200 OK\r\n"
66 "Cache-Control: no-cache\r\n"
67 "Connection: close\r\n"
68 "Content-Type: text/html\r\n"
69 "\r\n"
70 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
71
Willy Tarreau0f772532006-12-23 20:51:41 +010072const struct chunk http_200_chunk = {
73 .str = (char *)&HTTP_200,
74 .len = sizeof(HTTP_200)-1
75};
76
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
78 "HTTP/1.0 301 Moved Permantenly\r\n"
79 "Cache-Control: no-cache\r\n"
80 "Connection: close\r\n"
81 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
84 "HTTP/1.0 302 Found\r\n"
85 "Cache-Control: no-cache\r\n"
86 "Connection: close\r\n"
87 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
91 "HTTP/1.0 303 See Other\r\n"
92 "Cache-Control: no-cache\r\n"
93 "Connection: close\r\n"
94 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Willy Tarreaubaaee002006-06-26 02:48:02 +020096/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
97const char *HTTP_401_fmt =
98 "HTTP/1.0 401 Unauthorized\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200101 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
103 "\r\n"
104 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
105
Willy Tarreau0f772532006-12-23 20:51:41 +0100106
107const int http_err_codes[HTTP_ERR_SIZE] = {
108 [HTTP_ERR_400] = 400,
109 [HTTP_ERR_403] = 403,
110 [HTTP_ERR_408] = 408,
111 [HTTP_ERR_500] = 500,
112 [HTTP_ERR_502] = 502,
113 [HTTP_ERR_503] = 503,
114 [HTTP_ERR_504] = 504,
115};
116
Willy Tarreau80587432006-12-24 17:47:20 +0100117static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100118 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100119 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "\r\n"
124 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
125
126 [HTTP_ERR_403] =
127 "HTTP/1.0 403 Forbidden\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
133
134 [HTTP_ERR_408] =
135 "HTTP/1.0 408 Request Time-out\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
141
142 [HTTP_ERR_500] =
143 "HTTP/1.0 500 Server Error\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
149
150 [HTTP_ERR_502] =
151 "HTTP/1.0 502 Bad Gateway\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
157
158 [HTTP_ERR_503] =
159 "HTTP/1.0 503 Service Unavailable\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
165
166 [HTTP_ERR_504] =
167 "HTTP/1.0 504 Gateway Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
173
174};
175
Willy Tarreau80587432006-12-24 17:47:20 +0100176/* We must put the messages here since GCC cannot initialize consts depending
177 * on strlen().
178 */
179struct chunk http_err_chunks[HTTP_ERR_SIZE];
180
Willy Tarreau42250582007-04-01 01:30:43 +0200181#define FD_SETS_ARE_BITFIELDS
182#ifdef FD_SETS_ARE_BITFIELDS
183/*
184 * This map is used with all the FD_* macros to check whether a particular bit
185 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
186 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
187 * byte should be encoded. Be careful to always pass bytes from 0 to 255
188 * exclusively to the macros.
189 */
190fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
191fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
192
193#else
194#error "Check if your OS uses bitfields for fd_sets"
195#endif
196
Willy Tarreau80587432006-12-24 17:47:20 +0100197void init_proto_http()
198{
Willy Tarreau42250582007-04-01 01:30:43 +0200199 int i;
200 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100201 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200202
Willy Tarreau80587432006-12-24 17:47:20 +0100203 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
204 if (!http_err_msgs[msg]) {
205 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
206 abort();
207 }
208
209 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
210 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
211 }
Willy Tarreau42250582007-04-01 01:30:43 +0200212
213 /* initialize the log header encoding map : '{|}"#' should be encoded with
214 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
215 * URL encoding only requires '"', '#' to be encoded as well as non-
216 * printable characters above.
217 */
218 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
219 memset(url_encode_map, 0, sizeof(url_encode_map));
220 for (i = 0; i < 32; i++) {
221 FD_SET(i, hdr_encode_map);
222 FD_SET(i, url_encode_map);
223 }
224 for (i = 127; i < 256; i++) {
225 FD_SET(i, hdr_encode_map);
226 FD_SET(i, url_encode_map);
227 }
228
229 tmp = "\"#{|}";
230 while (*tmp) {
231 FD_SET(*tmp, hdr_encode_map);
232 tmp++;
233 }
234
235 tmp = "\"#";
236 while (*tmp) {
237 FD_SET(*tmp, url_encode_map);
238 tmp++;
239 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200240
241 /* memory allocations */
242 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200243 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100244}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245
Willy Tarreau53b6c742006-12-17 13:37:46 +0100246/*
247 * We have 26 list of methods (1 per first letter), each of which can have
248 * up to 3 entries (2 valid, 1 null).
249 */
250struct http_method_desc {
251 http_meth_t meth;
252 int len;
253 const char text[8];
254};
255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100256const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257 ['C' - 'A'] = {
258 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
259 },
260 ['D' - 'A'] = {
261 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
262 },
263 ['G' - 'A'] = {
264 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
265 },
266 ['H' - 'A'] = {
267 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
268 },
269 ['P' - 'A'] = {
270 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
271 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
272 },
273 ['T' - 'A'] = {
274 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
275 },
276 /* rest is empty like this :
277 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
278 */
279};
280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100281/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200282 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100283 * RFC2616 for those chars.
284 */
285
286const char http_is_spht[256] = {
287 [' '] = 1, ['\t'] = 1,
288};
289
290const char http_is_crlf[256] = {
291 ['\r'] = 1, ['\n'] = 1,
292};
293
294const char http_is_lws[256] = {
295 [' '] = 1, ['\t'] = 1,
296 ['\r'] = 1, ['\n'] = 1,
297};
298
299const char http_is_sep[256] = {
300 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
301 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
302 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
303 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
304 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
305};
306
307const char http_is_ctl[256] = {
308 [0 ... 31] = 1,
309 [127] = 1,
310};
311
312/*
313 * A token is any ASCII char that is neither a separator nor a CTL char.
314 * Do not overwrite values in assignment since gcc-2.95 will not handle
315 * them correctly. Instead, define every non-CTL char's status.
316 */
317const char http_is_token[256] = {
318 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
319 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
320 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
321 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
322 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
323 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
324 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
325 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
326 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
327 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
328 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
329 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
330 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
331 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
332 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
333 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
334 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
335 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
336 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
337 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
338 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
339 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
340 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
341 ['|'] = 1, ['}'] = 0, ['~'] = 1,
342};
343
344
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100345/*
346 * An http ver_token is any ASCII which can be found in an HTTP version,
347 * which includes 'H', 'T', 'P', '/', '.' and any digit.
348 */
349const char http_is_ver_token[256] = {
350 ['.'] = 1, ['/'] = 1,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
352 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
353 ['H'] = 1, ['P'] = 1, ['T'] = 1,
354};
355
356
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100357/*
358 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
359 * CRLF. Text length is measured first, so it cannot be NULL.
360 * The header is also automatically added to the index <hdr_idx>, and the end
361 * of headers is automatically adjusted. The number of bytes added is returned
362 * on success, otherwise <0 is returned indicating an error.
363 */
364int http_header_add_tail(struct buffer *b, struct http_msg *msg,
365 struct hdr_idx *hdr_idx, const char *text)
366{
367 int bytes, len;
368
369 len = strlen(text);
370 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
371 if (!bytes)
372 return -1;
373 msg->eoh += bytes;
374 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
375}
376
377/*
378 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
379 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
380 * the buffer is only opened and the space reserved, but nothing is copied.
381 * The header is also automatically added to the index <hdr_idx>, and the end
382 * of headers is automatically adjusted. The number of bytes added is returned
383 * on success, otherwise <0 is returned indicating an error.
384 */
385int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
386 struct hdr_idx *hdr_idx, const char *text, int len)
387{
388 int bytes;
389
390 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
391 if (!bytes)
392 return -1;
393 msg->eoh += bytes;
394 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
395}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396
397/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100398 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
399 * If so, returns the position of the first non-space character relative to
400 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
401 * to return a pointer to the place after the first space. Returns 0 if the
402 * header name does not match. Checks are case-insensitive.
403 */
404int http_header_match2(const char *hdr, const char *end,
405 const char *name, int len)
406{
407 const char *val;
408
409 if (hdr + len >= end)
410 return 0;
411 if (hdr[len] != ':')
412 return 0;
413 if (strncasecmp(hdr, name, len) != 0)
414 return 0;
415 val = hdr + len + 1;
416 while (val < end && HTTP_IS_SPHT(*val))
417 val++;
418 if ((val >= end) && (len + 2 <= end - hdr))
419 return len + 2; /* we may replace starting from second space */
420 return val - hdr;
421}
422
Willy Tarreau33a7e692007-06-10 19:45:56 +0200423/* Find the end of the header value contained between <s> and <e>.
424 * See RFC2616, par 2.2 for more information. Note that it requires
425 * a valid header to return a valid result.
426 */
427const char *find_hdr_value_end(const char *s, const char *e)
428{
429 int quoted, qdpair;
430
431 quoted = qdpair = 0;
432 for (; s < e; s++) {
433 if (qdpair) qdpair = 0;
434 else if (quoted && *s == '\\') qdpair = 1;
435 else if (quoted && *s == '"') quoted = 0;
436 else if (*s == '"') quoted = 1;
437 else if (*s == ',') return s;
438 }
439 return s;
440}
441
442/* Find the first or next occurrence of header <name> in message buffer <sol>
443 * using headers index <idx>, and return it in the <ctx> structure. This
444 * structure holds everything necessary to use the header and find next
445 * occurrence. If its <idx> member is 0, the header is searched from the
446 * beginning. Otherwise, the next occurrence is returned. The function returns
447 * 1 when it finds a value, and 0 when there is no more.
448 */
449int http_find_header2(const char *name, int len,
450 const char *sol, struct hdr_idx *idx,
451 struct hdr_ctx *ctx)
452{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200453 const char *eol, *sov;
454 int cur_idx;
455
456 if (ctx->idx) {
457 /* We have previously returned a value, let's search
458 * another one on the same line.
459 */
460 cur_idx = ctx->idx;
461 sol = ctx->line;
462 sov = sol + ctx->val + ctx->vlen;
463 eol = sol + idx->v[cur_idx].len;
464
465 if (sov >= eol)
466 /* no more values in this header */
467 goto next_hdr;
468
469 /* values remaining for this header, skip the comma */
470 sov++;
471 while (sov < eol && http_is_lws[(unsigned char)*sov])
472 sov++;
473
474 goto return_hdr;
475 }
476
477 /* first request for this header */
478 sol += hdr_idx_first_pos(idx);
479 cur_idx = hdr_idx_first_idx(idx);
480
481 while (cur_idx) {
482 eol = sol + idx->v[cur_idx].len;
483
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200484 if (len == 0) {
485 /* No argument was passed, we want any header.
486 * To achieve this, we simply build a fake request. */
487 while (sol + len < eol && sol[len] != ':')
488 len++;
489 name = sol;
490 }
491
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 if ((len < eol - sol) &&
493 (sol[len] == ':') &&
494 (strncasecmp(sol, name, len) == 0)) {
495
496 sov = sol + len + 1;
497 while (sov < eol && http_is_lws[(unsigned char)*sov])
498 sov++;
499 return_hdr:
500 ctx->line = sol;
501 ctx->idx = cur_idx;
502 ctx->val = sov - sol;
503
504 eol = find_hdr_value_end(sov, eol);
505 ctx->vlen = eol - sov;
506 return 1;
507 }
508 next_hdr:
509 sol = eol + idx->v[cur_idx].cr + 1;
510 cur_idx = idx->v[cur_idx].next;
511 }
512 return 0;
513}
514
515int http_find_header(const char *name,
516 const char *sol, struct hdr_idx *idx,
517 struct hdr_ctx *ctx)
518{
519 return http_find_header2(name, strlen(name), sol, idx, ctx);
520}
521
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100522/* This function handles a server error at the stream interface level. The
523 * stream interface is assumed to be already in a closed state. An optional
524 * message is copied into the input buffer, and an HTTP status code stored.
525 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100526 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100528static void http_server_error(struct session *t, struct stream_interface *si,
529 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100531 buffer_erase(si->ob);
532 buffer_erase(si->ib);
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100533 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100534 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100535 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100536 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 }
538 if (!(t->flags & SN_ERR_MASK))
539 t->flags |= err;
540 if (!(t->flags & SN_FINST_MASK))
541 t->flags |= finst;
542}
543
Willy Tarreau80587432006-12-24 17:47:20 +0100544/* This function returns the appropriate error location for the given session
545 * and message.
546 */
547
548struct chunk *error_message(struct session *s, int msgnum)
549{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200550 if (s->be->errmsg[msgnum].str)
551 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100552 else if (s->fe->errmsg[msgnum].str)
553 return &s->fe->errmsg[msgnum];
554 else
555 return &http_err_chunks[msgnum];
556}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557
Willy Tarreau53b6c742006-12-17 13:37:46 +0100558/*
559 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
560 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
561 */
562static http_meth_t find_http_meth(const char *str, const int len)
563{
564 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100565 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100566
567 m = ((unsigned)*str - 'A');
568
569 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100570 for (h = http_methods[m]; h->len > 0; h++) {
571 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100572 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100573 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100574 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575 };
576 return HTTP_METH_OTHER;
577 }
578 return HTTP_METH_NONE;
579
580}
581
Willy Tarreau21d2af32008-02-14 20:25:24 +0100582/* Parse the URI from the given transaction (which is assumed to be in request
583 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
584 * It is returned otherwise.
585 */
586static char *
587http_get_path(struct http_txn *txn)
588{
589 char *ptr, *end;
590
591 ptr = txn->req.sol + txn->req.sl.rq.u;
592 end = ptr + txn->req.sl.rq.u_l;
593
594 if (ptr >= end)
595 return NULL;
596
597 /* RFC2616, par. 5.1.2 :
598 * Request-URI = "*" | absuri | abspath | authority
599 */
600
601 if (*ptr == '*')
602 return NULL;
603
604 if (isalpha((unsigned char)*ptr)) {
605 /* this is a scheme as described by RFC3986, par. 3.1 */
606 ptr++;
607 while (ptr < end &&
608 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
609 ptr++;
610 /* skip '://' */
611 if (ptr == end || *ptr++ != ':')
612 return NULL;
613 if (ptr == end || *ptr++ != '/')
614 return NULL;
615 if (ptr == end || *ptr++ != '/')
616 return NULL;
617 }
618 /* skip [user[:passwd]@]host[:[port]] */
619
620 while (ptr < end && *ptr != '/')
621 ptr++;
622
623 if (ptr == end)
624 return NULL;
625
626 /* OK, we got the '/' ! */
627 return ptr;
628}
629
Willy Tarreauefb453c2008-10-26 20:49:47 +0100630/* Returns a 302 for a redirectable request. This may only be called just after
631 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
632 * left unchanged and will follow normal proxy processing.
633 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100635{
636 struct http_txn *txn;
637 struct chunk rdr;
638 char *path;
639 int len;
640
641 /* 1: create the response header */
642 rdr.len = strlen(HTTP_302);
643 rdr.str = trash;
644 memcpy(rdr.str, HTTP_302, rdr.len);
645
646 /* 2: add the server's prefix */
647 if (rdr.len + s->srv->rdr_len > sizeof(trash))
648 return;
649
650 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
651 rdr.len += s->srv->rdr_len;
652
653 /* 3: add the request URI */
654 txn = &s->txn;
655 path = http_get_path(txn);
656 if (!path)
657 return;
658
659 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
660 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
661 return;
662
663 memcpy(rdr.str + rdr.len, path, len);
664 rdr.len += len;
665 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
666 rdr.len += 4;
667
668 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100669 si->shutr(si);
670 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100671 si->err_type = SI_ET_NONE;
672 si->err_loc = NULL;
673 si->state = SI_ST_CLO;
674
675 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100676 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100677
678 /* FIXME: we should increase a counter of redirects per server and per backend. */
679 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100680 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100681}
682
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100683/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100684 * that the server side is closed. Note that err_type is actually a
685 * bitmask, where almost only aborts may be cumulated with other
686 * values. We consider that aborted operations are more important
687 * than timeouts or errors due to the fact that nobody else in the
688 * logs might explain incomplete retries. All others should avoid
689 * being cumulated. It should normally not be possible to have multiple
690 * aborts at once, but just in case, the first one in sequence is reported.
691 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100692void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100693{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100694 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695
696 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100697 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
698 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100700 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
701 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100702 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100703 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
704 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100706 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
707 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100709 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
710 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100712 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
713 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
716 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717}
718
Willy Tarreau42250582007-04-01 01:30:43 +0200719extern const char sess_term_cond[8];
720extern const char sess_fin_state[8];
721extern const char *monthname[12];
722const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
723const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
724 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
725 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200726struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200727struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100728
Willy Tarreau42250582007-04-01 01:30:43 +0200729/*
730 * send a log for the session when we have enough info about it.
731 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100732 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200734{
735 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
736 struct proxy *fe = s->fe;
737 struct proxy *be = s->be;
738 struct proxy *prx_log;
739 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200740 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200741 char *uri, *h;
742 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200743 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200744 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200745 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200746 int hdr;
747
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200748 /* if we don't want to log normal traffic, return now */
749 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
750 (s->conn_retries != be->conn_retries) ||
751 txn->status >= 500;
752 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
753 return;
754
Willy Tarreau42250582007-04-01 01:30:43 +0200755 if (fe->logfac1 < 0 && fe->logfac2 < 0)
756 return;
757 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100758
Willy Tarreau42250582007-04-01 01:30:43 +0200759 if (s->cli_addr.ss_family == AF_INET)
760 inet_ntop(AF_INET,
761 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
762 pn, sizeof(pn));
763 else
764 inet_ntop(AF_INET6,
765 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
766 pn, sizeof(pn));
767
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200768 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200769
770 /* FIXME: let's limit ourselves to frontend logging for now. */
771 tolog = fe->to_log;
772
773 h = tmpline;
774 if (fe->to_log & LW_REQHDR &&
775 txn->req.cap &&
776 (h < tmpline + sizeof(tmpline) - 10)) {
777 *(h++) = ' ';
778 *(h++) = '{';
779 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
780 if (hdr)
781 *(h++) = '|';
782 if (txn->req.cap[hdr] != NULL)
783 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
784 '#', hdr_encode_map, txn->req.cap[hdr]);
785 }
786 *(h++) = '}';
787 }
788
789 if (fe->to_log & LW_RSPHDR &&
790 txn->rsp.cap &&
791 (h < tmpline + sizeof(tmpline) - 7)) {
792 *(h++) = ' ';
793 *(h++) = '{';
794 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
795 if (hdr)
796 *(h++) = '|';
797 if (txn->rsp.cap[hdr] != NULL)
798 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
799 '#', hdr_encode_map, txn->rsp.cap[hdr]);
800 }
801 *(h++) = '}';
802 }
803
804 if (h < tmpline + sizeof(tmpline) - 4) {
805 *(h++) = ' ';
806 *(h++) = '"';
807 uri = txn->uri ? txn->uri : "<BADREQ>";
808 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
809 '#', url_encode_map, uri);
810 *(h++) = '"';
811 }
812 *h = '\0';
813
814 svid = (tolog & LW_SVID) ?
815 (s->data_source != DATA_SRC_STATS) ?
816 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
817
Willy Tarreau70089872008-06-13 21:12:51 +0200818 t_request = -1;
819 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
820 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
821
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200822 level = LOG_INFO;
823 if (err && (fe->options2 & PR_O2_LOGERRORS))
824 level = LOG_ERR;
825
826 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +0200827 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200828 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
829 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200830 pn,
831 (s->cli_addr.ss_family == AF_INET) ?
832 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
833 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200834 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200835 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200836 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200837 t_request,
838 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200839 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
840 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
841 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
842 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100843 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200844 txn->cli_cookie ? txn->cli_cookie : "-",
845 txn->srv_cookie ? txn->srv_cookie : "-",
846 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
847 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
848 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
849 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
850 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100851 (s->flags & SN_REDISP)?"+":"",
852 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200853 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
854
855 s->logs.logwait = 0;
856}
857
Willy Tarreau117f59e2007-03-04 18:17:17 +0100858
859/*
860 * Capture headers from message starting at <som> according to header list
861 * <cap_hdr>, and fill the <idx> structure appropriately.
862 */
863void capture_headers(char *som, struct hdr_idx *idx,
864 char **cap, struct cap_hdr *cap_hdr)
865{
866 char *eol, *sol, *col, *sov;
867 int cur_idx;
868 struct cap_hdr *h;
869 int len;
870
871 sol = som + hdr_idx_first_pos(idx);
872 cur_idx = hdr_idx_first_idx(idx);
873
874 while (cur_idx) {
875 eol = sol + idx->v[cur_idx].len;
876
877 col = sol;
878 while (col < eol && *col != ':')
879 col++;
880
881 sov = col + 1;
882 while (sov < eol && http_is_lws[(unsigned char)*sov])
883 sov++;
884
885 for (h = cap_hdr; h; h = h->next) {
886 if ((h->namelen == col - sol) &&
887 (strncasecmp(sol, h->name, h->namelen) == 0)) {
888 if (cap[h->index] == NULL)
889 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200890 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100891
892 if (cap[h->index] == NULL) {
893 Alert("HTTP capture : out of memory.\n");
894 continue;
895 }
896
897 len = eol - sov;
898 if (len > h->len)
899 len = h->len;
900
901 memcpy(cap[h->index], sov, len);
902 cap[h->index][len]=0;
903 }
904 }
905 sol = eol + idx->v[cur_idx].cr + 1;
906 cur_idx = idx->v[cur_idx].next;
907 }
908}
909
910
Willy Tarreau42250582007-04-01 01:30:43 +0200911/* either we find an LF at <ptr> or we jump to <bad>.
912 */
913#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
914
915/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
916 * otherwise to <http_msg_ood> with <state> set to <st>.
917 */
918#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
919 ptr++; \
920 if (likely(ptr < end)) \
921 goto good; \
922 else { \
923 state = (st); \
924 goto http_msg_ood; \
925 } \
926 } while (0)
927
928
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100930 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100931 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
932 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
933 * will give undefined results.
934 * Note that it is upon the caller's responsibility to ensure that ptr < end,
935 * and that msg->sol points to the beginning of the response.
936 * If a complete line is found (which implies that at least one CR or LF is
937 * found before <end>, the updated <ptr> is returned, otherwise NULL is
938 * returned indicating an incomplete line (which does not mean that parts have
939 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
940 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
941 * upon next call.
942 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200943 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
945 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100948const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
949 unsigned int state, const char *ptr, const char *end,
950 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100951{
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 switch (state) {
953 http_msg_rpver:
954 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100955 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
957
958 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100959 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
961 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100962 state = HTTP_MSG_ERROR;
963 break;
964
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 http_msg_rpver_sp:
966 case HTTP_MSG_RPVER_SP:
967 if (likely(!HTTP_IS_LWS(*ptr))) {
968 msg->sl.st.c = ptr - msg_buf;
969 goto http_msg_rpcode;
970 }
971 if (likely(HTTP_IS_SPHT(*ptr)))
972 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
973 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100974 state = HTTP_MSG_ERROR;
975 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100976
977 http_msg_rpcode:
978 case HTTP_MSG_RPCODE:
979 if (likely(!HTTP_IS_LWS(*ptr)))
980 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
981
982 if (likely(HTTP_IS_SPHT(*ptr))) {
983 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
984 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
985 }
986
987 /* so it's a CR/LF, so there is no reason phrase */
988 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
989 http_msg_rsp_reason:
990 /* FIXME: should we support HTTP responses without any reason phrase ? */
991 msg->sl.st.r = ptr - msg_buf;
992 msg->sl.st.r_l = 0;
993 goto http_msg_rpline_eol;
994
995 http_msg_rpcode_sp:
996 case HTTP_MSG_RPCODE_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.st.r = ptr - msg_buf;
999 goto http_msg_rpreason;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1003 /* so it's a CR/LF, so there is no reason phrase */
1004 goto http_msg_rsp_reason;
1005
1006 http_msg_rpreason:
1007 case HTTP_MSG_RPREASON:
1008 if (likely(!HTTP_IS_CRLF(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1010 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1011 http_msg_rpline_eol:
1012 /* We have seen the end of line. Note that we do not
1013 * necessarily have the \n yet, but at least we know that we
1014 * have EITHER \r OR \n, otherwise the response would not be
1015 * complete. We can then record the response length and return
1016 * to the caller which will be able to register it.
1017 */
1018 msg->sl.st.l = ptr - msg->sol;
1019 return ptr;
1020
1021#ifdef DEBUG_FULL
1022 default:
1023 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1024 exit(1);
1025#endif
1026 }
1027
1028 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001029 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001030 if (ret_state)
1031 *ret_state = state;
1032 if (ret_ptr)
1033 *ret_ptr = (char *)ptr;
1034 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001035}
1036
1037
1038/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001039 * This function parses a request line between <ptr> and <end>, starting with
1040 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1041 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1042 * will give undefined results.
1043 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1044 * and that msg->sol points to the beginning of the request.
1045 * If a complete line is found (which implies that at least one CR or LF is
1046 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1047 * returned indicating an incomplete line (which does not mean that parts have
1048 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1049 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1050 * upon next call.
1051 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001052 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001053 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1054 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001055 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001056 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001057const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1058 unsigned int state, const char *ptr, const char *end,
1059 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001061 switch (state) {
1062 http_msg_rqmeth:
1063 case HTTP_MSG_RQMETH:
1064 if (likely(HTTP_IS_TOKEN(*ptr)))
1065 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001068 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 if (likely(HTTP_IS_CRLF(*ptr))) {
1073 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001074 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 http_msg_req09_uri:
1076 msg->sl.rq.u = ptr - msg_buf;
1077 http_msg_req09_uri_e:
1078 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1079 http_msg_req09_ver:
1080 msg->sl.rq.v = ptr - msg_buf;
1081 msg->sl.rq.v_l = 0;
1082 goto http_msg_rqline_eol;
1083 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001084 state = HTTP_MSG_ERROR;
1085 break;
1086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_rqmeth_sp:
1088 case HTTP_MSG_RQMETH_SP:
1089 if (likely(!HTTP_IS_LWS(*ptr))) {
1090 msg->sl.rq.u = ptr - msg_buf;
1091 goto http_msg_rquri;
1092 }
1093 if (likely(HTTP_IS_SPHT(*ptr)))
1094 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1095 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1096 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 http_msg_rquri:
1099 case HTTP_MSG_RQURI:
1100 if (likely(!HTTP_IS_LWS(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 if (likely(HTTP_IS_SPHT(*ptr))) {
1104 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1109 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 http_msg_rquri_sp:
1112 case HTTP_MSG_RQURI_SP:
1113 if (likely(!HTTP_IS_LWS(*ptr))) {
1114 msg->sl.rq.v = ptr - msg_buf;
1115 goto http_msg_rqver;
1116 }
1117 if (likely(HTTP_IS_SPHT(*ptr)))
1118 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1119 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1120 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001122 http_msg_rqver:
1123 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001124 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001126
1127 if (likely(HTTP_IS_CRLF(*ptr))) {
1128 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1129 http_msg_rqline_eol:
1130 /* We have seen the end of line. Note that we do not
1131 * necessarily have the \n yet, but at least we know that we
1132 * have EITHER \r OR \n, otherwise the request would not be
1133 * complete. We can then record the request length and return
1134 * to the caller which will be able to register it.
1135 */
1136 msg->sl.rq.l = ptr - msg->sol;
1137 return ptr;
1138 }
1139
1140 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001141 state = HTTP_MSG_ERROR;
1142 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144#ifdef DEBUG_FULL
1145 default:
1146 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1147 exit(1);
1148#endif
1149 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001152 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 if (ret_state)
1154 *ret_state = state;
1155 if (ret_ptr)
1156 *ret_ptr = (char *)ptr;
1157 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001159
1160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161/*
1162 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001163 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 * when data are missing and recalled at the exact same location with no
1165 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001166 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1167 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1170{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001171 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001173
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001174 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 ptr = buf->lr;
1176 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 if (unlikely(ptr >= end))
1179 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001181 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 /*
1183 * First, states that are specific to the response only.
1184 * We check them first so that request and headers are
1185 * closer to each other (accessed more often).
1186 */
1187 http_msg_rpbefore:
1188 case HTTP_MSG_RPBEFORE:
1189 if (likely(HTTP_IS_TOKEN(*ptr))) {
1190 if (likely(ptr == buf->data)) {
1191 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001192 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 } else {
1194#if PARSE_PRESERVE_EMPTY_LINES
1195 /* only skip empty leading lines, don't remove them */
1196 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001197 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001198#else
1199 /* Remove empty leading lines, as recommended by
1200 * RFC2616. This takes a lot of time because we
1201 * must move all the buffer backwards, but this
1202 * is rarely needed. The method above will be
1203 * cleaner when we'll be able to start sending
1204 * the request from any place in the buffer.
1205 */
1206 buf->lr = ptr;
1207 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 msg->sol = buf->data;
1210 ptr = buf->data;
1211 end = buf->r;
1212#endif
1213 }
1214 hdr_idx_init(idx);
1215 state = HTTP_MSG_RPVER;
1216 goto http_msg_rpver;
1217 }
1218
1219 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1220 goto http_msg_invalid;
1221
1222 if (unlikely(*ptr == '\n'))
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1224 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1225 /* stop here */
1226
1227 http_msg_rpbefore_cr:
1228 case HTTP_MSG_RPBEFORE_CR:
1229 EXPECT_LF_HERE(ptr, http_msg_invalid);
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1231 /* stop here */
1232
1233 http_msg_rpver:
1234 case HTTP_MSG_RPVER:
1235 case HTTP_MSG_RPVER_SP:
1236 case HTTP_MSG_RPCODE:
1237 case HTTP_MSG_RPCODE_SP:
1238 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001239 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001240 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 if (unlikely(!ptr))
1242 return;
1243
1244 /* we have a full response and we know that we have either a CR
1245 * or an LF at <ptr>.
1246 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001247 //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 +01001248 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1249
1250 msg->sol = ptr;
1251 if (likely(*ptr == '\r'))
1252 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1253 goto http_msg_rpline_end;
1254
1255 http_msg_rpline_end:
1256 case HTTP_MSG_RPLINE_END:
1257 /* msg->sol must point to the first of CR or LF. */
1258 EXPECT_LF_HERE(ptr, http_msg_invalid);
1259 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1260 /* stop here */
1261
1262 /*
1263 * Second, states that are specific to the request only
1264 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_rqbefore:
1266 case HTTP_MSG_RQBEFORE:
1267 if (likely(HTTP_IS_TOKEN(*ptr))) {
1268 if (likely(ptr == buf->data)) {
1269 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001270 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 } else {
1272#if PARSE_PRESERVE_EMPTY_LINES
1273 /* only skip empty leading lines, don't remove them */
1274 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001275 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001276#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 /* Remove empty leading lines, as recommended by
1278 * RFC2616. This takes a lot of time because we
1279 * must move all the buffer backwards, but this
1280 * is rarely needed. The method above will be
1281 * cleaner when we'll be able to start sending
1282 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 buf->lr = ptr;
1285 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 msg->sol = buf->data;
1288 ptr = buf->data;
1289 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001290#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001292 /* we will need this when keep-alive will be supported
1293 hdr_idx_init(idx);
1294 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 state = HTTP_MSG_RQMETH;
1296 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1300 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 if (unlikely(*ptr == '\n'))
1303 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 http_msg_rqbefore_cr:
1308 case HTTP_MSG_RQBEFORE_CR:
1309 EXPECT_LF_HERE(ptr, http_msg_invalid);
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 http_msg_rqmeth:
1314 case HTTP_MSG_RQMETH:
1315 case HTTP_MSG_RQMETH_SP:
1316 case HTTP_MSG_RQURI:
1317 case HTTP_MSG_RQURI_SP:
1318 case HTTP_MSG_RQVER:
1319 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 if (unlikely(!ptr))
1322 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001323
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 /* we have a full request and we know that we have either a CR
1325 * or an LF at <ptr>.
1326 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001327 //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 +01001328 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 msg->sol = ptr;
1331 if (likely(*ptr == '\r'))
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 http_msg_rqline_end:
1336 case HTTP_MSG_RQLINE_END:
1337 /* check for HTTP/0.9 request : no version information available.
1338 * msg->sol must point to the first of CR or LF.
1339 */
1340 if (unlikely(msg->sl.rq.v_l == 0))
1341 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 EXPECT_LF_HERE(ptr, http_msg_invalid);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 /*
1348 * Common states below
1349 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 http_msg_hdr_first:
1351 case HTTP_MSG_HDR_FIRST:
1352 msg->sol = ptr;
1353 if (likely(!HTTP_IS_CRLF(*ptr))) {
1354 goto http_msg_hdr_name;
1355 }
1356
1357 if (likely(*ptr == '\r'))
1358 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1359 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 http_msg_hdr_name:
1362 case HTTP_MSG_HDR_NAME:
1363 /* assumes msg->sol points to the first char */
1364 if (likely(HTTP_IS_TOKEN(*ptr)))
1365 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 if (likely(*ptr == ':')) {
1368 msg->col = ptr - buf->data;
1369 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001372 if (likely(msg->err_pos < -1) || *ptr == '\n')
1373 goto http_msg_invalid;
1374
1375 if (msg->err_pos == -1) /* capture error pointer */
1376 msg->err_pos = ptr - buf->data; /* >= 0 now */
1377
1378 /* and we still accept this non-token character */
1379 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 http_msg_hdr_l1_sp:
1382 case HTTP_MSG_HDR_L1_SP:
1383 /* assumes msg->sol points to the first char and msg->col to the colon */
1384 if (likely(HTTP_IS_SPHT(*ptr)))
1385 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 /* header value can be basically anything except CR/LF */
1388 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (likely(!HTTP_IS_CRLF(*ptr))) {
1391 goto http_msg_hdr_val;
1392 }
1393
1394 if (likely(*ptr == '\r'))
1395 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1396 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 http_msg_hdr_l1_lf:
1399 case HTTP_MSG_HDR_L1_LF:
1400 EXPECT_LF_HERE(ptr, http_msg_invalid);
1401 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 http_msg_hdr_l1_lws:
1404 case HTTP_MSG_HDR_L1_LWS:
1405 if (likely(HTTP_IS_SPHT(*ptr))) {
1406 /* replace HT,CR,LF with spaces */
1407 for (; buf->data+msg->sov < ptr; msg->sov++)
1408 buf->data[msg->sov] = ' ';
1409 goto http_msg_hdr_l1_sp;
1410 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001411 /* we had a header consisting only in spaces ! */
1412 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 goto http_msg_complete_header;
1414
1415 http_msg_hdr_val:
1416 case HTTP_MSG_HDR_VAL:
1417 /* assumes msg->sol points to the first char, msg->col to the
1418 * colon, and msg->sov points to the first character of the
1419 * value.
1420 */
1421 if (likely(!HTTP_IS_CRLF(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->eol = ptr;
1425 /* Note: we could also copy eol into ->eoh so that we have the
1426 * real header end in case it ends with lots of LWS, but is this
1427 * really needed ?
1428 */
1429 if (likely(*ptr == '\r'))
1430 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1431 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 http_msg_hdr_l2_lf:
1434 case HTTP_MSG_HDR_L2_LF:
1435 EXPECT_LF_HERE(ptr, http_msg_invalid);
1436 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 http_msg_hdr_l2_lws:
1439 case HTTP_MSG_HDR_L2_LWS:
1440 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1441 /* LWS: replace HT,CR,LF with spaces */
1442 for (; msg->eol < ptr; msg->eol++)
1443 *msg->eol = ' ';
1444 goto http_msg_hdr_val;
1445 }
1446 http_msg_complete_header:
1447 /*
1448 * It was a new header, so the last one is finished.
1449 * Assumes msg->sol points to the first char, msg->col to the
1450 * colon, msg->sov points to the first character of the value
1451 * and msg->eol to the first CR or LF so we know how the line
1452 * ends. We insert last header into the index.
1453 */
1454 /*
1455 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1456 write(2, msg->sol, msg->eol-msg->sol);
1457 fprintf(stderr,"\n");
1458 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1461 idx, idx->tail) < 0))
1462 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 msg->sol = ptr;
1465 if (likely(!HTTP_IS_CRLF(*ptr))) {
1466 goto http_msg_hdr_name;
1467 }
1468
1469 if (likely(*ptr == '\r'))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1471 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 http_msg_last_lf:
1474 case HTTP_MSG_LAST_LF:
1475 /* Assumes msg->sol points to the first of either CR or LF */
1476 EXPECT_LF_HERE(ptr, http_msg_invalid);
1477 ptr++;
1478 buf->lr = ptr;
1479 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 return;
1482#ifdef DEBUG_FULL
1483 default:
1484 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1485 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001486#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 }
1488 http_msg_ood:
1489 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001490 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 buf->lr = ptr;
1492 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_invalid:
1495 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001497 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 return;
1499}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001500
Willy Tarreaud787e662009-07-07 10:14:51 +02001501/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1502 * processing can continue on next analysers, or zero if it either needs more
1503 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1504 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1505 * when it has nothing left to do, and may remove any analyser when it wants to
1506 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001508int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509{
Willy Tarreau59234e92008-11-30 23:51:27 +01001510 /*
1511 * We will parse the partial (or complete) lines.
1512 * We will check the request syntax, and also join multi-line
1513 * headers. An index of all the lines will be elaborated while
1514 * parsing.
1515 *
1516 * For the parsing, we use a 28 states FSM.
1517 *
1518 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001519 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001520 * req->data + req->eoh = end of processed headers / start of current one
1521 * req->data + req->eol = end of current header or line (LF or CRLF)
1522 * req->lr = first non-visited byte
1523 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001524 *
1525 * At end of parsing, we may perform a capture of the error (if any), and
1526 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1527 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1528 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001529 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau59234e92008-11-30 23:51:27 +01001531 int cur_idx;
1532 struct http_txn *txn = &s->txn;
1533 struct http_msg *msg = &txn->req;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau6bf17362009-02-24 10:48:35 +01001535 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1536 now_ms, __FUNCTION__,
1537 s,
1538 req,
1539 req->rex, req->wex,
1540 req->flags,
1541 req->l,
1542 req->analysers);
1543
Willy Tarreau59234e92008-11-30 23:51:27 +01001544 if (likely(req->lr < req->r))
1545 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001546
Willy Tarreau59234e92008-11-30 23:51:27 +01001547 /* 1: we might have to print this header in debug mode */
1548 if (unlikely((global.mode & MODE_DEBUG) &&
1549 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1550 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1551 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552
Willy Tarreau59234e92008-11-30 23:51:27 +01001553 sol = req->data + msg->som;
1554 eol = sol + msg->sl.rq.l;
1555 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001556
Willy Tarreau59234e92008-11-30 23:51:27 +01001557 sol += hdr_idx_first_pos(&txn->hdr_idx);
1558 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001559
Willy Tarreau59234e92008-11-30 23:51:27 +01001560 while (cur_idx) {
1561 eol = sol + txn->hdr_idx.v[cur_idx].len;
1562 debug_hdr("clihdr", s, sol, eol);
1563 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1564 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001566 }
1567
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau59234e92008-11-30 23:51:27 +01001569 /*
1570 * Now we quickly check if we have found a full valid request.
1571 * If not so, we check the FD and buffer states before leaving.
1572 * A full request is indicated by the fact that we have seen
1573 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1574 * requests are checked first.
1575 *
1576 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau59234e92008-11-30 23:51:27 +01001578 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001580 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001582 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1583 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001584
Willy Tarreau59234e92008-11-30 23:51:27 +01001585 /* 1: Since we are in header mode, if there's no space
1586 * left for headers, we won't be able to free more
1587 * later, so the session will never terminate. We
1588 * must terminate it now.
1589 */
1590 if (unlikely(req->flags & BF_FULL)) {
1591 /* FIXME: check if URI is set and return Status
1592 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001594 goto return_bad_req;
1595 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau59234e92008-11-30 23:51:27 +01001597 /* 2: have we encountered a read error ? */
1598 else if (req->flags & BF_READ_ERROR) {
1599 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001600 if (msg->err_pos >= 0)
1601 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 msg->msg_state = HTTP_MSG_ERROR;
1603 req->analysers = 0;
1604 s->fe->failed_req++;
1605 if (!(s->flags & SN_ERR_MASK))
1606 s->flags |= SN_ERR_CLICL;
1607 if (!(s->flags & SN_FINST_MASK))
1608 s->flags |= SN_FINST_R;
1609 return 0;
1610 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001611
Willy Tarreau59234e92008-11-30 23:51:27 +01001612 /* 3: has the read timeout expired ? */
1613 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1614 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001615 if (msg->err_pos >= 0)
1616 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001617 txn->status = 408;
1618 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1619 msg->msg_state = HTTP_MSG_ERROR;
1620 req->analysers = 0;
1621 s->fe->failed_req++;
1622 if (!(s->flags & SN_ERR_MASK))
1623 s->flags |= SN_ERR_CLITO;
1624 if (!(s->flags & SN_FINST_MASK))
1625 s->flags |= SN_FINST_R;
1626 return 0;
1627 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001628
Willy Tarreau59234e92008-11-30 23:51:27 +01001629 /* 4: have we encountered a close ? */
1630 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001631 if (msg->err_pos >= 0)
1632 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001633 txn->status = 400;
1634 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1635 msg->msg_state = HTTP_MSG_ERROR;
1636 req->analysers = 0;
1637 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001638
Willy Tarreau59234e92008-11-30 23:51:27 +01001639 if (!(s->flags & SN_ERR_MASK))
1640 s->flags |= SN_ERR_CLICL;
1641 if (!(s->flags & SN_FINST_MASK))
1642 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001643 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001644 }
1645
Willy Tarreau59234e92008-11-30 23:51:27 +01001646 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001647 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1648
Willy Tarreau59234e92008-11-30 23:51:27 +01001649 /* just set the request timeout once at the beginning of the request */
1650 if (!tick_isset(req->analyse_exp))
1651 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001652
Willy Tarreau59234e92008-11-30 23:51:27 +01001653 /* we're not ready yet */
1654 return 0;
1655 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Willy Tarreaud787e662009-07-07 10:14:51 +02001657 /* OK now we have a complete HTTP request with indexed headers. Let's
1658 * complete the request parsing by setting a few fields we will need
1659 * later.
1660 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001661
Willy Tarreaud787e662009-07-07 10:14:51 +02001662 /* Maybe we found in invalid header name while we were configured not
1663 * to block on that, so we have to capture it now.
1664 */
1665 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001666 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 /* ensure we keep this pointer to the beginning of the message */
1669 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 /*
1672 * 1: identify the method
1673 */
1674 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1675
1676 /* we can make use of server redirect on GET and HEAD */
1677 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1678 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001679
Willy Tarreau59234e92008-11-30 23:51:27 +01001680 /*
1681 * 2: check if the URI matches the monitor_uri.
1682 * We have to do this for every request which gets in, because
1683 * the monitor-uri is defined by the frontend.
1684 */
1685 if (unlikely((s->fe->monitor_uri_len != 0) &&
1686 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1687 !memcmp(&req->data[msg->sl.rq.u],
1688 s->fe->monitor_uri,
1689 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001694
Willy Tarreau59234e92008-11-30 23:51:27 +01001695 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001696
Willy Tarreau59234e92008-11-30 23:51:27 +01001697 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001698 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1699 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001700
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 ret = acl_pass(ret);
1702 if (cond->pol == ACL_COND_UNLESS)
1703 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001704
Willy Tarreau59234e92008-11-30 23:51:27 +01001705 if (ret) {
1706 /* we fail this request, let's return 503 service unavail */
1707 txn->status = 503;
1708 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1709 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001710 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001712
Willy Tarreau59234e92008-11-30 23:51:27 +01001713 /* nothing to fail, let's reply normaly */
1714 txn->status = 200;
1715 stream_int_retnclose(req->prod, &http_200_chunk);
1716 goto return_prx_cond;
1717 }
1718
1719 /*
1720 * 3: Maybe we have to copy the original REQURI for the logs ?
1721 * Note: we cannot log anymore if the request has been
1722 * classified as invalid.
1723 */
1724 if (unlikely(s->logs.logwait & LW_REQ)) {
1725 /* we have a complete HTTP request that we must log */
1726 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1727 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728
Willy Tarreau59234e92008-11-30 23:51:27 +01001729 if (urilen >= REQURI_LEN)
1730 urilen = REQURI_LEN - 1;
1731 memcpy(txn->uri, &req->data[msg->som], urilen);
1732 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733
Willy Tarreau59234e92008-11-30 23:51:27 +01001734 if (!(s->logs.logwait &= ~LW_REQ))
1735 s->do_log(s);
1736 } else {
1737 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001739 }
Willy Tarreau06619262006-12-17 08:37:22 +01001740
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1742 if (unlikely(msg->sl.rq.v_l == 0)) {
1743 int delta;
1744 char *cur_end;
1745 msg->sol = req->data + msg->som;
1746 cur_end = msg->sol + msg->sl.rq.l;
1747 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001748
Willy Tarreau59234e92008-11-30 23:51:27 +01001749 if (msg->sl.rq.u_l == 0) {
1750 /* if no URI was set, add "/" */
1751 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001753 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001754 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001755 /* add HTTP version */
1756 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1757 msg->eoh += delta;
1758 cur_end += delta;
1759 cur_end = (char *)http_parse_reqline(msg, req->data,
1760 HTTP_MSG_RQMETH,
1761 msg->sol, cur_end + 1,
1762 NULL, NULL);
1763 if (unlikely(!cur_end))
1764 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765
Willy Tarreau59234e92008-11-30 23:51:27 +01001766 /* we have a full HTTP/1.0 request now and we know that
1767 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001768 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001769 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001771
1772
Willy Tarreau59234e92008-11-30 23:51:27 +01001773 /* 5: we may need to capture headers */
1774 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1775 capture_headers(req->data + msg->som, &txn->hdr_idx,
1776 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001777
Willy Tarreaud787e662009-07-07 10:14:51 +02001778 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001779 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001780 req->analyse_exp = TICK_ETERNITY;
1781 return 1;
1782
1783 return_bad_req:
1784 /* We centralize bad requests processing here */
1785 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1786 /* we detected a parsing error. We want to archive this request
1787 * in the dedicated proxy area for later troubleshooting.
1788 */
1789 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1790 }
1791
1792 txn->req.msg_state = HTTP_MSG_ERROR;
1793 txn->status = 400;
1794 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1795 s->fe->failed_req++;
1796
1797 return_prx_cond:
1798 if (!(s->flags & SN_ERR_MASK))
1799 s->flags |= SN_ERR_PRXCOND;
1800 if (!(s->flags & SN_FINST_MASK))
1801 s->flags |= SN_FINST_R;
1802
1803 req->analysers = 0;
1804 req->analyse_exp = TICK_ETERNITY;
1805 return 0;
1806}
1807
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001808/* This stream analyser runs all HTTP request processing which is common to
1809 * frontends and backends, which means blocking ACLs, filters, connection-close,
1810 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02001811 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001812 * either needs more data or wants to immediately abort the request (eg: deny,
1813 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02001814 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001815int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02001816{
Willy Tarreaud787e662009-07-07 10:14:51 +02001817 struct http_txn *txn = &s->txn;
1818 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001819 struct acl_cond *cond;
1820 struct redirect_rule *rule;
1821 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02001822
Willy Tarreau3a816292009-07-07 10:55:49 +02001823 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001824 req->analyse_exp = TICK_ETERNITY;
1825
1826 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1827 now_ms, __FUNCTION__,
1828 s,
1829 req,
1830 req->rex, req->wex,
1831 req->flags,
1832 req->l,
1833 req->analysers);
1834
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001835 /* first check whether we have some ACLs set to block this request */
1836 list_for_each_entry(cond, &px->block_cond, list) {
1837 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001839 ret = acl_pass(ret);
1840 if (cond->pol == ACL_COND_UNLESS)
1841 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001843 if (ret) {
1844 txn->status = 403;
1845 /* let's log the request time */
1846 s->logs.tv_request = now;
1847 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1848 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01001849 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001850 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001851
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001852 /* try headers filters */
1853 if (px->req_exp != NULL) {
1854 if (apply_filters_to_request(s, req, px->req_exp) < 0)
1855 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001856
Willy Tarreau59234e92008-11-30 23:51:27 +01001857 /* has the request been denied ? */
1858 if (txn->flags & TX_CLDENY) {
1859 /* no need to go further */
1860 txn->status = 403;
1861 /* let's log the request time */
1862 s->logs.tv_request = now;
1863 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1864 goto return_prx_cond;
1865 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001866 }
Willy Tarreau06619262006-12-17 08:37:22 +01001867
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001868 /* We might have to check for "Connection:" */
1869 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1870 !(s->flags & SN_CONN_CLOSED)) {
1871 char *cur_ptr, *cur_end, *cur_next;
1872 int old_idx, delta, val;
1873 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001875 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1876 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001878 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1879 cur_hdr = &txn->hdr_idx.v[cur_idx];
1880 cur_ptr = cur_next;
1881 cur_end = cur_ptr + cur_hdr->len;
1882 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01001883
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001884 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1885 if (val) {
1886 /* 3 possibilities :
1887 * - we have already set Connection: close,
1888 * so we remove this line.
1889 * - we have not yet set Connection: close,
1890 * but this line indicates close. We leave
1891 * it untouched and set the flag.
1892 * - we have not yet set Connection: close,
1893 * and this line indicates non-close. We
1894 * replace it.
1895 */
1896 if (s->flags & SN_CONN_CLOSED) {
1897 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
1898 txn->req.eoh += delta;
1899 cur_next += delta;
1900 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1901 txn->hdr_idx.used--;
1902 cur_hdr->len = 0;
1903 } else {
1904 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1905 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1906 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01001907 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001908 cur_hdr->len += delta;
1909 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001911 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 }
Willy Tarreau06619262006-12-17 08:37:22 +01001913 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001914 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01001915 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001916 }
1917 /* add request headers from the rule sets in the same order */
1918 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
1919 if (unlikely(http_header_add_tail(req,
1920 &txn->req,
1921 &txn->hdr_idx,
1922 px->req_add[cur_idx])) < 0)
1923 goto return_bad_req;
1924 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001925
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001926 /* check if stats URI was requested, and if an auth is needed */
1927 if (px->uri_auth != NULL &&
1928 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
1929 /* we have to check the URI and auth for this request.
1930 * FIXME!!! that one is rather dangerous, we want to
1931 * make it follow standard rules (eg: clear req->analysers).
1932 */
1933 if (stats_check_uri_auth(s, px)) {
1934 req->analysers = 0;
1935 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001936 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001937 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001938
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001939 /* check whether we have some ACLs set to redirect this request */
1940 list_for_each_entry(rule, &px->redirect_rules, list) {
1941 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001942
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001943 ret = acl_pass(ret);
1944 if (rule->cond->pol == ACL_COND_UNLESS)
1945 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001946
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001947 if (ret) {
1948 struct chunk rdr = { trash, 0 };
1949 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001950
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001951 /* build redirect message */
1952 switch(rule->code) {
1953 case 303:
1954 rdr.len = strlen(HTTP_303);
1955 msg_fmt = HTTP_303;
1956 break;
1957 case 301:
1958 rdr.len = strlen(HTTP_301);
1959 msg_fmt = HTTP_301;
1960 break;
1961 case 302:
1962 default:
1963 rdr.len = strlen(HTTP_302);
1964 msg_fmt = HTTP_302;
1965 break;
1966 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02001967
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001968 if (unlikely(rdr.len > sizeof(trash)))
1969 goto return_bad_req;
1970 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001971
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001972 switch(rule->type) {
1973 case REDIRECT_TYPE_PREFIX: {
1974 const char *path;
1975 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001976
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001977 path = http_get_path(txn);
1978 /* build message using path */
1979 if (path) {
1980 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1981 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1982 int qs = 0;
1983 while (qs < pathlen) {
1984 if (path[qs] == '?') {
1985 pathlen = qs;
1986 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001987 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001988 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001989 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02001990 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001991 } else {
1992 path = "/";
1993 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001994 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02001995
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001996 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1997 goto return_bad_req;
1998
1999 /* add prefix. Note that if prefix == "/", we don't want to
2000 * add anything, otherwise it makes it hard for the user to
2001 * configure a self-redirection.
2002 */
2003 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002004 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2005 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002006 }
2007
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002008 /* add path */
2009 memcpy(rdr.str + rdr.len, path, pathlen);
2010 rdr.len += pathlen;
2011 break;
2012 }
2013 case REDIRECT_TYPE_LOCATION:
2014 default:
2015 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2016 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002017
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002018 /* add location */
2019 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2020 rdr.len += rule->rdr_len;
2021 break;
2022 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002024 if (rule->cookie_len) {
2025 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2026 rdr.len += 14;
2027 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2028 rdr.len += rule->cookie_len;
2029 memcpy(rdr.str + rdr.len, "\r\n", 2);
2030 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002031 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002033 /* add end of headers */
2034 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2035 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002036
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002037 txn->status = rule->code;
2038 /* let's log the request time */
2039 s->logs.tv_request = now;
2040 stream_int_retnclose(req->prod, &rdr);
2041 goto return_prx_cond;
2042 }
2043 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002045 /* that's OK for us now, let's move on to next analysers */
2046 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002048 return_bad_req:
2049 /* We centralize bad requests processing here */
2050 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2051 /* we detected a parsing error. We want to archive this request
2052 * in the dedicated proxy area for later troubleshooting.
2053 */
2054 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2055 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002057 txn->req.msg_state = HTTP_MSG_ERROR;
2058 txn->status = 400;
2059 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2060 s->fe->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002062 return_prx_cond:
2063 if (!(s->flags & SN_ERR_MASK))
2064 s->flags |= SN_ERR_PRXCOND;
2065 if (!(s->flags & SN_FINST_MASK))
2066 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002068 req->analysers = 0;
2069 req->analyse_exp = TICK_ETERNITY;
2070 return 0;
2071}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002073/* This function performs all the processing enabled for the current request.
2074 * It returns 1 if the processing can continue on next analysers, or zero if it
2075 * needs more data, encounters an error, or wants to immediately abort the
2076 * request. It relies on buffers flags, and updates s->req->analysers.
2077 */
2078int http_process_request(struct session *s, struct buffer *req, int an_bit)
2079{
2080 struct http_txn *txn = &s->txn;
2081 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002082
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002083 req->analysers &= ~an_bit;
2084 req->analyse_exp = TICK_ETERNITY;
2085
2086 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2087 now_ms, __FUNCTION__,
2088 s,
2089 req,
2090 req->rex, req->wex,
2091 req->flags,
2092 req->l,
2093 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002094
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 /*
2096 * Right now, we know that we have processed the entire headers
2097 * and that unwanted requests have been filtered out. We can do
2098 * whatever we want with the remaining request. Also, now we
2099 * may have separate values for ->fe, ->be.
2100 */
Willy Tarreau06619262006-12-17 08:37:22 +01002101
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 /*
2103 * If HTTP PROXY is set we simply get remote server address
2104 * parsing incoming request.
2105 */
2106 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2107 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 /*
2111 * 7: the appsession cookie was looked up very early in 1.2,
2112 * so let's do the same now.
2113 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002114
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 /* It needs to look into the URI */
2116 if (s->be->appsession_name) {
2117 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2118 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002119
Willy Tarreau59234e92008-11-30 23:51:27 +01002120 /*
2121 * 8: Now we can work with the cookies.
2122 * Note that doing so might move headers in the request, but
2123 * the fields will stay coherent and the URI will not move.
2124 * This should only be performed in the backend.
2125 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002126 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2128 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002129
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 /*
2131 * 9: add X-Forwarded-For if either the frontend or the backend
2132 * asks for it.
2133 */
2134 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2135 if (s->cli_addr.ss_family == AF_INET) {
2136 /* Add an X-Forwarded-For header unless the source IP is
2137 * in the 'except' network range.
2138 */
2139 if ((!s->fe->except_mask.s_addr ||
2140 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2141 != s->fe->except_net.s_addr) &&
2142 (!s->be->except_mask.s_addr ||
2143 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2144 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002145 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 unsigned char *pn;
2147 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002148
2149 /* Note: we rely on the backend to get the header name to be used for
2150 * x-forwarded-for, because the header is really meant for the backends.
2151 * However, if the backend did not specify any option, we have to rely
2152 * on the frontend's header name.
2153 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002154 if (s->be->fwdfor_hdr_len) {
2155 len = s->be->fwdfor_hdr_len;
2156 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002157 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 len = s->fe->fwdfor_hdr_len;
2159 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2160 }
2161 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002162
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002163 if (unlikely(http_header_add_tail2(req, &txn->req,
2164 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002165 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002166 }
2167 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002168 else if (s->cli_addr.ss_family == AF_INET6) {
2169 /* FIXME: for the sake of completeness, we should also support
2170 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002171 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 int len;
2173 char pn[INET6_ADDRSTRLEN];
2174 inet_ntop(AF_INET6,
2175 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2176 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002177
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 /* Note: we rely on the backend to get the header name to be used for
2179 * x-forwarded-for, because the header is really meant for the backends.
2180 * However, if the backend did not specify any option, we have to rely
2181 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002182 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002183 if (s->be->fwdfor_hdr_len) {
2184 len = s->be->fwdfor_hdr_len;
2185 memcpy(trash, s->be->fwdfor_hdr_name, len);
2186 } else {
2187 len = s->fe->fwdfor_hdr_len;
2188 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002189 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002191
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 if (unlikely(http_header_add_tail2(req, &txn->req,
2193 &txn->hdr_idx, trash, len)) < 0)
2194 goto return_bad_req;
2195 }
2196 }
2197
2198 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002199 * 10: add X-Original-To if either the frontend or the backend
2200 * asks for it.
2201 */
2202 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2203
2204 /* FIXME: don't know if IPv6 can handle that case too. */
2205 if (s->cli_addr.ss_family == AF_INET) {
2206 /* Add an X-Original-To header unless the destination IP is
2207 * in the 'except' network range.
2208 */
2209 if (!(s->flags & SN_FRT_ADDR_SET))
2210 get_frt_addr(s);
2211
2212 if ((!s->fe->except_mask_to.s_addr ||
2213 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2214 != s->fe->except_to.s_addr) &&
2215 (!s->be->except_mask_to.s_addr ||
2216 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2217 != s->be->except_to.s_addr)) {
2218 int len;
2219 unsigned char *pn;
2220 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2221
2222 /* Note: we rely on the backend to get the header name to be used for
2223 * x-original-to, because the header is really meant for the backends.
2224 * However, if the backend did not specify any option, we have to rely
2225 * on the frontend's header name.
2226 */
2227 if (s->be->orgto_hdr_len) {
2228 len = s->be->orgto_hdr_len;
2229 memcpy(trash, s->be->orgto_hdr_name, len);
2230 } else {
2231 len = s->fe->orgto_hdr_len;
2232 memcpy(trash, s->fe->orgto_hdr_name, len);
2233 }
2234 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2235
2236 if (unlikely(http_header_add_tail2(req, &txn->req,
2237 &txn->hdr_idx, trash, len)) < 0)
2238 goto return_bad_req;
2239 }
2240 }
2241 }
2242
2243 /*
2244 * 11: add "Connection: close" if needed and not yet set.
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 * Note that we do not need to add it in case of HTTP/1.0.
2246 */
2247 if (!(s->flags & SN_CONN_CLOSED) &&
2248 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2249 if ((unlikely(msg->sl.rq.v_l != 8) ||
2250 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2251 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2252 "Connection: close", 17)) < 0)
2253 goto return_bad_req;
2254 s->flags |= SN_CONN_CLOSED;
2255 }
2256 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2257 * If not assigned, perhaps we are balancing on url_param, but this is a
2258 * POST; and the parameters are in the body, maybe scan there to find our server.
2259 * (unless headers overflowed the buffer?)
2260 */
2261 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2262 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2263 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2264 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2265 /* are there enough bytes here? total == l || r || rlim ?
2266 * len is unsigned, but eoh is int,
2267 * how many bytes of body have we received?
2268 * eoh is the first empty line of the header
2269 */
2270 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2271 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2272
2273 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2274 * We can't assume responsibility for the server's decision,
2275 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2276 * We also can't change our mind later, about which server to choose, so round robin.
2277 */
2278 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2279 struct hdr_ctx ctx;
2280 ctx.idx = 0;
2281 /* Expect is allowed in 1.1, look for it */
2282 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2283 if (ctx.idx != 0 &&
2284 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2285 /* We can't reliablly stall and wait for data, because of
2286 * .NET clients that don't conform to rfc2616; so, no need for
2287 * the next block to check length expectations.
2288 * We could send 100 status back to the client, but then we need to
2289 * re-write headers, and send the message. And this isn't the right
2290 * place for that action.
2291 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002292 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002293 goto end_check_maybe_wait_for_body;
2294 }
2295
2296 if (likely(len > s->be->url_param_post_limit)) {
2297 /* nothing to do, we got enough */
2298 } else {
2299 /* limit implies we are supposed to need this many bytes
2300 * to find the parameter. Let's see how many bytes we can wait for.
2301 */
2302 long long hint = len;
2303 struct hdr_ctx ctx;
2304 ctx.idx = 0;
2305 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2306 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2307 buffer_write_dis(req);
2308 req->analysers |= AN_REQ_HTTP_BODY;
2309 }
2310 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002311 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2313 /* now if we have a length, we'll take the hint */
2314 if (ctx.idx) {
2315 /* We have Content-Length */
2316 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2317 hint = 0; /* parse failure, untrusted client */
2318 else {
2319 if (hint > 0)
2320 msg->hdr_content_len = hint;
2321 else
2322 hint = 0; /* bad client, sent negative length */
2323 }
2324 }
2325 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2326 if (s->be->url_param_post_limit < hint)
2327 hint = s->be->url_param_post_limit;
2328 /* now do we really need to buffer more data? */
2329 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002330 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002331 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002332 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002334 }
2335 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 }
2337 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /*************************************************************
2340 * OK, that's finished for the headers. We have done what we *
2341 * could. Let's switch to the DATA state. *
2342 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2345 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 /* When a connection is tarpitted, we use the tarpit timeout,
2348 * which may be the same as the connect timeout if unspecified.
2349 * If unset, then set it to zero because we really want it to
2350 * eventually expire. We build the tarpit as an analyser.
2351 */
2352 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002353 buffer_erase(s->req);
2354 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002356 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 buffer_write_dis(req);
2358 req->analysers |= AN_REQ_HTTP_TARPIT;
2359 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2360 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002361 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 /* OK let's go on with the BODY now */
2365 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002368 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002369 /* we detected a parsing error. We want to archive this request
2370 * in the dedicated proxy area for later troubleshooting.
2371 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002372 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002373 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002374
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 txn->req.msg_state = HTTP_MSG_ERROR;
2376 txn->status = 400;
2377 req->analysers = 0;
2378 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2379 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (!(s->flags & SN_ERR_MASK))
2382 s->flags |= SN_ERR_PRXCOND;
2383 if (!(s->flags & SN_FINST_MASK))
2384 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002385 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002386}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002387
Willy Tarreau60b85b02008-11-30 23:28:40 +01002388/* This function is an analyser which processes the HTTP tarpit. It always
2389 * returns zero, at the beginning because it prevents any other processing
2390 * from occurring, and at the end because it terminates the request.
2391 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002392int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002393{
2394 struct http_txn *txn = &s->txn;
2395
2396 /* This connection is being tarpitted. The CLIENT side has
2397 * already set the connect expiration date to the right
2398 * timeout. We just have to check that the client is still
2399 * there and that the timeout has not expired.
2400 */
2401 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2402 !tick_is_expired(req->analyse_exp, now_ms))
2403 return 0;
2404
2405 /* We will set the queue timer to the time spent, just for
2406 * logging purposes. We fake a 500 server error, so that the
2407 * attacker will not suspect his connection has been tarpitted.
2408 * It will not cause trouble to the logs because we can exclude
2409 * the tarpitted connections by filtering on the 'PT' status flags.
2410 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002411 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2412
2413 txn->status = 500;
2414 if (req->flags != BF_READ_ERROR)
2415 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2416
2417 req->analysers = 0;
2418 req->analyse_exp = TICK_ETERNITY;
2419
2420 s->fe->failed_req++;
2421 if (!(s->flags & SN_ERR_MASK))
2422 s->flags |= SN_ERR_PRXCOND;
2423 if (!(s->flags & SN_FINST_MASK))
2424 s->flags |= SN_FINST_T;
2425 return 0;
2426}
2427
Willy Tarreaud34af782008-11-30 23:36:37 +01002428/* This function is an analyser which processes the HTTP request body. It looks
2429 * for parameters to be used for the load balancing algorithm (url_param). It
2430 * must only be called after the standard HTTP request processing has occurred,
2431 * because it expects the request to be parsed. It returns zero if it needs to
2432 * read more data, or 1 once it has completed its analysis.
2433 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002434int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002435{
2436 struct http_msg *msg = &s->txn.req;
2437 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2438 long long limit = s->be->url_param_post_limit;
2439 struct hdr_ctx ctx;
2440
2441 /* We have to parse the HTTP request body to find any required data.
2442 * "balance url_param check_post" should have been the only way to get
2443 * into this. We were brought here after HTTP header analysis, so all
2444 * related structures are ready.
2445 */
2446
2447 ctx.idx = 0;
2448
2449 /* now if we have a length, we'll take the hint */
2450 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2451 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2452 unsigned int chunk = 0;
2453 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2454 char c = msg->sol[body];
2455 if (ishex(c)) {
2456 unsigned int hex = toupper(c) - '0';
2457 if (hex > 9)
2458 hex -= 'A' - '9' - 1;
2459 chunk = (chunk << 4) | hex;
2460 } else
2461 break;
2462 body++;
2463 }
2464 if (body + 2 >= req->l) /* we want CRLF too */
2465 goto http_body_end; /* end of buffer? data missing! */
2466
2467 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2468 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2469
2470 body += 2; // skip CRLF
2471
2472 /* if we support more then one chunk here, we have to do it again when assigning server
2473 * 1. how much entity data do we have? new var
2474 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2475 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2476 */
2477
2478 if (chunk < limit)
2479 limit = chunk; /* only reading one chunk */
2480 } else {
2481 if (msg->hdr_content_len < limit)
2482 limit = msg->hdr_content_len;
2483 }
2484
2485 http_body_end:
2486 /* we leave once we know we have nothing left to do. This means that we have
2487 * enough bytes, or that we know we'll not get any more (buffer full, read
2488 * buffer closed).
2489 */
2490 if (req->l - body >= limit || /* enough bytes! */
2491 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2492 tick_is_expired(req->analyse_exp, now_ms)) {
2493 /* The situation will not evolve, so let's give up on the analysis. */
2494 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002495 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002496 req->analyse_exp = TICK_ETERNITY;
2497 return 1;
2498 }
2499 else {
2500 /* Not enough data. We'll re-use the http-request
2501 * timeout here. Ideally, we should set the timeout
2502 * relative to the accept() date. We just set the
2503 * request timeout once at the beginning of the
2504 * request.
2505 */
2506 buffer_write_dis(req);
2507 if (!tick_isset(req->analyse_exp))
2508 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2509 return 0;
2510 }
2511}
2512
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002513/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002514 * It normally returns zero, but may return 1 if it absolutely needs to be
2515 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002516 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002517 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002518 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002519int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002520{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002521 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002522 struct buffer *req = t->req;
2523 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002524
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002525 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 +02002526 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002527 t,
2528 rep,
2529 rep->rex, rep->wex,
2530 rep->flags,
2531 rep->l,
2532 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002533
Willy Tarreau2df28e82008-08-17 15:20:19 +02002534 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002535 /*
2536 * Now parse the partial (or complete) lines.
2537 * We will check the response syntax, and also join multi-line
2538 * headers. An index of all the lines will be elaborated while
2539 * parsing.
2540 *
2541 * For the parsing, we use a 28 states FSM.
2542 *
2543 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002544 * rep->data + rep->som = beginning of response
2545 * rep->data + rep->eoh = end of processed headers / start of current one
2546 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002547 * rep->lr = first non-visited byte
2548 * rep->r = end of data
2549 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002550
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002551 int cur_idx;
2552 struct http_msg *msg = &txn->rsp;
2553 struct proxy *cur_proxy;
2554
2555 if (likely(rep->lr < rep->r))
2556 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2557
2558 /* 1: we might have to print this header in debug mode */
2559 if (unlikely((global.mode & MODE_DEBUG) &&
2560 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2561 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2562 char *eol, *sol;
2563
2564 sol = rep->data + msg->som;
2565 eol = sol + msg->sl.rq.l;
2566 debug_hdr("srvrep", t, sol, eol);
2567
2568 sol += hdr_idx_first_pos(&txn->hdr_idx);
2569 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2570
2571 while (cur_idx) {
2572 eol = sol + txn->hdr_idx.v[cur_idx].len;
2573 debug_hdr("srvhdr", t, sol, eol);
2574 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2575 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002576 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002577 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002578
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002579 /*
2580 * Now we quickly check if we have found a full valid response.
2581 * If not so, we check the FD and buffer states before leaving.
2582 * A full response is indicated by the fact that we have seen
2583 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2584 * responses are checked first.
2585 *
2586 * Depending on whether the client is still there or not, we
2587 * may send an error response back or not. Note that normally
2588 * we should only check for HTTP status there, and check I/O
2589 * errors somewhere else.
2590 */
2591
2592 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002593 /* Invalid response */
2594 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002595 /* we detected a parsing error. We want to archive this response
2596 * in the dedicated proxy area for later troubleshooting.
2597 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002598 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002599 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2600 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2601
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002602 buffer_shutr_now(rep);
2603 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002604 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002605 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002606 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002607 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002608 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002609 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002610 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002611 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002612 if (!(t->flags & SN_FINST_MASK))
2613 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002614
Willy Tarreaudafde432008-08-17 01:00:46 +02002615 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002616 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002617 /* too large response does not fit in buffer. */
2618 else if (rep->flags & BF_FULL) {
2619 goto hdr_response_bad;
2620 }
2621 /* read error */
2622 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002623 if (msg->err_pos >= 0)
2624 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002625 buffer_shutr_now(rep);
2626 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002627 if (t->srv)
2628 t->srv->failed_resp++;
2629 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002630 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002631 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002632 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002633 if (!(t->flags & SN_ERR_MASK))
2634 t->flags |= SN_ERR_SRVCL;
2635 if (!(t->flags & SN_FINST_MASK))
2636 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002637 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002638 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002639 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002640 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002641 if (msg->err_pos >= 0)
2642 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002643 buffer_shutr_now(rep);
2644 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002645 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002646 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002647 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002648 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002649 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002650 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002651 if (!(t->flags & SN_ERR_MASK))
2652 t->flags |= SN_ERR_SRVTO;
2653 if (!(t->flags & SN_FINST_MASK))
2654 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002655 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002656 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002657 /* close from server */
2658 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002659 if (msg->err_pos >= 0)
2660 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002661 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002662 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002663 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002664 t->be->failed_resp++;
2665 rep->analysers = 0;
2666 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002667 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002668 if (!(t->flags & SN_ERR_MASK))
2669 t->flags |= SN_ERR_SRVCL;
2670 if (!(t->flags & SN_FINST_MASK))
2671 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002672 return 0;
2673 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002674 /* write error to client (we don't send any message then) */
2675 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002676 if (msg->err_pos >= 0)
2677 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002678 buffer_shutr_now(rep);
2679 t->be->failed_resp++;
2680 rep->analysers = 0;
2681 if (!(t->flags & SN_ERR_MASK))
2682 t->flags |= SN_ERR_CLICL;
2683 if (!(t->flags & SN_FINST_MASK))
2684 t->flags |= SN_FINST_H;
2685 return 0;
2686 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002687 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002688 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002690
Willy Tarreau21d2af32008-02-14 20:25:24 +01002691
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002692 /*****************************************************************
2693 * More interesting part now : we know that we have a complete *
2694 * response which at least looks like HTTP. We have an indicator *
2695 * of each header's length, so we can parse them quickly. *
2696 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002697
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);
2700
Willy Tarreau2df28e82008-08-17 15:20:19 +02002701 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002702
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002703 /* ensure we keep this pointer to the beginning of the message */
2704 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002705
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002706 /*
2707 * 1: get the status code and check for cacheability.
2708 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002709
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002710 t->logs.logwait &= ~LW_RESP;
2711 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002712
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002713 switch (txn->status) {
2714 case 200:
2715 case 203:
2716 case 206:
2717 case 300:
2718 case 301:
2719 case 410:
2720 /* RFC2616 @13.4:
2721 * "A response received with a status code of
2722 * 200, 203, 206, 300, 301 or 410 MAY be stored
2723 * by a cache (...) unless a cache-control
2724 * directive prohibits caching."
2725 *
2726 * RFC2616 @9.5: POST method :
2727 * "Responses to this method are not cacheable,
2728 * unless the response includes appropriate
2729 * Cache-Control or Expires header fields."
2730 */
2731 if (likely(txn->meth != HTTP_METH_POST) &&
2732 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2733 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2734 break;
2735 default:
2736 break;
2737 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002738
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002739 /*
2740 * 2: we may need to capture headers
2741 */
2742 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2743 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2744 txn->rsp.cap, t->fe->rsp_cap);
2745
2746 /*
2747 * 3: we will have to evaluate the filters.
2748 * As opposed to version 1.2, now they will be evaluated in the
2749 * filters order and not in the header order. This means that
2750 * each filter has to be validated among all headers.
2751 *
2752 * Filters are tried with ->be first, then with ->fe if it is
2753 * different from ->be.
2754 */
2755
2756 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2757
2758 cur_proxy = t->be;
2759 while (1) {
2760 struct proxy *rule_set = cur_proxy;
2761
2762 /* try headers filters */
2763 if (rule_set->rsp_exp != NULL) {
2764 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2765 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01002766 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002767 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002768 cur_proxy->failed_resp++;
2769 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002770 buffer_shutr_now(rep);
2771 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002772 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002773 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002774 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002775 if (!(t->flags & SN_ERR_MASK))
2776 t->flags |= SN_ERR_PRXCOND;
2777 if (!(t->flags & SN_FINST_MASK))
2778 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002779 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002780 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002781 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002782
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002783 /* has the response been denied ? */
2784 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01002785 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002786 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002787 cur_proxy->denied_resp++;
2788 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002789 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002790
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002791 /* We might have to check for "Connection:" */
2792 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2793 !(t->flags & SN_CONN_CLOSED)) {
2794 char *cur_ptr, *cur_end, *cur_next;
2795 int cur_idx, old_idx, delta, val;
2796 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002797
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002798 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2799 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002800
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002801 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2802 cur_hdr = &txn->hdr_idx.v[cur_idx];
2803 cur_ptr = cur_next;
2804 cur_end = cur_ptr + cur_hdr->len;
2805 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002806
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002807 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2808 if (val) {
2809 /* 3 possibilities :
2810 * - we have already set Connection: close,
2811 * so we remove this line.
2812 * - we have not yet set Connection: close,
2813 * but this line indicates close. We leave
2814 * it untouched and set the flag.
2815 * - we have not yet set Connection: close,
2816 * and this line indicates non-close. We
2817 * replace it.
2818 */
2819 if (t->flags & SN_CONN_CLOSED) {
2820 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2821 txn->rsp.eoh += delta;
2822 cur_next += delta;
2823 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2824 txn->hdr_idx.used--;
2825 cur_hdr->len = 0;
2826 } else {
2827 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2828 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2829 "close", 5);
2830 cur_next += delta;
2831 cur_hdr->len += delta;
2832 txn->rsp.eoh += delta;
2833 }
2834 t->flags |= SN_CONN_CLOSED;
2835 }
2836 }
2837 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002838 }
2839 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002841 /* add response headers from the rule sets in the same order */
2842 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2843 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2844 rule_set->rsp_add[cur_idx])) < 0)
2845 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002846 }
2847
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002848 /* check whether we're already working on the frontend */
2849 if (cur_proxy == t->fe)
2850 break;
2851 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002853
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002854 /*
2855 * 4: check for server cookie.
2856 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002857 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002858 || (t->be->options & PR_O_CHK_CACHE))
2859 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860
Willy Tarreaubaaee002006-06-26 02:48:02 +02002861
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002864 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2866 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002867
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002868 /*
2869 * 6: add server cookie in the response if needed
2870 */
2871 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2872 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2873 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002875 /* the server is known, it's not the one the client requested, we have to
2876 * insert a set-cookie here, except if we want to insert only on POST
2877 * requests and this one isn't. Note that servers which don't have cookies
2878 * (eg: some backup servers) will return a full cookie removal request.
2879 */
2880 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2881 t->be->cookie_name,
2882 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002884 if (t->be->cookie_domain)
2885 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002887 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2888 trash, len)) < 0)
2889 goto return_bad_resp;
2890 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002892 /* Here, we will tell an eventual cache on the client side that we don't
2893 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2894 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2895 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2896 */
2897 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2900
2901 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2902 "Cache-control: private", 22)) < 0)
2903 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002904 }
2905 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002906
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002908 /*
2909 * 7: check if result will be cacheable with a cookie.
2910 * We'll block the response if security checks have caught
2911 * nasty things such as a cacheable cookie.
2912 */
2913 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2914 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2915 (t->be->options & PR_O_CHK_CACHE)) {
2916
2917 /* we're in presence of a cacheable response containing
2918 * a set-cookie header. We'll block it as requested by
2919 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002920 */
Willy Tarreau8365f932009-03-15 23:11:49 +01002921 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002922 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002924
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002925 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2926 t->be->id, t->srv?t->srv->id:"<dispatch>");
2927 send_log(t->be, LOG_ALERT,
2928 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2929 t->be->id, t->srv?t->srv->id:"<dispatch>");
2930 goto return_srv_prx_502;
2931 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002932
2933 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002934 * 8: add "Connection: close" if needed and not yet set.
2935 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002936 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937 if (!(t->flags & SN_CONN_CLOSED) &&
2938 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2939 if ((unlikely(msg->sl.st.v_l != 8) ||
2940 unlikely(req->data[msg->som + 7] != '0')) &&
2941 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2942 "Connection: close", 17)) < 0)
2943 goto return_bad_resp;
2944 t->flags |= SN_CONN_CLOSED;
2945 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002947 /*************************************************************
2948 * OK, that's finished for the headers. We have done what we *
2949 * could. Let's switch to the DATA state. *
2950 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002951
Willy Tarreaue393fe22008-08-16 22:18:07 +02002952 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955#ifdef CONFIG_HAP_TCPSPLICE
2956 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2957 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002958 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 }
2960#endif
2961 /* if the user wants to log as soon as possible, without counting
2962 * bytes from the server, then this is the right moment. We have
2963 * to temporarily assign bytes_out to log what we currently have.
2964 */
2965 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2966 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2967 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002968 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002969 t->logs.bytes_out = 0;
2970 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002972 /* Note: we must not try to cheat by jumping directly to DATA,
2973 * otherwise we would not let the client side wake up.
2974 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002975
Willy Tarreaudafde432008-08-17 01:00:46 +02002976 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002978
Willy Tarreau2df28e82008-08-17 15:20:19 +02002979 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2980 * probably reduce one day's debugging session.
2981 */
2982#ifdef DEBUG_DEV
2983 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
2984 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2985 __FILE__, __LINE__, rep->analysers);
2986 ABORT_NOW();
2987 }
2988#endif
2989 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002990 return 0;
2991}
Willy Tarreaua15645d2007-03-18 16:22:39 +01002992
Willy Tarreaubaaee002006-06-26 02:48:02 +02002993/*
2994 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02002995 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02002996 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
2997 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01002998 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003000void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003002 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003003 buffer_stop_hijack(rep);
3004 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005 }
3006 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003007 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003008 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003009 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003010 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003011 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003012 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003013
Willy Tarreau91861262007-10-17 17:06:05 +02003014 /* unknown data source or internal error */
3015 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003016 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02003017 if (!(s->flags & SN_ERR_MASK))
3018 s->flags |= SN_ERR_PRXCOND;
3019 if (!(s->flags & SN_FINST_MASK))
3020 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003021 buffer_stop_hijack(rep);
3022 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003023}
3024
3025
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003026/* Iterate the same filter through all request headers.
3027 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003028 * Since it can manage the switch to another backend, it updates the per-proxy
3029 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003030 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003031int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003032{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003033 char term;
3034 char *cur_ptr, *cur_end, *cur_next;
3035 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003036 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003037 struct hdr_idx_elem *cur_hdr;
3038 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003040 last_hdr = 0;
3041
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003042 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003043 old_idx = 0;
3044
3045 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003046 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003047 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003048 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003049 (exp->action == ACT_ALLOW ||
3050 exp->action == ACT_DENY ||
3051 exp->action == ACT_TARPIT))
3052 return 0;
3053
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003054 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003055 if (!cur_idx)
3056 break;
3057
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003058 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003059 cur_ptr = cur_next;
3060 cur_end = cur_ptr + cur_hdr->len;
3061 cur_next = cur_end + cur_hdr->cr + 1;
3062
3063 /* Now we have one header between cur_ptr and cur_end,
3064 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003065 */
3066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003067 /* The annoying part is that pattern matching needs
3068 * that we modify the contents to null-terminate all
3069 * strings before testing them.
3070 */
3071
3072 term = *cur_end;
3073 *cur_end = '\0';
3074
3075 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3076 switch (exp->action) {
3077 case ACT_SETBE:
3078 /* It is not possible to jump a second time.
3079 * FIXME: should we return an HTTP/500 here so that
3080 * the admin knows there's a problem ?
3081 */
3082 if (t->be != t->fe)
3083 break;
3084
3085 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003087 last_hdr = 1;
3088 break;
3089
3090 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003091 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003092 last_hdr = 1;
3093 break;
3094
3095 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003096 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003097 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003098 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003099 break;
3100
3101 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003102 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003103 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003104 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003105 break;
3106
3107 case ACT_REPLACE:
3108 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3109 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3110 /* FIXME: if the user adds a newline in the replacement, the
3111 * index will not be recalculated for now, and the new line
3112 * will not be counted as a new header.
3113 */
3114
3115 cur_end += delta;
3116 cur_next += delta;
3117 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003118 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003119 break;
3120
3121 case ACT_REMOVE:
3122 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3123 cur_next += delta;
3124
3125 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003126 txn->req.eoh += delta;
3127 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3128 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003129 cur_hdr->len = 0;
3130 cur_end = NULL; /* null-term has been rewritten */
3131 break;
3132
3133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003134 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003135 if (cur_end)
3136 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003138 /* keep the link from this header to next one in case of later
3139 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003140 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003141 old_idx = cur_idx;
3142 }
3143 return 0;
3144}
3145
3146
3147/* Apply the filter to the request line.
3148 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3149 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003150 * Since it can manage the switch to another backend, it updates the per-proxy
3151 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003152 */
3153int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3154{
3155 char term;
3156 char *cur_ptr, *cur_end;
3157 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003158 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003159 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003160
Willy Tarreau58f10d72006-12-04 02:26:12 +01003161
Willy Tarreau3d300592007-03-18 18:34:41 +01003162 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003163 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003164 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003165 (exp->action == ACT_ALLOW ||
3166 exp->action == ACT_DENY ||
3167 exp->action == ACT_TARPIT))
3168 return 0;
3169 else if (exp->action == ACT_REMOVE)
3170 return 0;
3171
3172 done = 0;
3173
Willy Tarreau9cdde232007-05-02 20:58:19 +02003174 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003175 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003176
3177 /* Now we have the request line between cur_ptr and cur_end */
3178
3179 /* The annoying part is that pattern matching needs
3180 * that we modify the contents to null-terminate all
3181 * strings before testing them.
3182 */
3183
3184 term = *cur_end;
3185 *cur_end = '\0';
3186
3187 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3188 switch (exp->action) {
3189 case ACT_SETBE:
3190 /* It is not possible to jump a second time.
3191 * FIXME: should we return an HTTP/500 here so that
3192 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003193 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003194 if (t->be != t->fe)
3195 break;
3196
3197 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003199 done = 1;
3200 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003202 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003203 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003204 done = 1;
3205 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003206
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003207 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003208 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003209 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003210 done = 1;
3211 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003212
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003213 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003214 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003215 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003216 done = 1;
3217 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003218
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003219 case ACT_REPLACE:
3220 *cur_end = term; /* restore the string terminator */
3221 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3222 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3223 /* FIXME: if the user adds a newline in the replacement, the
3224 * index will not be recalculated for now, and the new line
3225 * will not be counted as a new header.
3226 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003227
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003228 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003229 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003230
Willy Tarreau9cdde232007-05-02 20:58:19 +02003231 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003232 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003233 HTTP_MSG_RQMETH,
3234 cur_ptr, cur_end + 1,
3235 NULL, NULL);
3236 if (unlikely(!cur_end))
3237 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003238
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003239 /* we have a full request and we know that we have either a CR
3240 * or an LF at <ptr>.
3241 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003242 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3243 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003244 /* there is no point trying this regex on headers */
3245 return 1;
3246 }
3247 }
3248 *cur_end = term; /* restore the string terminator */
3249 return done;
3250}
Willy Tarreau97de6242006-12-27 17:18:38 +01003251
Willy Tarreau58f10d72006-12-04 02:26:12 +01003252
Willy Tarreau58f10d72006-12-04 02:26:12 +01003253
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003254/*
3255 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3256 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003257 * unparsable request. Since it can manage the switch to another backend, it
3258 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003259 */
3260int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3261{
Willy Tarreau3d300592007-03-18 18:34:41 +01003262 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003263 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003264 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003265 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003267 /*
3268 * The interleaving of transformations and verdicts
3269 * makes it difficult to decide to continue or stop
3270 * the evaluation.
3271 */
3272
Willy Tarreau3d300592007-03-18 18:34:41 +01003273 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003274 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3275 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3276 exp = exp->next;
3277 continue;
3278 }
3279
3280 /* Apply the filter to the request line. */
3281 ret = apply_filter_to_req_line(t, req, exp);
3282 if (unlikely(ret < 0))
3283 return -1;
3284
3285 if (likely(ret == 0)) {
3286 /* The filter did not match the request, it can be
3287 * iterated through all headers.
3288 */
3289 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003290 }
3291 exp = exp->next;
3292 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003293 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003294}
3295
3296
Willy Tarreaua15645d2007-03-18 16:22:39 +01003297
Willy Tarreau58f10d72006-12-04 02:26:12 +01003298/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003299 * Manage client-side cookie. It can impact performance by about 2% so it is
3300 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003301 */
3302void manage_client_side_cookies(struct session *t, struct buffer *req)
3303{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003304 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003305 char *p1, *p2, *p3, *p4;
3306 char *del_colon, *del_cookie, *colon;
3307 int app_cookies;
3308
3309 appsess *asession_temp = NULL;
3310 appsess local_asession;
3311
3312 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003313 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003314
Willy Tarreau2a324282006-12-05 00:05:46 +01003315 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003316 * we start with the start line.
3317 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003318 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003319 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003320
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003321 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003322 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003323 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003324
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003325 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003326 cur_ptr = cur_next;
3327 cur_end = cur_ptr + cur_hdr->len;
3328 cur_next = cur_end + cur_hdr->cr + 1;
3329
3330 /* We have one full header between cur_ptr and cur_end, and the
3331 * next header starts at cur_next. We're only interested in
3332 * "Cookie:" headers.
3333 */
3334
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003335 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3336 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003337 old_idx = cur_idx;
3338 continue;
3339 }
3340
3341 /* Now look for cookies. Conforming to RFC2109, we have to support
3342 * attributes whose name begin with a '$', and associate them with
3343 * the right cookie, if we want to delete this cookie.
3344 * So there are 3 cases for each cookie read :
3345 * 1) it's a special attribute, beginning with a '$' : ignore it.
3346 * 2) it's a server id cookie that we *MAY* want to delete : save
3347 * some pointers on it (last semi-colon, beginning of cookie...)
3348 * 3) it's an application cookie : we *MAY* have to delete a previous
3349 * "special" cookie.
3350 * At the end of loop, if a "special" cookie remains, we may have to
3351 * remove it. If no application cookie persists in the header, we
3352 * *MUST* delete it
3353 */
3354
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003355 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003356
Willy Tarreau58f10d72006-12-04 02:26:12 +01003357 /* del_cookie == NULL => nothing to be deleted */
3358 del_colon = del_cookie = NULL;
3359 app_cookies = 0;
3360
3361 while (p1 < cur_end) {
3362 /* skip spaces and colons, but keep an eye on these ones */
3363 while (p1 < cur_end) {
3364 if (*p1 == ';' || *p1 == ',')
3365 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003366 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003367 break;
3368 p1++;
3369 }
3370
3371 if (p1 == cur_end)
3372 break;
3373
3374 /* p1 is at the beginning of the cookie name */
3375 p2 = p1;
3376 while (p2 < cur_end && *p2 != '=')
3377 p2++;
3378
3379 if (p2 == cur_end)
3380 break;
3381
3382 p3 = p2 + 1; /* skips the '=' sign */
3383 if (p3 == cur_end)
3384 break;
3385
3386 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003387 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003388 p4++;
3389
3390 /* here, we have the cookie name between p1 and p2,
3391 * and its value between p3 and p4.
3392 * we can process it :
3393 *
3394 * Cookie: NAME=VALUE;
3395 * | || || |
3396 * | || || +--> p4
3397 * | || |+-------> p3
3398 * | || +--------> p2
3399 * | |+------------> p1
3400 * | +-------------> colon
3401 * +--------------------> cur_ptr
3402 */
3403
3404 if (*p1 == '$') {
3405 /* skip this one */
3406 }
3407 else {
3408 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003409 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003410 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003411 (p4 - p1 >= t->fe->capture_namelen) &&
3412 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003413 int log_len = p4 - p1;
3414
Willy Tarreau086b3b42007-05-13 21:45:51 +02003415 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003416 Alert("HTTP logging : out of memory.\n");
3417 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003418 if (log_len > t->fe->capture_len)
3419 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003420 memcpy(txn->cli_cookie, p1, log_len);
3421 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003422 }
3423 }
3424
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003425 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3426 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003427 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003428 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003429 char *delim;
3430
3431 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3432 * have the server ID betweek p3 and delim, and the original cookie between
3433 * delim+1 and p4. Otherwise, delim==p4 :
3434 *
3435 * Cookie: NAME=SRV~VALUE;
3436 * | || || | |
3437 * | || || | +--> p4
3438 * | || || +--------> delim
3439 * | || |+-----------> p3
3440 * | || +------------> p2
3441 * | |+----------------> p1
3442 * | +-----------------> colon
3443 * +------------------------> cur_ptr
3444 */
3445
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003446 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003447 for (delim = p3; delim < p4; delim++)
3448 if (*delim == COOKIE_DELIM)
3449 break;
3450 }
3451 else
3452 delim = p4;
3453
3454
3455 /* Here, we'll look for the first running server which supports the cookie.
3456 * This allows to share a same cookie between several servers, for example
3457 * to dedicate backup servers to specific servers only.
3458 * However, to prevent clients from sticking to cookie-less backup server
3459 * when they have incidentely learned an empty cookie, we simply ignore
3460 * empty cookies and mark them as invalid.
3461 */
3462 if (delim == p3)
3463 srv = NULL;
3464
3465 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003466 if (srv->cookie && (srv->cklen == delim - p3) &&
3467 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003468 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003469 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003470 txn->flags &= ~TX_CK_MASK;
3471 txn->flags |= TX_CK_VALID;
3472 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003473 t->srv = srv;
3474 break;
3475 } else {
3476 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003477 txn->flags &= ~TX_CK_MASK;
3478 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003479 }
3480 }
3481 srv = srv->next;
3482 }
3483
Willy Tarreau3d300592007-03-18 18:34:41 +01003484 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003485 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003486 txn->flags &= ~TX_CK_MASK;
3487 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003488 }
3489
3490 /* depending on the cookie mode, we may have to either :
3491 * - delete the complete cookie if we're in insert+indirect mode, so that
3492 * the server never sees it ;
3493 * - remove the server id from the cookie value, and tag the cookie as an
3494 * application cookie so that it does not get accidentely removed later,
3495 * if we're in cookie prefix mode
3496 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003497 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003498 int delta; /* negative */
3499
3500 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3501 p4 += delta;
3502 cur_end += delta;
3503 cur_next += delta;
3504 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003505 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003506
3507 del_cookie = del_colon = NULL;
3508 app_cookies++; /* protect the header from deletion */
3509 }
3510 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003511 (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 +01003512 del_cookie = p1;
3513 del_colon = colon;
3514 }
3515 } else {
3516 /* now we know that we must keep this cookie since it's
3517 * not ours. But if we wanted to delete our cookie
3518 * earlier, we cannot remove the complete header, but we
3519 * can remove the previous block itself.
3520 */
3521 app_cookies++;
3522
3523 if (del_cookie != NULL) {
3524 int delta; /* negative */
3525
3526 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3527 p4 += delta;
3528 cur_end += delta;
3529 cur_next += delta;
3530 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003531 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003532 del_cookie = del_colon = NULL;
3533 }
3534 }
3535
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003536 if ((t->be->appsession_name != NULL) &&
3537 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003538 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003539
Willy Tarreau58f10d72006-12-04 02:26:12 +01003540 /* Cool... it's the right one */
3541
3542 asession_temp = &local_asession;
3543
Willy Tarreau63963c62007-05-13 21:29:55 +02003544 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003545 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3546 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3547 return;
3548 }
3549
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003550 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3551 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003553
Willy Tarreau58f10d72006-12-04 02:26:12 +01003554 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003555 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3556 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003557 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003558 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003559 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003560 Alert("Not enough memory process_cli():asession:calloc().\n");
3561 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3562 return;
3563 }
3564
3565 asession_temp->sessid = local_asession.sessid;
3566 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003567 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003568 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003569 } else {
3570 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003571 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003572 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003573 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003574 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003575 Alert("Found Application Session without matching server.\n");
3576 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003577 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003578 while (srv) {
3579 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003580 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003581 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003582 txn->flags &= ~TX_CK_MASK;
3583 txn->flags |= TX_CK_VALID;
3584 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003585 t->srv = srv;
3586 break;
3587 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003588 txn->flags &= ~TX_CK_MASK;
3589 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590 }
3591 }
3592 srv = srv->next;
3593 }/* end while(srv) */
3594 }/* end else if server == NULL */
3595
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003596 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003597 asession_temp->request_count++;
3598#if defined(DEBUG_HASH)
3599 Alert("manage_client_side_cookies\n");
3600 appsession_hash_dump(&(t->be->htbl_proxy));
3601#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602 }/* end if ((t->proxy->appsession_name != NULL) ... */
3603 }
3604
3605 /* we'll have to look for another cookie ... */
3606 p1 = p4;
3607 } /* while (p1 < cur_end) */
3608
3609 /* There's no more cookie on this line.
3610 * We may have marked the last one(s) for deletion.
3611 * We must do this now in two ways :
3612 * - if there is no app cookie, we simply delete the header ;
3613 * - if there are app cookies, we must delete the end of the
3614 * string properly, including the colon/semi-colon before
3615 * the cookie name.
3616 */
3617 if (del_cookie != NULL) {
3618 int delta;
3619 if (app_cookies) {
3620 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3621 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003622 cur_hdr->len += delta;
3623 } else {
3624 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003625
3626 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003627 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3628 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003629 cur_hdr->len = 0;
3630 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003631 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003632 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003633 }
3634
3635 /* keep the link from this header to next one */
3636 old_idx = cur_idx;
3637 } /* end of cookie processing on this header */
3638}
3639
3640
Willy Tarreaua15645d2007-03-18 16:22:39 +01003641/* Iterate the same filter through all response headers contained in <rtr>.
3642 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3643 */
3644int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3645{
3646 char term;
3647 char *cur_ptr, *cur_end, *cur_next;
3648 int cur_idx, old_idx, last_hdr;
3649 struct http_txn *txn = &t->txn;
3650 struct hdr_idx_elem *cur_hdr;
3651 int len, delta;
3652
3653 last_hdr = 0;
3654
3655 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3656 old_idx = 0;
3657
3658 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003659 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003660 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003661 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003662 (exp->action == ACT_ALLOW ||
3663 exp->action == ACT_DENY))
3664 return 0;
3665
3666 cur_idx = txn->hdr_idx.v[old_idx].next;
3667 if (!cur_idx)
3668 break;
3669
3670 cur_hdr = &txn->hdr_idx.v[cur_idx];
3671 cur_ptr = cur_next;
3672 cur_end = cur_ptr + cur_hdr->len;
3673 cur_next = cur_end + cur_hdr->cr + 1;
3674
3675 /* Now we have one header between cur_ptr and cur_end,
3676 * and the next header starts at cur_next.
3677 */
3678
3679 /* The annoying part is that pattern matching needs
3680 * that we modify the contents to null-terminate all
3681 * strings before testing them.
3682 */
3683
3684 term = *cur_end;
3685 *cur_end = '\0';
3686
3687 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3688 switch (exp->action) {
3689 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003690 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003691 last_hdr = 1;
3692 break;
3693
3694 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003695 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003696 last_hdr = 1;
3697 break;
3698
3699 case ACT_REPLACE:
3700 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3701 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3702 /* FIXME: if the user adds a newline in the replacement, the
3703 * index will not be recalculated for now, and the new line
3704 * will not be counted as a new header.
3705 */
3706
3707 cur_end += delta;
3708 cur_next += delta;
3709 cur_hdr->len += delta;
3710 txn->rsp.eoh += delta;
3711 break;
3712
3713 case ACT_REMOVE:
3714 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3715 cur_next += delta;
3716
3717 /* FIXME: this should be a separate function */
3718 txn->rsp.eoh += delta;
3719 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3720 txn->hdr_idx.used--;
3721 cur_hdr->len = 0;
3722 cur_end = NULL; /* null-term has been rewritten */
3723 break;
3724
3725 }
3726 }
3727 if (cur_end)
3728 *cur_end = term; /* restore the string terminator */
3729
3730 /* keep the link from this header to next one in case of later
3731 * removal of next header.
3732 */
3733 old_idx = cur_idx;
3734 }
3735 return 0;
3736}
3737
3738
3739/* Apply the filter to the status line in the response buffer <rtr>.
3740 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3741 * or -1 if a replacement resulted in an invalid status line.
3742 */
3743int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3744{
3745 char term;
3746 char *cur_ptr, *cur_end;
3747 int done;
3748 struct http_txn *txn = &t->txn;
3749 int len, delta;
3750
3751
Willy Tarreau3d300592007-03-18 18:34:41 +01003752 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003753 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003754 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003755 (exp->action == ACT_ALLOW ||
3756 exp->action == ACT_DENY))
3757 return 0;
3758 else if (exp->action == ACT_REMOVE)
3759 return 0;
3760
3761 done = 0;
3762
Willy Tarreau9cdde232007-05-02 20:58:19 +02003763 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003764 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3765
3766 /* Now we have the status line between cur_ptr and cur_end */
3767
3768 /* The annoying part is that pattern matching needs
3769 * that we modify the contents to null-terminate all
3770 * strings before testing them.
3771 */
3772
3773 term = *cur_end;
3774 *cur_end = '\0';
3775
3776 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3777 switch (exp->action) {
3778 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003779 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003780 done = 1;
3781 break;
3782
3783 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003784 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003785 done = 1;
3786 break;
3787
3788 case ACT_REPLACE:
3789 *cur_end = term; /* restore the string terminator */
3790 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3791 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3792 /* FIXME: if the user adds a newline in the replacement, the
3793 * index will not be recalculated for now, and the new line
3794 * will not be counted as a new header.
3795 */
3796
3797 txn->rsp.eoh += delta;
3798 cur_end += delta;
3799
Willy Tarreau9cdde232007-05-02 20:58:19 +02003800 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003801 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003802 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003803 cur_ptr, cur_end + 1,
3804 NULL, NULL);
3805 if (unlikely(!cur_end))
3806 return -1;
3807
3808 /* we have a full respnse and we know that we have either a CR
3809 * or an LF at <ptr>.
3810 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003811 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003812 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3813 /* there is no point trying this regex on headers */
3814 return 1;
3815 }
3816 }
3817 *cur_end = term; /* restore the string terminator */
3818 return done;
3819}
3820
3821
3822
3823/*
3824 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3825 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3826 * unparsable response.
3827 */
3828int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3829{
Willy Tarreau3d300592007-03-18 18:34:41 +01003830 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003831 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003832 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003833 int ret;
3834
3835 /*
3836 * The interleaving of transformations and verdicts
3837 * makes it difficult to decide to continue or stop
3838 * the evaluation.
3839 */
3840
Willy Tarreau3d300592007-03-18 18:34:41 +01003841 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003842 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3843 exp->action == ACT_PASS)) {
3844 exp = exp->next;
3845 continue;
3846 }
3847
3848 /* Apply the filter to the status line. */
3849 ret = apply_filter_to_sts_line(t, rtr, exp);
3850 if (unlikely(ret < 0))
3851 return -1;
3852
3853 if (likely(ret == 0)) {
3854 /* The filter did not match the response, it can be
3855 * iterated through all headers.
3856 */
3857 apply_filter_to_resp_headers(t, rtr, exp);
3858 }
3859 exp = exp->next;
3860 }
3861 return 0;
3862}
3863
3864
3865
3866/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003867 * Manage server-side cookies. It can impact performance by about 2% so it is
3868 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003869 */
3870void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3871{
3872 struct http_txn *txn = &t->txn;
3873 char *p1, *p2, *p3, *p4;
3874
3875 appsess *asession_temp = NULL;
3876 appsess local_asession;
3877
3878 char *cur_ptr, *cur_end, *cur_next;
3879 int cur_idx, old_idx, delta;
3880
Willy Tarreaua15645d2007-03-18 16:22:39 +01003881 /* Iterate through the headers.
3882 * we start with the start line.
3883 */
3884 old_idx = 0;
3885 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3886
3887 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3888 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003889 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003890
3891 cur_hdr = &txn->hdr_idx.v[cur_idx];
3892 cur_ptr = cur_next;
3893 cur_end = cur_ptr + cur_hdr->len;
3894 cur_next = cur_end + cur_hdr->cr + 1;
3895
3896 /* We have one full header between cur_ptr and cur_end, and the
3897 * next header starts at cur_next. We're only interested in
3898 * "Cookie:" headers.
3899 */
3900
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003901 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3902 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003903 old_idx = cur_idx;
3904 continue;
3905 }
3906
3907 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003908 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003909
3910
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003911 /* maybe we only wanted to see if there was a set-cookie. Note that
3912 * the cookie capture is declared in the fronend.
3913 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003914 if (t->be->cookie_name == NULL &&
3915 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003916 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003917 return;
3918
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003919 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003920
3921 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003922 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3923 break;
3924
3925 /* p1 is at the beginning of the cookie name */
3926 p2 = p1;
3927
3928 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3929 p2++;
3930
3931 if (p2 == cur_end || *p2 == ';') /* next cookie */
3932 break;
3933
3934 p3 = p2 + 1; /* skip the '=' sign */
3935 if (p3 == cur_end)
3936 break;
3937
3938 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003939 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003940 p4++;
3941
3942 /* here, we have the cookie name between p1 and p2,
3943 * and its value between p3 and p4.
3944 * we can process it.
3945 */
3946
3947 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003948 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003949 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003950 (p4 - p1 >= t->fe->capture_namelen) &&
3951 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003952 int log_len = p4 - p1;
3953
Willy Tarreau086b3b42007-05-13 21:45:51 +02003954 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003955 Alert("HTTP logging : out of memory.\n");
3956 }
3957
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003958 if (log_len > t->fe->capture_len)
3959 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003960 memcpy(txn->srv_cookie, p1, log_len);
3961 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003962 }
3963
3964 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003965 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3966 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003967 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003968 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003969
3970 /* If the cookie is in insert mode on a known server, we'll delete
3971 * this occurrence because we'll insert another one later.
3972 * We'll delete it too if the "indirect" option is set and we're in
3973 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003974 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
3975 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003976 /* this header must be deleted */
3977 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3978 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3979 txn->hdr_idx.used--;
3980 cur_hdr->len = 0;
3981 cur_next += delta;
3982 txn->rsp.eoh += delta;
3983
Willy Tarreau3d300592007-03-18 18:34:41 +01003984 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003985 }
3986 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003987 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003988 /* replace bytes p3->p4 with the cookie name associated
3989 * with this server since we know it.
3990 */
3991 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
3992 cur_hdr->len += delta;
3993 cur_next += delta;
3994 txn->rsp.eoh += delta;
3995
Willy Tarreau3d300592007-03-18 18:34:41 +01003996 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003997 }
3998 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003999 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004000 /* insert the cookie name associated with this server
4001 * before existing cookie, and insert a delimitor between them..
4002 */
4003 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4004 cur_hdr->len += delta;
4005 cur_next += delta;
4006 txn->rsp.eoh += delta;
4007
4008 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004009 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004010 }
4011 }
4012 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004013 else if ((t->be->appsession_name != NULL) &&
4014 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004015
4016 /* Cool... it's the right one */
4017
4018 size_t server_id_len = strlen(t->srv->id) + 1;
4019 asession_temp = &local_asession;
4020
Willy Tarreau63963c62007-05-13 21:29:55 +02004021 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004022 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4023 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4024 return;
4025 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004026 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4027 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004028 asession_temp->serverid = NULL;
4029
4030 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004031 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4032 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004033 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034 Alert("Not enough Memory process_srv():asession:calloc().\n");
4035 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4036 return;
4037 }
4038 asession_temp->sessid = local_asession.sessid;
4039 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004040 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004041 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4042 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004043 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004044 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004045 }
4046
Willy Tarreaua15645d2007-03-18 16:22:39 +01004047 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004048 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004049 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4050 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4051 return;
4052 }
4053 asession_temp->serverid[0] = '\0';
4054 }
4055
4056 if (asession_temp->serverid[0] == '\0')
4057 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4058
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004059 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004060 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004061#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004062 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004063 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004064#endif
4065 }/* end if ((t->proxy->appsession_name != NULL) ... */
4066 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4067 } /* we're now at the end of the cookie value */
4068
4069 /* keep the link from this header to next one */
4070 old_idx = cur_idx;
4071 } /* end of cookie processing on this header */
4072}
4073
4074
4075
4076/*
4077 * Check if response is cacheable or not. Updates t->flags.
4078 */
4079void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4080{
4081 struct http_txn *txn = &t->txn;
4082 char *p1, *p2;
4083
4084 char *cur_ptr, *cur_end, *cur_next;
4085 int cur_idx;
4086
Willy Tarreau5df51872007-11-25 16:20:08 +01004087 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004088 return;
4089
4090 /* Iterate through the headers.
4091 * we start with the start line.
4092 */
4093 cur_idx = 0;
4094 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4095
4096 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4097 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004098 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004099
4100 cur_hdr = &txn->hdr_idx.v[cur_idx];
4101 cur_ptr = cur_next;
4102 cur_end = cur_ptr + cur_hdr->len;
4103 cur_next = cur_end + cur_hdr->cr + 1;
4104
4105 /* We have one full header between cur_ptr and cur_end, and the
4106 * next header starts at cur_next. We're only interested in
4107 * "Cookie:" headers.
4108 */
4109
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004110 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4111 if (val) {
4112 if ((cur_end - (cur_ptr + val) >= 8) &&
4113 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4114 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4115 return;
4116 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004117 }
4118
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004119 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4120 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004121 continue;
4122
4123 /* OK, right now we know we have a cache-control header at cur_ptr */
4124
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004125 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004126
4127 if (p1 >= cur_end) /* no more info */
4128 continue;
4129
4130 /* p1 is at the beginning of the value */
4131 p2 = p1;
4132
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004133 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004134 p2++;
4135
4136 /* we have a complete value between p1 and p2 */
4137 if (p2 < cur_end && *p2 == '=') {
4138 /* we have something of the form no-cache="set-cookie" */
4139 if ((cur_end - p1 >= 21) &&
4140 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4141 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004142 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004143 continue;
4144 }
4145
4146 /* OK, so we know that either p2 points to the end of string or to a comma */
4147 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4148 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4149 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4150 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004151 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004152 return;
4153 }
4154
4155 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004156 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004157 continue;
4158 }
4159 }
4160}
4161
4162
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163/*
4164 * Try to retrieve a known appsession in the URI, then the associated server.
4165 * If the server is found, it's assigned to the session.
4166 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168{
Willy Tarreau3d300592007-03-18 18:34:41 +01004169 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004170 appsess *asession_temp = NULL;
4171 appsess local_asession;
4172 char *request_line;
4173
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004174 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004175 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004176 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004177 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004178 return;
4179
4180 /* skip ';' */
4181 request_line++;
4182
4183 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004184 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004185 return;
4186
4187 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004188 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004189
4190 /* First try if we already have an appsession */
4191 asession_temp = &local_asession;
4192
Willy Tarreau63963c62007-05-13 21:29:55 +02004193 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004194 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4195 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4196 return;
4197 }
4198
4199 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004200 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4201 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004202 asession_temp->serverid = NULL;
4203
4204 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004205 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4206 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004207 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004209 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004210 Alert("Not enough memory process_cli():asession:calloc().\n");
4211 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4212 return;
4213 }
4214 asession_temp->sessid = local_asession.sessid;
4215 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004216 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004217 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218 }
4219 else {
4220 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004221 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004223
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004224 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004226
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004228 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004229 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230#endif
4231 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004232 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004233 Alert("Found Application Session without matching server.\n");
4234 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004235 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236 while (srv) {
4237 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004238 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004240 txn->flags &= ~TX_CK_MASK;
4241 txn->flags |= TX_CK_VALID;
4242 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004243 t->srv = srv;
4244 break;
4245 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004246 txn->flags &= ~TX_CK_MASK;
4247 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248 }
4249 }
4250 srv = srv->next;
4251 }
4252 }
4253}
4254
4255
Willy Tarreaub2513902006-12-17 14:52:38 +01004256/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004257 * In a GET or HEAD request, check if the requested URI matches the stats uri
4258 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004259 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004260 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004261 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004262 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004263 *
4264 * Returns 1 if the session's state changes, otherwise 0.
4265 */
4266int stats_check_uri_auth(struct session *t, struct proxy *backend)
4267{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004268 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004269 struct uri_auth *uri_auth = backend->uri_auth;
4270 struct user_auth *user;
4271 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004272 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004273
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004274 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004276 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004277 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004278 return 0;
4279
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004280 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004281
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004282 /* the URI is in h */
4283 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004284 return 0;
4285
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004286 h += uri_auth->uri_len;
4287 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4288 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004289 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004290 break;
4291 }
4292 h++;
4293 }
4294
4295 if (uri_auth->refresh) {
4296 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4297 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4298 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004299 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004300 break;
4301 }
4302 h++;
4303 }
4304 }
4305
Willy Tarreau55bb8452007-10-17 18:44:57 +02004306 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4307 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4308 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004309 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004310 break;
4311 }
4312 h++;
4313 }
4314
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004315 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4316
Willy Tarreaub2513902006-12-17 14:52:38 +01004317 /* we are in front of a interceptable URI. Let's check
4318 * if there's an authentication and if it's valid.
4319 */
4320 user = uri_auth->users;
4321 if (!user) {
4322 /* no user auth required, it's OK */
4323 authenticated = 1;
4324 } else {
4325 authenticated = 0;
4326
4327 /* a user list is defined, we have to check.
4328 * skip 21 chars for "Authorization: Basic ".
4329 */
4330
4331 /* FIXME: this should move to an earlier place */
4332 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004333 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4334 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4335 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004336 if (len > 14 &&
4337 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004338 txn->auth_hdr.str = h;
4339 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004340 break;
4341 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004342 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004343 }
4344
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004345 if (txn->auth_hdr.len < 21 ||
4346 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004347 user = NULL;
4348
4349 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004350 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4351 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004352 user->user_pwd, user->user_len)) {
4353 authenticated = 1;
4354 break;
4355 }
4356 user = user->next;
4357 }
4358 }
4359
4360 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004361 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004362
4363 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004364 msg.str = trash;
4365 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004366 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004367 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004368 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004369 if (!(t->flags & SN_ERR_MASK))
4370 t->flags |= SN_ERR_PRXCOND;
4371 if (!(t->flags & SN_FINST_MASK))
4372 t->flags |= SN_FINST_R;
4373 return 1;
4374 }
4375
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004376 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004377 * data.
4378 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004379 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004380 buffer_shutw_now(t->req);
4381 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004382 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004383 t->data_source = DATA_SRC_STATS;
4384 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004385 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004386 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004387 return 1;
4388}
4389
Willy Tarreau4076a152009-04-02 15:18:36 +02004390/*
4391 * Capture a bad request or response and archive it in the proxy's structure.
4392 */
4393void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4394 struct buffer *buf, struct http_msg *msg,
4395 struct proxy *other_end)
4396{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004397 es->len = buf->r - (buf->data + msg->som);
4398 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004399 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004400 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004401 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004402 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004403 es->when = date; // user-visible date
4404 es->sid = s->uniq_id;
4405 es->srv = s->srv;
4406 es->oe = other_end;
4407 es->src = s->cli_addr;
4408}
Willy Tarreaub2513902006-12-17 14:52:38 +01004409
Willy Tarreaubaaee002006-06-26 02:48:02 +02004410/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411 * Print a debug line with a header
4412 */
4413void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4414{
4415 int len, max;
4416 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004417 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004418 max = end - start;
4419 UBOUND(max, sizeof(trash) - len - 1);
4420 len += strlcpy2(trash + len, start, max + 1);
4421 trash[len++] = '\n';
4422 write(1, trash, len);
4423}
4424
4425
Willy Tarreau8797c062007-05-07 00:55:35 +02004426/************************************************************************/
4427/* The code below is dedicated to ACL parsing and matching */
4428/************************************************************************/
4429
4430
4431
4432
4433/* 1. Check on METHOD
4434 * We use the pre-parsed method if it is known, and store its number as an
4435 * integer. If it is unknown, we use the pointer and the length.
4436 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004437static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004438{
4439 int len, meth;
4440
Willy Tarreauae8b7962007-06-09 23:10:04 +02004441 len = strlen(*text);
4442 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004443
4444 pattern->val.i = meth;
4445 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004446 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004447 if (!pattern->ptr.str)
4448 return 0;
4449 pattern->len = len;
4450 }
4451 return 1;
4452}
4453
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004454static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004455acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4456 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004457{
4458 int meth;
4459 struct http_txn *txn = l7;
4460
Willy Tarreaub6866442008-07-14 23:54:42 +02004461 if (!txn)
4462 return 0;
4463
Willy Tarreauc11416f2007-06-17 16:58:38 +02004464 if (txn->req.msg_state != HTTP_MSG_BODY)
4465 return 0;
4466
Willy Tarreau8797c062007-05-07 00:55:35 +02004467 meth = txn->meth;
4468 test->i = meth;
4469 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004470 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4471 /* ensure the indexes are not affected */
4472 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004473 test->len = txn->req.sl.rq.m_l;
4474 test->ptr = txn->req.sol;
4475 }
4476 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4477 return 1;
4478}
4479
4480static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4481{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004482 int icase;
4483
Willy Tarreau8797c062007-05-07 00:55:35 +02004484 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004485 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004486
4487 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004488 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004489
4490 /* Other method, we must compare the strings */
4491 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004492 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004493
4494 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4495 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4496 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004497 return ACL_PAT_FAIL;
4498 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004499}
4500
4501/* 2. Check on Request/Status Version
4502 * We simply compare strings here.
4503 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004504static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004505{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004506 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004507 if (!pattern->ptr.str)
4508 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004509 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004510 return 1;
4511}
4512
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004513static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004514acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4515 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004516{
4517 struct http_txn *txn = l7;
4518 char *ptr;
4519 int len;
4520
Willy Tarreaub6866442008-07-14 23:54:42 +02004521 if (!txn)
4522 return 0;
4523
Willy Tarreauc11416f2007-06-17 16:58:38 +02004524 if (txn->req.msg_state != HTTP_MSG_BODY)
4525 return 0;
4526
Willy Tarreau8797c062007-05-07 00:55:35 +02004527 len = txn->req.sl.rq.v_l;
4528 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4529
4530 while ((len-- > 0) && (*ptr++ != '/'));
4531 if (len <= 0)
4532 return 0;
4533
4534 test->ptr = ptr;
4535 test->len = len;
4536
4537 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4538 return 1;
4539}
4540
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004541static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004542acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4543 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004544{
4545 struct http_txn *txn = l7;
4546 char *ptr;
4547 int len;
4548
Willy Tarreaub6866442008-07-14 23:54:42 +02004549 if (!txn)
4550 return 0;
4551
Willy Tarreauc11416f2007-06-17 16:58:38 +02004552 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4553 return 0;
4554
Willy Tarreau8797c062007-05-07 00:55:35 +02004555 len = txn->rsp.sl.st.v_l;
4556 ptr = txn->rsp.sol;
4557
4558 while ((len-- > 0) && (*ptr++ != '/'));
4559 if (len <= 0)
4560 return 0;
4561
4562 test->ptr = ptr;
4563 test->len = len;
4564
4565 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4566 return 1;
4567}
4568
4569/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004570static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004571acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4572 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004573{
4574 struct http_txn *txn = l7;
4575 char *ptr;
4576 int len;
4577
Willy Tarreaub6866442008-07-14 23:54:42 +02004578 if (!txn)
4579 return 0;
4580
Willy Tarreauc11416f2007-06-17 16:58:38 +02004581 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4582 return 0;
4583
Willy Tarreau8797c062007-05-07 00:55:35 +02004584 len = txn->rsp.sl.st.c_l;
4585 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4586
4587 test->i = __strl2ui(ptr, len);
4588 test->flags = ACL_TEST_F_VOL_1ST;
4589 return 1;
4590}
4591
4592/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004593static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004594acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4595 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004596{
4597 struct http_txn *txn = l7;
4598
Willy Tarreaub6866442008-07-14 23:54:42 +02004599 if (!txn)
4600 return 0;
4601
Willy Tarreauc11416f2007-06-17 16:58:38 +02004602 if (txn->req.msg_state != HTTP_MSG_BODY)
4603 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004604
Willy Tarreauc11416f2007-06-17 16:58:38 +02004605 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4606 /* ensure the indexes are not affected */
4607 return 0;
4608
Willy Tarreau8797c062007-05-07 00:55:35 +02004609 test->len = txn->req.sl.rq.u_l;
4610 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4611
Willy Tarreauf3d25982007-05-08 22:45:09 +02004612 /* we do not need to set READ_ONLY because the data is in a buffer */
4613 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004614 return 1;
4615}
4616
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004617static int
4618acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4619 struct acl_expr *expr, struct acl_test *test)
4620{
4621 struct http_txn *txn = l7;
4622
Willy Tarreaub6866442008-07-14 23:54:42 +02004623 if (!txn)
4624 return 0;
4625
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004626 if (txn->req.msg_state != HTTP_MSG_BODY)
4627 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004628
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004629 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4630 /* ensure the indexes are not affected */
4631 return 0;
4632
4633 /* Parse HTTP request */
4634 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4635 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4636 test->i = AF_INET;
4637
4638 /*
4639 * If we are parsing url in frontend space, we prepare backend stage
4640 * to not parse again the same url ! optimization lazyness...
4641 */
4642 if (px->options & PR_O_HTTP_PROXY)
4643 l4->flags |= SN_ADDR_SET;
4644
4645 test->flags = ACL_TEST_F_READ_ONLY;
4646 return 1;
4647}
4648
4649static int
4650acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4651 struct acl_expr *expr, struct acl_test *test)
4652{
4653 struct http_txn *txn = l7;
4654
Willy Tarreaub6866442008-07-14 23:54:42 +02004655 if (!txn)
4656 return 0;
4657
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004658 if (txn->req.msg_state != HTTP_MSG_BODY)
4659 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004660
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004661 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4662 /* ensure the indexes are not affected */
4663 return 0;
4664
4665 /* Same optimization as url_ip */
4666 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4667 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4668
4669 if (px->options & PR_O_HTTP_PROXY)
4670 l4->flags |= SN_ADDR_SET;
4671
4672 test->flags = ACL_TEST_F_READ_ONLY;
4673 return 1;
4674}
4675
Willy Tarreauc11416f2007-06-17 16:58:38 +02004676/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4677 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4678 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004679static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004680acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004681 struct acl_expr *expr, struct acl_test *test)
4682{
4683 struct http_txn *txn = l7;
4684 struct hdr_idx *idx = &txn->hdr_idx;
4685 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004686
Willy Tarreaub6866442008-07-14 23:54:42 +02004687 if (!txn)
4688 return 0;
4689
Willy Tarreau33a7e692007-06-10 19:45:56 +02004690 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4691 /* search for header from the beginning */
4692 ctx->idx = 0;
4693
Willy Tarreau33a7e692007-06-10 19:45:56 +02004694 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4695 test->flags |= ACL_TEST_F_FETCH_MORE;
4696 test->flags |= ACL_TEST_F_VOL_HDR;
4697 test->len = ctx->vlen;
4698 test->ptr = (char *)ctx->line + ctx->val;
4699 return 1;
4700 }
4701
4702 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4703 test->flags |= ACL_TEST_F_VOL_HDR;
4704 return 0;
4705}
4706
Willy Tarreau33a7e692007-06-10 19:45:56 +02004707static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004708acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4709 struct acl_expr *expr, struct acl_test *test)
4710{
4711 struct http_txn *txn = l7;
4712
Willy Tarreaub6866442008-07-14 23:54:42 +02004713 if (!txn)
4714 return 0;
4715
Willy Tarreauc11416f2007-06-17 16:58:38 +02004716 if (txn->req.msg_state != HTTP_MSG_BODY)
4717 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004718
Willy Tarreauc11416f2007-06-17 16:58:38 +02004719 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4720 /* ensure the indexes are not affected */
4721 return 0;
4722
4723 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4724}
4725
4726static int
4727acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4728 struct acl_expr *expr, struct acl_test *test)
4729{
4730 struct http_txn *txn = l7;
4731
Willy Tarreaub6866442008-07-14 23:54:42 +02004732 if (!txn)
4733 return 0;
4734
Willy Tarreauc11416f2007-06-17 16:58:38 +02004735 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4736 return 0;
4737
4738 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4739}
4740
4741/* 6. Check on HTTP header count. The number of occurrences is returned.
4742 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4743 */
4744static int
4745acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004746 struct acl_expr *expr, struct acl_test *test)
4747{
4748 struct http_txn *txn = l7;
4749 struct hdr_idx *idx = &txn->hdr_idx;
4750 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004751 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004752
Willy Tarreaub6866442008-07-14 23:54:42 +02004753 if (!txn)
4754 return 0;
4755
Willy Tarreau33a7e692007-06-10 19:45:56 +02004756 ctx.idx = 0;
4757 cnt = 0;
4758 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4759 cnt++;
4760
4761 test->i = cnt;
4762 test->flags = ACL_TEST_F_VOL_HDR;
4763 return 1;
4764}
4765
Willy Tarreauc11416f2007-06-17 16:58:38 +02004766static int
4767acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4768 struct acl_expr *expr, struct acl_test *test)
4769{
4770 struct http_txn *txn = l7;
4771
Willy Tarreaub6866442008-07-14 23:54:42 +02004772 if (!txn)
4773 return 0;
4774
Willy Tarreauc11416f2007-06-17 16:58:38 +02004775 if (txn->req.msg_state != HTTP_MSG_BODY)
4776 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004777
Willy Tarreauc11416f2007-06-17 16:58:38 +02004778 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4779 /* ensure the indexes are not affected */
4780 return 0;
4781
4782 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4783}
4784
4785static int
4786acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4787 struct acl_expr *expr, struct acl_test *test)
4788{
4789 struct http_txn *txn = l7;
4790
Willy Tarreaub6866442008-07-14 23:54:42 +02004791 if (!txn)
4792 return 0;
4793
Willy Tarreauc11416f2007-06-17 16:58:38 +02004794 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4795 return 0;
4796
4797 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4798}
4799
Willy Tarreau33a7e692007-06-10 19:45:56 +02004800/* 7. Check on HTTP header's integer value. The integer value is returned.
4801 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004802 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004803 */
4804static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004805acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004806 struct acl_expr *expr, struct acl_test *test)
4807{
4808 struct http_txn *txn = l7;
4809 struct hdr_idx *idx = &txn->hdr_idx;
4810 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004811
Willy Tarreaub6866442008-07-14 23:54:42 +02004812 if (!txn)
4813 return 0;
4814
Willy Tarreau33a7e692007-06-10 19:45:56 +02004815 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4816 /* search for header from the beginning */
4817 ctx->idx = 0;
4818
Willy Tarreau33a7e692007-06-10 19:45:56 +02004819 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4820 test->flags |= ACL_TEST_F_FETCH_MORE;
4821 test->flags |= ACL_TEST_F_VOL_HDR;
4822 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4823 return 1;
4824 }
4825
4826 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4827 test->flags |= ACL_TEST_F_VOL_HDR;
4828 return 0;
4829}
4830
Willy Tarreauc11416f2007-06-17 16:58:38 +02004831static int
4832acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4833 struct acl_expr *expr, struct acl_test *test)
4834{
4835 struct http_txn *txn = l7;
4836
Willy Tarreaub6866442008-07-14 23:54:42 +02004837 if (!txn)
4838 return 0;
4839
Willy Tarreauc11416f2007-06-17 16:58:38 +02004840 if (txn->req.msg_state != HTTP_MSG_BODY)
4841 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004842
Willy Tarreauc11416f2007-06-17 16:58:38 +02004843 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4844 /* ensure the indexes are not affected */
4845 return 0;
4846
4847 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4848}
4849
4850static int
4851acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4852 struct acl_expr *expr, struct acl_test *test)
4853{
4854 struct http_txn *txn = l7;
4855
Willy Tarreaub6866442008-07-14 23:54:42 +02004856 if (!txn)
4857 return 0;
4858
Willy Tarreauc11416f2007-06-17 16:58:38 +02004859 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4860 return 0;
4861
4862 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4863}
4864
Willy Tarreau737b0c12007-06-10 21:28:46 +02004865/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4866 * the first '/' after the possible hostname, and ends before the possible '?'.
4867 */
4868static int
4869acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4870 struct acl_expr *expr, struct acl_test *test)
4871{
4872 struct http_txn *txn = l7;
4873 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004874
Willy Tarreaub6866442008-07-14 23:54:42 +02004875 if (!txn)
4876 return 0;
4877
Willy Tarreauc11416f2007-06-17 16:58:38 +02004878 if (txn->req.msg_state != HTTP_MSG_BODY)
4879 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004880
Willy Tarreauc11416f2007-06-17 16:58:38 +02004881 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4882 /* ensure the indexes are not affected */
4883 return 0;
4884
Willy Tarreau21d2af32008-02-14 20:25:24 +01004885 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4886 ptr = http_get_path(txn);
4887 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004888 return 0;
4889
4890 /* OK, we got the '/' ! */
4891 test->ptr = ptr;
4892
4893 while (ptr < end && *ptr != '?')
4894 ptr++;
4895
4896 test->len = ptr - test->ptr;
4897
4898 /* we do not need to set READ_ONLY because the data is in a buffer */
4899 test->flags = ACL_TEST_F_VOL_1ST;
4900 return 1;
4901}
4902
4903
Willy Tarreau8797c062007-05-07 00:55:35 +02004904
4905/************************************************************************/
4906/* All supported keywords must be declared here. */
4907/************************************************************************/
4908
4909/* Note: must not be declared <const> as its list will be overwritten */
4910static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004911 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4912 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4913 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4914 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004915
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004916 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4917 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4918 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4919 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4920 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4921 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4922 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4923 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4924 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004925
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004926 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4927 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4928 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4929 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4930 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4931 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4932 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4933 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4934 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4935 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004936
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004937 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4938 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4939 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4940 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4941 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4942 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4943 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4944 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4945 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004946
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004947 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4948 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4949 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4950 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4951 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4952 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4953 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004954
Willy Tarreauf3d25982007-05-08 22:45:09 +02004955 { NULL, NULL, NULL, NULL },
4956
4957#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02004958 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
4959 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
4960 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
4961 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
4962 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
4963 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
4964 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
4965
Willy Tarreau8797c062007-05-07 00:55:35 +02004966 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
4967 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
4968 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
4969 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
4970 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
4971 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
4972 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
4973 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
4974
4975 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
4976 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
4977 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
4978 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
4979 { NULL, NULL, NULL, NULL },
4980#endif
4981}};
4982
4983
4984__attribute__((constructor))
4985static void __http_protocol_init(void)
4986{
4987 acl_register_keywords(&acl_kws);
4988}
4989
4990
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004992 * Local variables:
4993 * c-indent-level: 8
4994 * c-basic-offset: 8
4995 * End:
4996 */