blob: be31c488017f849daaf36450c8f027c62b34b9e5 [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 Tarreau2492d5b2009-07-11 00:06:00 +02001501/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1502 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1503 * nothing is done and 1 is returned.
1504 */
1505static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1506{
1507 int delta;
1508 char *cur_end;
1509
1510 if (msg->sl.rq.v_l != 0)
1511 return 1;
1512
1513 msg->sol = req->data + msg->som;
1514 cur_end = msg->sol + msg->sl.rq.l;
1515 delta = 0;
1516
1517 if (msg->sl.rq.u_l == 0) {
1518 /* if no URI was set, add "/" */
1519 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1520 cur_end += delta;
1521 msg->eoh += delta;
1522 }
1523 /* add HTTP version */
1524 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1525 msg->eoh += delta;
1526 cur_end += delta;
1527 cur_end = (char *)http_parse_reqline(msg, req->data,
1528 HTTP_MSG_RQMETH,
1529 msg->sol, cur_end + 1,
1530 NULL, NULL);
1531 if (unlikely(!cur_end))
1532 return 0;
1533
1534 /* we have a full HTTP/1.0 request now and we know that
1535 * we have either a CR or an LF at <ptr>.
1536 */
1537 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1538 return 1;
1539}
1540
Willy Tarreaud787e662009-07-07 10:14:51 +02001541/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1542 * processing can continue on next analysers, or zero if it either needs more
1543 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1544 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1545 * when it has nothing left to do, and may remove any analyser when it wants to
1546 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001548int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549{
Willy Tarreau59234e92008-11-30 23:51:27 +01001550 /*
1551 * We will parse the partial (or complete) lines.
1552 * We will check the request syntax, and also join multi-line
1553 * headers. An index of all the lines will be elaborated while
1554 * parsing.
1555 *
1556 * For the parsing, we use a 28 states FSM.
1557 *
1558 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001559 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001560 * req->data + req->eoh = end of processed headers / start of current one
1561 * req->data + req->eol = end of current header or line (LF or CRLF)
1562 * req->lr = first non-visited byte
1563 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001564 *
1565 * At end of parsing, we may perform a capture of the error (if any), and
1566 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1567 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1568 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001569 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau59234e92008-11-30 23:51:27 +01001571 int cur_idx;
1572 struct http_txn *txn = &s->txn;
1573 struct http_msg *msg = &txn->req;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau6bf17362009-02-24 10:48:35 +01001575 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1576 now_ms, __FUNCTION__,
1577 s,
1578 req,
1579 req->rex, req->wex,
1580 req->flags,
1581 req->l,
1582 req->analysers);
1583
Willy Tarreau59234e92008-11-30 23:51:27 +01001584 if (likely(req->lr < req->r))
1585 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 /* 1: we might have to print this header in debug mode */
1588 if (unlikely((global.mode & MODE_DEBUG) &&
1589 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1590 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1591 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreau59234e92008-11-30 23:51:27 +01001593 sol = req->data + msg->som;
1594 eol = sol + msg->sl.rq.l;
1595 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001596
Willy Tarreau59234e92008-11-30 23:51:27 +01001597 sol += hdr_idx_first_pos(&txn->hdr_idx);
1598 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau59234e92008-11-30 23:51:27 +01001600 while (cur_idx) {
1601 eol = sol + txn->hdr_idx.v[cur_idx].len;
1602 debug_hdr("clihdr", s, sol, eol);
1603 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1604 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001606 }
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau59234e92008-11-30 23:51:27 +01001609 /*
1610 * Now we quickly check if we have found a full valid request.
1611 * If not so, we check the FD and buffer states before leaving.
1612 * A full request is indicated by the fact that we have seen
1613 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1614 * requests are checked first.
1615 *
1616 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001617
Willy Tarreau59234e92008-11-30 23:51:27 +01001618 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001619 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001620 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001622 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1623 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau59234e92008-11-30 23:51:27 +01001625 /* 1: Since we are in header mode, if there's no space
1626 * left for headers, we won't be able to free more
1627 * later, so the session will never terminate. We
1628 * must terminate it now.
1629 */
1630 if (unlikely(req->flags & BF_FULL)) {
1631 /* FIXME: check if URI is set and return Status
1632 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 goto return_bad_req;
1635 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau59234e92008-11-30 23:51:27 +01001637 /* 2: have we encountered a read error ? */
1638 else if (req->flags & BF_READ_ERROR) {
1639 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001640 if (msg->err_pos >= 0)
1641 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001642 msg->msg_state = HTTP_MSG_ERROR;
1643 req->analysers = 0;
1644 s->fe->failed_req++;
1645 if (!(s->flags & SN_ERR_MASK))
1646 s->flags |= SN_ERR_CLICL;
1647 if (!(s->flags & SN_FINST_MASK))
1648 s->flags |= SN_FINST_R;
1649 return 0;
1650 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001651
Willy Tarreau59234e92008-11-30 23:51:27 +01001652 /* 3: has the read timeout expired ? */
1653 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1654 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001655 if (msg->err_pos >= 0)
1656 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 txn->status = 408;
1658 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1659 msg->msg_state = HTTP_MSG_ERROR;
1660 req->analysers = 0;
1661 s->fe->failed_req++;
1662 if (!(s->flags & SN_ERR_MASK))
1663 s->flags |= SN_ERR_CLITO;
1664 if (!(s->flags & SN_FINST_MASK))
1665 s->flags |= SN_FINST_R;
1666 return 0;
1667 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001668
Willy Tarreau59234e92008-11-30 23:51:27 +01001669 /* 4: have we encountered a close ? */
1670 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001671 if (msg->err_pos >= 0)
1672 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001673 txn->status = 400;
1674 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1675 msg->msg_state = HTTP_MSG_ERROR;
1676 req->analysers = 0;
1677 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 if (!(s->flags & SN_ERR_MASK))
1680 s->flags |= SN_ERR_CLICL;
1681 if (!(s->flags & SN_FINST_MASK))
1682 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001683 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001684 }
1685
Willy Tarreau59234e92008-11-30 23:51:27 +01001686 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001687 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1688
Willy Tarreau59234e92008-11-30 23:51:27 +01001689 /* just set the request timeout once at the beginning of the request */
1690 if (!tick_isset(req->analyse_exp))
1691 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 /* we're not ready yet */
1694 return 0;
1695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696
Willy Tarreaud787e662009-07-07 10:14:51 +02001697 /* OK now we have a complete HTTP request with indexed headers. Let's
1698 * complete the request parsing by setting a few fields we will need
1699 * later.
1700 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001701
Willy Tarreaud787e662009-07-07 10:14:51 +02001702 /* Maybe we found in invalid header name while we were configured not
1703 * to block on that, so we have to capture it now.
1704 */
1705 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001706 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1707
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 /* ensure we keep this pointer to the beginning of the message */
1709 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 /*
1712 * 1: identify the method
1713 */
1714 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1715
1716 /* we can make use of server redirect on GET and HEAD */
1717 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1718 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001719
Willy Tarreau59234e92008-11-30 23:51:27 +01001720 /*
1721 * 2: check if the URI matches the monitor_uri.
1722 * We have to do this for every request which gets in, because
1723 * the monitor-uri is defined by the frontend.
1724 */
1725 if (unlikely((s->fe->monitor_uri_len != 0) &&
1726 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1727 !memcmp(&req->data[msg->sl.rq.u],
1728 s->fe->monitor_uri,
1729 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001730 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001731 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001732 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001733 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001734
Willy Tarreau59234e92008-11-30 23:51:27 +01001735 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001736
Willy Tarreau59234e92008-11-30 23:51:27 +01001737 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001738 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1739 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001740
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 ret = acl_pass(ret);
1742 if (cond->pol == ACL_COND_UNLESS)
1743 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001744
Willy Tarreau59234e92008-11-30 23:51:27 +01001745 if (ret) {
1746 /* we fail this request, let's return 503 service unavail */
1747 txn->status = 503;
1748 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1749 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001750 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001752
Willy Tarreau59234e92008-11-30 23:51:27 +01001753 /* nothing to fail, let's reply normaly */
1754 txn->status = 200;
1755 stream_int_retnclose(req->prod, &http_200_chunk);
1756 goto return_prx_cond;
1757 }
1758
1759 /*
1760 * 3: Maybe we have to copy the original REQURI for the logs ?
1761 * Note: we cannot log anymore if the request has been
1762 * classified as invalid.
1763 */
1764 if (unlikely(s->logs.logwait & LW_REQ)) {
1765 /* we have a complete HTTP request that we must log */
1766 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1767 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768
Willy Tarreau59234e92008-11-30 23:51:27 +01001769 if (urilen >= REQURI_LEN)
1770 urilen = REQURI_LEN - 1;
1771 memcpy(txn->uri, &req->data[msg->som], urilen);
1772 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 if (!(s->logs.logwait &= ~LW_REQ))
1775 s->do_log(s);
1776 } else {
1777 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001779 }
Willy Tarreau06619262006-12-17 08:37:22 +01001780
Willy Tarreau59234e92008-11-30 23:51:27 +01001781 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001782 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
1783 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
Willy Tarreau59234e92008-11-30 23:51:27 +01001785 /* 5: we may need to capture headers */
1786 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1787 capture_headers(req->data + msg->som, &txn->hdr_idx,
1788 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001789
Willy Tarreaud787e662009-07-07 10:14:51 +02001790 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001791 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001792 req->analyse_exp = TICK_ETERNITY;
1793 return 1;
1794
1795 return_bad_req:
1796 /* We centralize bad requests processing here */
1797 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1798 /* we detected a parsing error. We want to archive this request
1799 * in the dedicated proxy area for later troubleshooting.
1800 */
1801 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1802 }
1803
1804 txn->req.msg_state = HTTP_MSG_ERROR;
1805 txn->status = 400;
1806 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1807 s->fe->failed_req++;
1808
1809 return_prx_cond:
1810 if (!(s->flags & SN_ERR_MASK))
1811 s->flags |= SN_ERR_PRXCOND;
1812 if (!(s->flags & SN_FINST_MASK))
1813 s->flags |= SN_FINST_R;
1814
1815 req->analysers = 0;
1816 req->analyse_exp = TICK_ETERNITY;
1817 return 0;
1818}
1819
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001820/* This stream analyser runs all HTTP request processing which is common to
1821 * frontends and backends, which means blocking ACLs, filters, connection-close,
1822 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02001823 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001824 * either needs more data or wants to immediately abort the request (eg: deny,
1825 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02001826 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001827int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02001828{
Willy Tarreaud787e662009-07-07 10:14:51 +02001829 struct http_txn *txn = &s->txn;
1830 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001831 struct acl_cond *cond;
1832 struct redirect_rule *rule;
1833 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02001834
Willy Tarreau3a816292009-07-07 10:55:49 +02001835 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001836 req->analyse_exp = TICK_ETERNITY;
1837
1838 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1839 now_ms, __FUNCTION__,
1840 s,
1841 req,
1842 req->rex, req->wex,
1843 req->flags,
1844 req->l,
1845 req->analysers);
1846
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001847 /* first check whether we have some ACLs set to block this request */
1848 list_for_each_entry(cond, &px->block_cond, list) {
1849 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001850
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001851 ret = acl_pass(ret);
1852 if (cond->pol == ACL_COND_UNLESS)
1853 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001854
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001855 if (ret) {
1856 txn->status = 403;
1857 /* let's log the request time */
1858 s->logs.tv_request = now;
1859 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1860 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001862 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001864 /* try headers filters */
1865 if (px->req_exp != NULL) {
1866 if (apply_filters_to_request(s, req, px->req_exp) < 0)
1867 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001868
Willy Tarreau59234e92008-11-30 23:51:27 +01001869 /* has the request been denied ? */
1870 if (txn->flags & TX_CLDENY) {
1871 /* no need to go further */
1872 txn->status = 403;
1873 /* let's log the request time */
1874 s->logs.tv_request = now;
1875 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1876 goto return_prx_cond;
1877 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001878 }
Willy Tarreau06619262006-12-17 08:37:22 +01001879
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001880 /* We might have to check for "Connection:" */
1881 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1882 !(s->flags & SN_CONN_CLOSED)) {
1883 char *cur_ptr, *cur_end, *cur_next;
1884 int old_idx, delta, val;
1885 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001887 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1888 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001890 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1891 cur_hdr = &txn->hdr_idx.v[cur_idx];
1892 cur_ptr = cur_next;
1893 cur_end = cur_ptr + cur_hdr->len;
1894 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01001895
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001896 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1897 if (val) {
1898 /* 3 possibilities :
1899 * - we have already set Connection: close,
1900 * so we remove this line.
1901 * - we have not yet set Connection: close,
1902 * but this line indicates close. We leave
1903 * it untouched and set the flag.
1904 * - we have not yet set Connection: close,
1905 * and this line indicates non-close. We
1906 * replace it.
1907 */
1908 if (s->flags & SN_CONN_CLOSED) {
1909 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
1910 txn->req.eoh += delta;
1911 cur_next += delta;
1912 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1913 txn->hdr_idx.used--;
1914 cur_hdr->len = 0;
1915 } else {
1916 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1917 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1918 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01001919 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001920 cur_hdr->len += delta;
1921 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001923 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 }
Willy Tarreau06619262006-12-17 08:37:22 +01001925 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001926 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01001927 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001928 }
1929 /* add request headers from the rule sets in the same order */
1930 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
1931 if (unlikely(http_header_add_tail(req,
1932 &txn->req,
1933 &txn->hdr_idx,
1934 px->req_add[cur_idx])) < 0)
1935 goto return_bad_req;
1936 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001937
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001938 /* check if stats URI was requested, and if an auth is needed */
1939 if (px->uri_auth != NULL &&
1940 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
1941 /* we have to check the URI and auth for this request.
1942 * FIXME!!! that one is rather dangerous, we want to
1943 * make it follow standard rules (eg: clear req->analysers).
1944 */
1945 if (stats_check_uri_auth(s, px)) {
1946 req->analysers = 0;
1947 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001948 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001949 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001950
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001951 /* check whether we have some ACLs set to redirect this request */
1952 list_for_each_entry(rule, &px->redirect_rules, list) {
1953 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001954
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001955 ret = acl_pass(ret);
1956 if (rule->cond->pol == ACL_COND_UNLESS)
1957 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001959 if (ret) {
1960 struct chunk rdr = { trash, 0 };
1961 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001962
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001963 /* build redirect message */
1964 switch(rule->code) {
1965 case 303:
1966 rdr.len = strlen(HTTP_303);
1967 msg_fmt = HTTP_303;
1968 break;
1969 case 301:
1970 rdr.len = strlen(HTTP_301);
1971 msg_fmt = HTTP_301;
1972 break;
1973 case 302:
1974 default:
1975 rdr.len = strlen(HTTP_302);
1976 msg_fmt = HTTP_302;
1977 break;
1978 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02001979
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001980 if (unlikely(rdr.len > sizeof(trash)))
1981 goto return_bad_req;
1982 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001983
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001984 switch(rule->type) {
1985 case REDIRECT_TYPE_PREFIX: {
1986 const char *path;
1987 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001989 path = http_get_path(txn);
1990 /* build message using path */
1991 if (path) {
1992 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1993 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1994 int qs = 0;
1995 while (qs < pathlen) {
1996 if (path[qs] == '?') {
1997 pathlen = qs;
1998 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001999 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002000 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002001 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002002 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002003 } else {
2004 path = "/";
2005 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002006 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002007
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002008 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2009 goto return_bad_req;
2010
2011 /* add prefix. Note that if prefix == "/", we don't want to
2012 * add anything, otherwise it makes it hard for the user to
2013 * configure a self-redirection.
2014 */
2015 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002016 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2017 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002018 }
2019
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002020 /* add path */
2021 memcpy(rdr.str + rdr.len, path, pathlen);
2022 rdr.len += pathlen;
2023 break;
2024 }
2025 case REDIRECT_TYPE_LOCATION:
2026 default:
2027 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2028 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002030 /* add location */
2031 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2032 rdr.len += rule->rdr_len;
2033 break;
2034 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002036 if (rule->cookie_len) {
2037 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2038 rdr.len += 14;
2039 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2040 rdr.len += rule->cookie_len;
2041 memcpy(rdr.str + rdr.len, "\r\n", 2);
2042 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002043 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002045 /* add end of headers */
2046 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2047 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002049 txn->status = rule->code;
2050 /* let's log the request time */
2051 s->logs.tv_request = now;
2052 stream_int_retnclose(req->prod, &rdr);
2053 goto return_prx_cond;
2054 }
2055 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002057 /* that's OK for us now, let's move on to next analysers */
2058 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002059
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002060 return_bad_req:
2061 /* We centralize bad requests processing here */
2062 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2063 /* we detected a parsing error. We want to archive this request
2064 * in the dedicated proxy area for later troubleshooting.
2065 */
2066 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2067 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002069 txn->req.msg_state = HTTP_MSG_ERROR;
2070 txn->status = 400;
2071 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2072 s->fe->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002073
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002074 return_prx_cond:
2075 if (!(s->flags & SN_ERR_MASK))
2076 s->flags |= SN_ERR_PRXCOND;
2077 if (!(s->flags & SN_FINST_MASK))
2078 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002080 req->analysers = 0;
2081 req->analyse_exp = TICK_ETERNITY;
2082 return 0;
2083}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002084
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002085/* This function performs all the processing enabled for the current request.
2086 * It returns 1 if the processing can continue on next analysers, or zero if it
2087 * needs more data, encounters an error, or wants to immediately abort the
2088 * request. It relies on buffers flags, and updates s->req->analysers.
2089 */
2090int http_process_request(struct session *s, struct buffer *req, int an_bit)
2091{
2092 struct http_txn *txn = &s->txn;
2093 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002094
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002095 req->analysers &= ~an_bit;
2096 req->analyse_exp = TICK_ETERNITY;
2097
2098 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2099 now_ms, __FUNCTION__,
2100 s,
2101 req,
2102 req->rex, req->wex,
2103 req->flags,
2104 req->l,
2105 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002106
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 /*
2108 * Right now, we know that we have processed the entire headers
2109 * and that unwanted requests have been filtered out. We can do
2110 * whatever we want with the remaining request. Also, now we
2111 * may have separate values for ->fe, ->be.
2112 */
Willy Tarreau06619262006-12-17 08:37:22 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 /*
2115 * If HTTP PROXY is set we simply get remote server address
2116 * parsing incoming request.
2117 */
2118 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2119 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002121
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 /*
2123 * 7: the appsession cookie was looked up very early in 1.2,
2124 * so let's do the same now.
2125 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002126
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 /* It needs to look into the URI */
2128 if (s->be->appsession_name) {
2129 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2130 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002131
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 /*
2133 * 8: Now we can work with the cookies.
2134 * Note that doing so might move headers in the request, but
2135 * the fields will stay coherent and the URI will not move.
2136 * This should only be performed in the backend.
2137 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002138 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2140 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002141
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 /*
2143 * 9: add X-Forwarded-For if either the frontend or the backend
2144 * asks for it.
2145 */
2146 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2147 if (s->cli_addr.ss_family == AF_INET) {
2148 /* Add an X-Forwarded-For header unless the source IP is
2149 * in the 'except' network range.
2150 */
2151 if ((!s->fe->except_mask.s_addr ||
2152 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2153 != s->fe->except_net.s_addr) &&
2154 (!s->be->except_mask.s_addr ||
2155 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2156 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002157 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 unsigned char *pn;
2159 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002160
2161 /* Note: we rely on the backend to get the header name to be used for
2162 * x-forwarded-for, because the header is really meant for the backends.
2163 * However, if the backend did not specify any option, we have to rely
2164 * on the frontend's header name.
2165 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 if (s->be->fwdfor_hdr_len) {
2167 len = s->be->fwdfor_hdr_len;
2168 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002169 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 len = s->fe->fwdfor_hdr_len;
2171 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2172 }
2173 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002174
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002175 if (unlikely(http_header_add_tail2(req, &txn->req,
2176 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002177 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002178 }
2179 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 else if (s->cli_addr.ss_family == AF_INET6) {
2181 /* FIXME: for the sake of completeness, we should also support
2182 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002183 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 int len;
2185 char pn[INET6_ADDRSTRLEN];
2186 inet_ntop(AF_INET6,
2187 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2188 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002189
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 /* Note: we rely on the backend to get the header name to be used for
2191 * x-forwarded-for, because the header is really meant for the backends.
2192 * However, if the backend did not specify any option, we have to rely
2193 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002194 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 if (s->be->fwdfor_hdr_len) {
2196 len = s->be->fwdfor_hdr_len;
2197 memcpy(trash, s->be->fwdfor_hdr_name, len);
2198 } else {
2199 len = s->fe->fwdfor_hdr_len;
2200 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002201 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002203
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 if (unlikely(http_header_add_tail2(req, &txn->req,
2205 &txn->hdr_idx, trash, len)) < 0)
2206 goto return_bad_req;
2207 }
2208 }
2209
2210 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002211 * 10: add X-Original-To if either the frontend or the backend
2212 * asks for it.
2213 */
2214 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2215
2216 /* FIXME: don't know if IPv6 can handle that case too. */
2217 if (s->cli_addr.ss_family == AF_INET) {
2218 /* Add an X-Original-To header unless the destination IP is
2219 * in the 'except' network range.
2220 */
2221 if (!(s->flags & SN_FRT_ADDR_SET))
2222 get_frt_addr(s);
2223
2224 if ((!s->fe->except_mask_to.s_addr ||
2225 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2226 != s->fe->except_to.s_addr) &&
2227 (!s->be->except_mask_to.s_addr ||
2228 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2229 != s->be->except_to.s_addr)) {
2230 int len;
2231 unsigned char *pn;
2232 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2233
2234 /* Note: we rely on the backend to get the header name to be used for
2235 * x-original-to, because the header is really meant for the backends.
2236 * However, if the backend did not specify any option, we have to rely
2237 * on the frontend's header name.
2238 */
2239 if (s->be->orgto_hdr_len) {
2240 len = s->be->orgto_hdr_len;
2241 memcpy(trash, s->be->orgto_hdr_name, len);
2242 } else {
2243 len = s->fe->orgto_hdr_len;
2244 memcpy(trash, s->fe->orgto_hdr_name, len);
2245 }
2246 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2247
2248 if (unlikely(http_header_add_tail2(req, &txn->req,
2249 &txn->hdr_idx, trash, len)) < 0)
2250 goto return_bad_req;
2251 }
2252 }
2253 }
2254
2255 /*
2256 * 11: add "Connection: close" if needed and not yet set.
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 * Note that we do not need to add it in case of HTTP/1.0.
2258 */
2259 if (!(s->flags & SN_CONN_CLOSED) &&
2260 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2261 if ((unlikely(msg->sl.rq.v_l != 8) ||
2262 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2263 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2264 "Connection: close", 17)) < 0)
2265 goto return_bad_req;
2266 s->flags |= SN_CONN_CLOSED;
2267 }
2268 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2269 * If not assigned, perhaps we are balancing on url_param, but this is a
2270 * POST; and the parameters are in the body, maybe scan there to find our server.
2271 * (unless headers overflowed the buffer?)
2272 */
2273 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2274 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2275 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2276 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2277 /* are there enough bytes here? total == l || r || rlim ?
2278 * len is unsigned, but eoh is int,
2279 * how many bytes of body have we received?
2280 * eoh is the first empty line of the header
2281 */
2282 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2283 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2284
2285 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2286 * We can't assume responsibility for the server's decision,
2287 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2288 * We also can't change our mind later, about which server to choose, so round robin.
2289 */
2290 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2291 struct hdr_ctx ctx;
2292 ctx.idx = 0;
2293 /* Expect is allowed in 1.1, look for it */
2294 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2295 if (ctx.idx != 0 &&
2296 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2297 /* We can't reliablly stall and wait for data, because of
2298 * .NET clients that don't conform to rfc2616; so, no need for
2299 * the next block to check length expectations.
2300 * We could send 100 status back to the client, but then we need to
2301 * re-write headers, and send the message. And this isn't the right
2302 * place for that action.
2303 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002304 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 goto end_check_maybe_wait_for_body;
2306 }
2307
2308 if (likely(len > s->be->url_param_post_limit)) {
2309 /* nothing to do, we got enough */
2310 } else {
2311 /* limit implies we are supposed to need this many bytes
2312 * to find the parameter. Let's see how many bytes we can wait for.
2313 */
2314 long long hint = len;
2315 struct hdr_ctx ctx;
2316 ctx.idx = 0;
2317 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2318 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2319 buffer_write_dis(req);
2320 req->analysers |= AN_REQ_HTTP_BODY;
2321 }
2322 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002323 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2325 /* now if we have a length, we'll take the hint */
2326 if (ctx.idx) {
2327 /* We have Content-Length */
2328 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2329 hint = 0; /* parse failure, untrusted client */
2330 else {
2331 if (hint > 0)
2332 msg->hdr_content_len = hint;
2333 else
2334 hint = 0; /* bad client, sent negative length */
2335 }
2336 }
2337 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2338 if (s->be->url_param_post_limit < hint)
2339 hint = s->be->url_param_post_limit;
2340 /* now do we really need to buffer more data? */
2341 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002342 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002343 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002344 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002346 }
2347 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 }
2349 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 /*************************************************************
2352 * OK, that's finished for the headers. We have done what we *
2353 * could. Let's switch to the DATA state. *
2354 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2357 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /* When a connection is tarpitted, we use the tarpit timeout,
2360 * which may be the same as the connect timeout if unspecified.
2361 * If unset, then set it to zero because we really want it to
2362 * eventually expire. We build the tarpit as an analyser.
2363 */
2364 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002365 buffer_erase(s->req);
2366 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002368 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 buffer_write_dis(req);
2370 req->analysers |= AN_REQ_HTTP_TARPIT;
2371 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2372 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002373 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 /* OK let's go on with the BODY now */
2377 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002378
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002380 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002381 /* we detected a parsing error. We want to archive this request
2382 * in the dedicated proxy area for later troubleshooting.
2383 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002384 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002385 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002386
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 txn->req.msg_state = HTTP_MSG_ERROR;
2388 txn->status = 400;
2389 req->analysers = 0;
2390 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2391 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 if (!(s->flags & SN_ERR_MASK))
2394 s->flags |= SN_ERR_PRXCOND;
2395 if (!(s->flags & SN_FINST_MASK))
2396 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002397 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002398}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002399
Willy Tarreau60b85b02008-11-30 23:28:40 +01002400/* This function is an analyser which processes the HTTP tarpit. It always
2401 * returns zero, at the beginning because it prevents any other processing
2402 * from occurring, and at the end because it terminates the request.
2403 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002404int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002405{
2406 struct http_txn *txn = &s->txn;
2407
2408 /* This connection is being tarpitted. The CLIENT side has
2409 * already set the connect expiration date to the right
2410 * timeout. We just have to check that the client is still
2411 * there and that the timeout has not expired.
2412 */
2413 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2414 !tick_is_expired(req->analyse_exp, now_ms))
2415 return 0;
2416
2417 /* We will set the queue timer to the time spent, just for
2418 * logging purposes. We fake a 500 server error, so that the
2419 * attacker will not suspect his connection has been tarpitted.
2420 * It will not cause trouble to the logs because we can exclude
2421 * the tarpitted connections by filtering on the 'PT' status flags.
2422 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002423 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2424
2425 txn->status = 500;
2426 if (req->flags != BF_READ_ERROR)
2427 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2428
2429 req->analysers = 0;
2430 req->analyse_exp = TICK_ETERNITY;
2431
2432 s->fe->failed_req++;
2433 if (!(s->flags & SN_ERR_MASK))
2434 s->flags |= SN_ERR_PRXCOND;
2435 if (!(s->flags & SN_FINST_MASK))
2436 s->flags |= SN_FINST_T;
2437 return 0;
2438}
2439
Willy Tarreaud34af782008-11-30 23:36:37 +01002440/* This function is an analyser which processes the HTTP request body. It looks
2441 * for parameters to be used for the load balancing algorithm (url_param). It
2442 * must only be called after the standard HTTP request processing has occurred,
2443 * because it expects the request to be parsed. It returns zero if it needs to
2444 * read more data, or 1 once it has completed its analysis.
2445 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002446int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002447{
2448 struct http_msg *msg = &s->txn.req;
2449 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2450 long long limit = s->be->url_param_post_limit;
2451 struct hdr_ctx ctx;
2452
2453 /* We have to parse the HTTP request body to find any required data.
2454 * "balance url_param check_post" should have been the only way to get
2455 * into this. We were brought here after HTTP header analysis, so all
2456 * related structures are ready.
2457 */
2458
2459 ctx.idx = 0;
2460
2461 /* now if we have a length, we'll take the hint */
2462 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2463 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2464 unsigned int chunk = 0;
2465 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2466 char c = msg->sol[body];
2467 if (ishex(c)) {
2468 unsigned int hex = toupper(c) - '0';
2469 if (hex > 9)
2470 hex -= 'A' - '9' - 1;
2471 chunk = (chunk << 4) | hex;
2472 } else
2473 break;
2474 body++;
2475 }
2476 if (body + 2 >= req->l) /* we want CRLF too */
2477 goto http_body_end; /* end of buffer? data missing! */
2478
2479 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2480 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2481
2482 body += 2; // skip CRLF
2483
2484 /* if we support more then one chunk here, we have to do it again when assigning server
2485 * 1. how much entity data do we have? new var
2486 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2487 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2488 */
2489
2490 if (chunk < limit)
2491 limit = chunk; /* only reading one chunk */
2492 } else {
2493 if (msg->hdr_content_len < limit)
2494 limit = msg->hdr_content_len;
2495 }
2496
2497 http_body_end:
2498 /* we leave once we know we have nothing left to do. This means that we have
2499 * enough bytes, or that we know we'll not get any more (buffer full, read
2500 * buffer closed).
2501 */
2502 if (req->l - body >= limit || /* enough bytes! */
2503 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2504 tick_is_expired(req->analyse_exp, now_ms)) {
2505 /* The situation will not evolve, so let's give up on the analysis. */
2506 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002507 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002508 req->analyse_exp = TICK_ETERNITY;
2509 return 1;
2510 }
2511 else {
2512 /* Not enough data. We'll re-use the http-request
2513 * timeout here. Ideally, we should set the timeout
2514 * relative to the accept() date. We just set the
2515 * request timeout once at the beginning of the
2516 * request.
2517 */
2518 buffer_write_dis(req);
2519 if (!tick_isset(req->analyse_exp))
2520 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2521 return 0;
2522 }
2523}
2524
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002525/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002526 * It normally returns zero, but may return 1 if it absolutely needs to be
2527 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002528 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002529 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002530 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002531int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002532{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002533 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002534 struct buffer *req = t->req;
2535 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002536
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002537 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 +02002538 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002539 t,
2540 rep,
2541 rep->rex, rep->wex,
2542 rep->flags,
2543 rep->l,
2544 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002545
Willy Tarreau2df28e82008-08-17 15:20:19 +02002546 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002547 /*
2548 * Now parse the partial (or complete) lines.
2549 * We will check the response syntax, and also join multi-line
2550 * headers. An index of all the lines will be elaborated while
2551 * parsing.
2552 *
2553 * For the parsing, we use a 28 states FSM.
2554 *
2555 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002556 * rep->data + rep->som = beginning of response
2557 * rep->data + rep->eoh = end of processed headers / start of current one
2558 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002559 * rep->lr = first non-visited byte
2560 * rep->r = end of data
2561 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002562
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002563 int cur_idx;
2564 struct http_msg *msg = &txn->rsp;
2565 struct proxy *cur_proxy;
2566
2567 if (likely(rep->lr < rep->r))
2568 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2569
2570 /* 1: we might have to print this header in debug mode */
2571 if (unlikely((global.mode & MODE_DEBUG) &&
2572 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2573 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2574 char *eol, *sol;
2575
2576 sol = rep->data + msg->som;
2577 eol = sol + msg->sl.rq.l;
2578 debug_hdr("srvrep", t, sol, eol);
2579
2580 sol += hdr_idx_first_pos(&txn->hdr_idx);
2581 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2582
2583 while (cur_idx) {
2584 eol = sol + txn->hdr_idx.v[cur_idx].len;
2585 debug_hdr("srvhdr", t, sol, eol);
2586 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2587 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002588 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002589 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002590
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002591 /*
2592 * Now we quickly check if we have found a full valid response.
2593 * If not so, we check the FD and buffer states before leaving.
2594 * A full response is indicated by the fact that we have seen
2595 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2596 * responses are checked first.
2597 *
2598 * Depending on whether the client is still there or not, we
2599 * may send an error response back or not. Note that normally
2600 * we should only check for HTTP status there, and check I/O
2601 * errors somewhere else.
2602 */
2603
2604 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002605 /* Invalid response */
2606 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002607 /* we detected a parsing error. We want to archive this response
2608 * in the dedicated proxy area for later troubleshooting.
2609 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002610 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002611 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2612 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2613
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002614 buffer_shutr_now(rep);
2615 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002616 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002617 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002618 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002619 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002620 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002621 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002622 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002623 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002624 if (!(t->flags & SN_FINST_MASK))
2625 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002626
Willy Tarreaudafde432008-08-17 01:00:46 +02002627 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002628 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002629 /* too large response does not fit in buffer. */
2630 else if (rep->flags & BF_FULL) {
2631 goto hdr_response_bad;
2632 }
2633 /* read error */
2634 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002635 if (msg->err_pos >= 0)
2636 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002637 buffer_shutr_now(rep);
2638 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002639 if (t->srv)
2640 t->srv->failed_resp++;
2641 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002642 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002643 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002644 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002645 if (!(t->flags & SN_ERR_MASK))
2646 t->flags |= SN_ERR_SRVCL;
2647 if (!(t->flags & SN_FINST_MASK))
2648 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002649 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002650 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002651 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002652 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002653 if (msg->err_pos >= 0)
2654 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002655 buffer_shutr_now(rep);
2656 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002657 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002658 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002659 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002660 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002661 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002662 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002663 if (!(t->flags & SN_ERR_MASK))
2664 t->flags |= SN_ERR_SRVTO;
2665 if (!(t->flags & SN_FINST_MASK))
2666 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002667 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002669 /* close from server */
2670 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002671 if (msg->err_pos >= 0)
2672 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002673 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002674 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002675 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002676 t->be->failed_resp++;
2677 rep->analysers = 0;
2678 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002679 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002680 if (!(t->flags & SN_ERR_MASK))
2681 t->flags |= SN_ERR_SRVCL;
2682 if (!(t->flags & SN_FINST_MASK))
2683 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002684 return 0;
2685 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002686 /* write error to client (we don't send any message then) */
2687 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002688 if (msg->err_pos >= 0)
2689 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002690 buffer_shutr_now(rep);
2691 t->be->failed_resp++;
2692 rep->analysers = 0;
2693 if (!(t->flags & SN_ERR_MASK))
2694 t->flags |= SN_ERR_CLICL;
2695 if (!(t->flags & SN_FINST_MASK))
2696 t->flags |= SN_FINST_H;
2697 return 0;
2698 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002699 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002700 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002701 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002702
Willy Tarreau21d2af32008-02-14 20:25:24 +01002703
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002704 /*****************************************************************
2705 * More interesting part now : we know that we have a complete *
2706 * response which at least looks like HTTP. We have an indicator *
2707 * of each header's length, so we can parse them quickly. *
2708 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002709
Willy Tarreau4076a152009-04-02 15:18:36 +02002710 if (msg->err_pos >= 0)
2711 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2712
Willy Tarreau2df28e82008-08-17 15:20:19 +02002713 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002714
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002715 /* ensure we keep this pointer to the beginning of the message */
2716 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002717
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002718 /*
2719 * 1: get the status code and check for cacheability.
2720 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002721
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002722 t->logs.logwait &= ~LW_RESP;
2723 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002724
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002725 switch (txn->status) {
2726 case 200:
2727 case 203:
2728 case 206:
2729 case 300:
2730 case 301:
2731 case 410:
2732 /* RFC2616 @13.4:
2733 * "A response received with a status code of
2734 * 200, 203, 206, 300, 301 or 410 MAY be stored
2735 * by a cache (...) unless a cache-control
2736 * directive prohibits caching."
2737 *
2738 * RFC2616 @9.5: POST method :
2739 * "Responses to this method are not cacheable,
2740 * unless the response includes appropriate
2741 * Cache-Control or Expires header fields."
2742 */
2743 if (likely(txn->meth != HTTP_METH_POST) &&
2744 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2745 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2746 break;
2747 default:
2748 break;
2749 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002750
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002751 /*
2752 * 2: we may need to capture headers
2753 */
2754 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2755 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2756 txn->rsp.cap, t->fe->rsp_cap);
2757
2758 /*
2759 * 3: we will have to evaluate the filters.
2760 * As opposed to version 1.2, now they will be evaluated in the
2761 * filters order and not in the header order. This means that
2762 * each filter has to be validated among all headers.
2763 *
2764 * Filters are tried with ->be first, then with ->fe if it is
2765 * different from ->be.
2766 */
2767
2768 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2769
2770 cur_proxy = t->be;
2771 while (1) {
2772 struct proxy *rule_set = cur_proxy;
2773
2774 /* try headers filters */
2775 if (rule_set->rsp_exp != NULL) {
2776 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2777 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01002778 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002779 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002780 cur_proxy->failed_resp++;
2781 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002782 buffer_shutr_now(rep);
2783 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002784 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002785 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002786 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002787 if (!(t->flags & SN_ERR_MASK))
2788 t->flags |= SN_ERR_PRXCOND;
2789 if (!(t->flags & SN_FINST_MASK))
2790 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002791 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002792 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002793 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002794
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002795 /* has the response been denied ? */
2796 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01002797 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002798 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002799 cur_proxy->denied_resp++;
2800 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002801 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002802
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002803 /* We might have to check for "Connection:" */
2804 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2805 !(t->flags & SN_CONN_CLOSED)) {
2806 char *cur_ptr, *cur_end, *cur_next;
2807 int cur_idx, old_idx, delta, val;
2808 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002809
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002810 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2811 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002812
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002813 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2814 cur_hdr = &txn->hdr_idx.v[cur_idx];
2815 cur_ptr = cur_next;
2816 cur_end = cur_ptr + cur_hdr->len;
2817 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002818
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002819 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2820 if (val) {
2821 /* 3 possibilities :
2822 * - we have already set Connection: close,
2823 * so we remove this line.
2824 * - we have not yet set Connection: close,
2825 * but this line indicates close. We leave
2826 * it untouched and set the flag.
2827 * - we have not yet set Connection: close,
2828 * and this line indicates non-close. We
2829 * replace it.
2830 */
2831 if (t->flags & SN_CONN_CLOSED) {
2832 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2833 txn->rsp.eoh += delta;
2834 cur_next += delta;
2835 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2836 txn->hdr_idx.used--;
2837 cur_hdr->len = 0;
2838 } else {
2839 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2840 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2841 "close", 5);
2842 cur_next += delta;
2843 cur_hdr->len += delta;
2844 txn->rsp.eoh += delta;
2845 }
2846 t->flags |= SN_CONN_CLOSED;
2847 }
2848 }
2849 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002850 }
2851 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002853 /* add response headers from the rule sets in the same order */
2854 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2855 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2856 rule_set->rsp_add[cur_idx])) < 0)
2857 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002858 }
2859
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002860 /* check whether we're already working on the frontend */
2861 if (cur_proxy == t->fe)
2862 break;
2863 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002864 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 /*
2867 * 4: check for server cookie.
2868 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002869 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002870 || (t->be->options & PR_O_CHK_CACHE))
2871 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872
Willy Tarreaubaaee002006-06-26 02:48:02 +02002873
Willy Tarreaua15645d2007-03-18 16:22:39 +01002874 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002875 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002876 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002877 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2878 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002879
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002880 /*
2881 * 6: add server cookie in the response if needed
2882 */
2883 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2884 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2885 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002887 /* the server is known, it's not the one the client requested, we have to
2888 * insert a set-cookie here, except if we want to insert only on POST
2889 * requests and this one isn't. Note that servers which don't have cookies
2890 * (eg: some backup servers) will return a full cookie removal request.
2891 */
2892 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2893 t->be->cookie_name,
2894 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002896 if (t->be->cookie_domain)
2897 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002899 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2900 trash, len)) < 0)
2901 goto return_bad_resp;
2902 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002904 /* Here, we will tell an eventual cache on the client side that we don't
2905 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2906 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2907 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2908 */
2909 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002910
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002911 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2912
2913 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2914 "Cache-control: private", 22)) < 0)
2915 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002916 }
2917 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002918
Willy Tarreaubaaee002006-06-26 02:48:02 +02002919
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002920 /*
2921 * 7: check if result will be cacheable with a cookie.
2922 * We'll block the response if security checks have caught
2923 * nasty things such as a cacheable cookie.
2924 */
2925 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2926 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2927 (t->be->options & PR_O_CHK_CACHE)) {
2928
2929 /* we're in presence of a cacheable response containing
2930 * a set-cookie header. We'll block it as requested by
2931 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002932 */
Willy Tarreau8365f932009-03-15 23:11:49 +01002933 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002934 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002936
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2938 t->be->id, t->srv?t->srv->id:"<dispatch>");
2939 send_log(t->be, LOG_ALERT,
2940 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2941 t->be->id, t->srv?t->srv->id:"<dispatch>");
2942 goto return_srv_prx_502;
2943 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002944
2945 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002946 * 8: add "Connection: close" if needed and not yet set.
2947 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002948 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002949 if (!(t->flags & SN_CONN_CLOSED) &&
2950 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2951 if ((unlikely(msg->sl.st.v_l != 8) ||
2952 unlikely(req->data[msg->som + 7] != '0')) &&
2953 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2954 "Connection: close", 17)) < 0)
2955 goto return_bad_resp;
2956 t->flags |= SN_CONN_CLOSED;
2957 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 /*************************************************************
2960 * OK, that's finished for the headers. We have done what we *
2961 * could. Let's switch to the DATA state. *
2962 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963
Willy Tarreaue393fe22008-08-16 22:18:07 +02002964 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002965 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002966
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002967#ifdef CONFIG_HAP_TCPSPLICE
2968 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2969 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002970 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002971 }
2972#endif
2973 /* if the user wants to log as soon as possible, without counting
2974 * bytes from the server, then this is the right moment. We have
2975 * to temporarily assign bytes_out to log what we currently have.
2976 */
2977 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2978 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2979 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002980 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002981 t->logs.bytes_out = 0;
2982 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002983
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002984 /* Note: we must not try to cheat by jumping directly to DATA,
2985 * otherwise we would not let the client side wake up.
2986 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002987
Willy Tarreaudafde432008-08-17 01:00:46 +02002988 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002989 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002990
Willy Tarreau2df28e82008-08-17 15:20:19 +02002991 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2992 * probably reduce one day's debugging session.
2993 */
2994#ifdef DEBUG_DEV
2995 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
2996 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2997 __FILE__, __LINE__, rep->analysers);
2998 ABORT_NOW();
2999 }
3000#endif
3001 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003002 return 0;
3003}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003004
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005/*
3006 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003007 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003008 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3009 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01003010 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003011 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003012void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003013{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003014 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003015 buffer_stop_hijack(rep);
3016 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017 }
3018 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003019 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003020 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003021 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003022 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003023 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003024 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003025
Willy Tarreau91861262007-10-17 17:06:05 +02003026 /* unknown data source or internal error */
3027 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003028 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02003029 if (!(s->flags & SN_ERR_MASK))
3030 s->flags |= SN_ERR_PRXCOND;
3031 if (!(s->flags & SN_FINST_MASK))
3032 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003033 buffer_stop_hijack(rep);
3034 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003035}
3036
3037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003038/* Iterate the same filter through all request headers.
3039 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003040 * Since it can manage the switch to another backend, it updates the per-proxy
3041 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003042 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003043int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003044{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003045 char term;
3046 char *cur_ptr, *cur_end, *cur_next;
3047 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003048 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003049 struct hdr_idx_elem *cur_hdr;
3050 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003052 last_hdr = 0;
3053
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003054 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003055 old_idx = 0;
3056
3057 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003058 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003059 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003060 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003061 (exp->action == ACT_ALLOW ||
3062 exp->action == ACT_DENY ||
3063 exp->action == ACT_TARPIT))
3064 return 0;
3065
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003066 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003067 if (!cur_idx)
3068 break;
3069
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003070 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003071 cur_ptr = cur_next;
3072 cur_end = cur_ptr + cur_hdr->len;
3073 cur_next = cur_end + cur_hdr->cr + 1;
3074
3075 /* Now we have one header between cur_ptr and cur_end,
3076 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003077 */
3078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003079 /* The annoying part is that pattern matching needs
3080 * that we modify the contents to null-terminate all
3081 * strings before testing them.
3082 */
3083
3084 term = *cur_end;
3085 *cur_end = '\0';
3086
3087 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3088 switch (exp->action) {
3089 case ACT_SETBE:
3090 /* It is not possible to jump a second time.
3091 * FIXME: should we return an HTTP/500 here so that
3092 * the admin knows there's a problem ?
3093 */
3094 if (t->be != t->fe)
3095 break;
3096
3097 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003099 last_hdr = 1;
3100 break;
3101
3102 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003103 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003104 last_hdr = 1;
3105 break;
3106
3107 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003108 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003109 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003110 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003111 break;
3112
3113 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003114 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003115 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003116 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003117 break;
3118
3119 case ACT_REPLACE:
3120 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3121 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3122 /* FIXME: if the user adds a newline in the replacement, the
3123 * index will not be recalculated for now, and the new line
3124 * will not be counted as a new header.
3125 */
3126
3127 cur_end += delta;
3128 cur_next += delta;
3129 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003130 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003131 break;
3132
3133 case ACT_REMOVE:
3134 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3135 cur_next += delta;
3136
3137 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003138 txn->req.eoh += delta;
3139 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3140 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003141 cur_hdr->len = 0;
3142 cur_end = NULL; /* null-term has been rewritten */
3143 break;
3144
3145 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003146 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003147 if (cur_end)
3148 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003150 /* keep the link from this header to next one in case of later
3151 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003152 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003153 old_idx = cur_idx;
3154 }
3155 return 0;
3156}
3157
3158
3159/* Apply the filter to the request line.
3160 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3161 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003162 * Since it can manage the switch to another backend, it updates the per-proxy
3163 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003164 */
3165int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3166{
3167 char term;
3168 char *cur_ptr, *cur_end;
3169 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003170 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003171 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003172
Willy Tarreau58f10d72006-12-04 02:26:12 +01003173
Willy Tarreau3d300592007-03-18 18:34:41 +01003174 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003175 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003176 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003177 (exp->action == ACT_ALLOW ||
3178 exp->action == ACT_DENY ||
3179 exp->action == ACT_TARPIT))
3180 return 0;
3181 else if (exp->action == ACT_REMOVE)
3182 return 0;
3183
3184 done = 0;
3185
Willy Tarreau9cdde232007-05-02 20:58:19 +02003186 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003187 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003188
3189 /* Now we have the request line between cur_ptr and cur_end */
3190
3191 /* The annoying part is that pattern matching needs
3192 * that we modify the contents to null-terminate all
3193 * strings before testing them.
3194 */
3195
3196 term = *cur_end;
3197 *cur_end = '\0';
3198
3199 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3200 switch (exp->action) {
3201 case ACT_SETBE:
3202 /* It is not possible to jump a second time.
3203 * FIXME: should we return an HTTP/500 here so that
3204 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003205 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003206 if (t->be != t->fe)
3207 break;
3208
3209 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003211 done = 1;
3212 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003213
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003214 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003215 txn->flags |= TX_CLALLOW;
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_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003220 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003221 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003222 done = 1;
3223 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003224
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003225 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003226 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003227 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003228 done = 1;
3229 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003231 case ACT_REPLACE:
3232 *cur_end = term; /* restore the string terminator */
3233 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3234 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3235 /* FIXME: if the user adds a newline in the replacement, the
3236 * index will not be recalculated for now, and the new line
3237 * will not be counted as a new header.
3238 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003239
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003240 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003241 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003242
Willy Tarreau9cdde232007-05-02 20:58:19 +02003243 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003244 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003245 HTTP_MSG_RQMETH,
3246 cur_ptr, cur_end + 1,
3247 NULL, NULL);
3248 if (unlikely(!cur_end))
3249 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003250
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003251 /* we have a full request and we know that we have either a CR
3252 * or an LF at <ptr>.
3253 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003254 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3255 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003256 /* there is no point trying this regex on headers */
3257 return 1;
3258 }
3259 }
3260 *cur_end = term; /* restore the string terminator */
3261 return done;
3262}
Willy Tarreau97de6242006-12-27 17:18:38 +01003263
Willy Tarreau58f10d72006-12-04 02:26:12 +01003264
Willy Tarreau58f10d72006-12-04 02:26:12 +01003265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003266/*
3267 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3268 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003269 * unparsable request. Since it can manage the switch to another backend, it
3270 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003271 */
3272int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3273{
Willy Tarreau3d300592007-03-18 18:34:41 +01003274 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003275 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003276 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003277 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003278
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003279 /*
3280 * The interleaving of transformations and verdicts
3281 * makes it difficult to decide to continue or stop
3282 * the evaluation.
3283 */
3284
Willy Tarreau3d300592007-03-18 18:34:41 +01003285 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003286 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3287 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3288 exp = exp->next;
3289 continue;
3290 }
3291
3292 /* Apply the filter to the request line. */
3293 ret = apply_filter_to_req_line(t, req, exp);
3294 if (unlikely(ret < 0))
3295 return -1;
3296
3297 if (likely(ret == 0)) {
3298 /* The filter did not match the request, it can be
3299 * iterated through all headers.
3300 */
3301 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003302 }
3303 exp = exp->next;
3304 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003305 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003306}
3307
3308
Willy Tarreaua15645d2007-03-18 16:22:39 +01003309
Willy Tarreau58f10d72006-12-04 02:26:12 +01003310/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003311 * Manage client-side cookie. It can impact performance by about 2% so it is
3312 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003313 */
3314void manage_client_side_cookies(struct session *t, struct buffer *req)
3315{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003316 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003317 char *p1, *p2, *p3, *p4;
3318 char *del_colon, *del_cookie, *colon;
3319 int app_cookies;
3320
3321 appsess *asession_temp = NULL;
3322 appsess local_asession;
3323
3324 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003325 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003326
Willy Tarreau2a324282006-12-05 00:05:46 +01003327 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003328 * we start with the start line.
3329 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003330 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003331 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003332
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003333 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003334 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003335 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003336
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003337 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003338 cur_ptr = cur_next;
3339 cur_end = cur_ptr + cur_hdr->len;
3340 cur_next = cur_end + cur_hdr->cr + 1;
3341
3342 /* We have one full header between cur_ptr and cur_end, and the
3343 * next header starts at cur_next. We're only interested in
3344 * "Cookie:" headers.
3345 */
3346
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003347 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3348 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003349 old_idx = cur_idx;
3350 continue;
3351 }
3352
3353 /* Now look for cookies. Conforming to RFC2109, we have to support
3354 * attributes whose name begin with a '$', and associate them with
3355 * the right cookie, if we want to delete this cookie.
3356 * So there are 3 cases for each cookie read :
3357 * 1) it's a special attribute, beginning with a '$' : ignore it.
3358 * 2) it's a server id cookie that we *MAY* want to delete : save
3359 * some pointers on it (last semi-colon, beginning of cookie...)
3360 * 3) it's an application cookie : we *MAY* have to delete a previous
3361 * "special" cookie.
3362 * At the end of loop, if a "special" cookie remains, we may have to
3363 * remove it. If no application cookie persists in the header, we
3364 * *MUST* delete it
3365 */
3366
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003367 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003368
Willy Tarreau58f10d72006-12-04 02:26:12 +01003369 /* del_cookie == NULL => nothing to be deleted */
3370 del_colon = del_cookie = NULL;
3371 app_cookies = 0;
3372
3373 while (p1 < cur_end) {
3374 /* skip spaces and colons, but keep an eye on these ones */
3375 while (p1 < cur_end) {
3376 if (*p1 == ';' || *p1 == ',')
3377 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003378 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003379 break;
3380 p1++;
3381 }
3382
3383 if (p1 == cur_end)
3384 break;
3385
3386 /* p1 is at the beginning of the cookie name */
3387 p2 = p1;
3388 while (p2 < cur_end && *p2 != '=')
3389 p2++;
3390
3391 if (p2 == cur_end)
3392 break;
3393
3394 p3 = p2 + 1; /* skips the '=' sign */
3395 if (p3 == cur_end)
3396 break;
3397
3398 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003399 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003400 p4++;
3401
3402 /* here, we have the cookie name between p1 and p2,
3403 * and its value between p3 and p4.
3404 * we can process it :
3405 *
3406 * Cookie: NAME=VALUE;
3407 * | || || |
3408 * | || || +--> p4
3409 * | || |+-------> p3
3410 * | || +--------> p2
3411 * | |+------------> p1
3412 * | +-------------> colon
3413 * +--------------------> cur_ptr
3414 */
3415
3416 if (*p1 == '$') {
3417 /* skip this one */
3418 }
3419 else {
3420 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003421 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003422 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003423 (p4 - p1 >= t->fe->capture_namelen) &&
3424 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003425 int log_len = p4 - p1;
3426
Willy Tarreau086b3b42007-05-13 21:45:51 +02003427 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003428 Alert("HTTP logging : out of memory.\n");
3429 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003430 if (log_len > t->fe->capture_len)
3431 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003432 memcpy(txn->cli_cookie, p1, log_len);
3433 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003434 }
3435 }
3436
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003437 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3438 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003439 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003440 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003441 char *delim;
3442
3443 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3444 * have the server ID betweek p3 and delim, and the original cookie between
3445 * delim+1 and p4. Otherwise, delim==p4 :
3446 *
3447 * Cookie: NAME=SRV~VALUE;
3448 * | || || | |
3449 * | || || | +--> p4
3450 * | || || +--------> delim
3451 * | || |+-----------> p3
3452 * | || +------------> p2
3453 * | |+----------------> p1
3454 * | +-----------------> colon
3455 * +------------------------> cur_ptr
3456 */
3457
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003458 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003459 for (delim = p3; delim < p4; delim++)
3460 if (*delim == COOKIE_DELIM)
3461 break;
3462 }
3463 else
3464 delim = p4;
3465
3466
3467 /* Here, we'll look for the first running server which supports the cookie.
3468 * This allows to share a same cookie between several servers, for example
3469 * to dedicate backup servers to specific servers only.
3470 * However, to prevent clients from sticking to cookie-less backup server
3471 * when they have incidentely learned an empty cookie, we simply ignore
3472 * empty cookies and mark them as invalid.
3473 */
3474 if (delim == p3)
3475 srv = NULL;
3476
3477 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003478 if (srv->cookie && (srv->cklen == delim - p3) &&
3479 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003480 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003481 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003482 txn->flags &= ~TX_CK_MASK;
3483 txn->flags |= TX_CK_VALID;
3484 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003485 t->srv = srv;
3486 break;
3487 } else {
3488 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003489 txn->flags &= ~TX_CK_MASK;
3490 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003491 }
3492 }
3493 srv = srv->next;
3494 }
3495
Willy Tarreau3d300592007-03-18 18:34:41 +01003496 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003497 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003498 txn->flags &= ~TX_CK_MASK;
3499 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003500 }
3501
3502 /* depending on the cookie mode, we may have to either :
3503 * - delete the complete cookie if we're in insert+indirect mode, so that
3504 * the server never sees it ;
3505 * - remove the server id from the cookie value, and tag the cookie as an
3506 * application cookie so that it does not get accidentely removed later,
3507 * if we're in cookie prefix mode
3508 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003509 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003510 int delta; /* negative */
3511
3512 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3513 p4 += delta;
3514 cur_end += delta;
3515 cur_next += delta;
3516 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003517 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003518
3519 del_cookie = del_colon = NULL;
3520 app_cookies++; /* protect the header from deletion */
3521 }
3522 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003523 (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 +01003524 del_cookie = p1;
3525 del_colon = colon;
3526 }
3527 } else {
3528 /* now we know that we must keep this cookie since it's
3529 * not ours. But if we wanted to delete our cookie
3530 * earlier, we cannot remove the complete header, but we
3531 * can remove the previous block itself.
3532 */
3533 app_cookies++;
3534
3535 if (del_cookie != NULL) {
3536 int delta; /* negative */
3537
3538 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3539 p4 += delta;
3540 cur_end += delta;
3541 cur_next += delta;
3542 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003543 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003544 del_cookie = del_colon = NULL;
3545 }
3546 }
3547
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003548 if ((t->be->appsession_name != NULL) &&
3549 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003550 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003551
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552 /* Cool... it's the right one */
3553
3554 asession_temp = &local_asession;
3555
Willy Tarreau63963c62007-05-13 21:29:55 +02003556 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003557 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3558 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3559 return;
3560 }
3561
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003562 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3563 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003564 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003565
Willy Tarreau58f10d72006-12-04 02:26:12 +01003566 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003567 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3568 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003569 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003570 /* 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 Alert("Not enough memory process_cli():asession:calloc().\n");
3573 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3574 return;
3575 }
3576
3577 asession_temp->sessid = local_asession.sessid;
3578 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003579 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003580 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003581 } else {
3582 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003583 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003585 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003586 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003587 Alert("Found Application Session without matching server.\n");
3588 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003589 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590 while (srv) {
3591 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003592 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003593 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003594 txn->flags &= ~TX_CK_MASK;
3595 txn->flags |= TX_CK_VALID;
3596 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003597 t->srv = srv;
3598 break;
3599 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003600 txn->flags &= ~TX_CK_MASK;
3601 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602 }
3603 }
3604 srv = srv->next;
3605 }/* end while(srv) */
3606 }/* end else if server == NULL */
3607
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003608 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003609 asession_temp->request_count++;
3610#if defined(DEBUG_HASH)
3611 Alert("manage_client_side_cookies\n");
3612 appsession_hash_dump(&(t->be->htbl_proxy));
3613#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003614 }/* end if ((t->proxy->appsession_name != NULL) ... */
3615 }
3616
3617 /* we'll have to look for another cookie ... */
3618 p1 = p4;
3619 } /* while (p1 < cur_end) */
3620
3621 /* There's no more cookie on this line.
3622 * We may have marked the last one(s) for deletion.
3623 * We must do this now in two ways :
3624 * - if there is no app cookie, we simply delete the header ;
3625 * - if there are app cookies, we must delete the end of the
3626 * string properly, including the colon/semi-colon before
3627 * the cookie name.
3628 */
3629 if (del_cookie != NULL) {
3630 int delta;
3631 if (app_cookies) {
3632 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3633 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003634 cur_hdr->len += delta;
3635 } else {
3636 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003637
3638 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003639 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3640 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003641 cur_hdr->len = 0;
3642 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003643 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003644 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003645 }
3646
3647 /* keep the link from this header to next one */
3648 old_idx = cur_idx;
3649 } /* end of cookie processing on this header */
3650}
3651
3652
Willy Tarreaua15645d2007-03-18 16:22:39 +01003653/* Iterate the same filter through all response headers contained in <rtr>.
3654 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3655 */
3656int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3657{
3658 char term;
3659 char *cur_ptr, *cur_end, *cur_next;
3660 int cur_idx, old_idx, last_hdr;
3661 struct http_txn *txn = &t->txn;
3662 struct hdr_idx_elem *cur_hdr;
3663 int len, delta;
3664
3665 last_hdr = 0;
3666
3667 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3668 old_idx = 0;
3669
3670 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003671 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003672 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003673 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003674 (exp->action == ACT_ALLOW ||
3675 exp->action == ACT_DENY))
3676 return 0;
3677
3678 cur_idx = txn->hdr_idx.v[old_idx].next;
3679 if (!cur_idx)
3680 break;
3681
3682 cur_hdr = &txn->hdr_idx.v[cur_idx];
3683 cur_ptr = cur_next;
3684 cur_end = cur_ptr + cur_hdr->len;
3685 cur_next = cur_end + cur_hdr->cr + 1;
3686
3687 /* Now we have one header between cur_ptr and cur_end,
3688 * and the next header starts at cur_next.
3689 */
3690
3691 /* The annoying part is that pattern matching needs
3692 * that we modify the contents to null-terminate all
3693 * strings before testing them.
3694 */
3695
3696 term = *cur_end;
3697 *cur_end = '\0';
3698
3699 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3700 switch (exp->action) {
3701 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003702 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003703 last_hdr = 1;
3704 break;
3705
3706 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003707 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003708 last_hdr = 1;
3709 break;
3710
3711 case ACT_REPLACE:
3712 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3713 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3714 /* FIXME: if the user adds a newline in the replacement, the
3715 * index will not be recalculated for now, and the new line
3716 * will not be counted as a new header.
3717 */
3718
3719 cur_end += delta;
3720 cur_next += delta;
3721 cur_hdr->len += delta;
3722 txn->rsp.eoh += delta;
3723 break;
3724
3725 case ACT_REMOVE:
3726 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3727 cur_next += delta;
3728
3729 /* FIXME: this should be a separate function */
3730 txn->rsp.eoh += delta;
3731 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3732 txn->hdr_idx.used--;
3733 cur_hdr->len = 0;
3734 cur_end = NULL; /* null-term has been rewritten */
3735 break;
3736
3737 }
3738 }
3739 if (cur_end)
3740 *cur_end = term; /* restore the string terminator */
3741
3742 /* keep the link from this header to next one in case of later
3743 * removal of next header.
3744 */
3745 old_idx = cur_idx;
3746 }
3747 return 0;
3748}
3749
3750
3751/* Apply the filter to the status line in the response buffer <rtr>.
3752 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3753 * or -1 if a replacement resulted in an invalid status line.
3754 */
3755int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3756{
3757 char term;
3758 char *cur_ptr, *cur_end;
3759 int done;
3760 struct http_txn *txn = &t->txn;
3761 int len, delta;
3762
3763
Willy Tarreau3d300592007-03-18 18:34:41 +01003764 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003765 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003766 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003767 (exp->action == ACT_ALLOW ||
3768 exp->action == ACT_DENY))
3769 return 0;
3770 else if (exp->action == ACT_REMOVE)
3771 return 0;
3772
3773 done = 0;
3774
Willy Tarreau9cdde232007-05-02 20:58:19 +02003775 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003776 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3777
3778 /* Now we have the status line between cur_ptr and cur_end */
3779
3780 /* The annoying part is that pattern matching needs
3781 * that we modify the contents to null-terminate all
3782 * strings before testing them.
3783 */
3784
3785 term = *cur_end;
3786 *cur_end = '\0';
3787
3788 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3789 switch (exp->action) {
3790 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003791 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003792 done = 1;
3793 break;
3794
3795 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003796 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003797 done = 1;
3798 break;
3799
3800 case ACT_REPLACE:
3801 *cur_end = term; /* restore the string terminator */
3802 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3803 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3804 /* FIXME: if the user adds a newline in the replacement, the
3805 * index will not be recalculated for now, and the new line
3806 * will not be counted as a new header.
3807 */
3808
3809 txn->rsp.eoh += delta;
3810 cur_end += delta;
3811
Willy Tarreau9cdde232007-05-02 20:58:19 +02003812 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003813 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003814 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003815 cur_ptr, cur_end + 1,
3816 NULL, NULL);
3817 if (unlikely(!cur_end))
3818 return -1;
3819
3820 /* we have a full respnse and we know that we have either a CR
3821 * or an LF at <ptr>.
3822 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003823 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003824 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3825 /* there is no point trying this regex on headers */
3826 return 1;
3827 }
3828 }
3829 *cur_end = term; /* restore the string terminator */
3830 return done;
3831}
3832
3833
3834
3835/*
3836 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3837 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3838 * unparsable response.
3839 */
3840int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3841{
Willy Tarreau3d300592007-03-18 18:34:41 +01003842 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003843 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003844 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003845 int ret;
3846
3847 /*
3848 * The interleaving of transformations and verdicts
3849 * makes it difficult to decide to continue or stop
3850 * the evaluation.
3851 */
3852
Willy Tarreau3d300592007-03-18 18:34:41 +01003853 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003854 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3855 exp->action == ACT_PASS)) {
3856 exp = exp->next;
3857 continue;
3858 }
3859
3860 /* Apply the filter to the status line. */
3861 ret = apply_filter_to_sts_line(t, rtr, exp);
3862 if (unlikely(ret < 0))
3863 return -1;
3864
3865 if (likely(ret == 0)) {
3866 /* The filter did not match the response, it can be
3867 * iterated through all headers.
3868 */
3869 apply_filter_to_resp_headers(t, rtr, exp);
3870 }
3871 exp = exp->next;
3872 }
3873 return 0;
3874}
3875
3876
3877
3878/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003879 * Manage server-side cookies. It can impact performance by about 2% so it is
3880 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003881 */
3882void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3883{
3884 struct http_txn *txn = &t->txn;
3885 char *p1, *p2, *p3, *p4;
3886
3887 appsess *asession_temp = NULL;
3888 appsess local_asession;
3889
3890 char *cur_ptr, *cur_end, *cur_next;
3891 int cur_idx, old_idx, delta;
3892
Willy Tarreaua15645d2007-03-18 16:22:39 +01003893 /* Iterate through the headers.
3894 * we start with the start line.
3895 */
3896 old_idx = 0;
3897 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3898
3899 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3900 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003901 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003902
3903 cur_hdr = &txn->hdr_idx.v[cur_idx];
3904 cur_ptr = cur_next;
3905 cur_end = cur_ptr + cur_hdr->len;
3906 cur_next = cur_end + cur_hdr->cr + 1;
3907
3908 /* We have one full header between cur_ptr and cur_end, and the
3909 * next header starts at cur_next. We're only interested in
3910 * "Cookie:" headers.
3911 */
3912
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003913 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3914 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003915 old_idx = cur_idx;
3916 continue;
3917 }
3918
3919 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003920 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003921
3922
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003923 /* maybe we only wanted to see if there was a set-cookie. Note that
3924 * the cookie capture is declared in the fronend.
3925 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003926 if (t->be->cookie_name == NULL &&
3927 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003928 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003929 return;
3930
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003931 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003932
3933 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003934 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3935 break;
3936
3937 /* p1 is at the beginning of the cookie name */
3938 p2 = p1;
3939
3940 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3941 p2++;
3942
3943 if (p2 == cur_end || *p2 == ';') /* next cookie */
3944 break;
3945
3946 p3 = p2 + 1; /* skip the '=' sign */
3947 if (p3 == cur_end)
3948 break;
3949
3950 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003951 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003952 p4++;
3953
3954 /* here, we have the cookie name between p1 and p2,
3955 * and its value between p3 and p4.
3956 * we can process it.
3957 */
3958
3959 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003960 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003961 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003962 (p4 - p1 >= t->fe->capture_namelen) &&
3963 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003964 int log_len = p4 - p1;
3965
Willy Tarreau086b3b42007-05-13 21:45:51 +02003966 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003967 Alert("HTTP logging : out of memory.\n");
3968 }
3969
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003970 if (log_len > t->fe->capture_len)
3971 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003972 memcpy(txn->srv_cookie, p1, log_len);
3973 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003974 }
3975
3976 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003977 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3978 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003979 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003980 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003981
3982 /* If the cookie is in insert mode on a known server, we'll delete
3983 * this occurrence because we'll insert another one later.
3984 * We'll delete it too if the "indirect" option is set and we're in
3985 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003986 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
3987 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003988 /* this header must be deleted */
3989 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3990 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3991 txn->hdr_idx.used--;
3992 cur_hdr->len = 0;
3993 cur_next += delta;
3994 txn->rsp.eoh += delta;
3995
Willy Tarreau3d300592007-03-18 18:34:41 +01003996 txn->flags |= 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_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004000 /* replace bytes p3->p4 with the cookie name associated
4001 * with this server since we know it.
4002 */
4003 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4004 cur_hdr->len += delta;
4005 cur_next += delta;
4006 txn->rsp.eoh += delta;
4007
Willy Tarreau3d300592007-03-18 18:34:41 +01004008 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004009 }
4010 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004011 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004012 /* insert the cookie name associated with this server
4013 * before existing cookie, and insert a delimitor between them..
4014 */
4015 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4016 cur_hdr->len += delta;
4017 cur_next += delta;
4018 txn->rsp.eoh += delta;
4019
4020 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004021 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004022 }
4023 }
4024 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004025 else if ((t->be->appsession_name != NULL) &&
4026 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004027
4028 /* Cool... it's the right one */
4029
4030 size_t server_id_len = strlen(t->srv->id) + 1;
4031 asession_temp = &local_asession;
4032
Willy Tarreau63963c62007-05-13 21:29:55 +02004033 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4035 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4036 return;
4037 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004038 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4039 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004040 asession_temp->serverid = NULL;
4041
4042 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004043 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4044 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004045 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004046 Alert("Not enough Memory process_srv():asession:calloc().\n");
4047 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4048 return;
4049 }
4050 asession_temp->sessid = local_asession.sessid;
4051 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004052 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004053 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4054 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004055 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004056 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004057 }
4058
Willy Tarreaua15645d2007-03-18 16:22:39 +01004059 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004060 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004061 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4062 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4063 return;
4064 }
4065 asession_temp->serverid[0] = '\0';
4066 }
4067
4068 if (asession_temp->serverid[0] == '\0')
4069 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4070
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004071 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004072 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004074 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004075 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004076#endif
4077 }/* end if ((t->proxy->appsession_name != NULL) ... */
4078 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4079 } /* we're now at the end of the cookie value */
4080
4081 /* keep the link from this header to next one */
4082 old_idx = cur_idx;
4083 } /* end of cookie processing on this header */
4084}
4085
4086
4087
4088/*
4089 * Check if response is cacheable or not. Updates t->flags.
4090 */
4091void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4092{
4093 struct http_txn *txn = &t->txn;
4094 char *p1, *p2;
4095
4096 char *cur_ptr, *cur_end, *cur_next;
4097 int cur_idx;
4098
Willy Tarreau5df51872007-11-25 16:20:08 +01004099 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004100 return;
4101
4102 /* Iterate through the headers.
4103 * we start with the start line.
4104 */
4105 cur_idx = 0;
4106 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4107
4108 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4109 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004110 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004111
4112 cur_hdr = &txn->hdr_idx.v[cur_idx];
4113 cur_ptr = cur_next;
4114 cur_end = cur_ptr + cur_hdr->len;
4115 cur_next = cur_end + cur_hdr->cr + 1;
4116
4117 /* We have one full header between cur_ptr and cur_end, and the
4118 * next header starts at cur_next. We're only interested in
4119 * "Cookie:" headers.
4120 */
4121
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004122 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4123 if (val) {
4124 if ((cur_end - (cur_ptr + val) >= 8) &&
4125 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4126 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4127 return;
4128 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004129 }
4130
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004131 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4132 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004133 continue;
4134
4135 /* OK, right now we know we have a cache-control header at cur_ptr */
4136
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004137 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004138
4139 if (p1 >= cur_end) /* no more info */
4140 continue;
4141
4142 /* p1 is at the beginning of the value */
4143 p2 = p1;
4144
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004145 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004146 p2++;
4147
4148 /* we have a complete value between p1 and p2 */
4149 if (p2 < cur_end && *p2 == '=') {
4150 /* we have something of the form no-cache="set-cookie" */
4151 if ((cur_end - p1 >= 21) &&
4152 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4153 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004154 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004155 continue;
4156 }
4157
4158 /* OK, so we know that either p2 points to the end of string or to a comma */
4159 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4160 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4161 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4162 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004163 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004164 return;
4165 }
4166
4167 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004168 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004169 continue;
4170 }
4171 }
4172}
4173
4174
Willy Tarreau58f10d72006-12-04 02:26:12 +01004175/*
4176 * Try to retrieve a known appsession in the URI, then the associated server.
4177 * If the server is found, it's assigned to the session.
4178 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004179void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180{
Willy Tarreau3d300592007-03-18 18:34:41 +01004181 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004182 appsess *asession_temp = NULL;
4183 appsess local_asession;
4184 char *request_line;
4185
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004186 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004187 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004188 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004189 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190 return;
4191
4192 /* skip ';' */
4193 request_line++;
4194
4195 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004196 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004197 return;
4198
4199 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004200 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004201
4202 /* First try if we already have an appsession */
4203 asession_temp = &local_asession;
4204
Willy Tarreau63963c62007-05-13 21:29:55 +02004205 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4207 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4208 return;
4209 }
4210
4211 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004212 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4213 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214 asession_temp->serverid = NULL;
4215
4216 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004217 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4218 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004219 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004220 /* 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 Alert("Not enough memory process_cli():asession:calloc().\n");
4223 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4224 return;
4225 }
4226 asession_temp->sessid = local_asession.sessid;
4227 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004228 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004229 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230 }
4231 else {
4232 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004233 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004234 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004235
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004236 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004237 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004238
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004240 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004241 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242#endif
4243 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004244 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004245 Alert("Found Application Session without matching server.\n");
4246 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004247 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248 while (srv) {
4249 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004250 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004251 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004252 txn->flags &= ~TX_CK_MASK;
4253 txn->flags |= TX_CK_VALID;
4254 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004255 t->srv = srv;
4256 break;
4257 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004258 txn->flags &= ~TX_CK_MASK;
4259 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004260 }
4261 }
4262 srv = srv->next;
4263 }
4264 }
4265}
4266
4267
Willy Tarreaub2513902006-12-17 14:52:38 +01004268/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004269 * In a GET or HEAD request, check if the requested URI matches the stats uri
4270 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004271 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004272 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004273 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004274 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004275 *
4276 * Returns 1 if the session's state changes, otherwise 0.
4277 */
4278int stats_check_uri_auth(struct session *t, struct proxy *backend)
4279{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004280 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004281 struct uri_auth *uri_auth = backend->uri_auth;
4282 struct user_auth *user;
4283 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004284 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004285
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004286 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4287
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004288 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004289 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004290 return 0;
4291
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004292 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004293
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004294 /* the URI is in h */
4295 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004296 return 0;
4297
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004298 h += uri_auth->uri_len;
4299 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4300 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004301 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004302 break;
4303 }
4304 h++;
4305 }
4306
4307 if (uri_auth->refresh) {
4308 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4309 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4310 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004311 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004312 break;
4313 }
4314 h++;
4315 }
4316 }
4317
Willy Tarreau55bb8452007-10-17 18:44:57 +02004318 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4319 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4320 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004321 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004322 break;
4323 }
4324 h++;
4325 }
4326
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004327 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4328
Willy Tarreaub2513902006-12-17 14:52:38 +01004329 /* we are in front of a interceptable URI. Let's check
4330 * if there's an authentication and if it's valid.
4331 */
4332 user = uri_auth->users;
4333 if (!user) {
4334 /* no user auth required, it's OK */
4335 authenticated = 1;
4336 } else {
4337 authenticated = 0;
4338
4339 /* a user list is defined, we have to check.
4340 * skip 21 chars for "Authorization: Basic ".
4341 */
4342
4343 /* FIXME: this should move to an earlier place */
4344 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004345 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4346 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4347 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004348 if (len > 14 &&
4349 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004350 txn->auth_hdr.str = h;
4351 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004352 break;
4353 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004354 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004355 }
4356
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004357 if (txn->auth_hdr.len < 21 ||
4358 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004359 user = NULL;
4360
4361 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004362 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4363 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004364 user->user_pwd, user->user_len)) {
4365 authenticated = 1;
4366 break;
4367 }
4368 user = user->next;
4369 }
4370 }
4371
4372 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004373 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004374
4375 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004376 msg.str = trash;
4377 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004378 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004379 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004380 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004381 if (!(t->flags & SN_ERR_MASK))
4382 t->flags |= SN_ERR_PRXCOND;
4383 if (!(t->flags & SN_FINST_MASK))
4384 t->flags |= SN_FINST_R;
4385 return 1;
4386 }
4387
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004388 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004389 * data.
4390 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004391 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004392 buffer_shutw_now(t->req);
4393 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004394 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004395 t->data_source = DATA_SRC_STATS;
4396 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004397 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004398 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004399 return 1;
4400}
4401
Willy Tarreau4076a152009-04-02 15:18:36 +02004402/*
4403 * Capture a bad request or response and archive it in the proxy's structure.
4404 */
4405void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4406 struct buffer *buf, struct http_msg *msg,
4407 struct proxy *other_end)
4408{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004409 es->len = buf->r - (buf->data + msg->som);
4410 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004411 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004412 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004413 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004414 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004415 es->when = date; // user-visible date
4416 es->sid = s->uniq_id;
4417 es->srv = s->srv;
4418 es->oe = other_end;
4419 es->src = s->cli_addr;
4420}
Willy Tarreaub2513902006-12-17 14:52:38 +01004421
Willy Tarreaubaaee002006-06-26 02:48:02 +02004422/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004423 * Print a debug line with a header
4424 */
4425void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4426{
4427 int len, max;
4428 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004429 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004430 max = end - start;
4431 UBOUND(max, sizeof(trash) - len - 1);
4432 len += strlcpy2(trash + len, start, max + 1);
4433 trash[len++] = '\n';
4434 write(1, trash, len);
4435}
4436
4437
Willy Tarreau8797c062007-05-07 00:55:35 +02004438/************************************************************************/
4439/* The code below is dedicated to ACL parsing and matching */
4440/************************************************************************/
4441
4442
4443
4444
4445/* 1. Check on METHOD
4446 * We use the pre-parsed method if it is known, and store its number as an
4447 * integer. If it is unknown, we use the pointer and the length.
4448 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004449static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004450{
4451 int len, meth;
4452
Willy Tarreauae8b7962007-06-09 23:10:04 +02004453 len = strlen(*text);
4454 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004455
4456 pattern->val.i = meth;
4457 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004458 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004459 if (!pattern->ptr.str)
4460 return 0;
4461 pattern->len = len;
4462 }
4463 return 1;
4464}
4465
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004466static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004467acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4468 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004469{
4470 int meth;
4471 struct http_txn *txn = l7;
4472
Willy Tarreaub6866442008-07-14 23:54:42 +02004473 if (!txn)
4474 return 0;
4475
Willy Tarreauc11416f2007-06-17 16:58:38 +02004476 if (txn->req.msg_state != HTTP_MSG_BODY)
4477 return 0;
4478
Willy Tarreau8797c062007-05-07 00:55:35 +02004479 meth = txn->meth;
4480 test->i = meth;
4481 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004482 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4483 /* ensure the indexes are not affected */
4484 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004485 test->len = txn->req.sl.rq.m_l;
4486 test->ptr = txn->req.sol;
4487 }
4488 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4489 return 1;
4490}
4491
4492static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4493{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004494 int icase;
4495
Willy Tarreau8797c062007-05-07 00:55:35 +02004496 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004497 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004498
4499 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004500 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004501
4502 /* Other method, we must compare the strings */
4503 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004504 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004505
4506 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4507 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4508 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004509 return ACL_PAT_FAIL;
4510 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004511}
4512
4513/* 2. Check on Request/Status Version
4514 * We simply compare strings here.
4515 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004516static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004517{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004518 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004519 if (!pattern->ptr.str)
4520 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004521 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004522 return 1;
4523}
4524
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004525static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004526acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4527 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004528{
4529 struct http_txn *txn = l7;
4530 char *ptr;
4531 int len;
4532
Willy Tarreaub6866442008-07-14 23:54:42 +02004533 if (!txn)
4534 return 0;
4535
Willy Tarreauc11416f2007-06-17 16:58:38 +02004536 if (txn->req.msg_state != HTTP_MSG_BODY)
4537 return 0;
4538
Willy Tarreau8797c062007-05-07 00:55:35 +02004539 len = txn->req.sl.rq.v_l;
4540 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4541
4542 while ((len-- > 0) && (*ptr++ != '/'));
4543 if (len <= 0)
4544 return 0;
4545
4546 test->ptr = ptr;
4547 test->len = len;
4548
4549 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4550 return 1;
4551}
4552
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004553static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004554acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4555 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004556{
4557 struct http_txn *txn = l7;
4558 char *ptr;
4559 int len;
4560
Willy Tarreaub6866442008-07-14 23:54:42 +02004561 if (!txn)
4562 return 0;
4563
Willy Tarreauc11416f2007-06-17 16:58:38 +02004564 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4565 return 0;
4566
Willy Tarreau8797c062007-05-07 00:55:35 +02004567 len = txn->rsp.sl.st.v_l;
4568 ptr = txn->rsp.sol;
4569
4570 while ((len-- > 0) && (*ptr++ != '/'));
4571 if (len <= 0)
4572 return 0;
4573
4574 test->ptr = ptr;
4575 test->len = len;
4576
4577 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4578 return 1;
4579}
4580
4581/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004582static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004583acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4584 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004585{
4586 struct http_txn *txn = l7;
4587 char *ptr;
4588 int len;
4589
Willy Tarreaub6866442008-07-14 23:54:42 +02004590 if (!txn)
4591 return 0;
4592
Willy Tarreauc11416f2007-06-17 16:58:38 +02004593 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4594 return 0;
4595
Willy Tarreau8797c062007-05-07 00:55:35 +02004596 len = txn->rsp.sl.st.c_l;
4597 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4598
4599 test->i = __strl2ui(ptr, len);
4600 test->flags = ACL_TEST_F_VOL_1ST;
4601 return 1;
4602}
4603
4604/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004605static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004606acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4607 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004608{
4609 struct http_txn *txn = l7;
4610
Willy Tarreaub6866442008-07-14 23:54:42 +02004611 if (!txn)
4612 return 0;
4613
Willy Tarreauc11416f2007-06-17 16:58:38 +02004614 if (txn->req.msg_state != HTTP_MSG_BODY)
4615 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004616
Willy Tarreauc11416f2007-06-17 16:58:38 +02004617 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4618 /* ensure the indexes are not affected */
4619 return 0;
4620
Willy Tarreau8797c062007-05-07 00:55:35 +02004621 test->len = txn->req.sl.rq.u_l;
4622 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4623
Willy Tarreauf3d25982007-05-08 22:45:09 +02004624 /* we do not need to set READ_ONLY because the data is in a buffer */
4625 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004626 return 1;
4627}
4628
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004629static int
4630acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4631 struct acl_expr *expr, struct acl_test *test)
4632{
4633 struct http_txn *txn = l7;
4634
Willy Tarreaub6866442008-07-14 23:54:42 +02004635 if (!txn)
4636 return 0;
4637
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004638 if (txn->req.msg_state != HTTP_MSG_BODY)
4639 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004640
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004641 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4642 /* ensure the indexes are not affected */
4643 return 0;
4644
4645 /* Parse HTTP request */
4646 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4647 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4648 test->i = AF_INET;
4649
4650 /*
4651 * If we are parsing url in frontend space, we prepare backend stage
4652 * to not parse again the same url ! optimization lazyness...
4653 */
4654 if (px->options & PR_O_HTTP_PROXY)
4655 l4->flags |= SN_ADDR_SET;
4656
4657 test->flags = ACL_TEST_F_READ_ONLY;
4658 return 1;
4659}
4660
4661static int
4662acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4663 struct acl_expr *expr, struct acl_test *test)
4664{
4665 struct http_txn *txn = l7;
4666
Willy Tarreaub6866442008-07-14 23:54:42 +02004667 if (!txn)
4668 return 0;
4669
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004670 if (txn->req.msg_state != HTTP_MSG_BODY)
4671 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004672
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004673 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4674 /* ensure the indexes are not affected */
4675 return 0;
4676
4677 /* Same optimization as url_ip */
4678 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4679 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4680
4681 if (px->options & PR_O_HTTP_PROXY)
4682 l4->flags |= SN_ADDR_SET;
4683
4684 test->flags = ACL_TEST_F_READ_ONLY;
4685 return 1;
4686}
4687
Willy Tarreauc11416f2007-06-17 16:58:38 +02004688/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4689 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4690 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004691static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004692acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004693 struct acl_expr *expr, struct acl_test *test)
4694{
4695 struct http_txn *txn = l7;
4696 struct hdr_idx *idx = &txn->hdr_idx;
4697 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004698
Willy Tarreaub6866442008-07-14 23:54:42 +02004699 if (!txn)
4700 return 0;
4701
Willy Tarreau33a7e692007-06-10 19:45:56 +02004702 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4703 /* search for header from the beginning */
4704 ctx->idx = 0;
4705
Willy Tarreau33a7e692007-06-10 19:45:56 +02004706 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4707 test->flags |= ACL_TEST_F_FETCH_MORE;
4708 test->flags |= ACL_TEST_F_VOL_HDR;
4709 test->len = ctx->vlen;
4710 test->ptr = (char *)ctx->line + ctx->val;
4711 return 1;
4712 }
4713
4714 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4715 test->flags |= ACL_TEST_F_VOL_HDR;
4716 return 0;
4717}
4718
Willy Tarreau33a7e692007-06-10 19:45:56 +02004719static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004720acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4721 struct acl_expr *expr, struct acl_test *test)
4722{
4723 struct http_txn *txn = l7;
4724
Willy Tarreaub6866442008-07-14 23:54:42 +02004725 if (!txn)
4726 return 0;
4727
Willy Tarreauc11416f2007-06-17 16:58:38 +02004728 if (txn->req.msg_state != HTTP_MSG_BODY)
4729 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004730
Willy Tarreauc11416f2007-06-17 16:58:38 +02004731 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4732 /* ensure the indexes are not affected */
4733 return 0;
4734
4735 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4736}
4737
4738static int
4739acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4740 struct acl_expr *expr, struct acl_test *test)
4741{
4742 struct http_txn *txn = l7;
4743
Willy Tarreaub6866442008-07-14 23:54:42 +02004744 if (!txn)
4745 return 0;
4746
Willy Tarreauc11416f2007-06-17 16:58:38 +02004747 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4748 return 0;
4749
4750 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4751}
4752
4753/* 6. Check on HTTP header count. The number of occurrences is returned.
4754 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4755 */
4756static int
4757acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004758 struct acl_expr *expr, struct acl_test *test)
4759{
4760 struct http_txn *txn = l7;
4761 struct hdr_idx *idx = &txn->hdr_idx;
4762 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004763 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004764
Willy Tarreaub6866442008-07-14 23:54:42 +02004765 if (!txn)
4766 return 0;
4767
Willy Tarreau33a7e692007-06-10 19:45:56 +02004768 ctx.idx = 0;
4769 cnt = 0;
4770 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4771 cnt++;
4772
4773 test->i = cnt;
4774 test->flags = ACL_TEST_F_VOL_HDR;
4775 return 1;
4776}
4777
Willy Tarreauc11416f2007-06-17 16:58:38 +02004778static int
4779acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4780 struct acl_expr *expr, struct acl_test *test)
4781{
4782 struct http_txn *txn = l7;
4783
Willy Tarreaub6866442008-07-14 23:54:42 +02004784 if (!txn)
4785 return 0;
4786
Willy Tarreauc11416f2007-06-17 16:58:38 +02004787 if (txn->req.msg_state != HTTP_MSG_BODY)
4788 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004789
Willy Tarreauc11416f2007-06-17 16:58:38 +02004790 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4791 /* ensure the indexes are not affected */
4792 return 0;
4793
4794 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4795}
4796
4797static int
4798acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4799 struct acl_expr *expr, struct acl_test *test)
4800{
4801 struct http_txn *txn = l7;
4802
Willy Tarreaub6866442008-07-14 23:54:42 +02004803 if (!txn)
4804 return 0;
4805
Willy Tarreauc11416f2007-06-17 16:58:38 +02004806 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4807 return 0;
4808
4809 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4810}
4811
Willy Tarreau33a7e692007-06-10 19:45:56 +02004812/* 7. Check on HTTP header's integer value. The integer value is returned.
4813 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004814 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004815 */
4816static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004817acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004818 struct acl_expr *expr, struct acl_test *test)
4819{
4820 struct http_txn *txn = l7;
4821 struct hdr_idx *idx = &txn->hdr_idx;
4822 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004823
Willy Tarreaub6866442008-07-14 23:54:42 +02004824 if (!txn)
4825 return 0;
4826
Willy Tarreau33a7e692007-06-10 19:45:56 +02004827 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4828 /* search for header from the beginning */
4829 ctx->idx = 0;
4830
Willy Tarreau33a7e692007-06-10 19:45:56 +02004831 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4832 test->flags |= ACL_TEST_F_FETCH_MORE;
4833 test->flags |= ACL_TEST_F_VOL_HDR;
4834 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4835 return 1;
4836 }
4837
4838 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4839 test->flags |= ACL_TEST_F_VOL_HDR;
4840 return 0;
4841}
4842
Willy Tarreauc11416f2007-06-17 16:58:38 +02004843static int
4844acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4845 struct acl_expr *expr, struct acl_test *test)
4846{
4847 struct http_txn *txn = l7;
4848
Willy Tarreaub6866442008-07-14 23:54:42 +02004849 if (!txn)
4850 return 0;
4851
Willy Tarreauc11416f2007-06-17 16:58:38 +02004852 if (txn->req.msg_state != HTTP_MSG_BODY)
4853 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004854
Willy Tarreauc11416f2007-06-17 16:58:38 +02004855 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4856 /* ensure the indexes are not affected */
4857 return 0;
4858
4859 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4860}
4861
4862static int
4863acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4864 struct acl_expr *expr, struct acl_test *test)
4865{
4866 struct http_txn *txn = l7;
4867
Willy Tarreaub6866442008-07-14 23:54:42 +02004868 if (!txn)
4869 return 0;
4870
Willy Tarreauc11416f2007-06-17 16:58:38 +02004871 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4872 return 0;
4873
4874 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4875}
4876
Willy Tarreau737b0c12007-06-10 21:28:46 +02004877/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4878 * the first '/' after the possible hostname, and ends before the possible '?'.
4879 */
4880static int
4881acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4882 struct acl_expr *expr, struct acl_test *test)
4883{
4884 struct http_txn *txn = l7;
4885 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004886
Willy Tarreaub6866442008-07-14 23:54:42 +02004887 if (!txn)
4888 return 0;
4889
Willy Tarreauc11416f2007-06-17 16:58:38 +02004890 if (txn->req.msg_state != HTTP_MSG_BODY)
4891 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004892
Willy Tarreauc11416f2007-06-17 16:58:38 +02004893 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4894 /* ensure the indexes are not affected */
4895 return 0;
4896
Willy Tarreau21d2af32008-02-14 20:25:24 +01004897 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4898 ptr = http_get_path(txn);
4899 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004900 return 0;
4901
4902 /* OK, we got the '/' ! */
4903 test->ptr = ptr;
4904
4905 while (ptr < end && *ptr != '?')
4906 ptr++;
4907
4908 test->len = ptr - test->ptr;
4909
4910 /* we do not need to set READ_ONLY because the data is in a buffer */
4911 test->flags = ACL_TEST_F_VOL_1ST;
4912 return 1;
4913}
4914
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004915static int
4916acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
4917 struct acl_expr *expr, struct acl_test *test)
4918{
4919 struct buffer *req = s->req;
4920 struct http_txn *txn = &s->txn;
4921 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02004922
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004923 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
4924 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
4925 */
4926
4927 if (!s || !req)
4928 return 0;
4929
4930 if (unlikely(msg->msg_state == HTTP_MSG_BODY)) {
4931 /* Already decoded as OK */
4932 test->flags |= ACL_TEST_F_SET_RES_PASS;
4933 return 1;
4934 }
4935
4936 /* Try to decode HTTP request */
4937 if (likely(req->lr < req->r))
4938 http_msg_analyzer(req, msg, &txn->hdr_idx);
4939
4940 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
4941 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
4942 test->flags |= ACL_TEST_F_SET_RES_FAIL;
4943 return 1;
4944 }
4945 /* wait for final state */
4946 test->flags |= ACL_TEST_F_MAY_CHANGE;
4947 return 0;
4948 }
4949
4950 /* OK we got a valid HTTP request. We have some minor preparation to
4951 * perform so that further checks can rely on HTTP tests.
4952 */
4953 msg->sol = req->data + msg->som;
4954 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
4955 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4956 s->flags |= SN_REDIRECTABLE;
4957
4958 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
4959 test->flags |= ACL_TEST_F_SET_RES_FAIL;
4960 return 1;
4961 }
4962
4963 test->flags |= ACL_TEST_F_SET_RES_PASS;
4964 return 1;
4965}
4966
Willy Tarreau8797c062007-05-07 00:55:35 +02004967
4968/************************************************************************/
4969/* All supported keywords must be declared here. */
4970/************************************************************************/
4971
4972/* Note: must not be declared <const> as its list will be overwritten */
4973static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004974 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
4975
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004976 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4977 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4978 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4979 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004980
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004981 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4982 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4983 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4984 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4985 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4986 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4987 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4988 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4989 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004990
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004991 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4992 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4993 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4994 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4995 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4996 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4997 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4998 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4999 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5000 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005001
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005002 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5003 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5004 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5005 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5006 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5007 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5008 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5009 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5010 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005011
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005012 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5013 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5014 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5015 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5016 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5017 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5018 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005019
Willy Tarreauf3d25982007-05-08 22:45:09 +02005020 { NULL, NULL, NULL, NULL },
5021
5022#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005023 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5024 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5025 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5026 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5027 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5028 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5029 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5030
Willy Tarreau8797c062007-05-07 00:55:35 +02005031 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5032 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5033 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5034 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5035 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5036 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5037 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5038 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5039
5040 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5041 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5042 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5043 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5044 { NULL, NULL, NULL, NULL },
5045#endif
5046}};
5047
5048
5049__attribute__((constructor))
5050static void __http_protocol_init(void)
5051{
5052 acl_register_keywords(&acl_kws);
5053}
5054
5055
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005057 * Local variables:
5058 * c-indent-level: 8
5059 * c-basic-offset: 8
5060 * End:
5061 */