blob: d5760aedd7f7fcb0b7962467b69491f3560d595e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020053#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/task.h>
55
Willy Tarreau6d1a9882007-01-07 02:03:04 +010056#ifdef CONFIG_HAP_TCPSPLICE
57#include <libtcpsplice.h>
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau58f10d72006-12-04 02:26:12 +010060#define DEBUG_PARSE_NO_SPEEDUP
61#undef DEBUG_PARSE_NO_SPEEDUP
62
Willy Tarreau976f1ee2006-12-17 10:06:03 +010063/* This is used to perform a quick jump as an alternative to a break/continue
64 * instruction. The first argument is the label for normal operation, and the
65 * second one is the break/continue instruction in the no_speedup mode.
66 */
67
68#ifdef DEBUG_PARSE_NO_SPEEDUP
69#define QUICK_JUMP(x,y) y
70#else
71#define QUICK_JUMP(x,y) goto x
72#endif
73
Willy Tarreau1c47f852006-07-09 08:22:27 +020074/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010075const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020076 "HTTP/1.0 200 OK\r\n"
77 "Cache-Control: no-cache\r\n"
78 "Connection: close\r\n"
79 "Content-Type: text/html\r\n"
80 "\r\n"
81 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const struct chunk http_200_chunk = {
84 .str = (char *)&HTTP_200,
85 .len = sizeof(HTTP_200)-1
86};
87
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
89 "HTTP/1.0 301 Moved Permantenly\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreau0f772532006-12-23 20:51:41 +010094const char *HTTP_302 =
95 "HTTP/1.0 302 Found\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
100/* same as 302 except that the browser MUST retry with the GET method */
101const char *HTTP_303 =
102 "HTTP/1.0 303 See Other\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
108const char *HTTP_401_fmt =
109 "HTTP/1.0 401 Unauthorized\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200112 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
114 "\r\n"
115 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
116
Willy Tarreau0f772532006-12-23 20:51:41 +0100117
118const int http_err_codes[HTTP_ERR_SIZE] = {
119 [HTTP_ERR_400] = 400,
120 [HTTP_ERR_403] = 403,
121 [HTTP_ERR_408] = 408,
122 [HTTP_ERR_500] = 500,
123 [HTTP_ERR_502] = 502,
124 [HTTP_ERR_503] = 503,
125 [HTTP_ERR_504] = 504,
126};
127
Willy Tarreau80587432006-12-24 17:47:20 +0100128static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100129 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100130 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
136
137 [HTTP_ERR_403] =
138 "HTTP/1.0 403 Forbidden\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
144
145 [HTTP_ERR_408] =
146 "HTTP/1.0 408 Request Time-out\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
152
153 [HTTP_ERR_500] =
154 "HTTP/1.0 500 Server Error\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
160
161 [HTTP_ERR_502] =
162 "HTTP/1.0 502 Bad Gateway\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
168
169 [HTTP_ERR_503] =
170 "HTTP/1.0 503 Service Unavailable\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
176
177 [HTTP_ERR_504] =
178 "HTTP/1.0 504 Gateway Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
184
185};
186
Willy Tarreau80587432006-12-24 17:47:20 +0100187/* We must put the messages here since GCC cannot initialize consts depending
188 * on strlen().
189 */
190struct chunk http_err_chunks[HTTP_ERR_SIZE];
191
Willy Tarreau42250582007-04-01 01:30:43 +0200192#define FD_SETS_ARE_BITFIELDS
193#ifdef FD_SETS_ARE_BITFIELDS
194/*
195 * This map is used with all the FD_* macros to check whether a particular bit
196 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
197 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
198 * byte should be encoded. Be careful to always pass bytes from 0 to 255
199 * exclusively to the macros.
200 */
201fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203
204#else
205#error "Check if your OS uses bitfields for fd_sets"
206#endif
207
Willy Tarreau80587432006-12-24 17:47:20 +0100208void init_proto_http()
209{
Willy Tarreau42250582007-04-01 01:30:43 +0200210 int i;
211 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100212 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200213
Willy Tarreau80587432006-12-24 17:47:20 +0100214 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
215 if (!http_err_msgs[msg]) {
216 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
217 abort();
218 }
219
220 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
221 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
222 }
Willy Tarreau42250582007-04-01 01:30:43 +0200223
224 /* initialize the log header encoding map : '{|}"#' should be encoded with
225 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
226 * URL encoding only requires '"', '#' to be encoded as well as non-
227 * printable characters above.
228 */
229 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
230 memset(url_encode_map, 0, sizeof(url_encode_map));
231 for (i = 0; i < 32; i++) {
232 FD_SET(i, hdr_encode_map);
233 FD_SET(i, url_encode_map);
234 }
235 for (i = 127; i < 256; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239
240 tmp = "\"#{|}";
241 while (*tmp) {
242 FD_SET(*tmp, hdr_encode_map);
243 tmp++;
244 }
245
246 tmp = "\"#";
247 while (*tmp) {
248 FD_SET(*tmp, url_encode_map);
249 tmp++;
250 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200251
252 /* memory allocations */
253 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200254 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100255}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257/*
258 * We have 26 list of methods (1 per first letter), each of which can have
259 * up to 3 entries (2 valid, 1 null).
260 */
261struct http_method_desc {
262 http_meth_t meth;
263 int len;
264 const char text[8];
265};
266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100267const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100268 ['C' - 'A'] = {
269 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
270 },
271 ['D' - 'A'] = {
272 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
273 },
274 ['G' - 'A'] = {
275 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
276 },
277 ['H' - 'A'] = {
278 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
279 },
280 ['P' - 'A'] = {
281 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
282 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
283 },
284 ['T' - 'A'] = {
285 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
286 },
287 /* rest is empty like this :
288 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
289 */
290};
291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100292/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294 * RFC2616 for those chars.
295 */
296
297const char http_is_spht[256] = {
298 [' '] = 1, ['\t'] = 1,
299};
300
301const char http_is_crlf[256] = {
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_lws[256] = {
306 [' '] = 1, ['\t'] = 1,
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_sep[256] = {
311 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
312 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
313 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
314 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
315 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
316};
317
318const char http_is_ctl[256] = {
319 [0 ... 31] = 1,
320 [127] = 1,
321};
322
323/*
324 * A token is any ASCII char that is neither a separator nor a CTL char.
325 * Do not overwrite values in assignment since gcc-2.95 will not handle
326 * them correctly. Instead, define every non-CTL char's status.
327 */
328const char http_is_token[256] = {
329 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
330 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
331 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
332 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
333 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
334 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
335 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
336 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
337 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
338 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
339 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
340 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
341 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
342 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
343 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
344 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
345 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
346 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
347 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
348 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
349 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
350 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
351 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
352 ['|'] = 1, ['}'] = 0, ['~'] = 1,
353};
354
355
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100356/*
357 * An http ver_token is any ASCII which can be found in an HTTP version,
358 * which includes 'H', 'T', 'P', '/', '.' and any digit.
359 */
360const char http_is_ver_token[256] = {
361 ['.'] = 1, ['/'] = 1,
362 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
363 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
364 ['H'] = 1, ['P'] = 1, ['T'] = 1,
365};
366
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200369static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200540/* This function shuts down the buffers on the server side, and sets indicators
541 * accordingly. The server's fd is supposed to already be closed. Note that if
542 * <status> is 0, or if the message pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
Willy Tarreau3da77c52008-08-29 09:58:42 +0200547 buffer_write_ena(t->rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +0200548 buffer_shutw(t->req);
549 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100550 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100551 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100552 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100553 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 }
555 if (!(t->flags & SN_ERR_MASK))
556 t->flags |= err;
557 if (!(t->flags & SN_FINST_MASK))
558 t->flags |= finst;
559}
560
Willy Tarreau80587432006-12-24 17:47:20 +0100561/* This function returns the appropriate error location for the given session
562 * and message.
563 */
564
565struct chunk *error_message(struct session *s, int msgnum)
566{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200567 if (s->be->errmsg[msgnum].str)
568 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100569 else if (s->fe->errmsg[msgnum].str)
570 return &s->fe->errmsg[msgnum];
571 else
572 return &http_err_chunks[msgnum];
573}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575/*
576 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
577 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
578 */
579static http_meth_t find_http_meth(const char *str, const int len)
580{
581 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100583
584 m = ((unsigned)*str - 'A');
585
586 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 for (h = http_methods[m]; h->len > 0; h++) {
588 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100590 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 };
593 return HTTP_METH_OTHER;
594 }
595 return HTTP_METH_NONE;
596
597}
598
Willy Tarreau21d2af32008-02-14 20:25:24 +0100599/* Parse the URI from the given transaction (which is assumed to be in request
600 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
601 * It is returned otherwise.
602 */
603static char *
604http_get_path(struct http_txn *txn)
605{
606 char *ptr, *end;
607
608 ptr = txn->req.sol + txn->req.sl.rq.u;
609 end = ptr + txn->req.sl.rq.u_l;
610
611 if (ptr >= end)
612 return NULL;
613
614 /* RFC2616, par. 5.1.2 :
615 * Request-URI = "*" | absuri | abspath | authority
616 */
617
618 if (*ptr == '*')
619 return NULL;
620
621 if (isalpha((unsigned char)*ptr)) {
622 /* this is a scheme as described by RFC3986, par. 3.1 */
623 ptr++;
624 while (ptr < end &&
625 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
626 ptr++;
627 /* skip '://' */
628 if (ptr == end || *ptr++ != ':')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 if (ptr == end || *ptr++ != '/')
633 return NULL;
634 }
635 /* skip [user[:passwd]@]host[:[port]] */
636
637 while (ptr < end && *ptr != '/')
638 ptr++;
639
640 if (ptr == end)
641 return NULL;
642
643 /* OK, we got the '/' ! */
644 return ptr;
645}
646
Willy Tarreaudafde432008-08-17 01:00:46 +0200647/* Processes the client, server, request and response jobs of a session task,
648 * then puts it back to the wait queue in a clean state, or cleans up its
649 * resources if it must be deleted. Returns in <next> the date the task wants
650 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
651 * nothing too many times, the request and response buffers flags are monitored
652 * and each function is called only if at least another function has changed at
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200653 * least one flag it is interested in.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200655void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656{
657 struct session *s = t->context;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200658 int resync;
659 unsigned int rqf_cli, rpf_cli;
660 unsigned int rqf_srv, rpf_srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200662 /* Check timeouts only during data phase for now */
663 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200664 buffer_check_timeouts(s->req);
665 buffer_check_timeouts(s->rep);
666
667 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
668 if (s->req->flags & BF_READ_TIMEOUT) {
669 buffer_shutw(s->req);
670 s->req->cons->shutr(s->req->prod);
671 }
672 if (s->req->flags & BF_WRITE_TIMEOUT) {
673 buffer_shutw(s->req);
674 s->req->cons->shutw(s->req->cons);
675 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200676 }
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200677
678 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
679 if (s->rep->flags & BF_READ_TIMEOUT) {
680 buffer_shutw(s->rep);
681 s->rep->cons->shutr(s->rep->prod);
682 }
683 if (s->rep->flags & BF_WRITE_TIMEOUT) {
684 buffer_shutw(s->rep);
685 s->rep->cons->shutw(s->rep->cons);
686 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200687 }
Willy Tarreau35374672008-09-03 18:11:02 +0200688 /* Note that we don't check nor indicate if we wake up because
689 * of a timeout on a stream interface.
690 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200691 }
692
Willy Tarreau48adac52008-08-30 04:58:38 +0200693 /* Check if we need to close the write side. This can only happen
694 * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear
695 * from low level, and neither HIJACK nor SHUTW can disappear from low
696 * level.
697 */
698 if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
699 buffer_shutw(s->req);
700 s->req->cons->shutw(s->req->cons);
701 }
702
703 if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
704 buffer_shutw(s->rep);
705 s->rep->cons->shutw(s->rep->cons);
706 }
707
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200708 /* When a server-side connection is released, we have to
709 * count it and check for pending connections on this server.
710 */
711 if (unlikely(s->req->cons->state == SI_ST_CLO &&
712 s->req->cons->prev_state == SI_ST_EST)) {
713 /* Count server-side errors (but not timeouts). */
714 if (s->req->flags & BF_WRITE_ERROR) {
715 s->be->failed_resp++;
716 if (s->srv)
717 s->srv->failed_resp++;
718 }
719
720 if (s->srv) {
721 s->srv->cur_sess--;
722 sess_change_server(s, NULL);
723 if (may_dequeue_tasks(s->srv, s->be))
724 process_srv_queue(s->srv);
725 }
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200726 }
727
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200728 /* Dirty trick: force one first pass everywhere */
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200729 rqf_cli = rqf_srv = ~s->req->flags;
730 rpf_cli = rpf_srv = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +0200731
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200732 /* well, the ST_CONN state is already handled properly */
733 if (s->req->prod->state == SI_ST_EST) {
734 rqf_cli = s->req->flags;
735 rpf_cli = s->rep->flags;
736 }
737
738 if (s->req->cons->state == SI_ST_EST) {
739 rqf_srv = s->req->flags;
740 rpf_srv = s->rep->flags;
741 }
742
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 do {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200744 DPRINTF(stderr,"[%u] %s: task=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
745 now_ms, __FUNCTION__,
746 t,
747 s->req, s->rep,
748 s->req->rex, s->rep->wex,
749 s->req->flags, s->rep->flags,
750 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
751
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200752 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +0200753
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200754 /* Maybe resync client FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200755 if (s->rep->cons->state != SI_ST_CLO) {
756 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
757 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200758 stream_sock_data_update(s->rep->cons->fd);
759 rqf_cli = s->req->flags;
760 rpf_cli = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +0200761 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200762 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200763
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200764 /* Maybe resync server FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200765 if (s->req->cons->state != SI_ST_CLO) {
766 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
767 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200768 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200769 process_srv_conn(s);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200770 resync = 1; /* we might have to resync */
771 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200772
773 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +0200774 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +0200775 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +0200776 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200777 /* We want to force the connection to the server to close,
778 * and the server has begun to respond. That's the right
779 * time.
780 */
781 buffer_shutw_now(s->req);
782 }
783
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200784 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +0200785
786 /* When a server-side connection is released, we have to
787 * count it and check for pending connections on this server.
788 */
789 if (s->req->cons->state == SI_ST_CLO) {
790 if (s->srv) {
791 s->srv->cur_sess--;
792 sess_change_server(s, NULL);
793 if (may_dequeue_tasks(s->srv, s->be))
794 process_srv_queue(s->srv);
795 }
796 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200797 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200798 rqf_srv = s->req->flags;
799 rpf_srv = s->rep->flags;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200800 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200801 }
802
803 /* we may have to resync because of pending connections */
804 if (resync)
805 continue;
806
807 /**** Process layer 7 below ****/
808
809 /* Analyse request */
810 if (s->req->flags & BF_MASK_ANALYSER) {
811 unsigned int flags = s->req->flags;
812
813 if (s->req->prod->state >= SI_ST_EST) {
814 /* it's up to the analysers to reset write_ena */
815 buffer_write_ena(s->req);
816 if (s->req->analysers)
817 process_request(s);
818 }
819 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
820 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
821 if (s->req->flags != flags)
822 resync = 1;
823 }
824
825 /* Analyse response */
826 if (unlikely(s->rep->flags & BF_HIJACK)) {
827 /* In inject mode, we wake up everytime something has
828 * happened on the write side of the buffer.
829 */
830 unsigned int flags = s->rep->flags;
831
832 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
833 !(s->rep->flags & BF_FULL)) {
834 produce_content(s);
835 }
836 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
837 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
838 if (s->rep->flags != flags)
839 resync = 1;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200840 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200841 else if (s->rep->flags & BF_MASK_ANALYSER) {
842 unsigned int flags = s->rep->flags;
843
844 if (s->rep->prod->state >= SI_ST_EST) {
845 /* it's up to the analysers to reset write_ena */
846 buffer_write_ena(s->rep);
847 if (s->rep->analysers)
848 process_response(s);
849 }
850 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
851 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
852 if (s->rep->flags != flags)
853 resync = 1;
854 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200855
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200856 /* For the moment, we need to clean the client and server flags that
857 * have vanished. This is just a temporary measure though.
858 */
859 rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
860 rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
861 rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
862 rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaudafde432008-08-17 01:00:46 +0200863 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864
Willy Tarreaua37095b2008-09-03 11:37:47 +0200865 /* This is needed only when debugging is enabled, to indicate
866 * client-side or server-side close. Please note that in the unlikely
867 * event where both sides would close at once, the sequence is reported
868 * on the server side first.
869 */
870 if (unlikely((global.mode & MODE_DEBUG) &&
871 (!(global.mode & MODE_QUIET) ||
872 (global.mode & MODE_VERBOSE)))) {
873 int len;
874
875 if (s->si[1].state == SI_ST_CLO &&
876 s->si[1].prev_state == SI_ST_EST) {
877 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
878 s->uniq_id, s->be->id,
879 (unsigned short)s->si[0].fd,
880 (unsigned short)s->si[1].fd);
881 write(1, trash, len);
882 }
883
884 if (s->si[0].state == SI_ST_CLO &&
885 s->si[0].prev_state == SI_ST_EST) {
886 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
887 s->uniq_id, s->be->id,
888 (unsigned short)s->si[0].fd,
889 (unsigned short)s->si[1].fd);
890 write(1, trash, len);
891 }
892 }
893
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200894 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200895 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100896
897 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
898 session_process_counters(s);
899
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200900 if (s->rep->cons->state == SI_ST_EST)
901 stream_sock_data_finish(s->rep->cons->fd);
902
903 if (s->req->cons->state == SI_ST_EST)
904 stream_sock_data_finish(s->req->cons->fd);
905
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200906 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
907 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200908 s->si[0].prev_state = s->si[0].state;
909 s->si[1].prev_state = s->si[1].state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910
Willy Tarreau7f875f62008-08-11 17:35:01 +0200911 /* Trick: if a request is being waiting for the server to respond,
912 * and if we know the server can timeout, we don't want the timeout
913 * to expire on the client side first, but we're still interested
914 * in passing data from the client to the server (eg: POST). Thus,
915 * we can cancel the client's request timeout if the server's
916 * request timeout is set and the server has not yet sent a response.
917 */
918
Willy Tarreau3da77c52008-08-29 09:58:42 +0200919 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200920 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200921 s->req->rex = TICK_ETERNITY;
922
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200923 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
924 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200925 if (s->req->analysers)
926 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927
Willy Tarreau35374672008-09-03 18:11:02 +0200928 if (s->si[0].exp)
929 t->expire = tick_first(t->expire, s->si[0].exp);
930
931 if (s->si[1].exp)
932 t->expire = tick_first(t->expire, s->si[1].exp);
933
Willy Tarreaucb651252008-08-29 13:57:30 +0200934#ifdef DEBUG_FULL
935 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n",
936 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex);
937#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 /* restore t to its place in the task list */
939 task_queue(t);
940
Willy Tarreaua7c52762008-08-16 18:40:18 +0200941#ifdef DEBUG_DEV
942 /* this may only happen when no timeout is set or in case of an FSM bug */
943 if (!t->expire)
944 ABORT_NOW();
945#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200946 *next = t->expire;
947 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 }
949
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100950 s->fe->feconn--;
951 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200952 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953 actconn--;
954
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200955 if (unlikely((global.mode & MODE_DEBUG) &&
956 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200958 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200959 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200960 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200961 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 write(1, trash, len);
963 }
964
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200965 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100966 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967
968 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200969 if (s->logs.logwait &&
970 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200971 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
972 if (s->fe->to_log & LW_REQ)
973 http_sess_log(s);
974 else
975 tcp_sess_log(s);
976 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977
978 /* the task MUST not be in the run queue anymore */
979 task_delete(t);
980 session_free(s);
981 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200982 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983}
984
985
Willy Tarreau42250582007-04-01 01:30:43 +0200986extern const char sess_term_cond[8];
987extern const char sess_fin_state[8];
988extern const char *monthname[12];
989const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
990const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
991 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
992 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200993struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200994struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100995
Willy Tarreau42250582007-04-01 01:30:43 +0200996/*
997 * send a log for the session when we have enough info about it.
998 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100999 */
Willy Tarreau42250582007-04-01 01:30:43 +02001000static void http_sess_log(struct session *s)
1001{
1002 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1003 struct proxy *fe = s->fe;
1004 struct proxy *be = s->be;
1005 struct proxy *prx_log;
1006 struct http_txn *txn = &s->txn;
1007 int tolog;
1008 char *uri, *h;
1009 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001010 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001011 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001012 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001013 int hdr;
1014
1015 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1016 return;
1017 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018
Willy Tarreau42250582007-04-01 01:30:43 +02001019 if (s->cli_addr.ss_family == AF_INET)
1020 inet_ntop(AF_INET,
1021 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1022 pn, sizeof(pn));
1023 else
1024 inet_ntop(AF_INET6,
1025 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1026 pn, sizeof(pn));
1027
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001028 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001029
1030 /* FIXME: let's limit ourselves to frontend logging for now. */
1031 tolog = fe->to_log;
1032
1033 h = tmpline;
1034 if (fe->to_log & LW_REQHDR &&
1035 txn->req.cap &&
1036 (h < tmpline + sizeof(tmpline) - 10)) {
1037 *(h++) = ' ';
1038 *(h++) = '{';
1039 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1040 if (hdr)
1041 *(h++) = '|';
1042 if (txn->req.cap[hdr] != NULL)
1043 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1044 '#', hdr_encode_map, txn->req.cap[hdr]);
1045 }
1046 *(h++) = '}';
1047 }
1048
1049 if (fe->to_log & LW_RSPHDR &&
1050 txn->rsp.cap &&
1051 (h < tmpline + sizeof(tmpline) - 7)) {
1052 *(h++) = ' ';
1053 *(h++) = '{';
1054 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1055 if (hdr)
1056 *(h++) = '|';
1057 if (txn->rsp.cap[hdr] != NULL)
1058 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1059 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1060 }
1061 *(h++) = '}';
1062 }
1063
1064 if (h < tmpline + sizeof(tmpline) - 4) {
1065 *(h++) = ' ';
1066 *(h++) = '"';
1067 uri = txn->uri ? txn->uri : "<BADREQ>";
1068 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1069 '#', url_encode_map, uri);
1070 *(h++) = '"';
1071 }
1072 *h = '\0';
1073
1074 svid = (tolog & LW_SVID) ?
1075 (s->data_source != DATA_SRC_STATS) ?
1076 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1077
Willy Tarreau70089872008-06-13 21:12:51 +02001078 t_request = -1;
1079 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1080 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082 send_log(prx_log, LOG_INFO,
1083 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
1084 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001085 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001086 pn,
1087 (s->cli_addr.ss_family == AF_INET) ?
1088 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1089 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001090 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001091 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001092 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001093 t_request,
1094 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001095 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1096 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1097 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1098 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001099 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001100 txn->cli_cookie ? txn->cli_cookie : "-",
1101 txn->srv_cookie ? txn->srv_cookie : "-",
1102 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1103 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1104 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1105 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1106 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001107 (s->flags & SN_REDISP)?"+":"",
1108 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001109 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1110
1111 s->logs.logwait = 0;
1112}
1113
Willy Tarreau117f59e2007-03-04 18:17:17 +01001114
1115/*
1116 * Capture headers from message starting at <som> according to header list
1117 * <cap_hdr>, and fill the <idx> structure appropriately.
1118 */
1119void capture_headers(char *som, struct hdr_idx *idx,
1120 char **cap, struct cap_hdr *cap_hdr)
1121{
1122 char *eol, *sol, *col, *sov;
1123 int cur_idx;
1124 struct cap_hdr *h;
1125 int len;
1126
1127 sol = som + hdr_idx_first_pos(idx);
1128 cur_idx = hdr_idx_first_idx(idx);
1129
1130 while (cur_idx) {
1131 eol = sol + idx->v[cur_idx].len;
1132
1133 col = sol;
1134 while (col < eol && *col != ':')
1135 col++;
1136
1137 sov = col + 1;
1138 while (sov < eol && http_is_lws[(unsigned char)*sov])
1139 sov++;
1140
1141 for (h = cap_hdr; h; h = h->next) {
1142 if ((h->namelen == col - sol) &&
1143 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1144 if (cap[h->index] == NULL)
1145 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001146 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001147
1148 if (cap[h->index] == NULL) {
1149 Alert("HTTP capture : out of memory.\n");
1150 continue;
1151 }
1152
1153 len = eol - sov;
1154 if (len > h->len)
1155 len = h->len;
1156
1157 memcpy(cap[h->index], sov, len);
1158 cap[h->index][len]=0;
1159 }
1160 }
1161 sol = eol + idx->v[cur_idx].cr + 1;
1162 cur_idx = idx->v[cur_idx].next;
1163 }
1164}
1165
1166
Willy Tarreau42250582007-04-01 01:30:43 +02001167/* either we find an LF at <ptr> or we jump to <bad>.
1168 */
1169#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1170
1171/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1172 * otherwise to <http_msg_ood> with <state> set to <st>.
1173 */
1174#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1175 ptr++; \
1176 if (likely(ptr < end)) \
1177 goto good; \
1178 else { \
1179 state = (st); \
1180 goto http_msg_ood; \
1181 } \
1182 } while (0)
1183
1184
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001186 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001187 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1188 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1189 * will give undefined results.
1190 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1191 * and that msg->sol points to the beginning of the response.
1192 * If a complete line is found (which implies that at least one CR or LF is
1193 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1194 * returned indicating an incomplete line (which does not mean that parts have
1195 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1196 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1197 * upon next call.
1198 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001199 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1201 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001202 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001204const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1205 unsigned int state, const char *ptr, const char *end,
1206 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001207{
1208 __label__
1209 http_msg_rpver,
1210 http_msg_rpver_sp,
1211 http_msg_rpcode,
1212 http_msg_rpcode_sp,
1213 http_msg_rpreason,
1214 http_msg_rpline_eol,
1215 http_msg_ood, /* out of data */
1216 http_msg_invalid;
1217
1218 switch (state) {
1219 http_msg_rpver:
1220 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001221 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001222 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1223
1224 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001225 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1227 }
1228 goto http_msg_invalid;
1229
1230 http_msg_rpver_sp:
1231 case HTTP_MSG_RPVER_SP:
1232 if (likely(!HTTP_IS_LWS(*ptr))) {
1233 msg->sl.st.c = ptr - msg_buf;
1234 goto http_msg_rpcode;
1235 }
1236 if (likely(HTTP_IS_SPHT(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1238 /* so it's a CR/LF, this is invalid */
1239 goto http_msg_invalid;
1240
1241 http_msg_rpcode:
1242 case HTTP_MSG_RPCODE:
1243 if (likely(!HTTP_IS_LWS(*ptr)))
1244 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1245
1246 if (likely(HTTP_IS_SPHT(*ptr))) {
1247 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1248 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1249 }
1250
1251 /* so it's a CR/LF, so there is no reason phrase */
1252 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1253 http_msg_rsp_reason:
1254 /* FIXME: should we support HTTP responses without any reason phrase ? */
1255 msg->sl.st.r = ptr - msg_buf;
1256 msg->sl.st.r_l = 0;
1257 goto http_msg_rpline_eol;
1258
1259 http_msg_rpcode_sp:
1260 case HTTP_MSG_RPCODE_SP:
1261 if (likely(!HTTP_IS_LWS(*ptr))) {
1262 msg->sl.st.r = ptr - msg_buf;
1263 goto http_msg_rpreason;
1264 }
1265 if (likely(HTTP_IS_SPHT(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1267 /* so it's a CR/LF, so there is no reason phrase */
1268 goto http_msg_rsp_reason;
1269
1270 http_msg_rpreason:
1271 case HTTP_MSG_RPREASON:
1272 if (likely(!HTTP_IS_CRLF(*ptr)))
1273 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1274 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1275 http_msg_rpline_eol:
1276 /* We have seen the end of line. Note that we do not
1277 * necessarily have the \n yet, but at least we know that we
1278 * have EITHER \r OR \n, otherwise the response would not be
1279 * complete. We can then record the response length and return
1280 * to the caller which will be able to register it.
1281 */
1282 msg->sl.st.l = ptr - msg->sol;
1283 return ptr;
1284
1285#ifdef DEBUG_FULL
1286 default:
1287 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1288 exit(1);
1289#endif
1290 }
1291
1292 http_msg_ood:
1293 /* out of data */
1294 if (ret_state)
1295 *ret_state = state;
1296 if (ret_ptr)
1297 *ret_ptr = (char *)ptr;
1298 return NULL;
1299
1300 http_msg_invalid:
1301 /* invalid message */
1302 if (ret_state)
1303 *ret_state = HTTP_MSG_ERROR;
1304 return NULL;
1305}
1306
1307
1308/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 * This function parses a request line between <ptr> and <end>, starting with
1310 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1311 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1312 * will give undefined results.
1313 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1314 * and that msg->sol points to the beginning of the request.
1315 * If a complete line is found (which implies that at least one CR or LF is
1316 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1317 * returned indicating an incomplete line (which does not mean that parts have
1318 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1319 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1320 * upon next call.
1321 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001322 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1324 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001325 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001326 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001327const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1328 unsigned int state, const char *ptr, const char *end,
1329 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 __label__
1332 http_msg_rqmeth,
1333 http_msg_rqmeth_sp,
1334 http_msg_rquri,
1335 http_msg_rquri_sp,
1336 http_msg_rqver,
1337 http_msg_rqline_eol,
1338 http_msg_ood, /* out of data */
1339 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 switch (state) {
1342 http_msg_rqmeth:
1343 case HTTP_MSG_RQMETH:
1344 if (likely(HTTP_IS_TOKEN(*ptr)))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001348 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 if (likely(HTTP_IS_CRLF(*ptr))) {
1353 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001354 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 http_msg_req09_uri:
1356 msg->sl.rq.u = ptr - msg_buf;
1357 http_msg_req09_uri_e:
1358 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1359 http_msg_req09_ver:
1360 msg->sl.rq.v = ptr - msg_buf;
1361 msg->sl.rq.v_l = 0;
1362 goto http_msg_rqline_eol;
1363 }
1364 goto http_msg_invalid;
1365
1366 http_msg_rqmeth_sp:
1367 case HTTP_MSG_RQMETH_SP:
1368 if (likely(!HTTP_IS_LWS(*ptr))) {
1369 msg->sl.rq.u = ptr - msg_buf;
1370 goto http_msg_rquri;
1371 }
1372 if (likely(HTTP_IS_SPHT(*ptr)))
1373 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1374 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1375 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 http_msg_rquri:
1378 case HTTP_MSG_RQURI:
1379 if (likely(!HTTP_IS_LWS(*ptr)))
1380 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 if (likely(HTTP_IS_SPHT(*ptr))) {
1383 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1384 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1388 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 http_msg_rquri_sp:
1391 case HTTP_MSG_RQURI_SP:
1392 if (likely(!HTTP_IS_LWS(*ptr))) {
1393 msg->sl.rq.v = ptr - msg_buf;
1394 goto http_msg_rqver;
1395 }
1396 if (likely(HTTP_IS_SPHT(*ptr)))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1398 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1399 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 http_msg_rqver:
1402 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001405
1406 if (likely(HTTP_IS_CRLF(*ptr))) {
1407 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1408 http_msg_rqline_eol:
1409 /* We have seen the end of line. Note that we do not
1410 * necessarily have the \n yet, but at least we know that we
1411 * have EITHER \r OR \n, otherwise the request would not be
1412 * complete. We can then record the request length and return
1413 * to the caller which will be able to register it.
1414 */
1415 msg->sl.rq.l = ptr - msg->sol;
1416 return ptr;
1417 }
1418
1419 /* neither an HTTP_VER token nor a CRLF */
1420 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422#ifdef DEBUG_FULL
1423 default:
1424 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1425 exit(1);
1426#endif
1427 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_ood:
1430 /* out of data */
1431 if (ret_state)
1432 *ret_state = state;
1433 if (ret_ptr)
1434 *ret_ptr = (char *)ptr;
1435 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 http_msg_invalid:
1438 /* invalid message */
1439 if (ret_state)
1440 *ret_state = HTTP_MSG_ERROR;
1441 return NULL;
1442}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
1444
Willy Tarreau8973c702007-01-21 23:58:29 +01001445/*
1446 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001447 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 * when data are missing and recalled at the exact same location with no
1449 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001450 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1451 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1454{
1455 __label__
1456 http_msg_rqbefore,
1457 http_msg_rqbefore_cr,
1458 http_msg_rqmeth,
1459 http_msg_rqline_end,
1460 http_msg_hdr_first,
1461 http_msg_hdr_name,
1462 http_msg_hdr_l1_sp,
1463 http_msg_hdr_l1_lf,
1464 http_msg_hdr_l1_lws,
1465 http_msg_hdr_val,
1466 http_msg_hdr_l2_lf,
1467 http_msg_hdr_l2_lws,
1468 http_msg_complete_header,
1469 http_msg_last_lf,
1470 http_msg_ood, /* out of data */
1471 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreaue69eada2008-01-27 00:34:10 +01001473 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001476 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 ptr = buf->lr;
1478 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 if (unlikely(ptr >= end))
1481 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 /*
1485 * First, states that are specific to the response only.
1486 * We check them first so that request and headers are
1487 * closer to each other (accessed more often).
1488 */
1489 http_msg_rpbefore:
1490 case HTTP_MSG_RPBEFORE:
1491 if (likely(HTTP_IS_TOKEN(*ptr))) {
1492 if (likely(ptr == buf->data)) {
1493 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001494 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 } else {
1496#if PARSE_PRESERVE_EMPTY_LINES
1497 /* only skip empty leading lines, don't remove them */
1498 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001499 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001500#else
1501 /* Remove empty leading lines, as recommended by
1502 * RFC2616. This takes a lot of time because we
1503 * must move all the buffer backwards, but this
1504 * is rarely needed. The method above will be
1505 * cleaner when we'll be able to start sending
1506 * the request from any place in the buffer.
1507 */
1508 buf->lr = ptr;
1509 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001510 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001511 msg->sol = buf->data;
1512 ptr = buf->data;
1513 end = buf->r;
1514#endif
1515 }
1516 hdr_idx_init(idx);
1517 state = HTTP_MSG_RPVER;
1518 goto http_msg_rpver;
1519 }
1520
1521 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1522 goto http_msg_invalid;
1523
1524 if (unlikely(*ptr == '\n'))
1525 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1526 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1527 /* stop here */
1528
1529 http_msg_rpbefore_cr:
1530 case HTTP_MSG_RPBEFORE_CR:
1531 EXPECT_LF_HERE(ptr, http_msg_invalid);
1532 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1533 /* stop here */
1534
1535 http_msg_rpver:
1536 case HTTP_MSG_RPVER:
1537 case HTTP_MSG_RPVER_SP:
1538 case HTTP_MSG_RPCODE:
1539 case HTTP_MSG_RPCODE_SP:
1540 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001541 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001542 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001543 if (unlikely(!ptr))
1544 return;
1545
1546 /* we have a full response and we know that we have either a CR
1547 * or an LF at <ptr>.
1548 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001549 //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 +01001550 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1551
1552 msg->sol = ptr;
1553 if (likely(*ptr == '\r'))
1554 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1555 goto http_msg_rpline_end;
1556
1557 http_msg_rpline_end:
1558 case HTTP_MSG_RPLINE_END:
1559 /* msg->sol must point to the first of CR or LF. */
1560 EXPECT_LF_HERE(ptr, http_msg_invalid);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1562 /* stop here */
1563
1564 /*
1565 * Second, states that are specific to the request only
1566 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 http_msg_rqbefore:
1568 case HTTP_MSG_RQBEFORE:
1569 if (likely(HTTP_IS_TOKEN(*ptr))) {
1570 if (likely(ptr == buf->data)) {
1571 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001572 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 } else {
1574#if PARSE_PRESERVE_EMPTY_LINES
1575 /* only skip empty leading lines, don't remove them */
1576 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 /* Remove empty leading lines, as recommended by
1580 * RFC2616. This takes a lot of time because we
1581 * must move all the buffer backwards, but this
1582 * is rarely needed. The method above will be
1583 * cleaner when we'll be able to start sending
1584 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 buf->lr = ptr;
1587 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 msg->sol = buf->data;
1590 ptr = buf->data;
1591 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001594 /* we will need this when keep-alive will be supported
1595 hdr_idx_init(idx);
1596 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001597 state = HTTP_MSG_RQMETH;
1598 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1602 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 if (unlikely(*ptr == '\n'))
1605 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1606 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 http_msg_rqbefore_cr:
1610 case HTTP_MSG_RQBEFORE_CR:
1611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 http_msg_rqmeth:
1616 case HTTP_MSG_RQMETH:
1617 case HTTP_MSG_RQMETH_SP:
1618 case HTTP_MSG_RQURI:
1619 case HTTP_MSG_RQURI_SP:
1620 case HTTP_MSG_RQVER:
1621 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001622 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(!ptr))
1624 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 /* we have a full request and we know that we have either a CR
1627 * or an LF at <ptr>.
1628 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001629 //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 +01001630 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 msg->sol = ptr;
1633 if (likely(*ptr == '\r'))
1634 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 http_msg_rqline_end:
1638 case HTTP_MSG_RQLINE_END:
1639 /* check for HTTP/0.9 request : no version information available.
1640 * msg->sol must point to the first of CR or LF.
1641 */
1642 if (unlikely(msg->sl.rq.v_l == 0))
1643 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 EXPECT_LF_HERE(ptr, http_msg_invalid);
1646 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001648
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 /*
1650 * Common states below
1651 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 http_msg_hdr_first:
1653 case HTTP_MSG_HDR_FIRST:
1654 msg->sol = ptr;
1655 if (likely(!HTTP_IS_CRLF(*ptr))) {
1656 goto http_msg_hdr_name;
1657 }
1658
1659 if (likely(*ptr == '\r'))
1660 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1661 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 http_msg_hdr_name:
1664 case HTTP_MSG_HDR_NAME:
1665 /* assumes msg->sol points to the first char */
1666 if (likely(HTTP_IS_TOKEN(*ptr)))
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 if (likely(*ptr == ':')) {
1670 msg->col = ptr - buf->data;
1671 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 http_msg_hdr_l1_sp:
1677 case HTTP_MSG_HDR_L1_SP:
1678 /* assumes msg->sol points to the first char and msg->col to the colon */
1679 if (likely(HTTP_IS_SPHT(*ptr)))
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 /* header value can be basically anything except CR/LF */
1683 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (likely(!HTTP_IS_CRLF(*ptr))) {
1686 goto http_msg_hdr_val;
1687 }
1688
1689 if (likely(*ptr == '\r'))
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1691 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 http_msg_hdr_l1_lf:
1694 case HTTP_MSG_HDR_L1_LF:
1695 EXPECT_LF_HERE(ptr, http_msg_invalid);
1696 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 http_msg_hdr_l1_lws:
1699 case HTTP_MSG_HDR_L1_LWS:
1700 if (likely(HTTP_IS_SPHT(*ptr))) {
1701 /* replace HT,CR,LF with spaces */
1702 for (; buf->data+msg->sov < ptr; msg->sov++)
1703 buf->data[msg->sov] = ' ';
1704 goto http_msg_hdr_l1_sp;
1705 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001706 /* we had a header consisting only in spaces ! */
1707 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 goto http_msg_complete_header;
1709
1710 http_msg_hdr_val:
1711 case HTTP_MSG_HDR_VAL:
1712 /* assumes msg->sol points to the first char, msg->col to the
1713 * colon, and msg->sov points to the first character of the
1714 * value.
1715 */
1716 if (likely(!HTTP_IS_CRLF(*ptr)))
1717 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 msg->eol = ptr;
1720 /* Note: we could also copy eol into ->eoh so that we have the
1721 * real header end in case it ends with lots of LWS, but is this
1722 * really needed ?
1723 */
1724 if (likely(*ptr == '\r'))
1725 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1726 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 http_msg_hdr_l2_lf:
1729 case HTTP_MSG_HDR_L2_LF:
1730 EXPECT_LF_HERE(ptr, http_msg_invalid);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 http_msg_hdr_l2_lws:
1734 case HTTP_MSG_HDR_L2_LWS:
1735 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1736 /* LWS: replace HT,CR,LF with spaces */
1737 for (; msg->eol < ptr; msg->eol++)
1738 *msg->eol = ' ';
1739 goto http_msg_hdr_val;
1740 }
1741 http_msg_complete_header:
1742 /*
1743 * It was a new header, so the last one is finished.
1744 * Assumes msg->sol points to the first char, msg->col to the
1745 * colon, msg->sov points to the first character of the value
1746 * and msg->eol to the first CR or LF so we know how the line
1747 * ends. We insert last header into the index.
1748 */
1749 /*
1750 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1751 write(2, msg->sol, msg->eol-msg->sol);
1752 fprintf(stderr,"\n");
1753 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1756 idx, idx->tail) < 0))
1757 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 http_msg_last_lf:
1769 case HTTP_MSG_LAST_LF:
1770 /* Assumes msg->sol points to the first of either CR or LF */
1771 EXPECT_LF_HERE(ptr, http_msg_invalid);
1772 ptr++;
1773 buf->lr = ptr;
1774 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001775 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 return;
1777#ifdef DEBUG_FULL
1778 default:
1779 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1780 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001781#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 }
1783 http_msg_ood:
1784 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001785 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 buf->lr = ptr;
1787 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 http_msg_invalid:
1790 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001791 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 return;
1793}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001794
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001795/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001796 * It normally returns zero, but may return 1 if it absolutely needs to be
1797 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001798 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001799 * functions. Its behaviour is rather simple :
1800 * - all enabled analysers are called in turn from the lower to the higher
1801 * bit.
1802 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02001803 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001804 * that unprocessed data will not be forwarded. But that probably depends on
1805 * the protocol. Generally it is not reset in case of errors.
1806 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02001807 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001808 * analysis).
1809 * - if an analyser thinks it has no added value anymore staying here, it must
1810 * reset its bit from the analysers flags in order not to be called anymore.
1811 *
1812 * In the future, analysers should be able to indicate that they want to be
1813 * called after XXX bytes have been received (or transfered), and the min of
1814 * all's wishes will be used to ring back (unless a special condition occurs).
1815 *
1816 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001818int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 struct buffer *req = t->req;
1821 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001822
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001823 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 +02001824 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001825 t,
1826 req,
1827 req->rex, req->wex,
1828 req->flags,
1829 req->l,
1830 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001831
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001832 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001833 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001834 struct tcp_rule *rule;
1835 int partial;
1836
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001837 /* We will abort if we encounter a read error. In theory, we
1838 * should not abort if we get a close, it might be valid,
1839 * although very unlikely. FIXME: we'll abort for now, this
1840 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001841 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001842 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001843 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001844 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001845 if (!(t->flags & SN_ERR_MASK))
1846 t->flags |= SN_ERR_CLICL;
1847 if (!(t->flags & SN_FINST_MASK))
1848 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001849 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001850 }
1851
1852 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001853 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001854 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001855 t->fe->failed_req++;
1856 if (!(t->flags & SN_ERR_MASK))
1857 t->flags |= SN_ERR_CLITO;
1858 if (!(t->flags & SN_FINST_MASK))
1859 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001860 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001861 }
1862
1863 /* We don't know whether we have enough data, so must proceed
1864 * this way :
1865 * - iterate through all rules in their declaration order
1866 * - if one rule returns MISS, it means the inspect delay is
1867 * not over yet, then return immediately, otherwise consider
1868 * it as a non-match.
1869 * - if one rule returns OK, then return OK
1870 * - if one rule returns KO, then return KO
1871 */
1872
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001873 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001874 partial = 0;
1875 else
1876 partial = ACL_PARTIAL;
1877
1878 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1879 int ret = ACL_PAT_PASS;
1880
1881 if (rule->cond) {
1882 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1883 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001884 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001885 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001886 if (!tick_isset(req->analyse_exp))
1887 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001888 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001889 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001890
Willy Tarreaub6866442008-07-14 23:54:42 +02001891 ret = acl_pass(ret);
1892 if (rule->cond->pol == ACL_COND_UNLESS)
1893 ret = !ret;
1894 }
1895
1896 if (ret) {
1897 /* we have a matching rule. */
1898 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001899 buffer_abort(req);
1900 buffer_abort(rep);
1901 //FIXME: this delete this
1902 //fd_delete(t->cli_fd);
1903 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001904 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001905 t->fe->failed_req++;
1906 if (!(t->flags & SN_ERR_MASK))
1907 t->flags |= SN_ERR_PRXCOND;
1908 if (!(t->flags & SN_FINST_MASK))
1909 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001910 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001911 }
1912 /* otherwise accept */
1913 break;
1914 }
1915 }
1916
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001917 /* if we get there, it means we have no rule which matches, or
1918 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001919 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001920 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001921 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001922 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001923
Willy Tarreau2df28e82008-08-17 15:20:19 +02001924 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 /*
1926 * Now parse the partial (or complete) lines.
1927 * We will check the request syntax, and also join multi-line
1928 * headers. An index of all the lines will be elaborated while
1929 * parsing.
1930 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001931 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001933 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001934 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 * req->data + req->eoh = end of processed headers / start of current one
1936 * req->data + req->eol = end of current header or line (LF or CRLF)
1937 * req->lr = first non-visited byte
1938 * req->r = end of data
1939 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001940
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001941 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001942 struct http_txn *txn = &t->txn;
1943 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001947 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001948
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 /* 1: we might have to print this header in debug mode */
1950 if (unlikely((global.mode & MODE_DEBUG) &&
1951 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001952 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001954
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001955 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 eol = sol + msg->sl.rq.l;
1957 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001958
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001959 sol += hdr_idx_first_pos(&txn->hdr_idx);
1960 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001961
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001963 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001965 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1966 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967 }
1968 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001969
Willy Tarreau58f10d72006-12-04 02:26:12 +01001970
1971 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001973 * If not so, we check the FD and buffer states before leaving.
1974 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1976 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001977 *
1978 */
1979
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001980 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001981 /*
1982 * First, let's catch bad requests.
1983 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001984 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001986
1987 /* 1: Since we are in header mode, if there's no space
1988 * left for headers, we won't be able to free more
1989 * later, so the session will never terminate. We
1990 * must terminate it now.
1991 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001992 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001993 /* FIXME: check if URI is set and return Status
1994 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001995 */
Willy Tarreau06619262006-12-17 08:37:22 +01001996 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001997 }
1998
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001999 /* 2: have we encountered a read error ? */
2000 else if (req->flags & BF_READ_ERROR) {
2001 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002002 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002003 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002004 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002005 if (!(t->flags & SN_ERR_MASK))
2006 t->flags |= SN_ERR_CLICL;
2007 if (!(t->flags & SN_FINST_MASK))
2008 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002009 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002010 }
2011
2012 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002013 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002014 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002015 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01002016 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002017 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002018 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002019 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002020 if (!(t->flags & SN_ERR_MASK))
2021 t->flags |= SN_ERR_CLITO;
2022 if (!(t->flags & SN_FINST_MASK))
2023 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002024 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002025 }
2026
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002027 /* 4: have we encountered a close ? */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002028 else if (req->flags & BF_SHUTR) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002029 txn->status = 400;
2030 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002031 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002032 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002033 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002034
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002035 if (!(t->flags & SN_ERR_MASK))
2036 t->flags |= SN_ERR_CLICL;
2037 if (!(t->flags & SN_FINST_MASK))
2038 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002039 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002040 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002041
Willy Tarreau3da77c52008-08-29 09:58:42 +02002042 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002043 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002044 if (!tick_isset(req->analyse_exp))
2045 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002046
2047 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02002048 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002049 }
2050
2051
2052 /****************************************************************
2053 * More interesting part now : we know that we have a complete *
2054 * request which at least looks like HTTP. We have an indicator *
2055 * of each header's length, so we can parse them quickly. *
2056 ****************************************************************/
2057
Willy Tarreau2df28e82008-08-17 15:20:19 +02002058 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002059 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002060
Willy Tarreau9cdde232007-05-02 20:58:19 +02002061 /* ensure we keep this pointer to the beginning of the message */
2062 msg->sol = req->data + msg->som;
2063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002064 /*
2065 * 1: identify the method
2066 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002067 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002068
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002069 /* we can make use of server redirect on GET and HEAD */
2070 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2071 t->flags |= SN_REDIRECTABLE;
2072
Willy Tarreau58f10d72006-12-04 02:26:12 +01002073 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002074 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01002075 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002076 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002077 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002078 if (unlikely((t->fe->monitor_uri_len != 0) &&
2079 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2080 !memcmp(&req->data[msg->sl.rq.u],
2081 t->fe->monitor_uri,
2082 t->fe->monitor_uri_len))) {
2083 /*
2084 * We have found the monitor URI
2085 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01002086 struct acl_cond *cond;
2087 cur_proxy = t->fe;
2088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002089 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002090
2091 /* Check if we want to fail this monitor request or not */
2092 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
2093 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002094
2095 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01002096 if (cond->pol == ACL_COND_UNLESS)
2097 ret = !ret;
2098
2099 if (ret) {
2100 /* we fail this request, let's return 503 service unavail */
2101 txn->status = 503;
2102 client_retnclose(t, error_message(t, HTTP_ERR_503));
2103 goto return_prx_cond;
2104 }
2105 }
2106
2107 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002108 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002109 client_retnclose(t, &http_200_chunk);
2110 goto return_prx_cond;
2111 }
2112
2113 /*
2114 * 3: Maybe we have to copy the original REQURI for the logs ?
2115 * Note: we cannot log anymore if the request has been
2116 * classified as invalid.
2117 */
2118 if (unlikely(t->logs.logwait & LW_REQ)) {
2119 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002120 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002121 int urilen = msg->sl.rq.l;
2122
2123 if (urilen >= REQURI_LEN)
2124 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002125 memcpy(txn->uri, &req->data[msg->som], urilen);
2126 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002127
2128 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002129 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130 } else {
2131 Alert("HTTP logging : out of memory.\n");
2132 }
2133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002134
Willy Tarreau06619262006-12-17 08:37:22 +01002135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002136 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2137 if (unlikely(msg->sl.rq.v_l == 0)) {
2138 int delta;
2139 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002140 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002141 cur_end = msg->sol + msg->sl.rq.l;
2142 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002144 if (msg->sl.rq.u_l == 0) {
2145 /* if no URI was set, add "/" */
2146 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2147 cur_end += delta;
2148 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002149 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002150 /* add HTTP version */
2151 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2152 msg->eoh += delta;
2153 cur_end += delta;
2154 cur_end = (char *)http_parse_reqline(msg, req->data,
2155 HTTP_MSG_RQMETH,
2156 msg->sol, cur_end + 1,
2157 NULL, NULL);
2158 if (unlikely(!cur_end))
2159 goto return_bad_req;
2160
2161 /* we have a full HTTP/1.0 request now and we know that
2162 * we have either a CR or an LF at <ptr>.
2163 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002164 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002165 }
2166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002167
2168 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002169 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002170 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002171 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002172
2173 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002174 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002175 * As opposed to version 1.2, now they will be evaluated in the
2176 * filters order and not in the header order. This means that
2177 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002178 *
2179 * We can now check whether we want to switch to another
2180 * backend, in which case we will re-check the backend's
2181 * filters and various options. In order to support 3-level
2182 * switching, here's how we should proceed :
2183 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002184 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002185 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002186 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002187 * There cannot be any switch from there, so ->be cannot be
2188 * changed anymore.
2189 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002190 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002191 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002192 * The response path will be able to apply either ->be, or
2193 * ->be then ->fe filters in order to match the reverse of
2194 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002195 */
2196
Willy Tarreau06619262006-12-17 08:37:22 +01002197 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002198 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002199 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002200 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002201 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002202
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002203 /* first check whether we have some ACLs set to redirect this request */
2204 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2205 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002206
2207 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002208 if (rule->cond->pol == ACL_COND_UNLESS)
2209 ret = !ret;
2210
2211 if (ret) {
2212 struct chunk rdr = { trash, 0 };
2213 const char *msg_fmt;
2214
2215 /* build redirect message */
2216 switch(rule->code) {
2217 case 303:
2218 rdr.len = strlen(HTTP_303);
2219 msg_fmt = HTTP_303;
2220 break;
2221 case 301:
2222 rdr.len = strlen(HTTP_301);
2223 msg_fmt = HTTP_301;
2224 break;
2225 case 302:
2226 default:
2227 rdr.len = strlen(HTTP_302);
2228 msg_fmt = HTTP_302;
2229 break;
2230 }
2231
2232 if (unlikely(rdr.len > sizeof(trash)))
2233 goto return_bad_req;
2234 memcpy(rdr.str, msg_fmt, rdr.len);
2235
2236 switch(rule->type) {
2237 case REDIRECT_TYPE_PREFIX: {
2238 const char *path;
2239 int pathlen;
2240
2241 path = http_get_path(txn);
2242 /* build message using path */
2243 if (path) {
2244 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2245 } else {
2246 path = "/";
2247 pathlen = 1;
2248 }
2249
2250 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2251 goto return_bad_req;
2252
2253 /* add prefix */
2254 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2255 rdr.len += rule->rdr_len;
2256
2257 /* add path */
2258 memcpy(rdr.str + rdr.len, path, pathlen);
2259 rdr.len += pathlen;
2260 break;
2261 }
2262 case REDIRECT_TYPE_LOCATION:
2263 default:
2264 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2265 goto return_bad_req;
2266
2267 /* add location */
2268 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2269 rdr.len += rule->rdr_len;
2270 break;
2271 }
2272
2273 /* add end of headers */
2274 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2275 rdr.len += 4;
2276
2277 txn->status = rule->code;
2278 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002279 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002280 client_retnclose(t, &rdr);
2281 goto return_prx_cond;
2282 }
2283 }
2284
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002285 /* first check whether we have some ACLs set to block this request */
2286 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002287 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002288
2289 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002290 if (cond->pol == ACL_COND_UNLESS)
2291 ret = !ret;
2292
2293 if (ret) {
2294 txn->status = 403;
2295 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002296 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002297 client_retnclose(t, error_message(t, HTTP_ERR_403));
2298 goto return_prx_cond;
2299 }
2300 }
2301
Willy Tarreau06619262006-12-17 08:37:22 +01002302 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002303 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002304 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2305 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002306 }
2307
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002308 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2309 /* to ensure correct connection accounting on
2310 * the backend, we count the connection for the
2311 * one managing the queue.
2312 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002313 t->be->beconn++;
2314 if (t->be->beconn > t->be->beconn_max)
2315 t->be->beconn_max = t->be->beconn;
2316 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002317 t->flags |= SN_BE_ASSIGNED;
2318 }
2319
Willy Tarreau06619262006-12-17 08:37:22 +01002320 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002321 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002322 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002323 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002324 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002325 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002326 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002327 goto return_prx_cond;
2328 }
2329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002330 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002331 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002332 !(t->flags & SN_CONN_CLOSED)) {
2333 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002334 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002336
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002337 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002338 old_idx = 0;
2339
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002340 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2341 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002342 cur_ptr = cur_next;
2343 cur_end = cur_ptr + cur_hdr->len;
2344 cur_next = cur_end + cur_hdr->cr + 1;
2345
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002346 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2347 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002348 /* 3 possibilities :
2349 * - we have already set Connection: close,
2350 * so we remove this line.
2351 * - we have not yet set Connection: close,
2352 * but this line indicates close. We leave
2353 * it untouched and set the flag.
2354 * - we have not yet set Connection: close,
2355 * and this line indicates non-close. We
2356 * replace it.
2357 */
2358 if (t->flags & SN_CONN_CLOSED) {
2359 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002360 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002361 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002362 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2363 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002364 cur_hdr->len = 0;
2365 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002366 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2367 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2368 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002369 cur_next += delta;
2370 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002371 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002372 }
2373 t->flags |= SN_CONN_CLOSED;
2374 }
2375 }
2376 old_idx = cur_idx;
2377 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002378 }
2379 /* add request headers from the rule sets in the same order */
2380 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2381 if (unlikely(http_header_add_tail(req,
2382 &txn->req,
2383 &txn->hdr_idx,
2384 rule_set->req_add[cur_idx])) < 0)
2385 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002386 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002388 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002389 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002390 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002391 /* we have to check the URI and auth for this request.
2392 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002393 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002394 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002395 if (stats_check_uri_auth(t, rule_set))
2396 return 1;
2397 }
2398
Willy Tarreau55ea7572007-06-17 19:56:27 +02002399 /* now check whether we have some switching rules for this request */
2400 if (!(t->flags & SN_BE_ASSIGNED)) {
2401 struct switching_rule *rule;
2402
2403 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2404 int ret;
2405
2406 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002407
2408 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002409 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002410 ret = !ret;
2411
2412 if (ret) {
2413 t->be = rule->be.backend;
2414 t->be->beconn++;
2415 if (t->be->beconn > t->be->beconn_max)
2416 t->be->beconn_max = t->be->beconn;
2417 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002418
2419 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002420 t->rep->rto = t->req->wto = t->be->timeout.server;
2421 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002422 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002423 t->flags |= SN_BE_ASSIGNED;
2424 break;
2425 }
2426 }
2427 }
2428
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002429 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2430 /* No backend was set, but there was a default
2431 * backend set in the frontend, so we use it and
2432 * loop again.
2433 */
2434 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002435 t->be->beconn++;
2436 if (t->be->beconn > t->be->beconn_max)
2437 t->be->beconn_max = t->be->beconn;
2438 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002439
2440 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002441 t->rep->rto = t->req->wto = t->be->timeout.server;
2442 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002443 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002444 t->flags |= SN_BE_ASSIGNED;
2445 }
2446 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002447
Willy Tarreau58f10d72006-12-04 02:26:12 +01002448
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002449 if (!(t->flags & SN_BE_ASSIGNED)) {
2450 /* To ensure correct connection accounting on
2451 * the backend, we count the connection for the
2452 * one managing the queue.
2453 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002454 t->be->beconn++;
2455 if (t->be->beconn > t->be->beconn_max)
2456 t->be->beconn_max = t->be->beconn;
2457 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002458 t->flags |= SN_BE_ASSIGNED;
2459 }
2460
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002461 /*
2462 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002463 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002464 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002465 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002466 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002467
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002468 /*
2469 * If HTTP PROXY is set we simply get remote server address
2470 * parsing incoming request.
2471 */
2472 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2473 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002475
Willy Tarreau2a324282006-12-05 00:05:46 +01002476 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002477 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002478 * so let's do the same now.
2479 */
2480
2481 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002482 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002483 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002484 }
2485
2486
2487 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002488 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002489 * Note that doing so might move headers in the request, but
2490 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002491 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002492 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002493 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2494 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002495 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002496
Willy Tarreau58f10d72006-12-04 02:26:12 +01002497
Willy Tarreau2a324282006-12-05 00:05:46 +01002498 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002499 * 9: add X-Forwarded-For if either the frontend or the backend
2500 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002501 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002502 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002503 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002504 /* Add an X-Forwarded-For header unless the source IP is
2505 * in the 'except' network range.
2506 */
2507 if ((!t->fe->except_mask.s_addr ||
2508 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2509 != t->fe->except_net.s_addr) &&
2510 (!t->be->except_mask.s_addr ||
2511 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2512 != t->be->except_net.s_addr)) {
2513 int len;
2514 unsigned char *pn;
2515 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002516
Ross Westaf72a1d2008-08-03 10:51:45 +02002517 /* Note: we rely on the backend to get the header name to be used for
2518 * x-forwarded-for, because the header is really meant for the backends.
2519 * However, if the backend did not specify any option, we have to rely
2520 * on the frontend's header name.
2521 */
2522 if (t->be->fwdfor_hdr_len) {
2523 len = t->be->fwdfor_hdr_len;
2524 memcpy(trash, t->be->fwdfor_hdr_name, len);
2525 } else {
2526 len = t->fe->fwdfor_hdr_len;
2527 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2528 }
2529 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002530
Ross Westaf72a1d2008-08-03 10:51:45 +02002531 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002532 &txn->hdr_idx, trash, len)) < 0)
2533 goto return_bad_req;
2534 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002535 }
2536 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002537 /* FIXME: for the sake of completeness, we should also support
2538 * 'except' here, although it is mostly useless in this case.
2539 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002540 int len;
2541 char pn[INET6_ADDRSTRLEN];
2542 inet_ntop(AF_INET6,
2543 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2544 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002545
2546 /* Note: we rely on the backend to get the header name to be used for
2547 * x-forwarded-for, because the header is really meant for the backends.
2548 * However, if the backend did not specify any option, we have to rely
2549 * on the frontend's header name.
2550 */
2551 if (t->be->fwdfor_hdr_len) {
2552 len = t->be->fwdfor_hdr_len;
2553 memcpy(trash, t->be->fwdfor_hdr_name, len);
2554 } else {
2555 len = t->fe->fwdfor_hdr_len;
2556 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2557 }
2558 len += sprintf(trash + len, ": %s", pn);
2559
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002560 if (unlikely(http_header_add_tail2(req, &txn->req,
2561 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002562 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002563 }
2564 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002565
Willy Tarreau2a324282006-12-05 00:05:46 +01002566 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002567 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002568 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002569 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002570 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002571 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002572 if ((unlikely(msg->sl.rq.v_l != 8) ||
2573 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2574 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002575 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002576 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002577 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002578 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002579 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2580 * If not assigned, perhaps we are balancing on url_param, but this is a
2581 * POST; and the parameters are in the body, maybe scan there to find our server.
2582 * (unless headers overflowed the buffer?)
2583 */
2584 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2585 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002586 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002587 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2588 /* are there enough bytes here? total == l || r || rlim ?
2589 * len is unsigned, but eoh is int,
2590 * how many bytes of body have we received?
2591 * eoh is the first empty line of the header
2592 */
2593 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002594 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002595
2596 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2597 * We can't assume responsibility for the server's decision,
2598 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2599 * We also can't change our mind later, about which server to choose, so round robin.
2600 */
2601 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2602 struct hdr_ctx ctx;
2603 ctx.idx = 0;
2604 /* Expect is allowed in 1.1, look for it */
2605 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2606 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002607 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002608 /* We can't reliablly stall and wait for data, because of
2609 * .NET clients that don't conform to rfc2616; so, no need for
2610 * the next block to check length expectations.
2611 * We could send 100 status back to the client, but then we need to
2612 * re-write headers, and send the message. And this isn't the right
2613 * place for that action.
2614 * TODO: support Expect elsewhere and delete this block.
2615 */
2616 goto end_check_maybe_wait_for_body;
2617 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002618
2619 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002620 /* nothing to do, we got enough */
2621 } else {
2622 /* limit implies we are supposed to need this many bytes
2623 * to find the parameter. Let's see how many bytes we can wait for.
2624 */
2625 long long hint = len;
2626 struct hdr_ctx ctx;
2627 ctx.idx = 0;
2628 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002629 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002630 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002631 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002632 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002633 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002634 ctx.idx = 0;
2635 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2636 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002637 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002638 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002639 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002640 hint = 0; /* parse failure, untrusted client */
2641 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002642 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002643 msg->hdr_content_len = hint;
2644 else
2645 hint = 0; /* bad client, sent negative length */
2646 }
2647 }
2648 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002649 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002650 hint = t->be->url_param_post_limit;
2651 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002652 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002653 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002654 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002655 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002656 /* else... There are no body bytes to wait for */
2657 }
2658 }
2659 }
2660 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661
Willy Tarreau2a324282006-12-05 00:05:46 +01002662 /*************************************************************
2663 * OK, that's finished for the headers. We have done what we *
2664 * could. Let's switch to the DATA state. *
2665 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002666
Willy Tarreaue393fe22008-08-16 22:18:07 +02002667 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002668 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002669
Willy Tarreau1fa31262007-12-03 00:36:16 +01002670 /* When a connection is tarpitted, we use the tarpit timeout,
2671 * which may be the same as the connect timeout if unspecified.
2672 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002673 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002674 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002675 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002676 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002677 /* flush the request so that we can drop the connection early
2678 * if the client closes first.
2679 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002680 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002681 req->analysers |= AN_REQ_HTTP_TARPIT;
2682 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2683 if (!req->analyse_exp)
2684 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002685 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002687 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002688 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002689
2690 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002691 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002692 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002693 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002694 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002695 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002696 return_prx_cond:
2697 if (!(t->flags & SN_ERR_MASK))
2698 t->flags |= SN_ERR_PRXCOND;
2699 if (!(t->flags & SN_FINST_MASK))
2700 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002701 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002702 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002703 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002704 }
2705
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002706 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2707 struct http_txn *txn = &t->txn;
2708
2709 /* This connection is being tarpitted. The CLIENT side has
2710 * already set the connect expiration date to the right
2711 * timeout. We just have to check that the client is still
2712 * there and that the timeout has not expired.
2713 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002714 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002715 !tick_is_expired(req->analyse_exp, now_ms))
2716 return 0;
2717
2718 /* We will set the queue timer to the time spent, just for
2719 * logging purposes. We fake a 500 server error, so that the
2720 * attacker will not suspect his connection has been tarpitted.
2721 * It will not cause trouble to the logs because we can exclude
2722 * the tarpitted connections by filtering on the 'PT' status flags.
2723 */
2724 trace_term(t, TT_HTTP_SRV_2);
2725 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2726
2727 txn->status = 500;
2728 if (req->flags != BF_READ_ERROR)
2729 client_retnclose(t, error_message(t, HTTP_ERR_500));
2730
2731 req->analysers = 0;
2732 req->analyse_exp = TICK_ETERNITY;
2733
2734 t->fe->failed_req++;
2735 if (!(t->flags & SN_ERR_MASK))
2736 t->flags |= SN_ERR_PRXCOND;
2737 if (!(t->flags & SN_FINST_MASK))
2738 t->flags |= SN_FINST_T;
2739 return 0;
2740 }
2741
Willy Tarreau2df28e82008-08-17 15:20:19 +02002742 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002743 /* We have to parse the HTTP request body to find any required data.
2744 * "balance url_param check_post" should have been the only way to get
2745 * into this. We were brought here after HTTP header analysis, so all
2746 * related structures are ready.
2747 */
2748 struct http_msg *msg = &t->txn.req;
2749 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2750 long long limit = t->be->url_param_post_limit;
2751 struct hdr_ctx ctx;
2752
2753 ctx.idx = 0;
2754
2755 /* now if we have a length, we'll take the hint */
2756 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2757 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2758 unsigned int chunk = 0;
2759 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2760 char c = msg->sol[body];
2761 if (ishex(c)) {
2762 unsigned int hex = toupper(c) - '0';
2763 if (hex > 9)
2764 hex -= 'A' - '9' - 1;
2765 chunk = (chunk << 4) | hex;
2766 } else
2767 break;
2768 body++;
2769 }
2770 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002771 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002772
2773 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002774 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002775
2776 body += 2; // skip CRLF
2777
2778 /* if we support more then one chunk here, we have to do it again when assigning server
2779 * 1. how much entity data do we have? new var
2780 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2781 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2782 */
2783
2784 if (chunk < limit)
2785 limit = chunk; /* only reading one chunk */
2786 } else {
2787 if (msg->hdr_content_len < limit)
2788 limit = msg->hdr_content_len;
2789 }
2790
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002791 http_body_end:
2792 /* we leave once we know we have nothing left to do. This means that we have
2793 * enough bytes, or that we know we'll not get any more (buffer full, read
2794 * buffer closed).
2795 */
2796 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002797 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002798 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002799 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002800 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002801 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002802 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002803 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002804 else {
2805 /* Not enough data. We'll re-use the http-request
2806 * timeout here. Ideally, we should set the timeout
2807 * relative to the accept() date. We just set the
2808 * request timeout once at the beginning of the
2809 * request.
2810 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002811 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02002812 if (!tick_isset(req->analyse_exp))
2813 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2814 return 0;
2815 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002816 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002817
Willy Tarreau2df28e82008-08-17 15:20:19 +02002818 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2819 * probably reduce one day's debugging session.
2820 */
2821#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002822 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002823 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2824 __FILE__, __LINE__, req->analysers);
2825 ABORT_NOW();
2826 }
2827#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002828 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002829 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002830}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002831
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002832/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002833 * It normally returns zero, but may return 1 if it absolutely needs to be
2834 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002835 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002836 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002837 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002838int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002839{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002840 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002841 struct buffer *req = t->req;
2842 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002843
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002844 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 +02002845 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002846 t,
2847 rep,
2848 rep->rex, rep->wex,
2849 rep->flags,
2850 rep->l,
2851 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002852
Willy Tarreau2df28e82008-08-17 15:20:19 +02002853 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002854 /*
2855 * Now parse the partial (or complete) lines.
2856 * We will check the response syntax, and also join multi-line
2857 * headers. An index of all the lines will be elaborated while
2858 * parsing.
2859 *
2860 * For the parsing, we use a 28 states FSM.
2861 *
2862 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002863 * rep->data + rep->som = beginning of response
2864 * rep->data + rep->eoh = end of processed headers / start of current one
2865 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 * rep->lr = first non-visited byte
2867 * rep->r = end of data
2868 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002870 int cur_idx;
2871 struct http_msg *msg = &txn->rsp;
2872 struct proxy *cur_proxy;
2873
2874 if (likely(rep->lr < rep->r))
2875 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2876
2877 /* 1: we might have to print this header in debug mode */
2878 if (unlikely((global.mode & MODE_DEBUG) &&
2879 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2880 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2881 char *eol, *sol;
2882
2883 sol = rep->data + msg->som;
2884 eol = sol + msg->sl.rq.l;
2885 debug_hdr("srvrep", t, sol, eol);
2886
2887 sol += hdr_idx_first_pos(&txn->hdr_idx);
2888 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2889
2890 while (cur_idx) {
2891 eol = sol + txn->hdr_idx.v[cur_idx].len;
2892 debug_hdr("srvhdr", t, sol, eol);
2893 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2894 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002898 /*
2899 * Now we quickly check if we have found a full valid response.
2900 * If not so, we check the FD and buffer states before leaving.
2901 * A full response is indicated by the fact that we have seen
2902 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2903 * responses are checked first.
2904 *
2905 * Depending on whether the client is still there or not, we
2906 * may send an error response back or not. Note that normally
2907 * we should only check for HTTP status there, and check I/O
2908 * errors somewhere else.
2909 */
2910
2911 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002912 /* Invalid response */
2913 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2914 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002915 //buffer_shutr(rep);
2916 //buffer_shutw(req);
2917 //fd_delete(req->cons->fd);
2918 //req->cons->state = SI_ST_CLO;
2919 buffer_shutr_now(rep);
2920 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002921 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002922 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002924 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002925 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002927 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002928 txn->status = 502;
2929 client_return(t, error_message(t, HTTP_ERR_502));
2930 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002931 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002932 if (!(t->flags & SN_FINST_MASK))
2933 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002934
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002935 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2936 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937
Willy Tarreaudafde432008-08-17 01:00:46 +02002938 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002939 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002940 /* too large response does not fit in buffer. */
2941 else if (rep->flags & BF_FULL) {
2942 goto hdr_response_bad;
2943 }
2944 /* read error */
2945 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002946 buffer_shutr_now(rep);
2947 buffer_shutw_now(req);
2948 //fd_delete(req->cons->fd);
2949 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002950 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002951 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002952 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002953 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002954 //}
2955 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002956 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002957 txn->status = 502;
2958 client_return(t, error_message(t, HTTP_ERR_502));
2959 if (!(t->flags & SN_ERR_MASK))
2960 t->flags |= SN_ERR_SRVCL;
2961 if (!(t->flags & SN_FINST_MASK))
2962 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002963
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002964 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2965 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002966
Willy Tarreaudafde432008-08-17 01:00:46 +02002967 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002968 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002969 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002970 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002971 buffer_shutr_now(rep);
2972 buffer_shutw_now(req);
2973 //fd_delete(req->cons->fd);
2974 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002975 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002976 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002978 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002979 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002980 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002981 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002982 txn->status = 504;
2983 client_return(t, error_message(t, HTTP_ERR_504));
2984 if (!(t->flags & SN_ERR_MASK))
2985 t->flags |= SN_ERR_SRVTO;
2986 if (!(t->flags & SN_FINST_MASK))
2987 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002988
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002989 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2990 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002991 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002992 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002993 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002994 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002995 buffer_shutr_now(rep);
2996 buffer_shutw_now(req);
2997 //fd_delete(req->cons->fd);
2998 //req->cons->state = SI_ST_CLO;
2999 if (t->srv) {
3000 //t->srv->cur_sess--;
3001 t->srv->failed_resp++;
3002 //sess_change_server(t, NULL);
3003 }
3004 t->be->failed_resp++;
3005 rep->analysers = 0;
3006 txn->status = 502;
3007 client_return(t, error_message(t, HTTP_ERR_502));
3008 if (!(t->flags & SN_ERR_MASK))
3009 t->flags |= SN_ERR_SRVCL;
3010 if (!(t->flags & SN_FINST_MASK))
3011 t->flags |= SN_FINST_H;
3012
3013 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3014 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003015
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003016 return 0;
3017 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003018 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003019 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021
Willy Tarreau21d2af32008-02-14 20:25:24 +01003022
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003023 /*****************************************************************
3024 * More interesting part now : we know that we have a complete *
3025 * response which at least looks like HTTP. We have an indicator *
3026 * of each header's length, so we can parse them quickly. *
3027 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01003028
Willy Tarreau2df28e82008-08-17 15:20:19 +02003029 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02003030
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003031 /* ensure we keep this pointer to the beginning of the message */
3032 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /*
3035 * 1: get the status code and check for cacheability.
3036 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01003037
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003038 t->logs.logwait &= ~LW_RESP;
3039 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003041 switch (txn->status) {
3042 case 200:
3043 case 203:
3044 case 206:
3045 case 300:
3046 case 301:
3047 case 410:
3048 /* RFC2616 @13.4:
3049 * "A response received with a status code of
3050 * 200, 203, 206, 300, 301 or 410 MAY be stored
3051 * by a cache (...) unless a cache-control
3052 * directive prohibits caching."
3053 *
3054 * RFC2616 @9.5: POST method :
3055 * "Responses to this method are not cacheable,
3056 * unless the response includes appropriate
3057 * Cache-Control or Expires header fields."
3058 */
3059 if (likely(txn->meth != HTTP_METH_POST) &&
3060 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3061 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3062 break;
3063 default:
3064 break;
3065 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003066
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003067 /*
3068 * 2: we may need to capture headers
3069 */
3070 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3071 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3072 txn->rsp.cap, t->fe->rsp_cap);
3073
3074 /*
3075 * 3: we will have to evaluate the filters.
3076 * As opposed to version 1.2, now they will be evaluated in the
3077 * filters order and not in the header order. This means that
3078 * each filter has to be validated among all headers.
3079 *
3080 * Filters are tried with ->be first, then with ->fe if it is
3081 * different from ->be.
3082 */
3083
3084 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3085
3086 cur_proxy = t->be;
3087 while (1) {
3088 struct proxy *rule_set = cur_proxy;
3089
3090 /* try headers filters */
3091 if (rule_set->rsp_exp != NULL) {
3092 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3093 return_bad_resp:
3094 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003095 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003096 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003097 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003098 }
3099 cur_proxy->failed_resp++;
3100 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003101 buffer_shutr_now(rep);
3102 buffer_shutw_now(req);
3103 //fd_delete(req->cons->fd);
3104 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003105 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003106 txn->status = 502;
3107 client_return(t, error_message(t, HTTP_ERR_502));
3108 if (!(t->flags & SN_ERR_MASK))
3109 t->flags |= SN_ERR_PRXCOND;
3110 if (!(t->flags & SN_FINST_MASK))
3111 t->flags |= SN_FINST_H;
3112 /* We used to have a free connection slot. Since we'll never use it,
3113 * we have to inform the server that it may be used by another session.
3114 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003115 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3116 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003117 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003118 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003119 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003121 /* has the response been denied ? */
3122 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003123 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003124 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003125 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003126 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003127 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 cur_proxy->denied_resp++;
3129 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003130 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003132 /* We might have to check for "Connection:" */
3133 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3134 !(t->flags & SN_CONN_CLOSED)) {
3135 char *cur_ptr, *cur_end, *cur_next;
3136 int cur_idx, old_idx, delta, val;
3137 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003139 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3140 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003141
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003142 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3143 cur_hdr = &txn->hdr_idx.v[cur_idx];
3144 cur_ptr = cur_next;
3145 cur_end = cur_ptr + cur_hdr->len;
3146 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003148 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3149 if (val) {
3150 /* 3 possibilities :
3151 * - we have already set Connection: close,
3152 * so we remove this line.
3153 * - we have not yet set Connection: close,
3154 * but this line indicates close. We leave
3155 * it untouched and set the flag.
3156 * - we have not yet set Connection: close,
3157 * and this line indicates non-close. We
3158 * replace it.
3159 */
3160 if (t->flags & SN_CONN_CLOSED) {
3161 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3162 txn->rsp.eoh += delta;
3163 cur_next += delta;
3164 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3165 txn->hdr_idx.used--;
3166 cur_hdr->len = 0;
3167 } else {
3168 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3169 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3170 "close", 5);
3171 cur_next += delta;
3172 cur_hdr->len += delta;
3173 txn->rsp.eoh += delta;
3174 }
3175 t->flags |= SN_CONN_CLOSED;
3176 }
3177 }
3178 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003179 }
3180 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003181
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003182 /* add response headers from the rule sets in the same order */
3183 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3184 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3185 rule_set->rsp_add[cur_idx])) < 0)
3186 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003187 }
3188
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003189 /* check whether we're already working on the frontend */
3190 if (cur_proxy == t->fe)
3191 break;
3192 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003193 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195 /*
3196 * 4: check for server cookie.
3197 */
3198 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3199 || (t->be->options & PR_O_CHK_CACHE))
3200 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202
Willy Tarreaua15645d2007-03-18 16:22:39 +01003203 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003204 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003205 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003206 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3207 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003208
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003209 /*
3210 * 6: add server cookie in the response if needed
3211 */
3212 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3213 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3214 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003215
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003216 /* the server is known, it's not the one the client requested, we have to
3217 * insert a set-cookie here, except if we want to insert only on POST
3218 * requests and this one isn't. Note that servers which don't have cookies
3219 * (eg: some backup servers) will return a full cookie removal request.
3220 */
3221 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3222 t->be->cookie_name,
3223 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003224
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003225 if (t->be->cookie_domain)
3226 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003227
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003228 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3229 trash, len)) < 0)
3230 goto return_bad_resp;
3231 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003232
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003233 /* Here, we will tell an eventual cache on the client side that we don't
3234 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3235 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3236 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3237 */
3238 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003240 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3241
3242 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3243 "Cache-control: private", 22)) < 0)
3244 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003245 }
3246 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003247
Willy Tarreaubaaee002006-06-26 02:48:02 +02003248
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003249 /*
3250 * 7: check if result will be cacheable with a cookie.
3251 * We'll block the response if security checks have caught
3252 * nasty things such as a cacheable cookie.
3253 */
3254 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3255 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3256 (t->be->options & PR_O_CHK_CACHE)) {
3257
3258 /* we're in presence of a cacheable response containing
3259 * a set-cookie header. We'll block it as requested by
3260 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003261 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003262 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003263 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003265 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003266 }
3267 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3270 t->be->id, t->srv?t->srv->id:"<dispatch>");
3271 send_log(t->be, LOG_ALERT,
3272 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3273 t->be->id, t->srv?t->srv->id:"<dispatch>");
3274 goto return_srv_prx_502;
3275 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003276
3277 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003278 * 8: add "Connection: close" if needed and not yet set.
3279 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003280 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003281 if (!(t->flags & SN_CONN_CLOSED) &&
3282 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3283 if ((unlikely(msg->sl.st.v_l != 8) ||
3284 unlikely(req->data[msg->som + 7] != '0')) &&
3285 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3286 "Connection: close", 17)) < 0)
3287 goto return_bad_resp;
3288 t->flags |= SN_CONN_CLOSED;
3289 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003290
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003291 /*************************************************************
3292 * OK, that's finished for the headers. We have done what we *
3293 * could. Let's switch to the DATA state. *
3294 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003295
Willy Tarreaue393fe22008-08-16 22:18:07 +02003296 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003297 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003298
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003299#ifdef CONFIG_HAP_TCPSPLICE
3300 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3301 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003302 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003303 }
3304#endif
3305 /* if the user wants to log as soon as possible, without counting
3306 * bytes from the server, then this is the right moment. We have
3307 * to temporarily assign bytes_out to log what we currently have.
3308 */
3309 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3310 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3311 t->logs.bytes_out = txn->rsp.eoh;
3312 if (t->fe->to_log & LW_REQ)
3313 http_sess_log(t);
3314 else
3315 tcp_sess_log(t);
3316 t->logs.bytes_out = 0;
3317 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003318
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003319 /* Note: we must not try to cheat by jumping directly to DATA,
3320 * otherwise we would not let the client side wake up.
3321 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003322
Willy Tarreaudafde432008-08-17 01:00:46 +02003323 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003324 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003325
Willy Tarreau2df28e82008-08-17 15:20:19 +02003326 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3327 * probably reduce one day's debugging session.
3328 */
3329#ifdef DEBUG_DEV
3330 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3331 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3332 __FILE__, __LINE__, rep->analysers);
3333 ABORT_NOW();
3334 }
3335#endif
3336 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003337 return 0;
3338}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003339
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003340///*
3341// * Manages the client FSM and its socket. It normally returns zero, but may
3342// * return 1 if it absolutely wants to be called again.
3343// *
3344// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3345// * but CL_STCLOSE.
3346// */
3347//int process_cli(struct session *t)
3348//{
3349// struct buffer *req = t->req;
3350// struct buffer *rep = t->rep;
3351//
3352// DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3353// now_ms, __FUNCTION__,
3354// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3355// cli_stnames[t->cli_state],
3356// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3357// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3358// req->rex, rep->wex,
3359// req->flags, rep->flags,
3360// req->l, rep->l);
3361//
3362// update_state:
3363// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3364// * still set this state (and will do until unix sockets are converted).
3365// */
3366// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3367// /* we can skip most of the tests at once if some conditions are not met */
3368// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3369// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3370// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3371// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3372// (!(rep->flags & BF_SHUTW) &&
3373// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3374// goto update_timeouts;
3375//
3376// /* read or write error */
3377// if (fdtab[t->cli_fd].state == FD_STERROR) {
3378// buffer_shutr(req);
3379// req->flags |= BF_READ_ERROR;
3380// buffer_shutw(rep);
3381// rep->flags |= BF_WRITE_ERROR;
3382// fd_delete(t->cli_fd);
3383// t->cli_state = CL_STCLOSE;
3384// trace_term(t, TT_HTTP_CLI_1);
3385// if (!req->analysers) {
3386// if (!(t->flags & SN_ERR_MASK))
3387// t->flags |= SN_ERR_CLICL;
3388// if (!(t->flags & SN_FINST_MASK)) {
3389// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3390// t->flags |= SN_FINST_Q;
3391// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3392// t->flags |= SN_FINST_C;
3393// else
3394// t->flags |= SN_FINST_D;
3395// }
3396// }
3397// goto update_state;
3398// }
3399// /* last read, or end of server write */
3400// else if (!(req->flags & BF_SHUTR) && /* not already done */
3401// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3402// buffer_shutr(req);
3403// if (!(rep->flags & BF_SHUTW)) {
3404// EV_FD_CLR(t->cli_fd, DIR_RD);
3405// trace_term(t, TT_HTTP_CLI_2);
3406// } else {
3407// /* output was already closed */
3408// fd_delete(t->cli_fd);
3409// t->cli_state = CL_STCLOSE;
3410// trace_term(t, TT_HTTP_CLI_3);
3411// }
3412// goto update_state;
3413// }
3414// /* last server read and buffer empty : we only check them when we're
3415// * allowed to forward the data.
3416// */
3417// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3418// ((rep->flags & BF_SHUTW_NOW) ||
3419// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3420// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3421// buffer_shutw(rep);
3422// if (!(req->flags & BF_SHUTR)) {
3423// EV_FD_CLR(t->cli_fd, DIR_WR);
3424// shutdown(t->cli_fd, SHUT_WR);
3425// trace_term(t, TT_HTTP_CLI_4);
3426// } else {
3427// fd_delete(t->cli_fd);
3428// t->cli_state = CL_STCLOSE;
3429// trace_term(t, TT_HTTP_CLI_5);
3430// }
3431// goto update_state;
3432// }
3433// /* read timeout */
3434// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3435// buffer_shutr(req);
3436// if (!(rep->flags & BF_SHUTW)) {
3437// EV_FD_CLR(t->cli_fd, DIR_RD);
3438// trace_term(t, TT_HTTP_CLI_6);
3439// } else {
3440// /* output was already closed */
3441// fd_delete(t->cli_fd);
3442// t->cli_state = CL_STCLOSE;
3443// trace_term(t, TT_HTTP_CLI_7);
3444// }
3445// if (!req->analysers) {
3446// if (!(t->flags & SN_ERR_MASK))
3447// t->flags |= SN_ERR_CLITO;
3448// if (!(t->flags & SN_FINST_MASK)) {
3449// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3450// t->flags |= SN_FINST_Q;
3451// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3452// t->flags |= SN_FINST_C;
3453// else
3454// t->flags |= SN_FINST_D;
3455// }
3456// }
3457// goto update_state;
3458// }
3459// /* write timeout */
3460// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3461// buffer_shutw(rep);
3462// if (!(req->flags & BF_SHUTR)) {
3463// EV_FD_CLR(t->cli_fd, DIR_WR);
3464// shutdown(t->cli_fd, SHUT_WR);
3465// trace_term(t, TT_HTTP_CLI_8);
3466// } else {
3467// fd_delete(t->cli_fd);
3468// t->cli_state = CL_STCLOSE;
3469// trace_term(t, TT_HTTP_CLI_9);
3470// }
3471// if (!req->analysers) {
3472// if (!(t->flags & SN_ERR_MASK))
3473// t->flags |= SN_ERR_CLITO;
3474// if (!(t->flags & SN_FINST_MASK)) {
3475// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3476// t->flags |= SN_FINST_Q;
3477// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3478// t->flags |= SN_FINST_C;
3479// else
3480// t->flags |= SN_FINST_D;
3481// }
3482// }
3483// goto update_state;
3484// }
3485//
3486// update_timeouts:
3487// /* manage read timeout */
3488// if (!(req->flags & BF_SHUTR)) {
3489// if (req->flags & BF_FULL) {
3490// /* no room to read more data */
3491// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3492// /* stop reading until we get some space */
3493// req->rex = TICK_ETERNITY;
3494// }
3495// } else {
3496// EV_FD_COND_S(t->cli_fd, DIR_RD);
3497// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3498// }
3499// }
3500//
3501// /* manage write timeout */
3502// if (!(rep->flags & BF_SHUTW)) {
3503// /* first, we may have to produce data (eg: stats).
3504// * right now, this is limited to the SHUTR state.
3505// */
3506// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3507// produce_content(t);
3508// if (rep->flags & BF_EMPTY) {
3509// buffer_shutw(rep);
3510// fd_delete(t->cli_fd);
3511// t->cli_state = CL_STCLOSE;
3512// trace_term(t, TT_HTTP_CLI_10);
3513// goto update_state;
3514// }
3515// }
3516//
3517// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3518// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3519// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3520// /* stop writing */
3521// rep->wex = TICK_ETERNITY;
3522// }
3523// } else {
3524// /* buffer not empty */
3525// EV_FD_COND_S(t->cli_fd, DIR_WR);
3526// if (!tick_isset(rep->wex)) {
3527// /* restart writing */
3528// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3529// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3530// /* FIXME: to prevent the client from expiring read timeouts during writes,
3531// * we refresh it, except if it was already infinite. */
3532// req->rex = rep->wex;
3533// }
3534// }
3535// }
3536// }
3537// return 0; /* other cases change nothing */
3538// }
3539// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3540// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3541// int len;
3542// len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd);
3543// write(1, trash, len);
3544// }
3545// return 0;
3546// }
3547//#ifdef DEBUG_DEV
3548// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3549// ABORT_NOW();
3550//#endif
3551// return 0;
3552//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003553
Willy Tarreaubaaee002006-06-26 02:48:02 +02003554
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003555/* Return 1 if the pending connection has failed and should be retried,
3556 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3557 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003558 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003559int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003560{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003561 struct buffer *req = t->req;
3562 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003563 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003564
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003565 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3566 now_ms, __FUNCTION__,
3567 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003569 req->flags, rep->flags,
3570 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003571
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003572 if ((req->flags & BF_SHUTW_NOW) ||
3573 (rep->flags & BF_SHUTW) ||
3574 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003575 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003576 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3577
3578 trace_term(t, TT_HTTP_SRV_5);
3579 req->wex = TICK_ETERNITY;
3580 fd_delete(req->cons->fd);
3581 if (t->srv) {
3582 t->srv->cur_sess--;
3583 sess_change_server(t, NULL);
3584 }
3585 /* note that this must not return any error because it would be able to
3586 * overwrite the client_retnclose() output.
3587 */
3588 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3589
3590 // FIXME: should we set rep->MAY_FORWARD ?
3591 buffer_shutw(req);
3592 buffer_shutr(rep);
3593 req->cons->state = SI_ST_CLO;
3594 if (!req->cons->err_type)
3595 req->cons->err_type = SI_ET_CONN_ABRT;
3596 req->cons->err_loc = t->srv;
3597 return 0;
3598 }
3599
3600 /* check for timeouts and asynchronous connect errors */
3601 if (fdtab[req->cons->fd].state == FD_STERROR) {
3602 conn_err = SI_ET_CONN_ERR;
3603 if (!req->cons->err_type)
3604 req->cons->err_type = SI_ET_CONN_ERR;
3605 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003606 else if (!(req->flags & BF_WRITE_ACTIVITY)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003607 /* nothing happened, maybe we timed out */
3608 if (tick_is_expired(req->wex, now_ms)) {
3609 conn_err = SI_ET_CONN_TO;
3610 if (!req->cons->err_type)
3611 req->cons->err_type = SI_ET_CONN_TO;
3612 }
3613 else
3614 return 0; /* let's wait a bit more */
3615 }
3616
3617 if (conn_err) {
3618 fd_delete(req->cons->fd);
3619 req->cons->state = SI_ST_CLO;
3620
3621 if (t->srv) {
3622 t->srv->cur_sess--;
3623 sess_change_server(t, NULL);
3624 req->cons->err_loc = t->srv;
3625 }
3626
3627 /* ensure that we have enough retries left */
3628 if (srv_count_retry_down(t, conn_err))
3629 return 0;
3630
3631 if (conn_err == SI_ET_CONN_ERR) {
3632 /* we encountered an immediate connection error, and we
3633 * will have to retry connecting to the same server, most
3634 * likely leading to the same result. To avoid this, we
3635 * fake a connection timeout to retry after a turn-around
3636 * time of 1 second. We will wait in the previous if block.
3637 */
3638 req->cons->state = SI_ST_TAR;
Willy Tarreau35374672008-09-03 18:11:02 +02003639 req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003640 return 0;
3641 }
3642
3643 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3644 /* We're on our last chance, and the REDISP option was specified.
3645 * We will ignore cookie and force to balance or use the dispatcher.
3646 */
3647 /* let's try to offer this slot to anybody */
3648 if (may_dequeue_tasks(t->srv, t->be))
3649 process_srv_queue(t->srv);
3650
3651 /* it's left to the dispatcher to choose a server */
3652 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3653 t->prev_srv = t->srv;
3654 } else {
3655 /* we just want to retry */
3656 if (t->srv)
3657 t->srv->retries++;
3658 t->be->retries++;
3659
3660 /* Now we will try to either reconnect to the same server or
3661 * connect to another server. If the connection gets queued
3662 * because all servers are saturated, then we will go back to
3663 * the idle state where the buffer's consumer is marked as
3664 * unknown.
3665 */
3666 if (srv_retryable_connect(t)) {
3667 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003668 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003669 return 0;
3670 }
3671 }
3672
3673 /* We'll rely on the caller to try to get a connection again */
3674 return 1;
3675 }
3676 else {
3677 /* no error and write OK : connection succeeded */
3678 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3679 req->cons->state = SI_ST_EST;
3680 req->cons->err_type = SI_ET_NONE;
3681 req->cons->err_loc = NULL;
3682
3683 if (req->flags & BF_EMPTY) {
3684 EV_FD_CLR(req->cons->fd, DIR_WR);
3685 req->wex = TICK_ETERNITY;
3686 } else {
3687 EV_FD_SET(req->cons->fd, DIR_WR);
3688 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3689 if (tick_isset(req->wex)) {
3690 /* FIXME: to prevent the server from expiring read timeouts during writes,
3691 * we refresh it. */
3692 rep->rex = req->wex;
3693 }
3694 }
3695
3696 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3697 if (!(rep->flags & BF_HIJACK)) {
3698 EV_FD_SET(req->cons->fd, DIR_RD);
3699 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3700 }
3701 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3702
3703 /* if the user wants to log as soon as possible, without counting
3704 bytes from the server, then this is the right moment. */
3705 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3706 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3707 tcp_sess_log(t);
3708 }
3709#ifdef CONFIG_HAP_TCPSPLICE
3710 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3711 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003712 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003713 }
3714#endif
3715 }
3716 else {
3717 rep->analysers |= AN_RTR_HTTP_HDR;
3718 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3719 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3720 /* reset hdr_idx which was already initialized by the request.
3721 * right now, the http parser does it.
3722 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003723 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003724 }
3725
Willy Tarreau9a2d1542008-08-30 12:31:07 +02003726 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003727 req->wex = TICK_ETERNITY;
3728 return 0;
3729 }
3730}
3731
3732
3733/*
3734 * This function tries to assign a server to a stream_sock interface.
3735 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3736 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3737 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3738 * it returns SI_ST_ASS, otherwise zero.
3739 */
3740int stream_sock_assign_server(struct session *t)
3741{
3742 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3743 now_ms, __FUNCTION__,
3744 cli_stnames[t->cli_state],
3745 t->rep->rex, t->req->wex,
3746 t->req->flags, t->rep->flags,
3747 t->req->l, t->rep->l);
3748
3749 if (t->req->cons->state == SI_ST_TAR) {
3750 /* connection might be aborted */
3751 if ((t->req->flags & BF_SHUTW_NOW) ||
3752 (t->rep->flags & BF_SHUTW) ||
3753 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3754 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003755
Willy Tarreauf8533202008-08-16 14:55:08 +02003756 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003757 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003758
3759 // FIXME: should we set rep->MAY_FORWARD ?
3760 buffer_shutr(t->rep);
3761 buffer_shutw(t->req);
3762 if (!t->req->cons->err_type)
3763 t->req->cons->err_type = SI_ET_CONN_ABRT;
3764 t->req->cons->state = SI_ST_CLO;
3765 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003766 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003767
Willy Tarreau35374672008-09-03 18:11:02 +02003768 if (!tick_is_expired(t->req->cons->exp, now_ms))
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003769 return 0; /* still in turn-around */
3770
3771 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003772 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003773 }
3774 else if (t->req->cons->state == SI_ST_QUE) {
3775 if (t->pend_pos) {
3776 /* request still in queue... */
Willy Tarreau35374672008-09-03 18:11:02 +02003777 if (tick_is_expired(t->req->cons->exp, now_ms)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003778 /* ... and timeout expired */
3779 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreau35374672008-09-03 18:11:02 +02003780 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003781 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003782 if (t->srv)
3783 t->srv->failed_conns++;
3784 t->be->failed_conns++;
3785
3786 // FIXME: should we set rep->MAY_FORWARD ?
3787 buffer_shutr(t->rep);
3788 buffer_shutw(t->req);
3789 t->req->flags |= BF_WRITE_TIMEOUT;
3790 if (!t->req->cons->err_type)
3791 t->req->cons->err_type = SI_ET_QUEUE_TO;
3792 t->req->cons->state = SI_ST_CLO;
3793 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003794 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003795 /* connection remains in queue, check if we have to abort it */
3796 if ((t->req->flags & BF_SHUTW_NOW) ||
3797 (t->rep->flags & BF_SHUTW) ||
3798 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3799 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3800 /* give up */
3801 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003802 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003803 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003804
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003805 // FIXME: should we set rep->MAY_FORWARD ?
3806 buffer_shutr(t->rep);
3807 buffer_shutw(t->req);
3808 if (!t->req->cons->err_type)
3809 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3810 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003811 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003812 return 0;
3813 }
3814 /* The connection is not in the queue anymore */
3815 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003816 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003817 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003818
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003819 /* we may get here from above */
3820 if (t->req->cons->state == SI_ST_INI) {
3821 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003822
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003823 /* first, check if the connection has been aborted */
3824 if ((t->req->flags & BF_SHUTW_NOW) ||
3825 (t->rep->flags & BF_SHUTW) ||
3826 ((t->req->flags & BF_SHUTR) &&
3827 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003828
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003829 trace_term(t, TT_HTTP_SRV_1);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003830
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003831 // FIXME: should we set rep->MAY_FORWARD ?
3832 buffer_shutr(t->rep);
3833 buffer_shutw(t->req);
3834 if (!t->req->cons->err_type)
3835 t->req->cons->err_type = SI_ET_CONN_ABRT;
3836 t->req->cons->state = SI_ST_CLO;
3837 return 0;
3838 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003839
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003840 /* try to get a server assigned */
3841 if (srv_redispatch_connect(t) != 0) {
3842 /* we did not get any server, let's check the cause */
3843 if (t->req->cons->state == SI_ST_QUE) {
3844 /* the connection was queued, that's OK */
3845 return 0;
3846 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003847
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003848 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003849
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003850 // FIXME: should we set rep->MAY_FORWARD ?
3851 buffer_shutr(t->rep);
3852 buffer_shutw(t->req);
3853 t->req->flags |= BF_WRITE_ERROR;
3854 if (!t->req->cons->err_type)
3855 t->req->cons->err_type = SI_ET_CONN_OTHER;
3856 t->req->cons->state = SI_ST_CLO;
3857 return 0;
3858 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003859
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003860 t->req->cons->state = SI_ST_ASS;
3861 /* Once the server is assigned, we have to return because
3862 * the caller might be interested in checking several
3863 * things before connecting.
3864 */
3865 return 1;
3866 }
3867 return 0;
3868}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003869
Willy Tarreauf8533202008-08-16 14:55:08 +02003870
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003871/*
3872 * This function tries to establish a connection to an assigned server. It also
3873 * performs connection retries. It may only be called with t->req->cons->state
3874 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3875 * SI_ST_EST, or SI_ST_CLO.
3876 */
3877int stream_sock_connect_server(struct session *t)
3878{
3879 if (t->req->cons->state == SI_ST_ASS) {
3880 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003881
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003882 if (!srv_retryable_connect(t)) {
3883 /* we need to redispatch */
3884 t->req->cons->state = SI_ST_INI;
3885 return 0;
3886 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003887
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003888 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3889 if (t->req->cons->state != SI_ST_CON) {
3890 /* it was an error */
3891 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003892
3893 // FIXME: should we set rep->MAY_FORWARD ?
3894 buffer_shutr(t->rep);
3895 buffer_shutw(t->req);
3896 t->req->flags |= BF_WRITE_ERROR;
3897 if (!t->req->cons->err_type)
3898 t->req->cons->err_type = SI_ET_CONN_OTHER;
3899 t->req->cons->state = SI_ST_CLO;
3900 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003901 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003902 /* We have a socket and switched to SI_ST_CON */
3903 }
3904
3905 /* we may also get here from above */
3906 if (t->req->cons->state == SI_ST_CON) {
3907 /* connection in progress or just completed */
3908 if (!tcp_connection_status(t))
3909 return 0;
3910 }
3911 return 0;
3912}
3913
3914
3915/*
3916 * Tries to establish a connection to the server and associate it to the
3917 * request buffer's consumer side. It is assumed that this function will not be
Willy Tarreau3da77c52008-08-29 09:58:42 +02003918 * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003919 * returns zero, but may return 1 if it absolutely wants to be called again.
3920 */
3921int process_srv_conn(struct session *t)
3922{
3923 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3924 now_ms, __FUNCTION__,
3925 cli_stnames[t->cli_state],
3926 t->rep->rex, t->req->wex,
3927 t->req->flags, t->rep->flags,
3928 t->req->l, t->rep->l);
3929
3930 do {
3931 if (t->req->cons->state == SI_ST_INI ||
3932 t->req->cons->state == SI_ST_TAR ||
3933 t->req->cons->state == SI_ST_QUE) {
3934 /* try to assign a server */
3935 if (!stream_sock_assign_server(t))
3936 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003937 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003938
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003939 if (t->req->cons->state == SI_ST_ASS &&
3940 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3941 /* Server supporting redirection and it is possible.
3942 * Invalid requests are reported as such. It concerns all
3943 * the largest ones.
3944 */
3945 struct http_txn *txn = &t->txn;
3946 struct chunk rdr;
3947 char *path;
3948 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003949
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003950 /* 1: create the response header */
3951 rdr.len = strlen(HTTP_302);
3952 rdr.str = trash;
3953 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003954
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003955 /* 2: add the server's prefix */
3956 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3957 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003958
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003959 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3960 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003961
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003962 /* 3: add the request URI */
3963 path = http_get_path(txn);
3964 if (!path)
3965 goto cancel_redir;
3966 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3967 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3968 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003969
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003970 memcpy(rdr.str + rdr.len, path, len);
3971 rdr.len += len;
3972 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3973 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003974
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003975 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3976 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003977
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003978 /* FIXME: we should increase a counter of redirects per server and per backend. */
3979 if (t->srv)
3980 t->srv->cum_sess++;
3981
3982 t->req->cons->state = SI_ST_CLO;
3983 return 0;
3984 cancel_redir:
3985 //txn->status = 400;
3986 //t->fe->failed_req++;
3987 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3988 // 400, error_message(t, HTTP_ERR_400));
3989 trace_term(t, TT_HTTP_SRV_4);
3990
3991 // FIXME: should we set rep->MAY_FORWARD ?
3992 buffer_shutw(t->req);
3993 buffer_shutr(t->rep);
3994 if (!t->req->cons->err_type)
3995 t->req->cons->err_type = SI_ET_CONN_OTHER;
3996 t->req->cons->state = SI_ST_CLO;
3997 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003998 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003999
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004000 if (t->req->cons->state == SI_ST_CON ||
4001 t->req->cons->state == SI_ST_ASS) {
4002 stream_sock_connect_server(t);
4003 }
4004 } while (t->req->cons->state != SI_ST_CLO &&
4005 t->req->cons->state != SI_ST_CON &&
4006 t->req->cons->state != SI_ST_EST);
4007 return 0;
4008}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004009
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004010
Willy Tarreaubaaee002006-06-26 02:48:02 +02004011/*
4012 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004013 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02004014 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
4015 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004016 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004017 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004018 */
4019int produce_content(struct session *s)
4020{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004021 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02004022 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004023 return 1;
4024 }
4025 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004026 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004027 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004028 if (ret >= 0)
4029 return ret;
4030 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004031 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004032
Willy Tarreau91861262007-10-17 17:06:05 +02004033 /* unknown data source or internal error */
4034 s->txn.status = 500;
4035 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004036 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004037 if (!(s->flags & SN_ERR_MASK))
4038 s->flags |= SN_ERR_PRXCOND;
4039 if (!(s->flags & SN_FINST_MASK))
4040 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02004041 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02004042 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004043}
4044
4045
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046/* Iterate the same filter through all request headers.
4047 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004048 * Since it can manage the switch to another backend, it updates the per-proxy
4049 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004050 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004051int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004053 char term;
4054 char *cur_ptr, *cur_end, *cur_next;
4055 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004056 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004057 struct hdr_idx_elem *cur_hdr;
4058 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004060 last_hdr = 0;
4061
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004062 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 old_idx = 0;
4064
4065 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004066 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004068 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004069 (exp->action == ACT_ALLOW ||
4070 exp->action == ACT_DENY ||
4071 exp->action == ACT_TARPIT))
4072 return 0;
4073
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004074 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004075 if (!cur_idx)
4076 break;
4077
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004078 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004079 cur_ptr = cur_next;
4080 cur_end = cur_ptr + cur_hdr->len;
4081 cur_next = cur_end + cur_hdr->cr + 1;
4082
4083 /* Now we have one header between cur_ptr and cur_end,
4084 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004085 */
4086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004087 /* The annoying part is that pattern matching needs
4088 * that we modify the contents to null-terminate all
4089 * strings before testing them.
4090 */
4091
4092 term = *cur_end;
4093 *cur_end = '\0';
4094
4095 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4096 switch (exp->action) {
4097 case ACT_SETBE:
4098 /* It is not possible to jump a second time.
4099 * FIXME: should we return an HTTP/500 here so that
4100 * the admin knows there's a problem ?
4101 */
4102 if (t->be != t->fe)
4103 break;
4104
4105 /* Swithing Proxy */
4106 t->be = (struct proxy *) exp->replace;
4107
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004108 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004109 * because we have associated req_cap and rsp_cap to the
4110 * frontend, and the beconn will be updated later.
4111 */
4112
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004113 t->rep->rto = t->req->wto = t->be->timeout.server;
4114 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004115 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004116 last_hdr = 1;
4117 break;
4118
4119 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004120 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004121 last_hdr = 1;
4122 break;
4123
4124 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004125 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004126 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004127 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004128 break;
4129
4130 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004131 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004132 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004133 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 break;
4135
4136 case ACT_REPLACE:
4137 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4138 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4139 /* FIXME: if the user adds a newline in the replacement, the
4140 * index will not be recalculated for now, and the new line
4141 * will not be counted as a new header.
4142 */
4143
4144 cur_end += delta;
4145 cur_next += delta;
4146 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004147 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004148 break;
4149
4150 case ACT_REMOVE:
4151 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4152 cur_next += delta;
4153
4154 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004155 txn->req.eoh += delta;
4156 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4157 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004158 cur_hdr->len = 0;
4159 cur_end = NULL; /* null-term has been rewritten */
4160 break;
4161
4162 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 if (cur_end)
4165 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167 /* keep the link from this header to next one in case of later
4168 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004169 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004170 old_idx = cur_idx;
4171 }
4172 return 0;
4173}
4174
4175
4176/* Apply the filter to the request line.
4177 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4178 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179 * Since it can manage the switch to another backend, it updates the per-proxy
4180 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004181 */
4182int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4183{
4184 char term;
4185 char *cur_ptr, *cur_end;
4186 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004187 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004188 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004189
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190
Willy Tarreau3d300592007-03-18 18:34:41 +01004191 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004192 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004193 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004194 (exp->action == ACT_ALLOW ||
4195 exp->action == ACT_DENY ||
4196 exp->action == ACT_TARPIT))
4197 return 0;
4198 else if (exp->action == ACT_REMOVE)
4199 return 0;
4200
4201 done = 0;
4202
Willy Tarreau9cdde232007-05-02 20:58:19 +02004203 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004204 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004205
4206 /* Now we have the request line between cur_ptr and cur_end */
4207
4208 /* The annoying part is that pattern matching needs
4209 * that we modify the contents to null-terminate all
4210 * strings before testing them.
4211 */
4212
4213 term = *cur_end;
4214 *cur_end = '\0';
4215
4216 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4217 switch (exp->action) {
4218 case ACT_SETBE:
4219 /* It is not possible to jump a second time.
4220 * FIXME: should we return an HTTP/500 here so that
4221 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004223 if (t->be != t->fe)
4224 break;
4225
4226 /* Swithing Proxy */
4227 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004229 /* right now, the backend switch is not too much complicated
4230 * because we have associated req_cap and rsp_cap to the
4231 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004232 */
4233
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004234 t->rep->rto = t->req->wto = t->be->timeout.server;
4235 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004236 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004237 done = 1;
4238 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004240 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004241 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004242 done = 1;
4243 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004245 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004246 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004247 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004248 done = 1;
4249 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004250
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004251 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004252 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004253 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004254 done = 1;
4255 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004256
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004257 case ACT_REPLACE:
4258 *cur_end = term; /* restore the string terminator */
4259 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4260 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4261 /* FIXME: if the user adds a newline in the replacement, the
4262 * index will not be recalculated for now, and the new line
4263 * will not be counted as a new header.
4264 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004265
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004266 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004267 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004268
Willy Tarreau9cdde232007-05-02 20:58:19 +02004269 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004270 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004271 HTTP_MSG_RQMETH,
4272 cur_ptr, cur_end + 1,
4273 NULL, NULL);
4274 if (unlikely(!cur_end))
4275 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004277 /* we have a full request and we know that we have either a CR
4278 * or an LF at <ptr>.
4279 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004280 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4281 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004282 /* there is no point trying this regex on headers */
4283 return 1;
4284 }
4285 }
4286 *cur_end = term; /* restore the string terminator */
4287 return done;
4288}
Willy Tarreau97de6242006-12-27 17:18:38 +01004289
Willy Tarreau58f10d72006-12-04 02:26:12 +01004290
Willy Tarreau58f10d72006-12-04 02:26:12 +01004291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004292/*
4293 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4294 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004295 * unparsable request. Since it can manage the switch to another backend, it
4296 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004297 */
4298int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4299{
Willy Tarreau3d300592007-03-18 18:34:41 +01004300 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004301 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004302 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004303 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004305 /*
4306 * The interleaving of transformations and verdicts
4307 * makes it difficult to decide to continue or stop
4308 * the evaluation.
4309 */
4310
Willy Tarreau3d300592007-03-18 18:34:41 +01004311 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004312 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4313 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4314 exp = exp->next;
4315 continue;
4316 }
4317
4318 /* Apply the filter to the request line. */
4319 ret = apply_filter_to_req_line(t, req, exp);
4320 if (unlikely(ret < 0))
4321 return -1;
4322
4323 if (likely(ret == 0)) {
4324 /* The filter did not match the request, it can be
4325 * iterated through all headers.
4326 */
4327 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004328 }
4329 exp = exp->next;
4330 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004331 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004332}
4333
4334
Willy Tarreaua15645d2007-03-18 16:22:39 +01004335
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004337 * Manage client-side cookie. It can impact performance by about 2% so it is
4338 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004339 */
4340void manage_client_side_cookies(struct session *t, struct buffer *req)
4341{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004342 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004343 char *p1, *p2, *p3, *p4;
4344 char *del_colon, *del_cookie, *colon;
4345 int app_cookies;
4346
4347 appsess *asession_temp = NULL;
4348 appsess local_asession;
4349
4350 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004351 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352
Willy Tarreau2a324282006-12-05 00:05:46 +01004353 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354 * we start with the start line.
4355 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004356 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004357 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004358
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004359 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004360 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004361 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004362
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004363 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004364 cur_ptr = cur_next;
4365 cur_end = cur_ptr + cur_hdr->len;
4366 cur_next = cur_end + cur_hdr->cr + 1;
4367
4368 /* We have one full header between cur_ptr and cur_end, and the
4369 * next header starts at cur_next. We're only interested in
4370 * "Cookie:" headers.
4371 */
4372
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004373 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4374 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375 old_idx = cur_idx;
4376 continue;
4377 }
4378
4379 /* Now look for cookies. Conforming to RFC2109, we have to support
4380 * attributes whose name begin with a '$', and associate them with
4381 * the right cookie, if we want to delete this cookie.
4382 * So there are 3 cases for each cookie read :
4383 * 1) it's a special attribute, beginning with a '$' : ignore it.
4384 * 2) it's a server id cookie that we *MAY* want to delete : save
4385 * some pointers on it (last semi-colon, beginning of cookie...)
4386 * 3) it's an application cookie : we *MAY* have to delete a previous
4387 * "special" cookie.
4388 * At the end of loop, if a "special" cookie remains, we may have to
4389 * remove it. If no application cookie persists in the header, we
4390 * *MUST* delete it
4391 */
4392
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004393 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004394
Willy Tarreau58f10d72006-12-04 02:26:12 +01004395 /* del_cookie == NULL => nothing to be deleted */
4396 del_colon = del_cookie = NULL;
4397 app_cookies = 0;
4398
4399 while (p1 < cur_end) {
4400 /* skip spaces and colons, but keep an eye on these ones */
4401 while (p1 < cur_end) {
4402 if (*p1 == ';' || *p1 == ',')
4403 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004404 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 break;
4406 p1++;
4407 }
4408
4409 if (p1 == cur_end)
4410 break;
4411
4412 /* p1 is at the beginning of the cookie name */
4413 p2 = p1;
4414 while (p2 < cur_end && *p2 != '=')
4415 p2++;
4416
4417 if (p2 == cur_end)
4418 break;
4419
4420 p3 = p2 + 1; /* skips the '=' sign */
4421 if (p3 == cur_end)
4422 break;
4423
4424 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004425 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 p4++;
4427
4428 /* here, we have the cookie name between p1 and p2,
4429 * and its value between p3 and p4.
4430 * we can process it :
4431 *
4432 * Cookie: NAME=VALUE;
4433 * | || || |
4434 * | || || +--> p4
4435 * | || |+-------> p3
4436 * | || +--------> p2
4437 * | |+------------> p1
4438 * | +-------------> colon
4439 * +--------------------> cur_ptr
4440 */
4441
4442 if (*p1 == '$') {
4443 /* skip this one */
4444 }
4445 else {
4446 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004447 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004448 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004449 (p4 - p1 >= t->fe->capture_namelen) &&
4450 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004451 int log_len = p4 - p1;
4452
Willy Tarreau086b3b42007-05-13 21:45:51 +02004453 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004454 Alert("HTTP logging : out of memory.\n");
4455 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004456 if (log_len > t->fe->capture_len)
4457 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004458 memcpy(txn->cli_cookie, p1, log_len);
4459 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004460 }
4461 }
4462
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004463 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4464 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004465 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004466 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004467 char *delim;
4468
4469 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4470 * have the server ID betweek p3 and delim, and the original cookie between
4471 * delim+1 and p4. Otherwise, delim==p4 :
4472 *
4473 * Cookie: NAME=SRV~VALUE;
4474 * | || || | |
4475 * | || || | +--> p4
4476 * | || || +--------> delim
4477 * | || |+-----------> p3
4478 * | || +------------> p2
4479 * | |+----------------> p1
4480 * | +-----------------> colon
4481 * +------------------------> cur_ptr
4482 */
4483
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004484 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004485 for (delim = p3; delim < p4; delim++)
4486 if (*delim == COOKIE_DELIM)
4487 break;
4488 }
4489 else
4490 delim = p4;
4491
4492
4493 /* Here, we'll look for the first running server which supports the cookie.
4494 * This allows to share a same cookie between several servers, for example
4495 * to dedicate backup servers to specific servers only.
4496 * However, to prevent clients from sticking to cookie-less backup server
4497 * when they have incidentely learned an empty cookie, we simply ignore
4498 * empty cookies and mark them as invalid.
4499 */
4500 if (delim == p3)
4501 srv = NULL;
4502
4503 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004504 if (srv->cookie && (srv->cklen == delim - p3) &&
4505 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004506 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004507 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004508 txn->flags &= ~TX_CK_MASK;
4509 txn->flags |= TX_CK_VALID;
4510 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004511 t->srv = srv;
4512 break;
4513 } else {
4514 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004515 txn->flags &= ~TX_CK_MASK;
4516 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004517 }
4518 }
4519 srv = srv->next;
4520 }
4521
Willy Tarreau3d300592007-03-18 18:34:41 +01004522 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004524 txn->flags &= ~TX_CK_MASK;
4525 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004526 }
4527
4528 /* depending on the cookie mode, we may have to either :
4529 * - delete the complete cookie if we're in insert+indirect mode, so that
4530 * the server never sees it ;
4531 * - remove the server id from the cookie value, and tag the cookie as an
4532 * application cookie so that it does not get accidentely removed later,
4533 * if we're in cookie prefix mode
4534 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004535 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004536 int delta; /* negative */
4537
4538 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4539 p4 += delta;
4540 cur_end += delta;
4541 cur_next += delta;
4542 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004543 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004544
4545 del_cookie = del_colon = NULL;
4546 app_cookies++; /* protect the header from deletion */
4547 }
4548 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004549 (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 +01004550 del_cookie = p1;
4551 del_colon = colon;
4552 }
4553 } else {
4554 /* now we know that we must keep this cookie since it's
4555 * not ours. But if we wanted to delete our cookie
4556 * earlier, we cannot remove the complete header, but we
4557 * can remove the previous block itself.
4558 */
4559 app_cookies++;
4560
4561 if (del_cookie != NULL) {
4562 int delta; /* negative */
4563
4564 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4565 p4 += delta;
4566 cur_end += delta;
4567 cur_next += delta;
4568 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004569 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004570 del_cookie = del_colon = NULL;
4571 }
4572 }
4573
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004574 if ((t->be->appsession_name != NULL) &&
4575 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004577
Willy Tarreau58f10d72006-12-04 02:26:12 +01004578 /* Cool... it's the right one */
4579
4580 asession_temp = &local_asession;
4581
Willy Tarreau63963c62007-05-13 21:29:55 +02004582 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004583 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4584 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4585 return;
4586 }
4587
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004588 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4589 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004590 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004591
Willy Tarreau58f10d72006-12-04 02:26:12 +01004592 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004593 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4594 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004595 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004596 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004597 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004598 Alert("Not enough memory process_cli():asession:calloc().\n");
4599 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4600 return;
4601 }
4602
4603 asession_temp->sessid = local_asession.sessid;
4604 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004605 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004606 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004607 } else {
4608 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004609 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004611 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004612 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004613 Alert("Found Application Session without matching server.\n");
4614 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004615 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004616 while (srv) {
4617 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004618 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004619 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004620 txn->flags &= ~TX_CK_MASK;
4621 txn->flags |= TX_CK_VALID;
4622 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004623 t->srv = srv;
4624 break;
4625 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004626 txn->flags &= ~TX_CK_MASK;
4627 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004628 }
4629 }
4630 srv = srv->next;
4631 }/* end while(srv) */
4632 }/* end else if server == NULL */
4633
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004634 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004635 asession_temp->request_count++;
4636#if defined(DEBUG_HASH)
4637 Alert("manage_client_side_cookies\n");
4638 appsession_hash_dump(&(t->be->htbl_proxy));
4639#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004640 }/* end if ((t->proxy->appsession_name != NULL) ... */
4641 }
4642
4643 /* we'll have to look for another cookie ... */
4644 p1 = p4;
4645 } /* while (p1 < cur_end) */
4646
4647 /* There's no more cookie on this line.
4648 * We may have marked the last one(s) for deletion.
4649 * We must do this now in two ways :
4650 * - if there is no app cookie, we simply delete the header ;
4651 * - if there are app cookies, we must delete the end of the
4652 * string properly, including the colon/semi-colon before
4653 * the cookie name.
4654 */
4655 if (del_cookie != NULL) {
4656 int delta;
4657 if (app_cookies) {
4658 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4659 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004660 cur_hdr->len += delta;
4661 } else {
4662 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004663
4664 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004665 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4666 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004667 cur_hdr->len = 0;
4668 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004669 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004670 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004671 }
4672
4673 /* keep the link from this header to next one */
4674 old_idx = cur_idx;
4675 } /* end of cookie processing on this header */
4676}
4677
4678
Willy Tarreaua15645d2007-03-18 16:22:39 +01004679/* Iterate the same filter through all response headers contained in <rtr>.
4680 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4681 */
4682int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4683{
4684 char term;
4685 char *cur_ptr, *cur_end, *cur_next;
4686 int cur_idx, old_idx, last_hdr;
4687 struct http_txn *txn = &t->txn;
4688 struct hdr_idx_elem *cur_hdr;
4689 int len, delta;
4690
4691 last_hdr = 0;
4692
4693 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4694 old_idx = 0;
4695
4696 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004697 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004698 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004699 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004700 (exp->action == ACT_ALLOW ||
4701 exp->action == ACT_DENY))
4702 return 0;
4703
4704 cur_idx = txn->hdr_idx.v[old_idx].next;
4705 if (!cur_idx)
4706 break;
4707
4708 cur_hdr = &txn->hdr_idx.v[cur_idx];
4709 cur_ptr = cur_next;
4710 cur_end = cur_ptr + cur_hdr->len;
4711 cur_next = cur_end + cur_hdr->cr + 1;
4712
4713 /* Now we have one header between cur_ptr and cur_end,
4714 * and the next header starts at cur_next.
4715 */
4716
4717 /* The annoying part is that pattern matching needs
4718 * that we modify the contents to null-terminate all
4719 * strings before testing them.
4720 */
4721
4722 term = *cur_end;
4723 *cur_end = '\0';
4724
4725 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4726 switch (exp->action) {
4727 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004728 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004729 last_hdr = 1;
4730 break;
4731
4732 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004733 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 last_hdr = 1;
4735 break;
4736
4737 case ACT_REPLACE:
4738 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4739 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4740 /* FIXME: if the user adds a newline in the replacement, the
4741 * index will not be recalculated for now, and the new line
4742 * will not be counted as a new header.
4743 */
4744
4745 cur_end += delta;
4746 cur_next += delta;
4747 cur_hdr->len += delta;
4748 txn->rsp.eoh += delta;
4749 break;
4750
4751 case ACT_REMOVE:
4752 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4753 cur_next += delta;
4754
4755 /* FIXME: this should be a separate function */
4756 txn->rsp.eoh += delta;
4757 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4758 txn->hdr_idx.used--;
4759 cur_hdr->len = 0;
4760 cur_end = NULL; /* null-term has been rewritten */
4761 break;
4762
4763 }
4764 }
4765 if (cur_end)
4766 *cur_end = term; /* restore the string terminator */
4767
4768 /* keep the link from this header to next one in case of later
4769 * removal of next header.
4770 */
4771 old_idx = cur_idx;
4772 }
4773 return 0;
4774}
4775
4776
4777/* Apply the filter to the status line in the response buffer <rtr>.
4778 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4779 * or -1 if a replacement resulted in an invalid status line.
4780 */
4781int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4782{
4783 char term;
4784 char *cur_ptr, *cur_end;
4785 int done;
4786 struct http_txn *txn = &t->txn;
4787 int len, delta;
4788
4789
Willy Tarreau3d300592007-03-18 18:34:41 +01004790 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004791 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004792 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004793 (exp->action == ACT_ALLOW ||
4794 exp->action == ACT_DENY))
4795 return 0;
4796 else if (exp->action == ACT_REMOVE)
4797 return 0;
4798
4799 done = 0;
4800
Willy Tarreau9cdde232007-05-02 20:58:19 +02004801 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004802 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4803
4804 /* Now we have the status line between cur_ptr and cur_end */
4805
4806 /* The annoying part is that pattern matching needs
4807 * that we modify the contents to null-terminate all
4808 * strings before testing them.
4809 */
4810
4811 term = *cur_end;
4812 *cur_end = '\0';
4813
4814 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4815 switch (exp->action) {
4816 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004817 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004818 done = 1;
4819 break;
4820
4821 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004822 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004823 done = 1;
4824 break;
4825
4826 case ACT_REPLACE:
4827 *cur_end = term; /* restore the string terminator */
4828 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4829 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4830 /* FIXME: if the user adds a newline in the replacement, the
4831 * index will not be recalculated for now, and the new line
4832 * will not be counted as a new header.
4833 */
4834
4835 txn->rsp.eoh += delta;
4836 cur_end += delta;
4837
Willy Tarreau9cdde232007-05-02 20:58:19 +02004838 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004840 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004841 cur_ptr, cur_end + 1,
4842 NULL, NULL);
4843 if (unlikely(!cur_end))
4844 return -1;
4845
4846 /* we have a full respnse and we know that we have either a CR
4847 * or an LF at <ptr>.
4848 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004849 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4851 /* there is no point trying this regex on headers */
4852 return 1;
4853 }
4854 }
4855 *cur_end = term; /* restore the string terminator */
4856 return done;
4857}
4858
4859
4860
4861/*
4862 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4863 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4864 * unparsable response.
4865 */
4866int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4867{
Willy Tarreau3d300592007-03-18 18:34:41 +01004868 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004869 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004870 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004871 int ret;
4872
4873 /*
4874 * The interleaving of transformations and verdicts
4875 * makes it difficult to decide to continue or stop
4876 * the evaluation.
4877 */
4878
Willy Tarreau3d300592007-03-18 18:34:41 +01004879 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4881 exp->action == ACT_PASS)) {
4882 exp = exp->next;
4883 continue;
4884 }
4885
4886 /* Apply the filter to the status line. */
4887 ret = apply_filter_to_sts_line(t, rtr, exp);
4888 if (unlikely(ret < 0))
4889 return -1;
4890
4891 if (likely(ret == 0)) {
4892 /* The filter did not match the response, it can be
4893 * iterated through all headers.
4894 */
4895 apply_filter_to_resp_headers(t, rtr, exp);
4896 }
4897 exp = exp->next;
4898 }
4899 return 0;
4900}
4901
4902
4903
4904/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004905 * Manage server-side cookies. It can impact performance by about 2% so it is
4906 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004907 */
4908void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4909{
4910 struct http_txn *txn = &t->txn;
4911 char *p1, *p2, *p3, *p4;
4912
4913 appsess *asession_temp = NULL;
4914 appsess local_asession;
4915
4916 char *cur_ptr, *cur_end, *cur_next;
4917 int cur_idx, old_idx, delta;
4918
Willy Tarreaua15645d2007-03-18 16:22:39 +01004919 /* Iterate through the headers.
4920 * we start with the start line.
4921 */
4922 old_idx = 0;
4923 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4924
4925 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4926 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004927 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004928
4929 cur_hdr = &txn->hdr_idx.v[cur_idx];
4930 cur_ptr = cur_next;
4931 cur_end = cur_ptr + cur_hdr->len;
4932 cur_next = cur_end + cur_hdr->cr + 1;
4933
4934 /* We have one full header between cur_ptr and cur_end, and the
4935 * next header starts at cur_next. We're only interested in
4936 * "Cookie:" headers.
4937 */
4938
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004939 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4940 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004941 old_idx = cur_idx;
4942 continue;
4943 }
4944
4945 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004946 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004947
4948
4949 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004950 if (t->be->cookie_name == NULL &&
4951 t->be->appsession_name == NULL &&
4952 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004953 return;
4954
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004955 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956
4957 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004958 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4959 break;
4960
4961 /* p1 is at the beginning of the cookie name */
4962 p2 = p1;
4963
4964 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4965 p2++;
4966
4967 if (p2 == cur_end || *p2 == ';') /* next cookie */
4968 break;
4969
4970 p3 = p2 + 1; /* skip the '=' sign */
4971 if (p3 == cur_end)
4972 break;
4973
4974 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004975 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976 p4++;
4977
4978 /* here, we have the cookie name between p1 and p2,
4979 * and its value between p3 and p4.
4980 * we can process it.
4981 */
4982
4983 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004984 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004985 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004986 (p4 - p1 >= t->be->capture_namelen) &&
4987 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004988 int log_len = p4 - p1;
4989
Willy Tarreau086b3b42007-05-13 21:45:51 +02004990 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991 Alert("HTTP logging : out of memory.\n");
4992 }
4993
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004994 if (log_len > t->be->capture_len)
4995 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004996 memcpy(txn->srv_cookie, p1, log_len);
4997 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998 }
4999
5000 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005001 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5002 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005003 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005004 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005
5006 /* If the cookie is in insert mode on a known server, we'll delete
5007 * this occurrence because we'll insert another one later.
5008 * We'll delete it too if the "indirect" option is set and we're in
5009 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005010 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5011 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005012 /* this header must be deleted */
5013 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5014 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5015 txn->hdr_idx.used--;
5016 cur_hdr->len = 0;
5017 cur_next += delta;
5018 txn->rsp.eoh += delta;
5019
Willy Tarreau3d300592007-03-18 18:34:41 +01005020 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005021 }
5022 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005023 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005024 /* replace bytes p3->p4 with the cookie name associated
5025 * with this server since we know it.
5026 */
5027 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5028 cur_hdr->len += delta;
5029 cur_next += delta;
5030 txn->rsp.eoh += delta;
5031
Willy Tarreau3d300592007-03-18 18:34:41 +01005032 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005033 }
5034 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005035 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036 /* insert the cookie name associated with this server
5037 * before existing cookie, and insert a delimitor between them..
5038 */
5039 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5040 cur_hdr->len += delta;
5041 cur_next += delta;
5042 txn->rsp.eoh += delta;
5043
5044 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005045 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005046 }
5047 }
5048 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005049 else if ((t->be->appsession_name != NULL) &&
5050 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005051
5052 /* Cool... it's the right one */
5053
5054 size_t server_id_len = strlen(t->srv->id) + 1;
5055 asession_temp = &local_asession;
5056
Willy Tarreau63963c62007-05-13 21:29:55 +02005057 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005058 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5059 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5060 return;
5061 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005062 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5063 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005064 asession_temp->serverid = NULL;
5065
5066 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005067 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5068 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005069 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005070 Alert("Not enough Memory process_srv():asession:calloc().\n");
5071 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5072 return;
5073 }
5074 asession_temp->sessid = local_asession.sessid;
5075 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005076 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005077 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5078 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005079 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005080 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005081 }
5082
Willy Tarreaua15645d2007-03-18 16:22:39 +01005083 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005084 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005085 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5086 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5087 return;
5088 }
5089 asession_temp->serverid[0] = '\0';
5090 }
5091
5092 if (asession_temp->serverid[0] == '\0')
5093 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5094
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005095 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005096 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005097#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005098 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005099 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005100#endif
5101 }/* end if ((t->proxy->appsession_name != NULL) ... */
5102 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5103 } /* we're now at the end of the cookie value */
5104
5105 /* keep the link from this header to next one */
5106 old_idx = cur_idx;
5107 } /* end of cookie processing on this header */
5108}
5109
5110
5111
5112/*
5113 * Check if response is cacheable or not. Updates t->flags.
5114 */
5115void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5116{
5117 struct http_txn *txn = &t->txn;
5118 char *p1, *p2;
5119
5120 char *cur_ptr, *cur_end, *cur_next;
5121 int cur_idx;
5122
Willy Tarreau5df51872007-11-25 16:20:08 +01005123 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005124 return;
5125
5126 /* Iterate through the headers.
5127 * we start with the start line.
5128 */
5129 cur_idx = 0;
5130 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5131
5132 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5133 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005134 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005135
5136 cur_hdr = &txn->hdr_idx.v[cur_idx];
5137 cur_ptr = cur_next;
5138 cur_end = cur_ptr + cur_hdr->len;
5139 cur_next = cur_end + cur_hdr->cr + 1;
5140
5141 /* We have one full header between cur_ptr and cur_end, and the
5142 * next header starts at cur_next. We're only interested in
5143 * "Cookie:" headers.
5144 */
5145
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005146 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5147 if (val) {
5148 if ((cur_end - (cur_ptr + val) >= 8) &&
5149 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5150 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5151 return;
5152 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005153 }
5154
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005155 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5156 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005157 continue;
5158
5159 /* OK, right now we know we have a cache-control header at cur_ptr */
5160
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005161 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005162
5163 if (p1 >= cur_end) /* no more info */
5164 continue;
5165
5166 /* p1 is at the beginning of the value */
5167 p2 = p1;
5168
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005169 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005170 p2++;
5171
5172 /* we have a complete value between p1 and p2 */
5173 if (p2 < cur_end && *p2 == '=') {
5174 /* we have something of the form no-cache="set-cookie" */
5175 if ((cur_end - p1 >= 21) &&
5176 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5177 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005178 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005179 continue;
5180 }
5181
5182 /* OK, so we know that either p2 points to the end of string or to a comma */
5183 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5184 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5185 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5186 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005187 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005188 return;
5189 }
5190
5191 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005192 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005193 continue;
5194 }
5195 }
5196}
5197
5198
Willy Tarreau58f10d72006-12-04 02:26:12 +01005199/*
5200 * Try to retrieve a known appsession in the URI, then the associated server.
5201 * If the server is found, it's assigned to the session.
5202 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005203void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204{
Willy Tarreau3d300592007-03-18 18:34:41 +01005205 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206 appsess *asession_temp = NULL;
5207 appsess local_asession;
5208 char *request_line;
5209
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005210 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005211 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005212 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005213 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 return;
5215
5216 /* skip ';' */
5217 request_line++;
5218
5219 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005220 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005221 return;
5222
5223 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005224 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005225
5226 /* First try if we already have an appsession */
5227 asession_temp = &local_asession;
5228
Willy Tarreau63963c62007-05-13 21:29:55 +02005229 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005230 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5231 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5232 return;
5233 }
5234
5235 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005236 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5237 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005238 asession_temp->serverid = NULL;
5239
5240 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005241 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5242 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005243 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005244 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005245 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 Alert("Not enough memory process_cli():asession:calloc().\n");
5247 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5248 return;
5249 }
5250 asession_temp->sessid = local_asession.sessid;
5251 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005252 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005253 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005254 }
5255 else {
5256 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005257 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005259
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005260 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005262
Willy Tarreau58f10d72006-12-04 02:26:12 +01005263#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005264 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005265 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005266#endif
5267 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005268 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005269 Alert("Found Application Session without matching server.\n");
5270 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005271 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005272 while (srv) {
5273 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005274 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005275 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005276 txn->flags &= ~TX_CK_MASK;
5277 txn->flags |= TX_CK_VALID;
5278 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005279 t->srv = srv;
5280 break;
5281 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005282 txn->flags &= ~TX_CK_MASK;
5283 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005284 }
5285 }
5286 srv = srv->next;
5287 }
5288 }
5289}
5290
5291
Willy Tarreaub2513902006-12-17 14:52:38 +01005292/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005293 * In a GET or HEAD request, check if the requested URI matches the stats uri
5294 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005295 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005296 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005297 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005298 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005299 *
5300 * Returns 1 if the session's state changes, otherwise 0.
5301 */
5302int stats_check_uri_auth(struct session *t, struct proxy *backend)
5303{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005304 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005305 struct uri_auth *uri_auth = backend->uri_auth;
5306 struct user_auth *user;
5307 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005308 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005309
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005310 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005312 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005313 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005314 return 0;
5315
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005316 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005317
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005318 /* the URI is in h */
5319 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005320 return 0;
5321
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005322 h += uri_auth->uri_len;
5323 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5324 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005325 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005326 break;
5327 }
5328 h++;
5329 }
5330
5331 if (uri_auth->refresh) {
5332 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5333 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5334 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005335 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005336 break;
5337 }
5338 h++;
5339 }
5340 }
5341
Willy Tarreau55bb8452007-10-17 18:44:57 +02005342 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5343 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5344 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005345 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005346 break;
5347 }
5348 h++;
5349 }
5350
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005351 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5352
Willy Tarreaub2513902006-12-17 14:52:38 +01005353 /* we are in front of a interceptable URI. Let's check
5354 * if there's an authentication and if it's valid.
5355 */
5356 user = uri_auth->users;
5357 if (!user) {
5358 /* no user auth required, it's OK */
5359 authenticated = 1;
5360 } else {
5361 authenticated = 0;
5362
5363 /* a user list is defined, we have to check.
5364 * skip 21 chars for "Authorization: Basic ".
5365 */
5366
5367 /* FIXME: this should move to an earlier place */
5368 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005369 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5370 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5371 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005372 if (len > 14 &&
5373 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005374 txn->auth_hdr.str = h;
5375 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005376 break;
5377 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005378 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005379 }
5380
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005381 if (txn->auth_hdr.len < 21 ||
5382 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005383 user = NULL;
5384
5385 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005386 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5387 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005388 user->user_pwd, user->user_len)) {
5389 authenticated = 1;
5390 break;
5391 }
5392 user = user->next;
5393 }
5394 }
5395
5396 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005397 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005398
5399 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005400 msg.str = trash;
5401 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005402 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005403 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005404 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005405 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005406 if (!(t->flags & SN_ERR_MASK))
5407 t->flags |= SN_ERR_PRXCOND;
5408 if (!(t->flags & SN_FINST_MASK))
5409 t->flags |= SN_FINST_R;
5410 return 1;
5411 }
5412
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005413 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005414 * data.
5415 */
Willy Tarreau72b179a2008-08-28 16:01:32 +02005416 buffer_shutw_now(t->req);
5417 buffer_shutr_now(t->rep);
5418 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005419 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005420 t->data_source = DATA_SRC_STATS;
5421 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005422 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005423 produce_content(t);
5424 return 1;
5425}
5426
5427
Willy Tarreaubaaee002006-06-26 02:48:02 +02005428/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429 * Print a debug line with a header
5430 */
5431void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5432{
5433 int len, max;
5434 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005435 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436 max = end - start;
5437 UBOUND(max, sizeof(trash) - len - 1);
5438 len += strlcpy2(trash + len, start, max + 1);
5439 trash[len++] = '\n';
5440 write(1, trash, len);
5441}
5442
5443
Willy Tarreau8797c062007-05-07 00:55:35 +02005444/************************************************************************/
5445/* The code below is dedicated to ACL parsing and matching */
5446/************************************************************************/
5447
5448
5449
5450
5451/* 1. Check on METHOD
5452 * We use the pre-parsed method if it is known, and store its number as an
5453 * integer. If it is unknown, we use the pointer and the length.
5454 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005455static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005456{
5457 int len, meth;
5458
Willy Tarreauae8b7962007-06-09 23:10:04 +02005459 len = strlen(*text);
5460 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005461
5462 pattern->val.i = meth;
5463 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005464 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005465 if (!pattern->ptr.str)
5466 return 0;
5467 pattern->len = len;
5468 }
5469 return 1;
5470}
5471
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005472static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005473acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5474 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005475{
5476 int meth;
5477 struct http_txn *txn = l7;
5478
Willy Tarreaub6866442008-07-14 23:54:42 +02005479 if (!txn)
5480 return 0;
5481
Willy Tarreauc11416f2007-06-17 16:58:38 +02005482 if (txn->req.msg_state != HTTP_MSG_BODY)
5483 return 0;
5484
Willy Tarreau8797c062007-05-07 00:55:35 +02005485 meth = txn->meth;
5486 test->i = meth;
5487 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005488 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5489 /* ensure the indexes are not affected */
5490 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005491 test->len = txn->req.sl.rq.m_l;
5492 test->ptr = txn->req.sol;
5493 }
5494 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5495 return 1;
5496}
5497
5498static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5499{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005500 int icase;
5501
Willy Tarreau8797c062007-05-07 00:55:35 +02005502 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005503 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005504
5505 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005506 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005507
5508 /* Other method, we must compare the strings */
5509 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005510 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005511
5512 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5513 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5514 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005515 return ACL_PAT_FAIL;
5516 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005517}
5518
5519/* 2. Check on Request/Status Version
5520 * We simply compare strings here.
5521 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005522static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005523{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005524 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005525 if (!pattern->ptr.str)
5526 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005527 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005528 return 1;
5529}
5530
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005531static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005532acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5533 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005534{
5535 struct http_txn *txn = l7;
5536 char *ptr;
5537 int len;
5538
Willy Tarreaub6866442008-07-14 23:54:42 +02005539 if (!txn)
5540 return 0;
5541
Willy Tarreauc11416f2007-06-17 16:58:38 +02005542 if (txn->req.msg_state != HTTP_MSG_BODY)
5543 return 0;
5544
Willy Tarreau8797c062007-05-07 00:55:35 +02005545 len = txn->req.sl.rq.v_l;
5546 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5547
5548 while ((len-- > 0) && (*ptr++ != '/'));
5549 if (len <= 0)
5550 return 0;
5551
5552 test->ptr = ptr;
5553 test->len = len;
5554
5555 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5556 return 1;
5557}
5558
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005559static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005560acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5561 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005562{
5563 struct http_txn *txn = l7;
5564 char *ptr;
5565 int len;
5566
Willy Tarreaub6866442008-07-14 23:54:42 +02005567 if (!txn)
5568 return 0;
5569
Willy Tarreauc11416f2007-06-17 16:58:38 +02005570 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5571 return 0;
5572
Willy Tarreau8797c062007-05-07 00:55:35 +02005573 len = txn->rsp.sl.st.v_l;
5574 ptr = txn->rsp.sol;
5575
5576 while ((len-- > 0) && (*ptr++ != '/'));
5577 if (len <= 0)
5578 return 0;
5579
5580 test->ptr = ptr;
5581 test->len = len;
5582
5583 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5584 return 1;
5585}
5586
5587/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005588static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005589acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5590 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005591{
5592 struct http_txn *txn = l7;
5593 char *ptr;
5594 int len;
5595
Willy Tarreaub6866442008-07-14 23:54:42 +02005596 if (!txn)
5597 return 0;
5598
Willy Tarreauc11416f2007-06-17 16:58:38 +02005599 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5600 return 0;
5601
Willy Tarreau8797c062007-05-07 00:55:35 +02005602 len = txn->rsp.sl.st.c_l;
5603 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5604
5605 test->i = __strl2ui(ptr, len);
5606 test->flags = ACL_TEST_F_VOL_1ST;
5607 return 1;
5608}
5609
5610/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005611static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005612acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5613 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005614{
5615 struct http_txn *txn = l7;
5616
Willy Tarreaub6866442008-07-14 23:54:42 +02005617 if (!txn)
5618 return 0;
5619
Willy Tarreauc11416f2007-06-17 16:58:38 +02005620 if (txn->req.msg_state != HTTP_MSG_BODY)
5621 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005622
Willy Tarreauc11416f2007-06-17 16:58:38 +02005623 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5624 /* ensure the indexes are not affected */
5625 return 0;
5626
Willy Tarreau8797c062007-05-07 00:55:35 +02005627 test->len = txn->req.sl.rq.u_l;
5628 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5629
Willy Tarreauf3d25982007-05-08 22:45:09 +02005630 /* we do not need to set READ_ONLY because the data is in a buffer */
5631 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005632 return 1;
5633}
5634
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005635static int
5636acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5637 struct acl_expr *expr, struct acl_test *test)
5638{
5639 struct http_txn *txn = l7;
5640
Willy Tarreaub6866442008-07-14 23:54:42 +02005641 if (!txn)
5642 return 0;
5643
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005644 if (txn->req.msg_state != HTTP_MSG_BODY)
5645 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005646
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005647 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5648 /* ensure the indexes are not affected */
5649 return 0;
5650
5651 /* Parse HTTP request */
5652 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5653 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5654 test->i = AF_INET;
5655
5656 /*
5657 * If we are parsing url in frontend space, we prepare backend stage
5658 * to not parse again the same url ! optimization lazyness...
5659 */
5660 if (px->options & PR_O_HTTP_PROXY)
5661 l4->flags |= SN_ADDR_SET;
5662
5663 test->flags = ACL_TEST_F_READ_ONLY;
5664 return 1;
5665}
5666
5667static int
5668acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5669 struct acl_expr *expr, struct acl_test *test)
5670{
5671 struct http_txn *txn = l7;
5672
Willy Tarreaub6866442008-07-14 23:54:42 +02005673 if (!txn)
5674 return 0;
5675
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005676 if (txn->req.msg_state != HTTP_MSG_BODY)
5677 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005678
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005679 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5680 /* ensure the indexes are not affected */
5681 return 0;
5682
5683 /* Same optimization as url_ip */
5684 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5685 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5686
5687 if (px->options & PR_O_HTTP_PROXY)
5688 l4->flags |= SN_ADDR_SET;
5689
5690 test->flags = ACL_TEST_F_READ_ONLY;
5691 return 1;
5692}
5693
Willy Tarreauc11416f2007-06-17 16:58:38 +02005694/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5695 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005697static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005698acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005699 struct acl_expr *expr, struct acl_test *test)
5700{
5701 struct http_txn *txn = l7;
5702 struct hdr_idx *idx = &txn->hdr_idx;
5703 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005704
Willy Tarreaub6866442008-07-14 23:54:42 +02005705 if (!txn)
5706 return 0;
5707
Willy Tarreau33a7e692007-06-10 19:45:56 +02005708 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5709 /* search for header from the beginning */
5710 ctx->idx = 0;
5711
Willy Tarreau33a7e692007-06-10 19:45:56 +02005712 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5713 test->flags |= ACL_TEST_F_FETCH_MORE;
5714 test->flags |= ACL_TEST_F_VOL_HDR;
5715 test->len = ctx->vlen;
5716 test->ptr = (char *)ctx->line + ctx->val;
5717 return 1;
5718 }
5719
5720 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5721 test->flags |= ACL_TEST_F_VOL_HDR;
5722 return 0;
5723}
5724
Willy Tarreau33a7e692007-06-10 19:45:56 +02005725static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005726acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5727 struct acl_expr *expr, struct acl_test *test)
5728{
5729 struct http_txn *txn = l7;
5730
Willy Tarreaub6866442008-07-14 23:54:42 +02005731 if (!txn)
5732 return 0;
5733
Willy Tarreauc11416f2007-06-17 16:58:38 +02005734 if (txn->req.msg_state != HTTP_MSG_BODY)
5735 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005736
Willy Tarreauc11416f2007-06-17 16:58:38 +02005737 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5738 /* ensure the indexes are not affected */
5739 return 0;
5740
5741 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5742}
5743
5744static int
5745acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5746 struct acl_expr *expr, struct acl_test *test)
5747{
5748 struct http_txn *txn = l7;
5749
Willy Tarreaub6866442008-07-14 23:54:42 +02005750 if (!txn)
5751 return 0;
5752
Willy Tarreauc11416f2007-06-17 16:58:38 +02005753 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5754 return 0;
5755
5756 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5757}
5758
5759/* 6. Check on HTTP header count. The number of occurrences is returned.
5760 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5761 */
5762static int
5763acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005764 struct acl_expr *expr, struct acl_test *test)
5765{
5766 struct http_txn *txn = l7;
5767 struct hdr_idx *idx = &txn->hdr_idx;
5768 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005769 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005770
Willy Tarreaub6866442008-07-14 23:54:42 +02005771 if (!txn)
5772 return 0;
5773
Willy Tarreau33a7e692007-06-10 19:45:56 +02005774 ctx.idx = 0;
5775 cnt = 0;
5776 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5777 cnt++;
5778
5779 test->i = cnt;
5780 test->flags = ACL_TEST_F_VOL_HDR;
5781 return 1;
5782}
5783
Willy Tarreauc11416f2007-06-17 16:58:38 +02005784static int
5785acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5786 struct acl_expr *expr, struct acl_test *test)
5787{
5788 struct http_txn *txn = l7;
5789
Willy Tarreaub6866442008-07-14 23:54:42 +02005790 if (!txn)
5791 return 0;
5792
Willy Tarreauc11416f2007-06-17 16:58:38 +02005793 if (txn->req.msg_state != HTTP_MSG_BODY)
5794 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005795
Willy Tarreauc11416f2007-06-17 16:58:38 +02005796 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5797 /* ensure the indexes are not affected */
5798 return 0;
5799
5800 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5801}
5802
5803static int
5804acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5805 struct acl_expr *expr, struct acl_test *test)
5806{
5807 struct http_txn *txn = l7;
5808
Willy Tarreaub6866442008-07-14 23:54:42 +02005809 if (!txn)
5810 return 0;
5811
Willy Tarreauc11416f2007-06-17 16:58:38 +02005812 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5813 return 0;
5814
5815 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5816}
5817
Willy Tarreau33a7e692007-06-10 19:45:56 +02005818/* 7. Check on HTTP header's integer value. The integer value is returned.
5819 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005820 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005821 */
5822static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005823acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005824 struct acl_expr *expr, struct acl_test *test)
5825{
5826 struct http_txn *txn = l7;
5827 struct hdr_idx *idx = &txn->hdr_idx;
5828 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005829
Willy Tarreaub6866442008-07-14 23:54:42 +02005830 if (!txn)
5831 return 0;
5832
Willy Tarreau33a7e692007-06-10 19:45:56 +02005833 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5834 /* search for header from the beginning */
5835 ctx->idx = 0;
5836
Willy Tarreau33a7e692007-06-10 19:45:56 +02005837 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5838 test->flags |= ACL_TEST_F_FETCH_MORE;
5839 test->flags |= ACL_TEST_F_VOL_HDR;
5840 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5841 return 1;
5842 }
5843
5844 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5845 test->flags |= ACL_TEST_F_VOL_HDR;
5846 return 0;
5847}
5848
Willy Tarreauc11416f2007-06-17 16:58:38 +02005849static int
5850acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5851 struct acl_expr *expr, struct acl_test *test)
5852{
5853 struct http_txn *txn = l7;
5854
Willy Tarreaub6866442008-07-14 23:54:42 +02005855 if (!txn)
5856 return 0;
5857
Willy Tarreauc11416f2007-06-17 16:58:38 +02005858 if (txn->req.msg_state != HTTP_MSG_BODY)
5859 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005860
Willy Tarreauc11416f2007-06-17 16:58:38 +02005861 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5862 /* ensure the indexes are not affected */
5863 return 0;
5864
5865 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5866}
5867
5868static int
5869acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5870 struct acl_expr *expr, struct acl_test *test)
5871{
5872 struct http_txn *txn = l7;
5873
Willy Tarreaub6866442008-07-14 23:54:42 +02005874 if (!txn)
5875 return 0;
5876
Willy Tarreauc11416f2007-06-17 16:58:38 +02005877 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5878 return 0;
5879
5880 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5881}
5882
Willy Tarreau737b0c12007-06-10 21:28:46 +02005883/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5884 * the first '/' after the possible hostname, and ends before the possible '?'.
5885 */
5886static int
5887acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5888 struct acl_expr *expr, struct acl_test *test)
5889{
5890 struct http_txn *txn = l7;
5891 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005892
Willy Tarreaub6866442008-07-14 23:54:42 +02005893 if (!txn)
5894 return 0;
5895
Willy Tarreauc11416f2007-06-17 16:58:38 +02005896 if (txn->req.msg_state != HTTP_MSG_BODY)
5897 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005898
Willy Tarreauc11416f2007-06-17 16:58:38 +02005899 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5900 /* ensure the indexes are not affected */
5901 return 0;
5902
Willy Tarreau21d2af32008-02-14 20:25:24 +01005903 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5904 ptr = http_get_path(txn);
5905 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005906 return 0;
5907
5908 /* OK, we got the '/' ! */
5909 test->ptr = ptr;
5910
5911 while (ptr < end && *ptr != '?')
5912 ptr++;
5913
5914 test->len = ptr - test->ptr;
5915
5916 /* we do not need to set READ_ONLY because the data is in a buffer */
5917 test->flags = ACL_TEST_F_VOL_1ST;
5918 return 1;
5919}
5920
5921
Willy Tarreau8797c062007-05-07 00:55:35 +02005922
5923/************************************************************************/
5924/* All supported keywords must be declared here. */
5925/************************************************************************/
5926
5927/* Note: must not be declared <const> as its list will be overwritten */
5928static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005929 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5930 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5931 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5932 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005933
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005934 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5935 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5936 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5937 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5938 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5939 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5940 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5941 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5942 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005943
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005944 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5945 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5946 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5947 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5948 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5949 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5950 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5951 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5952 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5953 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005954
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005955 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5956 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5957 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5958 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5959 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5960 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5961 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5962 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5963 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005964
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005965 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5966 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5967 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5968 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5969 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5970 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5971 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005972
Willy Tarreauf3d25982007-05-08 22:45:09 +02005973 { NULL, NULL, NULL, NULL },
5974
5975#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005976 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5977 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5978 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5979 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5980 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5981 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5982 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5983
Willy Tarreau8797c062007-05-07 00:55:35 +02005984 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5985 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5986 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5987 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5988 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5989 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5990 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5991 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5992
5993 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5994 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5995 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5996 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5997 { NULL, NULL, NULL, NULL },
5998#endif
5999}};
6000
6001
6002__attribute__((constructor))
6003static void __http_protocol_init(void)
6004{
6005 acl_register_keywords(&acl_kws);
6006}
6007
6008
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006010 * Local variables:
6011 * c-indent-level: 8
6012 * c-basic-offset: 8
6013 * End:
6014 */