blob: 0a09b2d1ebdb19a82c07f8899f952aab4d65c6cb [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 Tarreau4ffd51a2008-08-30 13:36:43 +0200664 if (s->rep->cons->state == SI_ST_EST) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200665 stream_sock_data_check_timeouts(s->rep->cons->fd);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200666 if (tick_is_expired(s->rep->analyse_exp, now_ms))
667 s->rep->flags |= BF_ANA_TIMEOUT;
668 }
669 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200670 stream_sock_data_check_timeouts(s->req->cons->fd);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200671 if (tick_is_expired(s->req->analyse_exp, now_ms))
672 s->req->flags |= BF_ANA_TIMEOUT;
673 }
Willy Tarreau35374672008-09-03 18:11:02 +0200674 /* Note that we don't check nor indicate if we wake up because
675 * of a timeout on a stream interface.
676 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200677 }
678
Willy Tarreau48adac52008-08-30 04:58:38 +0200679 /* Check if we need to close the write side. This can only happen
680 * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear
681 * from low level, and neither HIJACK nor SHUTW can disappear from low
682 * level.
683 */
684 if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
685 buffer_shutw(s->req);
686 s->req->cons->shutw(s->req->cons);
687 }
688
689 if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
690 buffer_shutw(s->rep);
691 s->rep->cons->shutw(s->rep->cons);
692 }
693
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200694 /* When a server-side connection is released, we have to
695 * count it and check for pending connections on this server.
696 */
697 if (unlikely(s->req->cons->state == SI_ST_CLO &&
698 s->req->cons->prev_state == SI_ST_EST)) {
699 /* Count server-side errors (but not timeouts). */
700 if (s->req->flags & BF_WRITE_ERROR) {
701 s->be->failed_resp++;
702 if (s->srv)
703 s->srv->failed_resp++;
704 }
705
706 if (s->srv) {
707 s->srv->cur_sess--;
708 sess_change_server(s, NULL);
709 if (may_dequeue_tasks(s->srv, s->be))
710 process_srv_queue(s->srv);
711 }
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200712 }
713
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200714 /* Dirty trick: force one first pass everywhere */
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200715 rqf_cli = rqf_srv = ~s->req->flags;
716 rpf_cli = rpf_srv = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +0200717
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200718 /* well, the ST_CONN state is already handled properly */
719 if (s->req->prod->state == SI_ST_EST) {
720 rqf_cli = s->req->flags;
721 rpf_cli = s->rep->flags;
722 }
723
724 if (s->req->cons->state == SI_ST_EST) {
725 rqf_srv = s->req->flags;
726 rpf_srv = s->rep->flags;
727 }
728
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 do {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200730 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",
731 now_ms, __FUNCTION__,
732 t,
733 s->req, s->rep,
734 s->req->rex, s->rep->wex,
735 s->req->flags, s->rep->flags,
736 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
737
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200738 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +0200739
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200740 /* Maybe resync client FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200741 if (s->rep->cons->state != SI_ST_CLO) {
742 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
743 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200744 stream_sock_data_update(s->rep->cons->fd);
745 rqf_cli = s->req->flags;
746 rpf_cli = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +0200747 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200748 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200749
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200750 /* Maybe resync server FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200751 if (s->req->cons->state != SI_ST_CLO) {
752 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
753 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200754 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200755 process_srv_conn(s);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200756 resync = 1; /* we might have to resync */
757 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200758
759 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +0200760 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +0200761 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +0200762 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200763 /* We want to force the connection to the server to close,
764 * and the server has begun to respond. That's the right
765 * time.
766 */
767 buffer_shutw_now(s->req);
768 }
769
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200770 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +0200771
772 /* When a server-side connection is released, we have to
773 * count it and check for pending connections on this server.
774 */
775 if (s->req->cons->state == SI_ST_CLO) {
776 if (s->srv) {
777 s->srv->cur_sess--;
778 sess_change_server(s, NULL);
779 if (may_dequeue_tasks(s->srv, s->be))
780 process_srv_queue(s->srv);
781 }
782 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200783 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200784 rqf_srv = s->req->flags;
785 rpf_srv = s->rep->flags;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200786 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200787 }
788
789 /* we may have to resync because of pending connections */
790 if (resync)
791 continue;
792
793 /**** Process layer 7 below ****/
794
795 /* Analyse request */
796 if (s->req->flags & BF_MASK_ANALYSER) {
797 unsigned int flags = s->req->flags;
798
799 if (s->req->prod->state >= SI_ST_EST) {
800 /* it's up to the analysers to reset write_ena */
801 buffer_write_ena(s->req);
802 if (s->req->analysers)
803 process_request(s);
804 }
805 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
806 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
807 if (s->req->flags != flags)
808 resync = 1;
809 }
810
811 /* Analyse response */
812 if (unlikely(s->rep->flags & BF_HIJACK)) {
813 /* In inject mode, we wake up everytime something has
814 * happened on the write side of the buffer.
815 */
816 unsigned int flags = s->rep->flags;
817
818 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
819 !(s->rep->flags & BF_FULL)) {
820 produce_content(s);
821 }
822 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
823 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
824 if (s->rep->flags != flags)
825 resync = 1;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200826 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200827 else if (s->rep->flags & BF_MASK_ANALYSER) {
828 unsigned int flags = s->rep->flags;
829
830 if (s->rep->prod->state >= SI_ST_EST) {
831 /* it's up to the analysers to reset write_ena */
832 buffer_write_ena(s->rep);
833 if (s->rep->analysers)
834 process_response(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;
840 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200841
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200842 /* For the moment, we need to clean the client and server flags that
843 * have vanished. This is just a temporary measure though.
844 */
845 rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
846 rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
847 rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
848 rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaudafde432008-08-17 01:00:46 +0200849 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850
Willy Tarreaua37095b2008-09-03 11:37:47 +0200851 /* This is needed only when debugging is enabled, to indicate
852 * client-side or server-side close. Please note that in the unlikely
853 * event where both sides would close at once, the sequence is reported
854 * on the server side first.
855 */
856 if (unlikely((global.mode & MODE_DEBUG) &&
857 (!(global.mode & MODE_QUIET) ||
858 (global.mode & MODE_VERBOSE)))) {
859 int len;
860
861 if (s->si[1].state == SI_ST_CLO &&
862 s->si[1].prev_state == SI_ST_EST) {
863 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
864 s->uniq_id, s->be->id,
865 (unsigned short)s->si[0].fd,
866 (unsigned short)s->si[1].fd);
867 write(1, trash, len);
868 }
869
870 if (s->si[0].state == SI_ST_CLO &&
871 s->si[0].prev_state == SI_ST_EST) {
872 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
873 s->uniq_id, s->be->id,
874 (unsigned short)s->si[0].fd,
875 (unsigned short)s->si[1].fd);
876 write(1, trash, len);
877 }
878 }
879
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200880 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200881 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100882
883 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
884 session_process_counters(s);
885
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200886 if (s->rep->cons->state == SI_ST_EST)
887 stream_sock_data_finish(s->rep->cons->fd);
888
889 if (s->req->cons->state == SI_ST_EST)
890 stream_sock_data_finish(s->req->cons->fd);
891
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200892 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
893 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200894 s->si[0].prev_state = s->si[0].state;
895 s->si[1].prev_state = s->si[1].state;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896
Willy Tarreau7f875f62008-08-11 17:35:01 +0200897 /* Trick: if a request is being waiting for the server to respond,
898 * and if we know the server can timeout, we don't want the timeout
899 * to expire on the client side first, but we're still interested
900 * in passing data from the client to the server (eg: POST). Thus,
901 * we can cancel the client's request timeout if the server's
902 * request timeout is set and the server has not yet sent a response.
903 */
904
Willy Tarreau3da77c52008-08-29 09:58:42 +0200905 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200906 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200907 s->req->rex = TICK_ETERNITY;
908
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200909 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
910 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200911 if (s->req->analysers)
912 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913
Willy Tarreau35374672008-09-03 18:11:02 +0200914 if (s->si[0].exp)
915 t->expire = tick_first(t->expire, s->si[0].exp);
916
917 if (s->si[1].exp)
918 t->expire = tick_first(t->expire, s->si[1].exp);
919
Willy Tarreaucb651252008-08-29 13:57:30 +0200920#ifdef DEBUG_FULL
921 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n",
922 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex);
923#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 /* restore t to its place in the task list */
925 task_queue(t);
926
Willy Tarreaua7c52762008-08-16 18:40:18 +0200927#ifdef DEBUG_DEV
928 /* this may only happen when no timeout is set or in case of an FSM bug */
929 if (!t->expire)
930 ABORT_NOW();
931#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200932 *next = t->expire;
933 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 }
935
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100936 s->fe->feconn--;
937 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200938 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939 actconn--;
940
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200941 if (unlikely((global.mode & MODE_DEBUG) &&
942 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200944 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200945 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200946 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200947 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 write(1, trash, len);
949 }
950
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200951 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100952 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953
954 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200955 if (s->logs.logwait &&
956 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200957 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
958 if (s->fe->to_log & LW_REQ)
959 http_sess_log(s);
960 else
961 tcp_sess_log(s);
962 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963
964 /* the task MUST not be in the run queue anymore */
965 task_delete(t);
966 session_free(s);
967 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200968 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969}
970
971
Willy Tarreau42250582007-04-01 01:30:43 +0200972extern const char sess_term_cond[8];
973extern const char sess_fin_state[8];
974extern const char *monthname[12];
975const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
976const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
977 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
978 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200979struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200980struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981
Willy Tarreau42250582007-04-01 01:30:43 +0200982/*
983 * send a log for the session when we have enough info about it.
984 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100985 */
Willy Tarreau42250582007-04-01 01:30:43 +0200986static void http_sess_log(struct session *s)
987{
988 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
989 struct proxy *fe = s->fe;
990 struct proxy *be = s->be;
991 struct proxy *prx_log;
992 struct http_txn *txn = &s->txn;
993 int tolog;
994 char *uri, *h;
995 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200996 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200997 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200998 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200999 int hdr;
1000
1001 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1002 return;
1003 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004
Willy Tarreau42250582007-04-01 01:30:43 +02001005 if (s->cli_addr.ss_family == AF_INET)
1006 inet_ntop(AF_INET,
1007 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1008 pn, sizeof(pn));
1009 else
1010 inet_ntop(AF_INET6,
1011 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1012 pn, sizeof(pn));
1013
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001014 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001015
1016 /* FIXME: let's limit ourselves to frontend logging for now. */
1017 tolog = fe->to_log;
1018
1019 h = tmpline;
1020 if (fe->to_log & LW_REQHDR &&
1021 txn->req.cap &&
1022 (h < tmpline + sizeof(tmpline) - 10)) {
1023 *(h++) = ' ';
1024 *(h++) = '{';
1025 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1026 if (hdr)
1027 *(h++) = '|';
1028 if (txn->req.cap[hdr] != NULL)
1029 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1030 '#', hdr_encode_map, txn->req.cap[hdr]);
1031 }
1032 *(h++) = '}';
1033 }
1034
1035 if (fe->to_log & LW_RSPHDR &&
1036 txn->rsp.cap &&
1037 (h < tmpline + sizeof(tmpline) - 7)) {
1038 *(h++) = ' ';
1039 *(h++) = '{';
1040 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1041 if (hdr)
1042 *(h++) = '|';
1043 if (txn->rsp.cap[hdr] != NULL)
1044 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1045 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1046 }
1047 *(h++) = '}';
1048 }
1049
1050 if (h < tmpline + sizeof(tmpline) - 4) {
1051 *(h++) = ' ';
1052 *(h++) = '"';
1053 uri = txn->uri ? txn->uri : "<BADREQ>";
1054 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1055 '#', url_encode_map, uri);
1056 *(h++) = '"';
1057 }
1058 *h = '\0';
1059
1060 svid = (tolog & LW_SVID) ?
1061 (s->data_source != DATA_SRC_STATS) ?
1062 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1063
Willy Tarreau70089872008-06-13 21:12:51 +02001064 t_request = -1;
1065 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1066 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1067
Willy Tarreau42250582007-04-01 01:30:43 +02001068 send_log(prx_log, LOG_INFO,
1069 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
1070 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001071 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001072 pn,
1073 (s->cli_addr.ss_family == AF_INET) ?
1074 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1075 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001076 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001077 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001078 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001079 t_request,
1080 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001081 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1082 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1083 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1084 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001085 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001086 txn->cli_cookie ? txn->cli_cookie : "-",
1087 txn->srv_cookie ? txn->srv_cookie : "-",
1088 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1089 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1090 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1091 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1092 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001093 (s->flags & SN_REDISP)?"+":"",
1094 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001095 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1096
1097 s->logs.logwait = 0;
1098}
1099
Willy Tarreau117f59e2007-03-04 18:17:17 +01001100
1101/*
1102 * Capture headers from message starting at <som> according to header list
1103 * <cap_hdr>, and fill the <idx> structure appropriately.
1104 */
1105void capture_headers(char *som, struct hdr_idx *idx,
1106 char **cap, struct cap_hdr *cap_hdr)
1107{
1108 char *eol, *sol, *col, *sov;
1109 int cur_idx;
1110 struct cap_hdr *h;
1111 int len;
1112
1113 sol = som + hdr_idx_first_pos(idx);
1114 cur_idx = hdr_idx_first_idx(idx);
1115
1116 while (cur_idx) {
1117 eol = sol + idx->v[cur_idx].len;
1118
1119 col = sol;
1120 while (col < eol && *col != ':')
1121 col++;
1122
1123 sov = col + 1;
1124 while (sov < eol && http_is_lws[(unsigned char)*sov])
1125 sov++;
1126
1127 for (h = cap_hdr; h; h = h->next) {
1128 if ((h->namelen == col - sol) &&
1129 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1130 if (cap[h->index] == NULL)
1131 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001132 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001133
1134 if (cap[h->index] == NULL) {
1135 Alert("HTTP capture : out of memory.\n");
1136 continue;
1137 }
1138
1139 len = eol - sov;
1140 if (len > h->len)
1141 len = h->len;
1142
1143 memcpy(cap[h->index], sov, len);
1144 cap[h->index][len]=0;
1145 }
1146 }
1147 sol = eol + idx->v[cur_idx].cr + 1;
1148 cur_idx = idx->v[cur_idx].next;
1149 }
1150}
1151
1152
Willy Tarreau42250582007-04-01 01:30:43 +02001153/* either we find an LF at <ptr> or we jump to <bad>.
1154 */
1155#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1156
1157/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1158 * otherwise to <http_msg_ood> with <state> set to <st>.
1159 */
1160#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1161 ptr++; \
1162 if (likely(ptr < end)) \
1163 goto good; \
1164 else { \
1165 state = (st); \
1166 goto http_msg_ood; \
1167 } \
1168 } while (0)
1169
1170
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001172 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1174 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1175 * will give undefined results.
1176 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1177 * and that msg->sol points to the beginning of the response.
1178 * If a complete line is found (which implies that at least one CR or LF is
1179 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1180 * returned indicating an incomplete line (which does not mean that parts have
1181 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1182 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1183 * upon next call.
1184 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001185 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1187 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001188 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001190const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1191 unsigned int state, const char *ptr, const char *end,
1192 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001193{
1194 __label__
1195 http_msg_rpver,
1196 http_msg_rpver_sp,
1197 http_msg_rpcode,
1198 http_msg_rpcode_sp,
1199 http_msg_rpreason,
1200 http_msg_rpline_eol,
1201 http_msg_ood, /* out of data */
1202 http_msg_invalid;
1203
1204 switch (state) {
1205 http_msg_rpver:
1206 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001207 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1209
1210 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001211 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001212 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1213 }
1214 goto http_msg_invalid;
1215
1216 http_msg_rpver_sp:
1217 case HTTP_MSG_RPVER_SP:
1218 if (likely(!HTTP_IS_LWS(*ptr))) {
1219 msg->sl.st.c = ptr - msg_buf;
1220 goto http_msg_rpcode;
1221 }
1222 if (likely(HTTP_IS_SPHT(*ptr)))
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1224 /* so it's a CR/LF, this is invalid */
1225 goto http_msg_invalid;
1226
1227 http_msg_rpcode:
1228 case HTTP_MSG_RPCODE:
1229 if (likely(!HTTP_IS_LWS(*ptr)))
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1231
1232 if (likely(HTTP_IS_SPHT(*ptr))) {
1233 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1235 }
1236
1237 /* so it's a CR/LF, so there is no reason phrase */
1238 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1239 http_msg_rsp_reason:
1240 /* FIXME: should we support HTTP responses without any reason phrase ? */
1241 msg->sl.st.r = ptr - msg_buf;
1242 msg->sl.st.r_l = 0;
1243 goto http_msg_rpline_eol;
1244
1245 http_msg_rpcode_sp:
1246 case HTTP_MSG_RPCODE_SP:
1247 if (likely(!HTTP_IS_LWS(*ptr))) {
1248 msg->sl.st.r = ptr - msg_buf;
1249 goto http_msg_rpreason;
1250 }
1251 if (likely(HTTP_IS_SPHT(*ptr)))
1252 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1253 /* so it's a CR/LF, so there is no reason phrase */
1254 goto http_msg_rsp_reason;
1255
1256 http_msg_rpreason:
1257 case HTTP_MSG_RPREASON:
1258 if (likely(!HTTP_IS_CRLF(*ptr)))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1260 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1261 http_msg_rpline_eol:
1262 /* We have seen the end of line. Note that we do not
1263 * necessarily have the \n yet, but at least we know that we
1264 * have EITHER \r OR \n, otherwise the response would not be
1265 * complete. We can then record the response length and return
1266 * to the caller which will be able to register it.
1267 */
1268 msg->sl.st.l = ptr - msg->sol;
1269 return ptr;
1270
1271#ifdef DEBUG_FULL
1272 default:
1273 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1274 exit(1);
1275#endif
1276 }
1277
1278 http_msg_ood:
1279 /* out of data */
1280 if (ret_state)
1281 *ret_state = state;
1282 if (ret_ptr)
1283 *ret_ptr = (char *)ptr;
1284 return NULL;
1285
1286 http_msg_invalid:
1287 /* invalid message */
1288 if (ret_state)
1289 *ret_state = HTTP_MSG_ERROR;
1290 return NULL;
1291}
1292
1293
1294/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 * This function parses a request line between <ptr> and <end>, starting with
1296 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1297 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1298 * will give undefined results.
1299 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1300 * and that msg->sol points to the beginning of the request.
1301 * If a complete line is found (which implies that at least one CR or LF is
1302 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1303 * returned indicating an incomplete line (which does not mean that parts have
1304 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1305 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1306 * upon next call.
1307 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001308 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1310 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001311 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001312 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001313const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1314 unsigned int state, const char *ptr, const char *end,
1315 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001316{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 __label__
1318 http_msg_rqmeth,
1319 http_msg_rqmeth_sp,
1320 http_msg_rquri,
1321 http_msg_rquri_sp,
1322 http_msg_rqver,
1323 http_msg_rqline_eol,
1324 http_msg_ood, /* out of data */
1325 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 switch (state) {
1328 http_msg_rqmeth:
1329 case HTTP_MSG_RQMETH:
1330 if (likely(HTTP_IS_TOKEN(*ptr)))
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001334 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1336 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 if (likely(HTTP_IS_CRLF(*ptr))) {
1339 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001340 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 http_msg_req09_uri:
1342 msg->sl.rq.u = ptr - msg_buf;
1343 http_msg_req09_uri_e:
1344 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1345 http_msg_req09_ver:
1346 msg->sl.rq.v = ptr - msg_buf;
1347 msg->sl.rq.v_l = 0;
1348 goto http_msg_rqline_eol;
1349 }
1350 goto http_msg_invalid;
1351
1352 http_msg_rqmeth_sp:
1353 case HTTP_MSG_RQMETH_SP:
1354 if (likely(!HTTP_IS_LWS(*ptr))) {
1355 msg->sl.rq.u = ptr - msg_buf;
1356 goto http_msg_rquri;
1357 }
1358 if (likely(HTTP_IS_SPHT(*ptr)))
1359 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 http_msg_rquri:
1364 case HTTP_MSG_RQURI:
1365 if (likely(!HTTP_IS_LWS(*ptr)))
1366 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 if (likely(HTTP_IS_SPHT(*ptr))) {
1369 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1370 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1371 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1374 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 http_msg_rquri_sp:
1377 case HTTP_MSG_RQURI_SP:
1378 if (likely(!HTTP_IS_LWS(*ptr))) {
1379 msg->sl.rq.v = ptr - msg_buf;
1380 goto http_msg_rqver;
1381 }
1382 if (likely(HTTP_IS_SPHT(*ptr)))
1383 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1384 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1385 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 http_msg_rqver:
1388 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001389 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001391
1392 if (likely(HTTP_IS_CRLF(*ptr))) {
1393 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1394 http_msg_rqline_eol:
1395 /* We have seen the end of line. Note that we do not
1396 * necessarily have the \n yet, but at least we know that we
1397 * have EITHER \r OR \n, otherwise the request would not be
1398 * complete. We can then record the request length and return
1399 * to the caller which will be able to register it.
1400 */
1401 msg->sl.rq.l = ptr - msg->sol;
1402 return ptr;
1403 }
1404
1405 /* neither an HTTP_VER token nor a CRLF */
1406 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408#ifdef DEBUG_FULL
1409 default:
1410 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1411 exit(1);
1412#endif
1413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_ood:
1416 /* out of data */
1417 if (ret_state)
1418 *ret_state = state;
1419 if (ret_ptr)
1420 *ret_ptr = (char *)ptr;
1421 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_invalid:
1424 /* invalid message */
1425 if (ret_state)
1426 *ret_state = HTTP_MSG_ERROR;
1427 return NULL;
1428}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001429
1430
Willy Tarreau8973c702007-01-21 23:58:29 +01001431/*
1432 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001433 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 * when data are missing and recalled at the exact same location with no
1435 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001436 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1437 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1440{
1441 __label__
1442 http_msg_rqbefore,
1443 http_msg_rqbefore_cr,
1444 http_msg_rqmeth,
1445 http_msg_rqline_end,
1446 http_msg_hdr_first,
1447 http_msg_hdr_name,
1448 http_msg_hdr_l1_sp,
1449 http_msg_hdr_l1_lf,
1450 http_msg_hdr_l1_lws,
1451 http_msg_hdr_val,
1452 http_msg_hdr_l2_lf,
1453 http_msg_hdr_l2_lws,
1454 http_msg_complete_header,
1455 http_msg_last_lf,
1456 http_msg_ood, /* out of data */
1457 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001458
Willy Tarreaue69eada2008-01-27 00:34:10 +01001459 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001462 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 ptr = buf->lr;
1464 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 if (unlikely(ptr >= end))
1467 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 /*
1471 * First, states that are specific to the response only.
1472 * We check them first so that request and headers are
1473 * closer to each other (accessed more often).
1474 */
1475 http_msg_rpbefore:
1476 case HTTP_MSG_RPBEFORE:
1477 if (likely(HTTP_IS_TOKEN(*ptr))) {
1478 if (likely(ptr == buf->data)) {
1479 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 } else {
1482#if PARSE_PRESERVE_EMPTY_LINES
1483 /* only skip empty leading lines, don't remove them */
1484 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001485 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001486#else
1487 /* Remove empty leading lines, as recommended by
1488 * RFC2616. This takes a lot of time because we
1489 * must move all the buffer backwards, but this
1490 * is rarely needed. The method above will be
1491 * cleaner when we'll be able to start sending
1492 * the request from any place in the buffer.
1493 */
1494 buf->lr = ptr;
1495 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 msg->sol = buf->data;
1498 ptr = buf->data;
1499 end = buf->r;
1500#endif
1501 }
1502 hdr_idx_init(idx);
1503 state = HTTP_MSG_RPVER;
1504 goto http_msg_rpver;
1505 }
1506
1507 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1508 goto http_msg_invalid;
1509
1510 if (unlikely(*ptr == '\n'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1512 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1513 /* stop here */
1514
1515 http_msg_rpbefore_cr:
1516 case HTTP_MSG_RPBEFORE_CR:
1517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1519 /* stop here */
1520
1521 http_msg_rpver:
1522 case HTTP_MSG_RPVER:
1523 case HTTP_MSG_RPVER_SP:
1524 case HTTP_MSG_RPCODE:
1525 case HTTP_MSG_RPCODE_SP:
1526 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001527 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001528 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 if (unlikely(!ptr))
1530 return;
1531
1532 /* we have a full response and we know that we have either a CR
1533 * or an LF at <ptr>.
1534 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001535 //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 +01001536 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1537
1538 msg->sol = ptr;
1539 if (likely(*ptr == '\r'))
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1541 goto http_msg_rpline_end;
1542
1543 http_msg_rpline_end:
1544 case HTTP_MSG_RPLINE_END:
1545 /* msg->sol must point to the first of CR or LF. */
1546 EXPECT_LF_HERE(ptr, http_msg_invalid);
1547 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1548 /* stop here */
1549
1550 /*
1551 * Second, states that are specific to the request only
1552 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 http_msg_rqbefore:
1554 case HTTP_MSG_RQBEFORE:
1555 if (likely(HTTP_IS_TOKEN(*ptr))) {
1556 if (likely(ptr == buf->data)) {
1557 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001558 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 } else {
1560#if PARSE_PRESERVE_EMPTY_LINES
1561 /* only skip empty leading lines, don't remove them */
1562 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001563 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 /* Remove empty leading lines, as recommended by
1566 * RFC2616. This takes a lot of time because we
1567 * must move all the buffer backwards, but this
1568 * is rarely needed. The method above will be
1569 * cleaner when we'll be able to start sending
1570 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 buf->lr = ptr;
1573 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001574 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 msg->sol = buf->data;
1576 ptr = buf->data;
1577 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001580 /* we will need this when keep-alive will be supported
1581 hdr_idx_init(idx);
1582 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 state = HTTP_MSG_RQMETH;
1584 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1588 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(*ptr == '\n'))
1591 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1592 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 http_msg_rqbefore_cr:
1596 case HTTP_MSG_RQBEFORE_CR:
1597 EXPECT_LF_HERE(ptr, http_msg_invalid);
1598 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001599 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 http_msg_rqmeth:
1602 case HTTP_MSG_RQMETH:
1603 case HTTP_MSG_RQMETH_SP:
1604 case HTTP_MSG_RQURI:
1605 case HTTP_MSG_RQURI_SP:
1606 case HTTP_MSG_RQVER:
1607 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001608 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 if (unlikely(!ptr))
1610 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 /* we have a full request and we know that we have either a CR
1613 * or an LF at <ptr>.
1614 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 //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 +01001616 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 msg->sol = ptr;
1619 if (likely(*ptr == '\r'))
1620 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 http_msg_rqline_end:
1624 case HTTP_MSG_RQLINE_END:
1625 /* check for HTTP/0.9 request : no version information available.
1626 * msg->sol must point to the first of CR or LF.
1627 */
1628 if (unlikely(msg->sl.rq.v_l == 0))
1629 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 EXPECT_LF_HERE(ptr, http_msg_invalid);
1632 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001633 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001634
Willy Tarreau8973c702007-01-21 23:58:29 +01001635 /*
1636 * Common states below
1637 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 http_msg_hdr_first:
1639 case HTTP_MSG_HDR_FIRST:
1640 msg->sol = ptr;
1641 if (likely(!HTTP_IS_CRLF(*ptr))) {
1642 goto http_msg_hdr_name;
1643 }
1644
1645 if (likely(*ptr == '\r'))
1646 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1647 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 http_msg_hdr_name:
1650 case HTTP_MSG_HDR_NAME:
1651 /* assumes msg->sol points to the first char */
1652 if (likely(HTTP_IS_TOKEN(*ptr)))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 if (likely(*ptr == ':')) {
1656 msg->col = ptr - buf->data;
1657 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1658 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 http_msg_hdr_l1_sp:
1663 case HTTP_MSG_HDR_L1_SP:
1664 /* assumes msg->sol points to the first char and msg->col to the colon */
1665 if (likely(HTTP_IS_SPHT(*ptr)))
1666 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 /* header value can be basically anything except CR/LF */
1669 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (likely(!HTTP_IS_CRLF(*ptr))) {
1672 goto http_msg_hdr_val;
1673 }
1674
1675 if (likely(*ptr == '\r'))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1677 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 http_msg_hdr_l1_lf:
1680 case HTTP_MSG_HDR_L1_LF:
1681 EXPECT_LF_HERE(ptr, http_msg_invalid);
1682 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 http_msg_hdr_l1_lws:
1685 case HTTP_MSG_HDR_L1_LWS:
1686 if (likely(HTTP_IS_SPHT(*ptr))) {
1687 /* replace HT,CR,LF with spaces */
1688 for (; buf->data+msg->sov < ptr; msg->sov++)
1689 buf->data[msg->sov] = ' ';
1690 goto http_msg_hdr_l1_sp;
1691 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001692 /* we had a header consisting only in spaces ! */
1693 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 goto http_msg_complete_header;
1695
1696 http_msg_hdr_val:
1697 case HTTP_MSG_HDR_VAL:
1698 /* assumes msg->sol points to the first char, msg->col to the
1699 * colon, and msg->sov points to the first character of the
1700 * value.
1701 */
1702 if (likely(!HTTP_IS_CRLF(*ptr)))
1703 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 msg->eol = ptr;
1706 /* Note: we could also copy eol into ->eoh so that we have the
1707 * real header end in case it ends with lots of LWS, but is this
1708 * really needed ?
1709 */
1710 if (likely(*ptr == '\r'))
1711 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1712 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 http_msg_hdr_l2_lf:
1715 case HTTP_MSG_HDR_L2_LF:
1716 EXPECT_LF_HERE(ptr, http_msg_invalid);
1717 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 http_msg_hdr_l2_lws:
1720 case HTTP_MSG_HDR_L2_LWS:
1721 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1722 /* LWS: replace HT,CR,LF with spaces */
1723 for (; msg->eol < ptr; msg->eol++)
1724 *msg->eol = ' ';
1725 goto http_msg_hdr_val;
1726 }
1727 http_msg_complete_header:
1728 /*
1729 * It was a new header, so the last one is finished.
1730 * Assumes msg->sol points to the first char, msg->col to the
1731 * colon, msg->sov points to the first character of the value
1732 * and msg->eol to the first CR or LF so we know how the line
1733 * ends. We insert last header into the index.
1734 */
1735 /*
1736 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1737 write(2, msg->sol, msg->eol-msg->sol);
1738 fprintf(stderr,"\n");
1739 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1742 idx, idx->tail) < 0))
1743 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 msg->sol = ptr;
1746 if (likely(!HTTP_IS_CRLF(*ptr))) {
1747 goto http_msg_hdr_name;
1748 }
1749
1750 if (likely(*ptr == '\r'))
1751 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1752 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 http_msg_last_lf:
1755 case HTTP_MSG_LAST_LF:
1756 /* Assumes msg->sol points to the first of either CR or LF */
1757 EXPECT_LF_HERE(ptr, http_msg_invalid);
1758 ptr++;
1759 buf->lr = ptr;
1760 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001761 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 return;
1763#ifdef DEBUG_FULL
1764 default:
1765 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1766 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001767#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 }
1769 http_msg_ood:
1770 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001771 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 buf->lr = ptr;
1773 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 http_msg_invalid:
1776 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001777 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 return;
1779}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001780
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001781/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001782 * It normally returns zero, but may return 1 if it absolutely needs to be
1783 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001784 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001785 * functions. Its behaviour is rather simple :
1786 * - all enabled analysers are called in turn from the lower to the higher
1787 * bit.
1788 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02001789 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001790 * that unprocessed data will not be forwarded. But that probably depends on
1791 * the protocol. Generally it is not reset in case of errors.
1792 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02001793 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001794 * analysis).
1795 * - if an analyser thinks it has no added value anymore staying here, it must
1796 * reset its bit from the analysers flags in order not to be called anymore.
1797 *
1798 * In the future, analysers should be able to indicate that they want to be
1799 * called after XXX bytes have been received (or transfered), and the min of
1800 * all's wishes will be used to ring back (unless a special condition occurs).
1801 *
1802 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001804int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 struct buffer *req = t->req;
1807 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001808
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001809 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 +02001810 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001811 t,
1812 req,
1813 req->rex, req->wex,
1814 req->flags,
1815 req->l,
1816 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001817
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001818 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001819 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001820 struct tcp_rule *rule;
1821 int partial;
1822
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001823 /* We will abort if we encounter a read error. In theory, we
1824 * should not abort if we get a close, it might be valid,
1825 * although very unlikely. FIXME: we'll abort for now, this
1826 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001827 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001828 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001829 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001830 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001831 if (!(t->flags & SN_ERR_MASK))
1832 t->flags |= SN_ERR_CLICL;
1833 if (!(t->flags & SN_FINST_MASK))
1834 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001835 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001836 }
1837
1838 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001839 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001840 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001841 t->fe->failed_req++;
1842 if (!(t->flags & SN_ERR_MASK))
1843 t->flags |= SN_ERR_CLITO;
1844 if (!(t->flags & SN_FINST_MASK))
1845 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001846 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001847 }
1848
1849 /* We don't know whether we have enough data, so must proceed
1850 * this way :
1851 * - iterate through all rules in their declaration order
1852 * - if one rule returns MISS, it means the inspect delay is
1853 * not over yet, then return immediately, otherwise consider
1854 * it as a non-match.
1855 * - if one rule returns OK, then return OK
1856 * - if one rule returns KO, then return KO
1857 */
1858
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001859 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001860 partial = 0;
1861 else
1862 partial = ACL_PARTIAL;
1863
1864 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1865 int ret = ACL_PAT_PASS;
1866
1867 if (rule->cond) {
1868 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1869 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001870 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001871 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001872 if (!tick_isset(req->analyse_exp))
1873 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001874 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001875 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001876
Willy Tarreaub6866442008-07-14 23:54:42 +02001877 ret = acl_pass(ret);
1878 if (rule->cond->pol == ACL_COND_UNLESS)
1879 ret = !ret;
1880 }
1881
1882 if (ret) {
1883 /* we have a matching rule. */
1884 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001885 buffer_abort(req);
1886 buffer_abort(rep);
1887 //FIXME: this delete this
1888 //fd_delete(t->cli_fd);
1889 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001890 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001891 t->fe->failed_req++;
1892 if (!(t->flags & SN_ERR_MASK))
1893 t->flags |= SN_ERR_PRXCOND;
1894 if (!(t->flags & SN_FINST_MASK))
1895 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001896 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001897 }
1898 /* otherwise accept */
1899 break;
1900 }
1901 }
1902
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001903 /* if we get there, it means we have no rule which matches, or
1904 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001905 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001906 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001907 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001908 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001909
Willy Tarreau2df28e82008-08-17 15:20:19 +02001910 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 /*
1912 * Now parse the partial (or complete) lines.
1913 * We will check the request syntax, and also join multi-line
1914 * headers. An index of all the lines will be elaborated while
1915 * parsing.
1916 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001917 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001920 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 * req->data + req->eoh = end of processed headers / start of current one
1922 * req->data + req->eol = end of current header or line (LF or CRLF)
1923 * req->lr = first non-visited byte
1924 * req->r = end of data
1925 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001926
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001928 struct http_txn *txn = &t->txn;
1929 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001931
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001933 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001934
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 /* 1: we might have to print this header in debug mode */
1936 if (unlikely((global.mode & MODE_DEBUG) &&
1937 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001938 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001940
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001941 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 eol = sol + msg->sl.rq.l;
1943 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001944
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001945 sol += hdr_idx_first_pos(&txn->hdr_idx);
1946 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001947
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001948 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001949 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001950 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001951 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1952 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 }
1954 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001955
Willy Tarreau58f10d72006-12-04 02:26:12 +01001956
1957 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001958 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001959 * If not so, we check the FD and buffer states before leaving.
1960 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001961 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1962 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001963 *
1964 */
1965
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001966 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967 /*
1968 * First, let's catch bad requests.
1969 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001970 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001971 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001972
1973 /* 1: Since we are in header mode, if there's no space
1974 * left for headers, we won't be able to free more
1975 * later, so the session will never terminate. We
1976 * must terminate it now.
1977 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001978 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001979 /* FIXME: check if URI is set and return Status
1980 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981 */
Willy Tarreau06619262006-12-17 08:37:22 +01001982 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001983 }
1984
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001985 /* 2: have we encountered a read error ? */
1986 else if (req->flags & BF_READ_ERROR) {
1987 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001988 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001989 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001990 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001991 if (!(t->flags & SN_ERR_MASK))
1992 t->flags |= SN_ERR_CLICL;
1993 if (!(t->flags & SN_FINST_MASK))
1994 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001995 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001996 }
1997
1998 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001999 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002000 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002001 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01002002 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002003 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002004 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002005 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002006 if (!(t->flags & SN_ERR_MASK))
2007 t->flags |= SN_ERR_CLITO;
2008 if (!(t->flags & SN_FINST_MASK))
2009 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002010 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002011 }
2012
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002013 /* 4: have we encountered a close ? */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002014 else if (req->flags & BF_SHUTR) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002015 txn->status = 400;
2016 client_retnclose(t, error_message(t, HTTP_ERR_400));
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 Tarreauc65a3ba2008-08-11 23:42:50 +02002019 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002020
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002021 if (!(t->flags & SN_ERR_MASK))
2022 t->flags |= SN_ERR_CLICL;
2023 if (!(t->flags & SN_FINST_MASK))
2024 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002025 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002026 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002027
Willy Tarreau3da77c52008-08-29 09:58:42 +02002028 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002029 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002030 if (!tick_isset(req->analyse_exp))
2031 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002032
2033 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02002034 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002035 }
2036
2037
2038 /****************************************************************
2039 * More interesting part now : we know that we have a complete *
2040 * request which at least looks like HTTP. We have an indicator *
2041 * of each header's length, so we can parse them quickly. *
2042 ****************************************************************/
2043
Willy Tarreau2df28e82008-08-17 15:20:19 +02002044 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002045 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002046
Willy Tarreau9cdde232007-05-02 20:58:19 +02002047 /* ensure we keep this pointer to the beginning of the message */
2048 msg->sol = req->data + msg->som;
2049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002050 /*
2051 * 1: identify the method
2052 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002053 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002054
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002055 /* we can make use of server redirect on GET and HEAD */
2056 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2057 t->flags |= SN_REDIRECTABLE;
2058
Willy Tarreau58f10d72006-12-04 02:26:12 +01002059 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002060 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01002061 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002062 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002063 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002064 if (unlikely((t->fe->monitor_uri_len != 0) &&
2065 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2066 !memcmp(&req->data[msg->sl.rq.u],
2067 t->fe->monitor_uri,
2068 t->fe->monitor_uri_len))) {
2069 /*
2070 * We have found the monitor URI
2071 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01002072 struct acl_cond *cond;
2073 cur_proxy = t->fe;
2074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002075 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002076
2077 /* Check if we want to fail this monitor request or not */
2078 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
2079 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002080
2081 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01002082 if (cond->pol == ACL_COND_UNLESS)
2083 ret = !ret;
2084
2085 if (ret) {
2086 /* we fail this request, let's return 503 service unavail */
2087 txn->status = 503;
2088 client_retnclose(t, error_message(t, HTTP_ERR_503));
2089 goto return_prx_cond;
2090 }
2091 }
2092
2093 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002094 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002095 client_retnclose(t, &http_200_chunk);
2096 goto return_prx_cond;
2097 }
2098
2099 /*
2100 * 3: Maybe we have to copy the original REQURI for the logs ?
2101 * Note: we cannot log anymore if the request has been
2102 * classified as invalid.
2103 */
2104 if (unlikely(t->logs.logwait & LW_REQ)) {
2105 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002106 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002107 int urilen = msg->sl.rq.l;
2108
2109 if (urilen >= REQURI_LEN)
2110 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002111 memcpy(txn->uri, &req->data[msg->som], urilen);
2112 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002113
2114 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002115 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002116 } else {
2117 Alert("HTTP logging : out of memory.\n");
2118 }
2119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002120
Willy Tarreau06619262006-12-17 08:37:22 +01002121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002122 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2123 if (unlikely(msg->sl.rq.v_l == 0)) {
2124 int delta;
2125 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002126 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002127 cur_end = msg->sol + msg->sl.rq.l;
2128 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130 if (msg->sl.rq.u_l == 0) {
2131 /* if no URI was set, add "/" */
2132 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2133 cur_end += delta;
2134 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002135 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002136 /* add HTTP version */
2137 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2138 msg->eoh += delta;
2139 cur_end += delta;
2140 cur_end = (char *)http_parse_reqline(msg, req->data,
2141 HTTP_MSG_RQMETH,
2142 msg->sol, cur_end + 1,
2143 NULL, NULL);
2144 if (unlikely(!cur_end))
2145 goto return_bad_req;
2146
2147 /* we have a full HTTP/1.0 request now and we know that
2148 * we have either a CR or an LF at <ptr>.
2149 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002150 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002151 }
2152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002153
2154 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002155 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002156 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002157 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002158
2159 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002160 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002161 * As opposed to version 1.2, now they will be evaluated in the
2162 * filters order and not in the header order. This means that
2163 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002164 *
2165 * We can now check whether we want to switch to another
2166 * backend, in which case we will re-check the backend's
2167 * filters and various options. In order to support 3-level
2168 * switching, here's how we should proceed :
2169 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002170 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002171 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002172 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002173 * There cannot be any switch from there, so ->be cannot be
2174 * changed anymore.
2175 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002176 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002177 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002178 * The response path will be able to apply either ->be, or
2179 * ->be then ->fe filters in order to match the reverse of
2180 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002181 */
2182
Willy Tarreau06619262006-12-17 08:37:22 +01002183 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002184 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002185 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002186 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002187 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002188
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002189 /* first check whether we have some ACLs set to redirect this request */
2190 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2191 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002192
2193 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002194 if (rule->cond->pol == ACL_COND_UNLESS)
2195 ret = !ret;
2196
2197 if (ret) {
2198 struct chunk rdr = { trash, 0 };
2199 const char *msg_fmt;
2200
2201 /* build redirect message */
2202 switch(rule->code) {
2203 case 303:
2204 rdr.len = strlen(HTTP_303);
2205 msg_fmt = HTTP_303;
2206 break;
2207 case 301:
2208 rdr.len = strlen(HTTP_301);
2209 msg_fmt = HTTP_301;
2210 break;
2211 case 302:
2212 default:
2213 rdr.len = strlen(HTTP_302);
2214 msg_fmt = HTTP_302;
2215 break;
2216 }
2217
2218 if (unlikely(rdr.len > sizeof(trash)))
2219 goto return_bad_req;
2220 memcpy(rdr.str, msg_fmt, rdr.len);
2221
2222 switch(rule->type) {
2223 case REDIRECT_TYPE_PREFIX: {
2224 const char *path;
2225 int pathlen;
2226
2227 path = http_get_path(txn);
2228 /* build message using path */
2229 if (path) {
2230 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2231 } else {
2232 path = "/";
2233 pathlen = 1;
2234 }
2235
2236 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2237 goto return_bad_req;
2238
2239 /* add prefix */
2240 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2241 rdr.len += rule->rdr_len;
2242
2243 /* add path */
2244 memcpy(rdr.str + rdr.len, path, pathlen);
2245 rdr.len += pathlen;
2246 break;
2247 }
2248 case REDIRECT_TYPE_LOCATION:
2249 default:
2250 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2251 goto return_bad_req;
2252
2253 /* add location */
2254 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2255 rdr.len += rule->rdr_len;
2256 break;
2257 }
2258
2259 /* add end of headers */
2260 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2261 rdr.len += 4;
2262
2263 txn->status = rule->code;
2264 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002265 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002266 client_retnclose(t, &rdr);
2267 goto return_prx_cond;
2268 }
2269 }
2270
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002271 /* first check whether we have some ACLs set to block this request */
2272 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002273 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002274
2275 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002276 if (cond->pol == ACL_COND_UNLESS)
2277 ret = !ret;
2278
2279 if (ret) {
2280 txn->status = 403;
2281 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002282 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002283 client_retnclose(t, error_message(t, HTTP_ERR_403));
2284 goto return_prx_cond;
2285 }
2286 }
2287
Willy Tarreau06619262006-12-17 08:37:22 +01002288 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002289 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002290 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2291 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002292 }
2293
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002294 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2295 /* to ensure correct connection accounting on
2296 * the backend, we count the connection for the
2297 * one managing the queue.
2298 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002299 t->be->beconn++;
2300 if (t->be->beconn > t->be->beconn_max)
2301 t->be->beconn_max = t->be->beconn;
2302 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002303 t->flags |= SN_BE_ASSIGNED;
2304 }
2305
Willy Tarreau06619262006-12-17 08:37:22 +01002306 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002307 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002308 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002309 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002310 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002311 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002312 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002313 goto return_prx_cond;
2314 }
2315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002316 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002317 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002318 !(t->flags & SN_CONN_CLOSED)) {
2319 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002320 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002321 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002322
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002323 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002324 old_idx = 0;
2325
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002326 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2327 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002328 cur_ptr = cur_next;
2329 cur_end = cur_ptr + cur_hdr->len;
2330 cur_next = cur_end + cur_hdr->cr + 1;
2331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002332 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2333 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002334 /* 3 possibilities :
2335 * - we have already set Connection: close,
2336 * so we remove this line.
2337 * - we have not yet set Connection: close,
2338 * but this line indicates close. We leave
2339 * it untouched and set the flag.
2340 * - we have not yet set Connection: close,
2341 * and this line indicates non-close. We
2342 * replace it.
2343 */
2344 if (t->flags & SN_CONN_CLOSED) {
2345 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002346 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002347 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002348 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2349 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002350 cur_hdr->len = 0;
2351 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002352 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2353 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2354 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002355 cur_next += delta;
2356 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002357 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002358 }
2359 t->flags |= SN_CONN_CLOSED;
2360 }
2361 }
2362 old_idx = cur_idx;
2363 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002364 }
2365 /* add request headers from the rule sets in the same order */
2366 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2367 if (unlikely(http_header_add_tail(req,
2368 &txn->req,
2369 &txn->hdr_idx,
2370 rule_set->req_add[cur_idx])) < 0)
2371 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002372 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002374 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002375 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002376 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002377 /* we have to check the URI and auth for this request.
2378 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002379 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002380 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002381 if (stats_check_uri_auth(t, rule_set))
2382 return 1;
2383 }
2384
Willy Tarreau55ea7572007-06-17 19:56:27 +02002385 /* now check whether we have some switching rules for this request */
2386 if (!(t->flags & SN_BE_ASSIGNED)) {
2387 struct switching_rule *rule;
2388
2389 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2390 int ret;
2391
2392 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002393
2394 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002395 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002396 ret = !ret;
2397
2398 if (ret) {
2399 t->be = rule->be.backend;
2400 t->be->beconn++;
2401 if (t->be->beconn > t->be->beconn_max)
2402 t->be->beconn_max = t->be->beconn;
2403 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002404
2405 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002406 t->rep->rto = t->req->wto = t->be->timeout.server;
2407 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002408 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002409 t->flags |= SN_BE_ASSIGNED;
2410 break;
2411 }
2412 }
2413 }
2414
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002415 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2416 /* No backend was set, but there was a default
2417 * backend set in the frontend, so we use it and
2418 * loop again.
2419 */
2420 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002421 t->be->beconn++;
2422 if (t->be->beconn > t->be->beconn_max)
2423 t->be->beconn_max = t->be->beconn;
2424 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002425
2426 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002427 t->rep->rto = t->req->wto = t->be->timeout.server;
2428 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002429 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002430 t->flags |= SN_BE_ASSIGNED;
2431 }
2432 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002433
Willy Tarreau58f10d72006-12-04 02:26:12 +01002434
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002435 if (!(t->flags & SN_BE_ASSIGNED)) {
2436 /* To ensure correct connection accounting on
2437 * the backend, we count the connection for the
2438 * one managing the queue.
2439 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002440 t->be->beconn++;
2441 if (t->be->beconn > t->be->beconn_max)
2442 t->be->beconn_max = t->be->beconn;
2443 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002444 t->flags |= SN_BE_ASSIGNED;
2445 }
2446
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002447 /*
2448 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002449 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002450 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002451 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002452 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002454 /*
2455 * If HTTP PROXY is set we simply get remote server address
2456 * parsing incoming request.
2457 */
2458 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2459 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2460 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002461
Willy Tarreau2a324282006-12-05 00:05:46 +01002462 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002463 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002464 * so let's do the same now.
2465 */
2466
2467 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002468 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002469 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002470 }
2471
2472
2473 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002474 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002475 * Note that doing so might move headers in the request, but
2476 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002477 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002478 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002479 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2480 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002481 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482
Willy Tarreau58f10d72006-12-04 02:26:12 +01002483
Willy Tarreau2a324282006-12-05 00:05:46 +01002484 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002485 * 9: add X-Forwarded-For if either the frontend or the backend
2486 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002487 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002488 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002489 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002490 /* Add an X-Forwarded-For header unless the source IP is
2491 * in the 'except' network range.
2492 */
2493 if ((!t->fe->except_mask.s_addr ||
2494 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2495 != t->fe->except_net.s_addr) &&
2496 (!t->be->except_mask.s_addr ||
2497 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2498 != t->be->except_net.s_addr)) {
2499 int len;
2500 unsigned char *pn;
2501 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002502
Ross Westaf72a1d2008-08-03 10:51:45 +02002503 /* Note: we rely on the backend to get the header name to be used for
2504 * x-forwarded-for, because the header is really meant for the backends.
2505 * However, if the backend did not specify any option, we have to rely
2506 * on the frontend's header name.
2507 */
2508 if (t->be->fwdfor_hdr_len) {
2509 len = t->be->fwdfor_hdr_len;
2510 memcpy(trash, t->be->fwdfor_hdr_name, len);
2511 } else {
2512 len = t->fe->fwdfor_hdr_len;
2513 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2514 }
2515 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002516
Ross Westaf72a1d2008-08-03 10:51:45 +02002517 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002518 &txn->hdr_idx, trash, len)) < 0)
2519 goto return_bad_req;
2520 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002521 }
2522 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002523 /* FIXME: for the sake of completeness, we should also support
2524 * 'except' here, although it is mostly useless in this case.
2525 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002526 int len;
2527 char pn[INET6_ADDRSTRLEN];
2528 inet_ntop(AF_INET6,
2529 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2530 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002531
2532 /* Note: we rely on the backend to get the header name to be used for
2533 * x-forwarded-for, because the header is really meant for the backends.
2534 * However, if the backend did not specify any option, we have to rely
2535 * on the frontend's header name.
2536 */
2537 if (t->be->fwdfor_hdr_len) {
2538 len = t->be->fwdfor_hdr_len;
2539 memcpy(trash, t->be->fwdfor_hdr_name, len);
2540 } else {
2541 len = t->fe->fwdfor_hdr_len;
2542 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2543 }
2544 len += sprintf(trash + len, ": %s", pn);
2545
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002546 if (unlikely(http_header_add_tail2(req, &txn->req,
2547 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002548 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002549 }
2550 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002551
Willy Tarreau2a324282006-12-05 00:05:46 +01002552 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002553 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002554 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002555 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002556 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002557 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002558 if ((unlikely(msg->sl.rq.v_l != 8) ||
2559 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2560 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002561 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002562 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002563 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002564 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002565 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2566 * If not assigned, perhaps we are balancing on url_param, but this is a
2567 * POST; and the parameters are in the body, maybe scan there to find our server.
2568 * (unless headers overflowed the buffer?)
2569 */
2570 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2571 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002572 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002573 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2574 /* are there enough bytes here? total == l || r || rlim ?
2575 * len is unsigned, but eoh is int,
2576 * how many bytes of body have we received?
2577 * eoh is the first empty line of the header
2578 */
2579 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002580 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 +02002581
2582 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2583 * We can't assume responsibility for the server's decision,
2584 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2585 * We also can't change our mind later, about which server to choose, so round robin.
2586 */
2587 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2588 struct hdr_ctx ctx;
2589 ctx.idx = 0;
2590 /* Expect is allowed in 1.1, look for it */
2591 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2592 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002593 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002594 /* We can't reliablly stall and wait for data, because of
2595 * .NET clients that don't conform to rfc2616; so, no need for
2596 * the next block to check length expectations.
2597 * We could send 100 status back to the client, but then we need to
2598 * re-write headers, and send the message. And this isn't the right
2599 * place for that action.
2600 * TODO: support Expect elsewhere and delete this block.
2601 */
2602 goto end_check_maybe_wait_for_body;
2603 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002604
2605 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002606 /* nothing to do, we got enough */
2607 } else {
2608 /* limit implies we are supposed to need this many bytes
2609 * to find the parameter. Let's see how many bytes we can wait for.
2610 */
2611 long long hint = len;
2612 struct hdr_ctx ctx;
2613 ctx.idx = 0;
2614 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002615 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002616 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002617 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002618 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002619 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002620 ctx.idx = 0;
2621 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2622 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002623 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002624 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002625 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002626 hint = 0; /* parse failure, untrusted client */
2627 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002628 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002629 msg->hdr_content_len = hint;
2630 else
2631 hint = 0; /* bad client, sent negative length */
2632 }
2633 }
2634 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002635 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002636 hint = t->be->url_param_post_limit;
2637 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002638 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002639 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002640 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002641 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002642 /* else... There are no body bytes to wait for */
2643 }
2644 }
2645 }
2646 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002647
Willy Tarreau2a324282006-12-05 00:05:46 +01002648 /*************************************************************
2649 * OK, that's finished for the headers. We have done what we *
2650 * could. Let's switch to the DATA state. *
2651 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002652
Willy Tarreaue393fe22008-08-16 22:18:07 +02002653 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002654 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002655
Willy Tarreau1fa31262007-12-03 00:36:16 +01002656 /* When a connection is tarpitted, we use the tarpit timeout,
2657 * which may be the same as the connect timeout if unspecified.
2658 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002659 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002660 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002661 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002662 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002663 /* flush the request so that we can drop the connection early
2664 * if the client closes first.
2665 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002666 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002667 req->analysers |= AN_REQ_HTTP_TARPIT;
2668 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2669 if (!req->analyse_exp)
2670 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002673 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002674 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002675
2676 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002677 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002678 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002679 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002680 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002681 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002682 return_prx_cond:
2683 if (!(t->flags & SN_ERR_MASK))
2684 t->flags |= SN_ERR_PRXCOND;
2685 if (!(t->flags & SN_FINST_MASK))
2686 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002687 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002688 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002689 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002690 }
2691
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002692 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2693 struct http_txn *txn = &t->txn;
2694
2695 /* This connection is being tarpitted. The CLIENT side has
2696 * already set the connect expiration date to the right
2697 * timeout. We just have to check that the client is still
2698 * there and that the timeout has not expired.
2699 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002700 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002701 !tick_is_expired(req->analyse_exp, now_ms))
2702 return 0;
2703
2704 /* We will set the queue timer to the time spent, just for
2705 * logging purposes. We fake a 500 server error, so that the
2706 * attacker will not suspect his connection has been tarpitted.
2707 * It will not cause trouble to the logs because we can exclude
2708 * the tarpitted connections by filtering on the 'PT' status flags.
2709 */
2710 trace_term(t, TT_HTTP_SRV_2);
2711 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2712
2713 txn->status = 500;
2714 if (req->flags != BF_READ_ERROR)
2715 client_retnclose(t, error_message(t, HTTP_ERR_500));
2716
2717 req->analysers = 0;
2718 req->analyse_exp = TICK_ETERNITY;
2719
2720 t->fe->failed_req++;
2721 if (!(t->flags & SN_ERR_MASK))
2722 t->flags |= SN_ERR_PRXCOND;
2723 if (!(t->flags & SN_FINST_MASK))
2724 t->flags |= SN_FINST_T;
2725 return 0;
2726 }
2727
Willy Tarreau2df28e82008-08-17 15:20:19 +02002728 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002729 /* We have to parse the HTTP request body to find any required data.
2730 * "balance url_param check_post" should have been the only way to get
2731 * into this. We were brought here after HTTP header analysis, so all
2732 * related structures are ready.
2733 */
2734 struct http_msg *msg = &t->txn.req;
2735 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2736 long long limit = t->be->url_param_post_limit;
2737 struct hdr_ctx ctx;
2738
2739 ctx.idx = 0;
2740
2741 /* now if we have a length, we'll take the hint */
2742 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2743 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2744 unsigned int chunk = 0;
2745 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2746 char c = msg->sol[body];
2747 if (ishex(c)) {
2748 unsigned int hex = toupper(c) - '0';
2749 if (hex > 9)
2750 hex -= 'A' - '9' - 1;
2751 chunk = (chunk << 4) | hex;
2752 } else
2753 break;
2754 body++;
2755 }
2756 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002757 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002758
2759 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002760 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002761
2762 body += 2; // skip CRLF
2763
2764 /* if we support more then one chunk here, we have to do it again when assigning server
2765 * 1. how much entity data do we have? new var
2766 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2767 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2768 */
2769
2770 if (chunk < limit)
2771 limit = chunk; /* only reading one chunk */
2772 } else {
2773 if (msg->hdr_content_len < limit)
2774 limit = msg->hdr_content_len;
2775 }
2776
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002777 http_body_end:
2778 /* we leave once we know we have nothing left to do. This means that we have
2779 * enough bytes, or that we know we'll not get any more (buffer full, read
2780 * buffer closed).
2781 */
2782 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002783 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002784 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002785 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002786 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002787 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002788 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002789 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002790 else {
2791 /* Not enough data. We'll re-use the http-request
2792 * timeout here. Ideally, we should set the timeout
2793 * relative to the accept() date. We just set the
2794 * request timeout once at the beginning of the
2795 * request.
2796 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002797 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02002798 if (!tick_isset(req->analyse_exp))
2799 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2800 return 0;
2801 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002802 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002803
Willy Tarreau2df28e82008-08-17 15:20:19 +02002804 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2805 * probably reduce one day's debugging session.
2806 */
2807#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002808 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 +02002809 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2810 __FILE__, __LINE__, req->analysers);
2811 ABORT_NOW();
2812 }
2813#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002814 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002815 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002816}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002817
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002818/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002819 * It normally returns zero, but may return 1 if it absolutely needs to be
2820 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002821 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002822 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002823 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002824int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002825{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002826 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002827 struct buffer *req = t->req;
2828 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002829
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002830 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 +02002831 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002832 t,
2833 rep,
2834 rep->rex, rep->wex,
2835 rep->flags,
2836 rep->l,
2837 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002838
Willy Tarreau2df28e82008-08-17 15:20:19 +02002839 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002840 /*
2841 * Now parse the partial (or complete) lines.
2842 * We will check the response syntax, and also join multi-line
2843 * headers. An index of all the lines will be elaborated while
2844 * parsing.
2845 *
2846 * For the parsing, we use a 28 states FSM.
2847 *
2848 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002849 * rep->data + rep->som = beginning of response
2850 * rep->data + rep->eoh = end of processed headers / start of current one
2851 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002852 * rep->lr = first non-visited byte
2853 * rep->r = end of data
2854 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002855
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 int cur_idx;
2857 struct http_msg *msg = &txn->rsp;
2858 struct proxy *cur_proxy;
2859
2860 if (likely(rep->lr < rep->r))
2861 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2862
2863 /* 1: we might have to print this header in debug mode */
2864 if (unlikely((global.mode & MODE_DEBUG) &&
2865 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2866 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2867 char *eol, *sol;
2868
2869 sol = rep->data + msg->som;
2870 eol = sol + msg->sl.rq.l;
2871 debug_hdr("srvrep", t, sol, eol);
2872
2873 sol += hdr_idx_first_pos(&txn->hdr_idx);
2874 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2875
2876 while (cur_idx) {
2877 eol = sol + txn->hdr_idx.v[cur_idx].len;
2878 debug_hdr("srvhdr", t, sol, eol);
2879 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2880 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002881 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002884 /*
2885 * Now we quickly check if we have found a full valid response.
2886 * If not so, we check the FD and buffer states before leaving.
2887 * A full response is indicated by the fact that we have seen
2888 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2889 * responses are checked first.
2890 *
2891 * Depending on whether the client is still there or not, we
2892 * may send an error response back or not. Note that normally
2893 * we should only check for HTTP status there, and check I/O
2894 * errors somewhere else.
2895 */
2896
2897 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002898 /* Invalid response */
2899 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2900 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002901 //buffer_shutr(rep);
2902 //buffer_shutw(req);
2903 //fd_delete(req->cons->fd);
2904 //req->cons->state = SI_ST_CLO;
2905 buffer_shutr_now(rep);
2906 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002907 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002908 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002909 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002910 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002911 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002912 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002913 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002914 txn->status = 502;
2915 client_return(t, error_message(t, HTTP_ERR_502));
2916 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002917 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002918 if (!(t->flags & SN_FINST_MASK))
2919 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002920
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002921 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2922 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923
Willy Tarreaudafde432008-08-17 01:00:46 +02002924 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002925 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002926 /* too large response does not fit in buffer. */
2927 else if (rep->flags & BF_FULL) {
2928 goto hdr_response_bad;
2929 }
2930 /* read error */
2931 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002932 buffer_shutr_now(rep);
2933 buffer_shutw_now(req);
2934 //fd_delete(req->cons->fd);
2935 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002936 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002937 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002938 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002939 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002940 //}
2941 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002942 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002943 txn->status = 502;
2944 client_return(t, error_message(t, HTTP_ERR_502));
2945 if (!(t->flags & SN_ERR_MASK))
2946 t->flags |= SN_ERR_SRVCL;
2947 if (!(t->flags & SN_FINST_MASK))
2948 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002949
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002950 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2951 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002952
Willy Tarreaudafde432008-08-17 01:00:46 +02002953 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002954 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002956 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002957 buffer_shutr_now(rep);
2958 buffer_shutw_now(req);
2959 //fd_delete(req->cons->fd);
2960 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002961 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002962 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002963 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002964 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002965 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002966 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002967 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002968 txn->status = 504;
2969 client_return(t, error_message(t, HTTP_ERR_504));
2970 if (!(t->flags & SN_ERR_MASK))
2971 t->flags |= SN_ERR_SRVTO;
2972 if (!(t->flags & SN_FINST_MASK))
2973 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002974
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002975 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2976 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002977 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002979 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002980 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002981 buffer_shutr_now(rep);
2982 buffer_shutw_now(req);
2983 //fd_delete(req->cons->fd);
2984 //req->cons->state = SI_ST_CLO;
2985 if (t->srv) {
2986 //t->srv->cur_sess--;
2987 t->srv->failed_resp++;
2988 //sess_change_server(t, NULL);
2989 }
2990 t->be->failed_resp++;
2991 rep->analysers = 0;
2992 txn->status = 502;
2993 client_return(t, error_message(t, HTTP_ERR_502));
2994 if (!(t->flags & SN_ERR_MASK))
2995 t->flags |= SN_ERR_SRVCL;
2996 if (!(t->flags & SN_FINST_MASK))
2997 t->flags |= SN_FINST_H;
2998
2999 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3000 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003001
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003002 return 0;
3003 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003004 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003005 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003006 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003007
Willy Tarreau21d2af32008-02-14 20:25:24 +01003008
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003009 /*****************************************************************
3010 * More interesting part now : we know that we have a complete *
3011 * response which at least looks like HTTP. We have an indicator *
3012 * of each header's length, so we can parse them quickly. *
3013 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01003014
Willy Tarreau2df28e82008-08-17 15:20:19 +02003015 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02003016
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003017 /* ensure we keep this pointer to the beginning of the message */
3018 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003019
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 /*
3021 * 1: get the status code and check for cacheability.
3022 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01003023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003024 t->logs.logwait &= ~LW_RESP;
3025 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003026
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003027 switch (txn->status) {
3028 case 200:
3029 case 203:
3030 case 206:
3031 case 300:
3032 case 301:
3033 case 410:
3034 /* RFC2616 @13.4:
3035 * "A response received with a status code of
3036 * 200, 203, 206, 300, 301 or 410 MAY be stored
3037 * by a cache (...) unless a cache-control
3038 * directive prohibits caching."
3039 *
3040 * RFC2616 @9.5: POST method :
3041 * "Responses to this method are not cacheable,
3042 * unless the response includes appropriate
3043 * Cache-Control or Expires header fields."
3044 */
3045 if (likely(txn->meth != HTTP_METH_POST) &&
3046 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3047 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3048 break;
3049 default:
3050 break;
3051 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003052
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003053 /*
3054 * 2: we may need to capture headers
3055 */
3056 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3057 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3058 txn->rsp.cap, t->fe->rsp_cap);
3059
3060 /*
3061 * 3: we will have to evaluate the filters.
3062 * As opposed to version 1.2, now they will be evaluated in the
3063 * filters order and not in the header order. This means that
3064 * each filter has to be validated among all headers.
3065 *
3066 * Filters are tried with ->be first, then with ->fe if it is
3067 * different from ->be.
3068 */
3069
3070 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3071
3072 cur_proxy = t->be;
3073 while (1) {
3074 struct proxy *rule_set = cur_proxy;
3075
3076 /* try headers filters */
3077 if (rule_set->rsp_exp != NULL) {
3078 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3079 return_bad_resp:
3080 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003081 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003082 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003083 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003084 }
3085 cur_proxy->failed_resp++;
3086 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003087 buffer_shutr_now(rep);
3088 buffer_shutw_now(req);
3089 //fd_delete(req->cons->fd);
3090 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003091 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003092 txn->status = 502;
3093 client_return(t, error_message(t, HTTP_ERR_502));
3094 if (!(t->flags & SN_ERR_MASK))
3095 t->flags |= SN_ERR_PRXCOND;
3096 if (!(t->flags & SN_FINST_MASK))
3097 t->flags |= SN_FINST_H;
3098 /* We used to have a free connection slot. Since we'll never use it,
3099 * we have to inform the server that it may be used by another session.
3100 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003101 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3102 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003103 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003104 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003105 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003106
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 /* has the response been denied ? */
3108 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003109 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003110 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003111 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003112 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003113 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003114 cur_proxy->denied_resp++;
3115 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003117
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003118 /* We might have to check for "Connection:" */
3119 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3120 !(t->flags & SN_CONN_CLOSED)) {
3121 char *cur_ptr, *cur_end, *cur_next;
3122 int cur_idx, old_idx, delta, val;
3123 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003125 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3126 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003127
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3129 cur_hdr = &txn->hdr_idx.v[cur_idx];
3130 cur_ptr = cur_next;
3131 cur_end = cur_ptr + cur_hdr->len;
3132 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003134 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3135 if (val) {
3136 /* 3 possibilities :
3137 * - we have already set Connection: close,
3138 * so we remove this line.
3139 * - we have not yet set Connection: close,
3140 * but this line indicates close. We leave
3141 * it untouched and set the flag.
3142 * - we have not yet set Connection: close,
3143 * and this line indicates non-close. We
3144 * replace it.
3145 */
3146 if (t->flags & SN_CONN_CLOSED) {
3147 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3148 txn->rsp.eoh += delta;
3149 cur_next += delta;
3150 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3151 txn->hdr_idx.used--;
3152 cur_hdr->len = 0;
3153 } else {
3154 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3155 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3156 "close", 5);
3157 cur_next += delta;
3158 cur_hdr->len += delta;
3159 txn->rsp.eoh += delta;
3160 }
3161 t->flags |= SN_CONN_CLOSED;
3162 }
3163 }
3164 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003165 }
3166 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003167
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003168 /* add response headers from the rule sets in the same order */
3169 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3170 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3171 rule_set->rsp_add[cur_idx])) < 0)
3172 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003173 }
3174
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003175 /* check whether we're already working on the frontend */
3176 if (cur_proxy == t->fe)
3177 break;
3178 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003179 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003181 /*
3182 * 4: check for server cookie.
3183 */
3184 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3185 || (t->be->options & PR_O_CHK_CACHE))
3186 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187
Willy Tarreaubaaee002006-06-26 02:48:02 +02003188
Willy Tarreaua15645d2007-03-18 16:22:39 +01003189 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003190 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003191 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003192 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3193 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195 /*
3196 * 6: add server cookie in the response if needed
3197 */
3198 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3199 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3200 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003202 /* the server is known, it's not the one the client requested, we have to
3203 * insert a set-cookie here, except if we want to insert only on POST
3204 * requests and this one isn't. Note that servers which don't have cookies
3205 * (eg: some backup servers) will return a full cookie removal request.
3206 */
3207 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3208 t->be->cookie_name,
3209 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003210
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003211 if (t->be->cookie_domain)
3212 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003214 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3215 trash, len)) < 0)
3216 goto return_bad_resp;
3217 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003218
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 /* Here, we will tell an eventual cache on the client side that we don't
3220 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3221 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3222 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3223 */
3224 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003225
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003226 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3227
3228 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3229 "Cache-control: private", 22)) < 0)
3230 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003231 }
3232 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003233
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003235 /*
3236 * 7: check if result will be cacheable with a cookie.
3237 * We'll block the response if security checks have caught
3238 * nasty things such as a cacheable cookie.
3239 */
3240 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3241 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3242 (t->be->options & PR_O_CHK_CACHE)) {
3243
3244 /* we're in presence of a cacheable response containing
3245 * a set-cookie header. We'll block it as requested by
3246 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003247 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003248 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003249 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003250 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003251 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003252 }
3253 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003254
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003255 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3256 t->be->id, t->srv?t->srv->id:"<dispatch>");
3257 send_log(t->be, LOG_ALERT,
3258 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3259 t->be->id, t->srv?t->srv->id:"<dispatch>");
3260 goto return_srv_prx_502;
3261 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003262
3263 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264 * 8: add "Connection: close" if needed and not yet set.
3265 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003266 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003267 if (!(t->flags & SN_CONN_CLOSED) &&
3268 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3269 if ((unlikely(msg->sl.st.v_l != 8) ||
3270 unlikely(req->data[msg->som + 7] != '0')) &&
3271 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3272 "Connection: close", 17)) < 0)
3273 goto return_bad_resp;
3274 t->flags |= SN_CONN_CLOSED;
3275 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003276
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003277 /*************************************************************
3278 * OK, that's finished for the headers. We have done what we *
3279 * could. Let's switch to the DATA state. *
3280 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003281
Willy Tarreaue393fe22008-08-16 22:18:07 +02003282 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003283 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003284
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003285#ifdef CONFIG_HAP_TCPSPLICE
3286 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3287 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003288 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003289 }
3290#endif
3291 /* if the user wants to log as soon as possible, without counting
3292 * bytes from the server, then this is the right moment. We have
3293 * to temporarily assign bytes_out to log what we currently have.
3294 */
3295 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3296 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3297 t->logs.bytes_out = txn->rsp.eoh;
3298 if (t->fe->to_log & LW_REQ)
3299 http_sess_log(t);
3300 else
3301 tcp_sess_log(t);
3302 t->logs.bytes_out = 0;
3303 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003304
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003305 /* Note: we must not try to cheat by jumping directly to DATA,
3306 * otherwise we would not let the client side wake up.
3307 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003308
Willy Tarreaudafde432008-08-17 01:00:46 +02003309 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003310 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003311
Willy Tarreau2df28e82008-08-17 15:20:19 +02003312 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3313 * probably reduce one day's debugging session.
3314 */
3315#ifdef DEBUG_DEV
3316 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3317 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3318 __FILE__, __LINE__, rep->analysers);
3319 ABORT_NOW();
3320 }
3321#endif
3322 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003323 return 0;
3324}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003325
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003326///*
3327// * Manages the client FSM and its socket. It normally returns zero, but may
3328// * return 1 if it absolutely wants to be called again.
3329// *
3330// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3331// * but CL_STCLOSE.
3332// */
3333//int process_cli(struct session *t)
3334//{
3335// struct buffer *req = t->req;
3336// struct buffer *rep = t->rep;
3337//
3338// 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",
3339// now_ms, __FUNCTION__,
3340// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3341// cli_stnames[t->cli_state],
3342// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3343// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3344// req->rex, rep->wex,
3345// req->flags, rep->flags,
3346// req->l, rep->l);
3347//
3348// update_state:
3349// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3350// * still set this state (and will do until unix sockets are converted).
3351// */
3352// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3353// /* we can skip most of the tests at once if some conditions are not met */
3354// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3355// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3356// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3357// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3358// (!(rep->flags & BF_SHUTW) &&
3359// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3360// goto update_timeouts;
3361//
3362// /* read or write error */
3363// if (fdtab[t->cli_fd].state == FD_STERROR) {
3364// buffer_shutr(req);
3365// req->flags |= BF_READ_ERROR;
3366// buffer_shutw(rep);
3367// rep->flags |= BF_WRITE_ERROR;
3368// fd_delete(t->cli_fd);
3369// t->cli_state = CL_STCLOSE;
3370// trace_term(t, TT_HTTP_CLI_1);
3371// if (!req->analysers) {
3372// if (!(t->flags & SN_ERR_MASK))
3373// t->flags |= SN_ERR_CLICL;
3374// if (!(t->flags & SN_FINST_MASK)) {
3375// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3376// t->flags |= SN_FINST_Q;
3377// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3378// t->flags |= SN_FINST_C;
3379// else
3380// t->flags |= SN_FINST_D;
3381// }
3382// }
3383// goto update_state;
3384// }
3385// /* last read, or end of server write */
3386// else if (!(req->flags & BF_SHUTR) && /* not already done */
3387// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3388// buffer_shutr(req);
3389// if (!(rep->flags & BF_SHUTW)) {
3390// EV_FD_CLR(t->cli_fd, DIR_RD);
3391// trace_term(t, TT_HTTP_CLI_2);
3392// } else {
3393// /* output was already closed */
3394// fd_delete(t->cli_fd);
3395// t->cli_state = CL_STCLOSE;
3396// trace_term(t, TT_HTTP_CLI_3);
3397// }
3398// goto update_state;
3399// }
3400// /* last server read and buffer empty : we only check them when we're
3401// * allowed to forward the data.
3402// */
3403// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3404// ((rep->flags & BF_SHUTW_NOW) ||
3405// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3406// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3407// buffer_shutw(rep);
3408// if (!(req->flags & BF_SHUTR)) {
3409// EV_FD_CLR(t->cli_fd, DIR_WR);
3410// shutdown(t->cli_fd, SHUT_WR);
3411// trace_term(t, TT_HTTP_CLI_4);
3412// } else {
3413// fd_delete(t->cli_fd);
3414// t->cli_state = CL_STCLOSE;
3415// trace_term(t, TT_HTTP_CLI_5);
3416// }
3417// goto update_state;
3418// }
3419// /* read timeout */
3420// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3421// buffer_shutr(req);
3422// if (!(rep->flags & BF_SHUTW)) {
3423// EV_FD_CLR(t->cli_fd, DIR_RD);
3424// trace_term(t, TT_HTTP_CLI_6);
3425// } else {
3426// /* output was already closed */
3427// fd_delete(t->cli_fd);
3428// t->cli_state = CL_STCLOSE;
3429// trace_term(t, TT_HTTP_CLI_7);
3430// }
3431// if (!req->analysers) {
3432// if (!(t->flags & SN_ERR_MASK))
3433// t->flags |= SN_ERR_CLITO;
3434// if (!(t->flags & SN_FINST_MASK)) {
3435// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3436// t->flags |= SN_FINST_Q;
3437// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3438// t->flags |= SN_FINST_C;
3439// else
3440// t->flags |= SN_FINST_D;
3441// }
3442// }
3443// goto update_state;
3444// }
3445// /* write timeout */
3446// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3447// buffer_shutw(rep);
3448// if (!(req->flags & BF_SHUTR)) {
3449// EV_FD_CLR(t->cli_fd, DIR_WR);
3450// shutdown(t->cli_fd, SHUT_WR);
3451// trace_term(t, TT_HTTP_CLI_8);
3452// } else {
3453// fd_delete(t->cli_fd);
3454// t->cli_state = CL_STCLOSE;
3455// trace_term(t, TT_HTTP_CLI_9);
3456// }
3457// if (!req->analysers) {
3458// if (!(t->flags & SN_ERR_MASK))
3459// t->flags |= SN_ERR_CLITO;
3460// if (!(t->flags & SN_FINST_MASK)) {
3461// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3462// t->flags |= SN_FINST_Q;
3463// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3464// t->flags |= SN_FINST_C;
3465// else
3466// t->flags |= SN_FINST_D;
3467// }
3468// }
3469// goto update_state;
3470// }
3471//
3472// update_timeouts:
3473// /* manage read timeout */
3474// if (!(req->flags & BF_SHUTR)) {
3475// if (req->flags & BF_FULL) {
3476// /* no room to read more data */
3477// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3478// /* stop reading until we get some space */
3479// req->rex = TICK_ETERNITY;
3480// }
3481// } else {
3482// EV_FD_COND_S(t->cli_fd, DIR_RD);
3483// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3484// }
3485// }
3486//
3487// /* manage write timeout */
3488// if (!(rep->flags & BF_SHUTW)) {
3489// /* first, we may have to produce data (eg: stats).
3490// * right now, this is limited to the SHUTR state.
3491// */
3492// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3493// produce_content(t);
3494// if (rep->flags & BF_EMPTY) {
3495// buffer_shutw(rep);
3496// fd_delete(t->cli_fd);
3497// t->cli_state = CL_STCLOSE;
3498// trace_term(t, TT_HTTP_CLI_10);
3499// goto update_state;
3500// }
3501// }
3502//
3503// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3504// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3505// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3506// /* stop writing */
3507// rep->wex = TICK_ETERNITY;
3508// }
3509// } else {
3510// /* buffer not empty */
3511// EV_FD_COND_S(t->cli_fd, DIR_WR);
3512// if (!tick_isset(rep->wex)) {
3513// /* restart writing */
3514// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3515// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3516// /* FIXME: to prevent the client from expiring read timeouts during writes,
3517// * we refresh it, except if it was already infinite. */
3518// req->rex = rep->wex;
3519// }
3520// }
3521// }
3522// }
3523// return 0; /* other cases change nothing */
3524// }
3525// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3526// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3527// int len;
3528// 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);
3529// write(1, trash, len);
3530// }
3531// return 0;
3532// }
3533//#ifdef DEBUG_DEV
3534// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3535// ABORT_NOW();
3536//#endif
3537// return 0;
3538//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003539
Willy Tarreaubaaee002006-06-26 02:48:02 +02003540
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003541/* Return 1 if the pending connection has failed and should be retried,
3542 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3543 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003544 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003545int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003546{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003547 struct buffer *req = t->req;
3548 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003549 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003551 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3552 now_ms, __FUNCTION__,
3553 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003554 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003555 req->flags, rep->flags,
3556 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003557
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003558 if ((req->flags & BF_SHUTW_NOW) ||
3559 (rep->flags & BF_SHUTW) ||
3560 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003561 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003562 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3563
3564 trace_term(t, TT_HTTP_SRV_5);
3565 req->wex = TICK_ETERNITY;
3566 fd_delete(req->cons->fd);
3567 if (t->srv) {
3568 t->srv->cur_sess--;
3569 sess_change_server(t, NULL);
3570 }
3571 /* note that this must not return any error because it would be able to
3572 * overwrite the client_retnclose() output.
3573 */
3574 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3575
3576 // FIXME: should we set rep->MAY_FORWARD ?
3577 buffer_shutw(req);
3578 buffer_shutr(rep);
3579 req->cons->state = SI_ST_CLO;
3580 if (!req->cons->err_type)
3581 req->cons->err_type = SI_ET_CONN_ABRT;
3582 req->cons->err_loc = t->srv;
3583 return 0;
3584 }
3585
3586 /* check for timeouts and asynchronous connect errors */
3587 if (fdtab[req->cons->fd].state == FD_STERROR) {
3588 conn_err = SI_ET_CONN_ERR;
3589 if (!req->cons->err_type)
3590 req->cons->err_type = SI_ET_CONN_ERR;
3591 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003592 else if (!(req->flags & BF_WRITE_ACTIVITY)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003593 /* nothing happened, maybe we timed out */
3594 if (tick_is_expired(req->wex, now_ms)) {
3595 conn_err = SI_ET_CONN_TO;
3596 if (!req->cons->err_type)
3597 req->cons->err_type = SI_ET_CONN_TO;
3598 }
3599 else
3600 return 0; /* let's wait a bit more */
3601 }
3602
3603 if (conn_err) {
3604 fd_delete(req->cons->fd);
3605 req->cons->state = SI_ST_CLO;
3606
3607 if (t->srv) {
3608 t->srv->cur_sess--;
3609 sess_change_server(t, NULL);
3610 req->cons->err_loc = t->srv;
3611 }
3612
3613 /* ensure that we have enough retries left */
3614 if (srv_count_retry_down(t, conn_err))
3615 return 0;
3616
3617 if (conn_err == SI_ET_CONN_ERR) {
3618 /* we encountered an immediate connection error, and we
3619 * will have to retry connecting to the same server, most
3620 * likely leading to the same result. To avoid this, we
3621 * fake a connection timeout to retry after a turn-around
3622 * time of 1 second. We will wait in the previous if block.
3623 */
3624 req->cons->state = SI_ST_TAR;
Willy Tarreau35374672008-09-03 18:11:02 +02003625 req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003626 return 0;
3627 }
3628
3629 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3630 /* We're on our last chance, and the REDISP option was specified.
3631 * We will ignore cookie and force to balance or use the dispatcher.
3632 */
3633 /* let's try to offer this slot to anybody */
3634 if (may_dequeue_tasks(t->srv, t->be))
3635 process_srv_queue(t->srv);
3636
3637 /* it's left to the dispatcher to choose a server */
3638 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3639 t->prev_srv = t->srv;
3640 } else {
3641 /* we just want to retry */
3642 if (t->srv)
3643 t->srv->retries++;
3644 t->be->retries++;
3645
3646 /* Now we will try to either reconnect to the same server or
3647 * connect to another server. If the connection gets queued
3648 * because all servers are saturated, then we will go back to
3649 * the idle state where the buffer's consumer is marked as
3650 * unknown.
3651 */
3652 if (srv_retryable_connect(t)) {
3653 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003654 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003655 return 0;
3656 }
3657 }
3658
3659 /* We'll rely on the caller to try to get a connection again */
3660 return 1;
3661 }
3662 else {
3663 /* no error and write OK : connection succeeded */
3664 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3665 req->cons->state = SI_ST_EST;
3666 req->cons->err_type = SI_ET_NONE;
3667 req->cons->err_loc = NULL;
3668
3669 if (req->flags & BF_EMPTY) {
3670 EV_FD_CLR(req->cons->fd, DIR_WR);
3671 req->wex = TICK_ETERNITY;
3672 } else {
3673 EV_FD_SET(req->cons->fd, DIR_WR);
3674 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3675 if (tick_isset(req->wex)) {
3676 /* FIXME: to prevent the server from expiring read timeouts during writes,
3677 * we refresh it. */
3678 rep->rex = req->wex;
3679 }
3680 }
3681
3682 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3683 if (!(rep->flags & BF_HIJACK)) {
3684 EV_FD_SET(req->cons->fd, DIR_RD);
3685 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3686 }
3687 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3688
3689 /* if the user wants to log as soon as possible, without counting
3690 bytes from the server, then this is the right moment. */
3691 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3692 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3693 tcp_sess_log(t);
3694 }
3695#ifdef CONFIG_HAP_TCPSPLICE
3696 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3697 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003698 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003699 }
3700#endif
3701 }
3702 else {
3703 rep->analysers |= AN_RTR_HTTP_HDR;
3704 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3705 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3706 /* reset hdr_idx which was already initialized by the request.
3707 * right now, the http parser does it.
3708 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003709 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003710 }
3711
Willy Tarreau9a2d1542008-08-30 12:31:07 +02003712 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003713 req->wex = TICK_ETERNITY;
3714 return 0;
3715 }
3716}
3717
3718
3719/*
3720 * This function tries to assign a server to a stream_sock interface.
3721 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3722 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3723 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3724 * it returns SI_ST_ASS, otherwise zero.
3725 */
3726int stream_sock_assign_server(struct session *t)
3727{
3728 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3729 now_ms, __FUNCTION__,
3730 cli_stnames[t->cli_state],
3731 t->rep->rex, t->req->wex,
3732 t->req->flags, t->rep->flags,
3733 t->req->l, t->rep->l);
3734
3735 if (t->req->cons->state == SI_ST_TAR) {
3736 /* connection might be aborted */
3737 if ((t->req->flags & BF_SHUTW_NOW) ||
3738 (t->rep->flags & BF_SHUTW) ||
3739 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3740 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003741
Willy Tarreauf8533202008-08-16 14:55:08 +02003742 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003743 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003744
3745 // FIXME: should we set rep->MAY_FORWARD ?
3746 buffer_shutr(t->rep);
3747 buffer_shutw(t->req);
3748 if (!t->req->cons->err_type)
3749 t->req->cons->err_type = SI_ET_CONN_ABRT;
3750 t->req->cons->state = SI_ST_CLO;
3751 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003752 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003753
Willy Tarreau35374672008-09-03 18:11:02 +02003754 if (!tick_is_expired(t->req->cons->exp, now_ms))
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003755 return 0; /* still in turn-around */
3756
3757 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003758 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003759 }
3760 else if (t->req->cons->state == SI_ST_QUE) {
3761 if (t->pend_pos) {
3762 /* request still in queue... */
Willy Tarreau35374672008-09-03 18:11:02 +02003763 if (tick_is_expired(t->req->cons->exp, now_ms)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003764 /* ... and timeout expired */
3765 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreau35374672008-09-03 18:11:02 +02003766 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003767 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003768 if (t->srv)
3769 t->srv->failed_conns++;
3770 t->be->failed_conns++;
3771
3772 // FIXME: should we set rep->MAY_FORWARD ?
3773 buffer_shutr(t->rep);
3774 buffer_shutw(t->req);
3775 t->req->flags |= BF_WRITE_TIMEOUT;
3776 if (!t->req->cons->err_type)
3777 t->req->cons->err_type = SI_ET_QUEUE_TO;
3778 t->req->cons->state = SI_ST_CLO;
3779 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003780 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003781 /* connection remains in queue, check if we have to abort it */
3782 if ((t->req->flags & BF_SHUTW_NOW) ||
3783 (t->rep->flags & BF_SHUTW) ||
3784 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3785 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3786 /* give up */
3787 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003788 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003789 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003790
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003791 // FIXME: should we set rep->MAY_FORWARD ?
3792 buffer_shutr(t->rep);
3793 buffer_shutw(t->req);
3794 if (!t->req->cons->err_type)
3795 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3796 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003797 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003798 return 0;
3799 }
3800 /* The connection is not in the queue anymore */
3801 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003802 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003803 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003804
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003805 /* we may get here from above */
3806 if (t->req->cons->state == SI_ST_INI) {
3807 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003808
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003809 /* first, check if the connection has been aborted */
3810 if ((t->req->flags & BF_SHUTW_NOW) ||
3811 (t->rep->flags & BF_SHUTW) ||
3812 ((t->req->flags & BF_SHUTR) &&
3813 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003814
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003815 trace_term(t, TT_HTTP_SRV_1);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003816
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003817 // FIXME: should we set rep->MAY_FORWARD ?
3818 buffer_shutr(t->rep);
3819 buffer_shutw(t->req);
3820 if (!t->req->cons->err_type)
3821 t->req->cons->err_type = SI_ET_CONN_ABRT;
3822 t->req->cons->state = SI_ST_CLO;
3823 return 0;
3824 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003825
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003826 /* try to get a server assigned */
3827 if (srv_redispatch_connect(t) != 0) {
3828 /* we did not get any server, let's check the cause */
3829 if (t->req->cons->state == SI_ST_QUE) {
3830 /* the connection was queued, that's OK */
3831 return 0;
3832 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003833
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003834 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003835
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003836 // FIXME: should we set rep->MAY_FORWARD ?
3837 buffer_shutr(t->rep);
3838 buffer_shutw(t->req);
3839 t->req->flags |= BF_WRITE_ERROR;
3840 if (!t->req->cons->err_type)
3841 t->req->cons->err_type = SI_ET_CONN_OTHER;
3842 t->req->cons->state = SI_ST_CLO;
3843 return 0;
3844 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003845
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003846 t->req->cons->state = SI_ST_ASS;
3847 /* Once the server is assigned, we have to return because
3848 * the caller might be interested in checking several
3849 * things before connecting.
3850 */
3851 return 1;
3852 }
3853 return 0;
3854}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003855
Willy Tarreauf8533202008-08-16 14:55:08 +02003856
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003857/*
3858 * This function tries to establish a connection to an assigned server. It also
3859 * performs connection retries. It may only be called with t->req->cons->state
3860 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3861 * SI_ST_EST, or SI_ST_CLO.
3862 */
3863int stream_sock_connect_server(struct session *t)
3864{
3865 if (t->req->cons->state == SI_ST_ASS) {
3866 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003867
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003868 if (!srv_retryable_connect(t)) {
3869 /* we need to redispatch */
3870 t->req->cons->state = SI_ST_INI;
3871 return 0;
3872 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003873
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003874 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3875 if (t->req->cons->state != SI_ST_CON) {
3876 /* it was an error */
3877 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003878
3879 // FIXME: should we set rep->MAY_FORWARD ?
3880 buffer_shutr(t->rep);
3881 buffer_shutw(t->req);
3882 t->req->flags |= BF_WRITE_ERROR;
3883 if (!t->req->cons->err_type)
3884 t->req->cons->err_type = SI_ET_CONN_OTHER;
3885 t->req->cons->state = SI_ST_CLO;
3886 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003887 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003888 /* We have a socket and switched to SI_ST_CON */
3889 }
3890
3891 /* we may also get here from above */
3892 if (t->req->cons->state == SI_ST_CON) {
3893 /* connection in progress or just completed */
3894 if (!tcp_connection_status(t))
3895 return 0;
3896 }
3897 return 0;
3898}
3899
3900
3901/*
3902 * Tries to establish a connection to the server and associate it to the
3903 * request buffer's consumer side. It is assumed that this function will not be
Willy Tarreau3da77c52008-08-29 09:58:42 +02003904 * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003905 * returns zero, but may return 1 if it absolutely wants to be called again.
3906 */
3907int process_srv_conn(struct session *t)
3908{
3909 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3910 now_ms, __FUNCTION__,
3911 cli_stnames[t->cli_state],
3912 t->rep->rex, t->req->wex,
3913 t->req->flags, t->rep->flags,
3914 t->req->l, t->rep->l);
3915
3916 do {
3917 if (t->req->cons->state == SI_ST_INI ||
3918 t->req->cons->state == SI_ST_TAR ||
3919 t->req->cons->state == SI_ST_QUE) {
3920 /* try to assign a server */
3921 if (!stream_sock_assign_server(t))
3922 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003923 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003924
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003925 if (t->req->cons->state == SI_ST_ASS &&
3926 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3927 /* Server supporting redirection and it is possible.
3928 * Invalid requests are reported as such. It concerns all
3929 * the largest ones.
3930 */
3931 struct http_txn *txn = &t->txn;
3932 struct chunk rdr;
3933 char *path;
3934 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003935
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003936 /* 1: create the response header */
3937 rdr.len = strlen(HTTP_302);
3938 rdr.str = trash;
3939 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003940
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003941 /* 2: add the server's prefix */
3942 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3943 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003944
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003945 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3946 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003947
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003948 /* 3: add the request URI */
3949 path = http_get_path(txn);
3950 if (!path)
3951 goto cancel_redir;
3952 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3953 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3954 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003955
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003956 memcpy(rdr.str + rdr.len, path, len);
3957 rdr.len += len;
3958 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3959 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003960
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003961 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3962 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003963
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003964 /* FIXME: we should increase a counter of redirects per server and per backend. */
3965 if (t->srv)
3966 t->srv->cum_sess++;
3967
3968 t->req->cons->state = SI_ST_CLO;
3969 return 0;
3970 cancel_redir:
3971 //txn->status = 400;
3972 //t->fe->failed_req++;
3973 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3974 // 400, error_message(t, HTTP_ERR_400));
3975 trace_term(t, TT_HTTP_SRV_4);
3976
3977 // FIXME: should we set rep->MAY_FORWARD ?
3978 buffer_shutw(t->req);
3979 buffer_shutr(t->rep);
3980 if (!t->req->cons->err_type)
3981 t->req->cons->err_type = SI_ET_CONN_OTHER;
3982 t->req->cons->state = SI_ST_CLO;
3983 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003985
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003986 if (t->req->cons->state == SI_ST_CON ||
3987 t->req->cons->state == SI_ST_ASS) {
3988 stream_sock_connect_server(t);
3989 }
3990 } while (t->req->cons->state != SI_ST_CLO &&
3991 t->req->cons->state != SI_ST_CON &&
3992 t->req->cons->state != SI_ST_EST);
3993 return 0;
3994}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003995
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003996
Willy Tarreaubaaee002006-06-26 02:48:02 +02003997/*
3998 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003999 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02004000 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
4001 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004002 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004003 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004004 */
4005int produce_content(struct session *s)
4006{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004007 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02004008 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004009 return 1;
4010 }
4011 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004012 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004013 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004014 if (ret >= 0)
4015 return ret;
4016 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004017 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004018
Willy Tarreau91861262007-10-17 17:06:05 +02004019 /* unknown data source or internal error */
4020 s->txn.status = 500;
4021 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004022 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004023 if (!(s->flags & SN_ERR_MASK))
4024 s->flags |= SN_ERR_PRXCOND;
4025 if (!(s->flags & SN_FINST_MASK))
4026 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02004027 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02004028 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004029}
4030
4031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004032/* Iterate the same filter through all request headers.
4033 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034 * Since it can manage the switch to another backend, it updates the per-proxy
4035 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004037int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004038{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004039 char term;
4040 char *cur_ptr, *cur_end, *cur_next;
4041 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004042 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004043 struct hdr_idx_elem *cur_hdr;
4044 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004045
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 last_hdr = 0;
4047
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004048 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004049 old_idx = 0;
4050
4051 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004052 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004053 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004054 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004055 (exp->action == ACT_ALLOW ||
4056 exp->action == ACT_DENY ||
4057 exp->action == ACT_TARPIT))
4058 return 0;
4059
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004060 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 if (!cur_idx)
4062 break;
4063
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004064 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 cur_ptr = cur_next;
4066 cur_end = cur_ptr + cur_hdr->len;
4067 cur_next = cur_end + cur_hdr->cr + 1;
4068
4069 /* Now we have one header between cur_ptr and cur_end,
4070 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004071 */
4072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 /* The annoying part is that pattern matching needs
4074 * that we modify the contents to null-terminate all
4075 * strings before testing them.
4076 */
4077
4078 term = *cur_end;
4079 *cur_end = '\0';
4080
4081 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4082 switch (exp->action) {
4083 case ACT_SETBE:
4084 /* It is not possible to jump a second time.
4085 * FIXME: should we return an HTTP/500 here so that
4086 * the admin knows there's a problem ?
4087 */
4088 if (t->be != t->fe)
4089 break;
4090
4091 /* Swithing Proxy */
4092 t->be = (struct proxy *) exp->replace;
4093
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004094 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004095 * because we have associated req_cap and rsp_cap to the
4096 * frontend, and the beconn will be updated later.
4097 */
4098
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004099 t->rep->rto = t->req->wto = t->be->timeout.server;
4100 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004101 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004102 last_hdr = 1;
4103 break;
4104
4105 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004106 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004107 last_hdr = 1;
4108 break;
4109
4110 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004111 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004112 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004113 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004114 break;
4115
4116 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004117 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004118 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004119 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004120 break;
4121
4122 case ACT_REPLACE:
4123 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4124 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4125 /* FIXME: if the user adds a newline in the replacement, the
4126 * index will not be recalculated for now, and the new line
4127 * will not be counted as a new header.
4128 */
4129
4130 cur_end += delta;
4131 cur_next += delta;
4132 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004133 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 break;
4135
4136 case ACT_REMOVE:
4137 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4138 cur_next += delta;
4139
4140 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004141 txn->req.eoh += delta;
4142 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4143 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004144 cur_hdr->len = 0;
4145 cur_end = NULL; /* null-term has been rewritten */
4146 break;
4147
4148 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004149 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004150 if (cur_end)
4151 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004153 /* keep the link from this header to next one in case of later
4154 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004155 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004156 old_idx = cur_idx;
4157 }
4158 return 0;
4159}
4160
4161
4162/* Apply the filter to the request line.
4163 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4164 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004165 * Since it can manage the switch to another backend, it updates the per-proxy
4166 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167 */
4168int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4169{
4170 char term;
4171 char *cur_ptr, *cur_end;
4172 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004173 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004174 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004175
Willy Tarreau58f10d72006-12-04 02:26:12 +01004176
Willy Tarreau3d300592007-03-18 18:34:41 +01004177 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004178 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004179 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004180 (exp->action == ACT_ALLOW ||
4181 exp->action == ACT_DENY ||
4182 exp->action == ACT_TARPIT))
4183 return 0;
4184 else if (exp->action == ACT_REMOVE)
4185 return 0;
4186
4187 done = 0;
4188
Willy Tarreau9cdde232007-05-02 20:58:19 +02004189 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004190 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004191
4192 /* Now we have the request line between cur_ptr and cur_end */
4193
4194 /* The annoying part is that pattern matching needs
4195 * that we modify the contents to null-terminate all
4196 * strings before testing them.
4197 */
4198
4199 term = *cur_end;
4200 *cur_end = '\0';
4201
4202 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4203 switch (exp->action) {
4204 case ACT_SETBE:
4205 /* It is not possible to jump a second time.
4206 * FIXME: should we return an HTTP/500 here so that
4207 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004209 if (t->be != t->fe)
4210 break;
4211
4212 /* Swithing Proxy */
4213 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004215 /* right now, the backend switch is not too much complicated
4216 * because we have associated req_cap and rsp_cap to the
4217 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218 */
4219
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004220 t->rep->rto = t->req->wto = t->be->timeout.server;
4221 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004222 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004223 done = 1;
4224 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004226 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004227 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004228 done = 1;
4229 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004231 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004232 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004233 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004234 done = 1;
4235 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004237 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004238 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004239 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004240 done = 1;
4241 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004243 case ACT_REPLACE:
4244 *cur_end = term; /* restore the string terminator */
4245 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4246 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4247 /* FIXME: if the user adds a newline in the replacement, the
4248 * index will not be recalculated for now, and the new line
4249 * will not be counted as a new header.
4250 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004251
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004252 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004253 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004254
Willy Tarreau9cdde232007-05-02 20:58:19 +02004255 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004256 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004257 HTTP_MSG_RQMETH,
4258 cur_ptr, cur_end + 1,
4259 NULL, NULL);
4260 if (unlikely(!cur_end))
4261 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004263 /* we have a full request and we know that we have either a CR
4264 * or an LF at <ptr>.
4265 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004266 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4267 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004268 /* there is no point trying this regex on headers */
4269 return 1;
4270 }
4271 }
4272 *cur_end = term; /* restore the string terminator */
4273 return done;
4274}
Willy Tarreau97de6242006-12-27 17:18:38 +01004275
Willy Tarreau58f10d72006-12-04 02:26:12 +01004276
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004278/*
4279 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4280 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004281 * unparsable request. Since it can manage the switch to another backend, it
4282 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004283 */
4284int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4285{
Willy Tarreau3d300592007-03-18 18:34:41 +01004286 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004287 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004288 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004289 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004291 /*
4292 * The interleaving of transformations and verdicts
4293 * makes it difficult to decide to continue or stop
4294 * the evaluation.
4295 */
4296
Willy Tarreau3d300592007-03-18 18:34:41 +01004297 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004298 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4299 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4300 exp = exp->next;
4301 continue;
4302 }
4303
4304 /* Apply the filter to the request line. */
4305 ret = apply_filter_to_req_line(t, req, exp);
4306 if (unlikely(ret < 0))
4307 return -1;
4308
4309 if (likely(ret == 0)) {
4310 /* The filter did not match the request, it can be
4311 * iterated through all headers.
4312 */
4313 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004314 }
4315 exp = exp->next;
4316 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004317 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004318}
4319
4320
Willy Tarreaua15645d2007-03-18 16:22:39 +01004321
Willy Tarreau58f10d72006-12-04 02:26:12 +01004322/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004323 * Manage client-side cookie. It can impact performance by about 2% so it is
4324 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004325 */
4326void manage_client_side_cookies(struct session *t, struct buffer *req)
4327{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004328 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004329 char *p1, *p2, *p3, *p4;
4330 char *del_colon, *del_cookie, *colon;
4331 int app_cookies;
4332
4333 appsess *asession_temp = NULL;
4334 appsess local_asession;
4335
4336 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004337 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004338
Willy Tarreau2a324282006-12-05 00:05:46 +01004339 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 * we start with the start line.
4341 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004342 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004343 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004344
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004345 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004347 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004348
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004349 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004350 cur_ptr = cur_next;
4351 cur_end = cur_ptr + cur_hdr->len;
4352 cur_next = cur_end + cur_hdr->cr + 1;
4353
4354 /* We have one full header between cur_ptr and cur_end, and the
4355 * next header starts at cur_next. We're only interested in
4356 * "Cookie:" headers.
4357 */
4358
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004359 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4360 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004361 old_idx = cur_idx;
4362 continue;
4363 }
4364
4365 /* Now look for cookies. Conforming to RFC2109, we have to support
4366 * attributes whose name begin with a '$', and associate them with
4367 * the right cookie, if we want to delete this cookie.
4368 * So there are 3 cases for each cookie read :
4369 * 1) it's a special attribute, beginning with a '$' : ignore it.
4370 * 2) it's a server id cookie that we *MAY* want to delete : save
4371 * some pointers on it (last semi-colon, beginning of cookie...)
4372 * 3) it's an application cookie : we *MAY* have to delete a previous
4373 * "special" cookie.
4374 * At the end of loop, if a "special" cookie remains, we may have to
4375 * remove it. If no application cookie persists in the header, we
4376 * *MUST* delete it
4377 */
4378
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004379 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004380
Willy Tarreau58f10d72006-12-04 02:26:12 +01004381 /* del_cookie == NULL => nothing to be deleted */
4382 del_colon = del_cookie = NULL;
4383 app_cookies = 0;
4384
4385 while (p1 < cur_end) {
4386 /* skip spaces and colons, but keep an eye on these ones */
4387 while (p1 < cur_end) {
4388 if (*p1 == ';' || *p1 == ',')
4389 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004390 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004391 break;
4392 p1++;
4393 }
4394
4395 if (p1 == cur_end)
4396 break;
4397
4398 /* p1 is at the beginning of the cookie name */
4399 p2 = p1;
4400 while (p2 < cur_end && *p2 != '=')
4401 p2++;
4402
4403 if (p2 == cur_end)
4404 break;
4405
4406 p3 = p2 + 1; /* skips the '=' sign */
4407 if (p3 == cur_end)
4408 break;
4409
4410 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004411 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004412 p4++;
4413
4414 /* here, we have the cookie name between p1 and p2,
4415 * and its value between p3 and p4.
4416 * we can process it :
4417 *
4418 * Cookie: NAME=VALUE;
4419 * | || || |
4420 * | || || +--> p4
4421 * | || |+-------> p3
4422 * | || +--------> p2
4423 * | |+------------> p1
4424 * | +-------------> colon
4425 * +--------------------> cur_ptr
4426 */
4427
4428 if (*p1 == '$') {
4429 /* skip this one */
4430 }
4431 else {
4432 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004433 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004434 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004435 (p4 - p1 >= t->fe->capture_namelen) &&
4436 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004437 int log_len = p4 - p1;
4438
Willy Tarreau086b3b42007-05-13 21:45:51 +02004439 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004440 Alert("HTTP logging : out of memory.\n");
4441 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004442 if (log_len > t->fe->capture_len)
4443 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004444 memcpy(txn->cli_cookie, p1, log_len);
4445 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004446 }
4447 }
4448
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004449 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4450 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004451 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004452 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004453 char *delim;
4454
4455 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4456 * have the server ID betweek p3 and delim, and the original cookie between
4457 * delim+1 and p4. Otherwise, delim==p4 :
4458 *
4459 * Cookie: NAME=SRV~VALUE;
4460 * | || || | |
4461 * | || || | +--> p4
4462 * | || || +--------> delim
4463 * | || |+-----------> p3
4464 * | || +------------> p2
4465 * | |+----------------> p1
4466 * | +-----------------> colon
4467 * +------------------------> cur_ptr
4468 */
4469
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004470 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004471 for (delim = p3; delim < p4; delim++)
4472 if (*delim == COOKIE_DELIM)
4473 break;
4474 }
4475 else
4476 delim = p4;
4477
4478
4479 /* Here, we'll look for the first running server which supports the cookie.
4480 * This allows to share a same cookie between several servers, for example
4481 * to dedicate backup servers to specific servers only.
4482 * However, to prevent clients from sticking to cookie-less backup server
4483 * when they have incidentely learned an empty cookie, we simply ignore
4484 * empty cookies and mark them as invalid.
4485 */
4486 if (delim == p3)
4487 srv = NULL;
4488
4489 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004490 if (srv->cookie && (srv->cklen == delim - p3) &&
4491 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004492 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004493 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004494 txn->flags &= ~TX_CK_MASK;
4495 txn->flags |= TX_CK_VALID;
4496 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004497 t->srv = srv;
4498 break;
4499 } else {
4500 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004501 txn->flags &= ~TX_CK_MASK;
4502 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503 }
4504 }
4505 srv = srv->next;
4506 }
4507
Willy Tarreau3d300592007-03-18 18:34:41 +01004508 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004509 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 txn->flags &= ~TX_CK_MASK;
4511 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004512 }
4513
4514 /* depending on the cookie mode, we may have to either :
4515 * - delete the complete cookie if we're in insert+indirect mode, so that
4516 * the server never sees it ;
4517 * - remove the server id from the cookie value, and tag the cookie as an
4518 * application cookie so that it does not get accidentely removed later,
4519 * if we're in cookie prefix mode
4520 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004521 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004522 int delta; /* negative */
4523
4524 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4525 p4 += delta;
4526 cur_end += delta;
4527 cur_next += delta;
4528 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004529 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004530
4531 del_cookie = del_colon = NULL;
4532 app_cookies++; /* protect the header from deletion */
4533 }
4534 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004535 (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 +01004536 del_cookie = p1;
4537 del_colon = colon;
4538 }
4539 } else {
4540 /* now we know that we must keep this cookie since it's
4541 * not ours. But if we wanted to delete our cookie
4542 * earlier, we cannot remove the complete header, but we
4543 * can remove the previous block itself.
4544 */
4545 app_cookies++;
4546
4547 if (del_cookie != NULL) {
4548 int delta; /* negative */
4549
4550 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4551 p4 += delta;
4552 cur_end += delta;
4553 cur_next += delta;
4554 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004555 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004556 del_cookie = del_colon = NULL;
4557 }
4558 }
4559
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004560 if ((t->be->appsession_name != NULL) &&
4561 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004562 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004563
Willy Tarreau58f10d72006-12-04 02:26:12 +01004564 /* Cool... it's the right one */
4565
4566 asession_temp = &local_asession;
4567
Willy Tarreau63963c62007-05-13 21:29:55 +02004568 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004569 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4570 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4571 return;
4572 }
4573
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004574 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4575 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004577
Willy Tarreau58f10d72006-12-04 02:26:12 +01004578 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004579 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4580 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004581 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004582 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004583 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004584 Alert("Not enough memory process_cli():asession:calloc().\n");
4585 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4586 return;
4587 }
4588
4589 asession_temp->sessid = local_asession.sessid;
4590 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004591 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004592 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004593 } else {
4594 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004595 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004597 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004598 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004599 Alert("Found Application Session without matching server.\n");
4600 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004601 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004602 while (srv) {
4603 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004604 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004605 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004606 txn->flags &= ~TX_CK_MASK;
4607 txn->flags |= TX_CK_VALID;
4608 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004609 t->srv = srv;
4610 break;
4611 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004612 txn->flags &= ~TX_CK_MASK;
4613 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614 }
4615 }
4616 srv = srv->next;
4617 }/* end while(srv) */
4618 }/* end else if server == NULL */
4619
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004620 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004621 asession_temp->request_count++;
4622#if defined(DEBUG_HASH)
4623 Alert("manage_client_side_cookies\n");
4624 appsession_hash_dump(&(t->be->htbl_proxy));
4625#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004626 }/* end if ((t->proxy->appsession_name != NULL) ... */
4627 }
4628
4629 /* we'll have to look for another cookie ... */
4630 p1 = p4;
4631 } /* while (p1 < cur_end) */
4632
4633 /* There's no more cookie on this line.
4634 * We may have marked the last one(s) for deletion.
4635 * We must do this now in two ways :
4636 * - if there is no app cookie, we simply delete the header ;
4637 * - if there are app cookies, we must delete the end of the
4638 * string properly, including the colon/semi-colon before
4639 * the cookie name.
4640 */
4641 if (del_cookie != NULL) {
4642 int delta;
4643 if (app_cookies) {
4644 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4645 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004646 cur_hdr->len += delta;
4647 } else {
4648 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004649
4650 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004651 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4652 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004653 cur_hdr->len = 0;
4654 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004655 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004656 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004657 }
4658
4659 /* keep the link from this header to next one */
4660 old_idx = cur_idx;
4661 } /* end of cookie processing on this header */
4662}
4663
4664
Willy Tarreaua15645d2007-03-18 16:22:39 +01004665/* Iterate the same filter through all response headers contained in <rtr>.
4666 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4667 */
4668int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4669{
4670 char term;
4671 char *cur_ptr, *cur_end, *cur_next;
4672 int cur_idx, old_idx, last_hdr;
4673 struct http_txn *txn = &t->txn;
4674 struct hdr_idx_elem *cur_hdr;
4675 int len, delta;
4676
4677 last_hdr = 0;
4678
4679 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4680 old_idx = 0;
4681
4682 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004683 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004684 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004685 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004686 (exp->action == ACT_ALLOW ||
4687 exp->action == ACT_DENY))
4688 return 0;
4689
4690 cur_idx = txn->hdr_idx.v[old_idx].next;
4691 if (!cur_idx)
4692 break;
4693
4694 cur_hdr = &txn->hdr_idx.v[cur_idx];
4695 cur_ptr = cur_next;
4696 cur_end = cur_ptr + cur_hdr->len;
4697 cur_next = cur_end + cur_hdr->cr + 1;
4698
4699 /* Now we have one header between cur_ptr and cur_end,
4700 * and the next header starts at cur_next.
4701 */
4702
4703 /* The annoying part is that pattern matching needs
4704 * that we modify the contents to null-terminate all
4705 * strings before testing them.
4706 */
4707
4708 term = *cur_end;
4709 *cur_end = '\0';
4710
4711 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4712 switch (exp->action) {
4713 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004714 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715 last_hdr = 1;
4716 break;
4717
4718 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004719 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720 last_hdr = 1;
4721 break;
4722
4723 case ACT_REPLACE:
4724 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4725 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4726 /* FIXME: if the user adds a newline in the replacement, the
4727 * index will not be recalculated for now, and the new line
4728 * will not be counted as a new header.
4729 */
4730
4731 cur_end += delta;
4732 cur_next += delta;
4733 cur_hdr->len += delta;
4734 txn->rsp.eoh += delta;
4735 break;
4736
4737 case ACT_REMOVE:
4738 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4739 cur_next += delta;
4740
4741 /* FIXME: this should be a separate function */
4742 txn->rsp.eoh += delta;
4743 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4744 txn->hdr_idx.used--;
4745 cur_hdr->len = 0;
4746 cur_end = NULL; /* null-term has been rewritten */
4747 break;
4748
4749 }
4750 }
4751 if (cur_end)
4752 *cur_end = term; /* restore the string terminator */
4753
4754 /* keep the link from this header to next one in case of later
4755 * removal of next header.
4756 */
4757 old_idx = cur_idx;
4758 }
4759 return 0;
4760}
4761
4762
4763/* Apply the filter to the status line in the response buffer <rtr>.
4764 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4765 * or -1 if a replacement resulted in an invalid status line.
4766 */
4767int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4768{
4769 char term;
4770 char *cur_ptr, *cur_end;
4771 int done;
4772 struct http_txn *txn = &t->txn;
4773 int len, delta;
4774
4775
Willy Tarreau3d300592007-03-18 18:34:41 +01004776 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004777 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004778 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004779 (exp->action == ACT_ALLOW ||
4780 exp->action == ACT_DENY))
4781 return 0;
4782 else if (exp->action == ACT_REMOVE)
4783 return 0;
4784
4785 done = 0;
4786
Willy Tarreau9cdde232007-05-02 20:58:19 +02004787 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004788 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4789
4790 /* Now we have the status line between cur_ptr and cur_end */
4791
4792 /* The annoying part is that pattern matching needs
4793 * that we modify the contents to null-terminate all
4794 * strings before testing them.
4795 */
4796
4797 term = *cur_end;
4798 *cur_end = '\0';
4799
4800 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4801 switch (exp->action) {
4802 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004803 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004804 done = 1;
4805 break;
4806
4807 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004808 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004809 done = 1;
4810 break;
4811
4812 case ACT_REPLACE:
4813 *cur_end = term; /* restore the string terminator */
4814 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4815 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4816 /* FIXME: if the user adds a newline in the replacement, the
4817 * index will not be recalculated for now, and the new line
4818 * will not be counted as a new header.
4819 */
4820
4821 txn->rsp.eoh += delta;
4822 cur_end += delta;
4823
Willy Tarreau9cdde232007-05-02 20:58:19 +02004824 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004825 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004826 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004827 cur_ptr, cur_end + 1,
4828 NULL, NULL);
4829 if (unlikely(!cur_end))
4830 return -1;
4831
4832 /* we have a full respnse and we know that we have either a CR
4833 * or an LF at <ptr>.
4834 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004835 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004836 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4837 /* there is no point trying this regex on headers */
4838 return 1;
4839 }
4840 }
4841 *cur_end = term; /* restore the string terminator */
4842 return done;
4843}
4844
4845
4846
4847/*
4848 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4849 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4850 * unparsable response.
4851 */
4852int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4853{
Willy Tarreau3d300592007-03-18 18:34:41 +01004854 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004855 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004856 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004857 int ret;
4858
4859 /*
4860 * The interleaving of transformations and verdicts
4861 * makes it difficult to decide to continue or stop
4862 * the evaluation.
4863 */
4864
Willy Tarreau3d300592007-03-18 18:34:41 +01004865 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004866 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4867 exp->action == ACT_PASS)) {
4868 exp = exp->next;
4869 continue;
4870 }
4871
4872 /* Apply the filter to the status line. */
4873 ret = apply_filter_to_sts_line(t, rtr, exp);
4874 if (unlikely(ret < 0))
4875 return -1;
4876
4877 if (likely(ret == 0)) {
4878 /* The filter did not match the response, it can be
4879 * iterated through all headers.
4880 */
4881 apply_filter_to_resp_headers(t, rtr, exp);
4882 }
4883 exp = exp->next;
4884 }
4885 return 0;
4886}
4887
4888
4889
4890/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004891 * Manage server-side cookies. It can impact performance by about 2% so it is
4892 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004893 */
4894void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4895{
4896 struct http_txn *txn = &t->txn;
4897 char *p1, *p2, *p3, *p4;
4898
4899 appsess *asession_temp = NULL;
4900 appsess local_asession;
4901
4902 char *cur_ptr, *cur_end, *cur_next;
4903 int cur_idx, old_idx, delta;
4904
Willy Tarreaua15645d2007-03-18 16:22:39 +01004905 /* Iterate through the headers.
4906 * we start with the start line.
4907 */
4908 old_idx = 0;
4909 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4910
4911 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4912 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004913 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004914
4915 cur_hdr = &txn->hdr_idx.v[cur_idx];
4916 cur_ptr = cur_next;
4917 cur_end = cur_ptr + cur_hdr->len;
4918 cur_next = cur_end + cur_hdr->cr + 1;
4919
4920 /* We have one full header between cur_ptr and cur_end, and the
4921 * next header starts at cur_next. We're only interested in
4922 * "Cookie:" headers.
4923 */
4924
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004925 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4926 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927 old_idx = cur_idx;
4928 continue;
4929 }
4930
4931 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004932 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004933
4934
4935 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004936 if (t->be->cookie_name == NULL &&
4937 t->be->appsession_name == NULL &&
4938 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004939 return;
4940
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004941 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942
4943 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4945 break;
4946
4947 /* p1 is at the beginning of the cookie name */
4948 p2 = p1;
4949
4950 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4951 p2++;
4952
4953 if (p2 == cur_end || *p2 == ';') /* next cookie */
4954 break;
4955
4956 p3 = p2 + 1; /* skip the '=' sign */
4957 if (p3 == cur_end)
4958 break;
4959
4960 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004961 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004962 p4++;
4963
4964 /* here, we have the cookie name between p1 and p2,
4965 * and its value between p3 and p4.
4966 * we can process it.
4967 */
4968
4969 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004970 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004971 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004972 (p4 - p1 >= t->be->capture_namelen) &&
4973 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004974 int log_len = p4 - p1;
4975
Willy Tarreau086b3b42007-05-13 21:45:51 +02004976 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 Alert("HTTP logging : out of memory.\n");
4978 }
4979
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004980 if (log_len > t->be->capture_len)
4981 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004982 memcpy(txn->srv_cookie, p1, log_len);
4983 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004984 }
4985
4986 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004987 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4988 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004989 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004990 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991
4992 /* If the cookie is in insert mode on a known server, we'll delete
4993 * this occurrence because we'll insert another one later.
4994 * We'll delete it too if the "indirect" option is set and we're in
4995 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004996 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4997 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998 /* this header must be deleted */
4999 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5000 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5001 txn->hdr_idx.used--;
5002 cur_hdr->len = 0;
5003 cur_next += delta;
5004 txn->rsp.eoh += delta;
5005
Willy Tarreau3d300592007-03-18 18:34:41 +01005006 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005007 }
5008 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005009 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005010 /* replace bytes p3->p4 with the cookie name associated
5011 * with this server since we know it.
5012 */
5013 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5014 cur_hdr->len += delta;
5015 cur_next += delta;
5016 txn->rsp.eoh += delta;
5017
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005019 }
5020 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005021 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005022 /* insert the cookie name associated with this server
5023 * before existing cookie, and insert a delimitor between them..
5024 */
5025 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5026 cur_hdr->len += delta;
5027 cur_next += delta;
5028 txn->rsp.eoh += delta;
5029
5030 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005031 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005032 }
5033 }
5034 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005035 else if ((t->be->appsession_name != NULL) &&
5036 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005037
5038 /* Cool... it's the right one */
5039
5040 size_t server_id_len = strlen(t->srv->id) + 1;
5041 asession_temp = &local_asession;
5042
Willy Tarreau63963c62007-05-13 21:29:55 +02005043 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005044 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5045 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5046 return;
5047 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005048 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5049 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005050 asession_temp->serverid = NULL;
5051
5052 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005053 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5054 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005055 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005056 Alert("Not enough Memory process_srv():asession:calloc().\n");
5057 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5058 return;
5059 }
5060 asession_temp->sessid = local_asession.sessid;
5061 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005062 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005063 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5064 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005065 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005066 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005067 }
5068
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005070 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005071 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5072 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5073 return;
5074 }
5075 asession_temp->serverid[0] = '\0';
5076 }
5077
5078 if (asession_temp->serverid[0] == '\0')
5079 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5080
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005081 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005082 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005083#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005084 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005085 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005086#endif
5087 }/* end if ((t->proxy->appsession_name != NULL) ... */
5088 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5089 } /* we're now at the end of the cookie value */
5090
5091 /* keep the link from this header to next one */
5092 old_idx = cur_idx;
5093 } /* end of cookie processing on this header */
5094}
5095
5096
5097
5098/*
5099 * Check if response is cacheable or not. Updates t->flags.
5100 */
5101void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5102{
5103 struct http_txn *txn = &t->txn;
5104 char *p1, *p2;
5105
5106 char *cur_ptr, *cur_end, *cur_next;
5107 int cur_idx;
5108
Willy Tarreau5df51872007-11-25 16:20:08 +01005109 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005110 return;
5111
5112 /* Iterate through the headers.
5113 * we start with the start line.
5114 */
5115 cur_idx = 0;
5116 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5117
5118 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5119 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005120 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005121
5122 cur_hdr = &txn->hdr_idx.v[cur_idx];
5123 cur_ptr = cur_next;
5124 cur_end = cur_ptr + cur_hdr->len;
5125 cur_next = cur_end + cur_hdr->cr + 1;
5126
5127 /* We have one full header between cur_ptr and cur_end, and the
5128 * next header starts at cur_next. We're only interested in
5129 * "Cookie:" headers.
5130 */
5131
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005132 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5133 if (val) {
5134 if ((cur_end - (cur_ptr + val) >= 8) &&
5135 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5136 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5137 return;
5138 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005139 }
5140
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005141 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5142 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005143 continue;
5144
5145 /* OK, right now we know we have a cache-control header at cur_ptr */
5146
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005147 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005148
5149 if (p1 >= cur_end) /* no more info */
5150 continue;
5151
5152 /* p1 is at the beginning of the value */
5153 p2 = p1;
5154
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005155 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005156 p2++;
5157
5158 /* we have a complete value between p1 and p2 */
5159 if (p2 < cur_end && *p2 == '=') {
5160 /* we have something of the form no-cache="set-cookie" */
5161 if ((cur_end - p1 >= 21) &&
5162 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5163 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005164 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165 continue;
5166 }
5167
5168 /* OK, so we know that either p2 points to the end of string or to a comma */
5169 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5170 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5171 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5172 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005173 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005174 return;
5175 }
5176
5177 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005178 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005179 continue;
5180 }
5181 }
5182}
5183
5184
Willy Tarreau58f10d72006-12-04 02:26:12 +01005185/*
5186 * Try to retrieve a known appsession in the URI, then the associated server.
5187 * If the server is found, it's assigned to the session.
5188 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005189void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005190{
Willy Tarreau3d300592007-03-18 18:34:41 +01005191 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005192 appsess *asession_temp = NULL;
5193 appsess local_asession;
5194 char *request_line;
5195
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005196 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005197 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005198 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005199 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005200 return;
5201
5202 /* skip ';' */
5203 request_line++;
5204
5205 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005206 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005207 return;
5208
5209 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005210 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005211
5212 /* First try if we already have an appsession */
5213 asession_temp = &local_asession;
5214
Willy Tarreau63963c62007-05-13 21:29:55 +02005215 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005216 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5217 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5218 return;
5219 }
5220
5221 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005222 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5223 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 asession_temp->serverid = NULL;
5225
5226 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005227 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5228 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005229 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005230 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005231 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005232 Alert("Not enough memory process_cli():asession:calloc().\n");
5233 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5234 return;
5235 }
5236 asession_temp->sessid = local_asession.sessid;
5237 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005238 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005239 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005240 }
5241 else {
5242 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005243 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005244 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005245
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005246 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005248
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005250 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005251 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005252#endif
5253 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005254 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005255 Alert("Found Application Session without matching server.\n");
5256 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005257 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 while (srv) {
5259 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005260 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005262 txn->flags &= ~TX_CK_MASK;
5263 txn->flags |= TX_CK_VALID;
5264 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265 t->srv = srv;
5266 break;
5267 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005268 txn->flags &= ~TX_CK_MASK;
5269 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005270 }
5271 }
5272 srv = srv->next;
5273 }
5274 }
5275}
5276
5277
Willy Tarreaub2513902006-12-17 14:52:38 +01005278/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005279 * In a GET or HEAD request, check if the requested URI matches the stats uri
5280 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005281 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005282 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005283 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005284 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005285 *
5286 * Returns 1 if the session's state changes, otherwise 0.
5287 */
5288int stats_check_uri_auth(struct session *t, struct proxy *backend)
5289{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005290 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005291 struct uri_auth *uri_auth = backend->uri_auth;
5292 struct user_auth *user;
5293 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005294 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005295
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005296 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005298 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005299 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005300 return 0;
5301
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005302 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005303
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005304 /* the URI is in h */
5305 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005306 return 0;
5307
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005308 h += uri_auth->uri_len;
5309 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5310 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005311 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005312 break;
5313 }
5314 h++;
5315 }
5316
5317 if (uri_auth->refresh) {
5318 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5319 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5320 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005321 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005322 break;
5323 }
5324 h++;
5325 }
5326 }
5327
Willy Tarreau55bb8452007-10-17 18:44:57 +02005328 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5329 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5330 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005331 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005332 break;
5333 }
5334 h++;
5335 }
5336
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005337 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5338
Willy Tarreaub2513902006-12-17 14:52:38 +01005339 /* we are in front of a interceptable URI. Let's check
5340 * if there's an authentication and if it's valid.
5341 */
5342 user = uri_auth->users;
5343 if (!user) {
5344 /* no user auth required, it's OK */
5345 authenticated = 1;
5346 } else {
5347 authenticated = 0;
5348
5349 /* a user list is defined, we have to check.
5350 * skip 21 chars for "Authorization: Basic ".
5351 */
5352
5353 /* FIXME: this should move to an earlier place */
5354 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005355 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5356 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5357 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005358 if (len > 14 &&
5359 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005360 txn->auth_hdr.str = h;
5361 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005362 break;
5363 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005364 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005365 }
5366
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005367 if (txn->auth_hdr.len < 21 ||
5368 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005369 user = NULL;
5370
5371 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005372 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5373 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005374 user->user_pwd, user->user_len)) {
5375 authenticated = 1;
5376 break;
5377 }
5378 user = user->next;
5379 }
5380 }
5381
5382 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005383 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005384
5385 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005386 msg.str = trash;
5387 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005388 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005389 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005390 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005391 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005392 if (!(t->flags & SN_ERR_MASK))
5393 t->flags |= SN_ERR_PRXCOND;
5394 if (!(t->flags & SN_FINST_MASK))
5395 t->flags |= SN_FINST_R;
5396 return 1;
5397 }
5398
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005399 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005400 * data.
5401 */
Willy Tarreau72b179a2008-08-28 16:01:32 +02005402 buffer_shutw_now(t->req);
5403 buffer_shutr_now(t->rep);
5404 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005405 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005406 t->data_source = DATA_SRC_STATS;
5407 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005408 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005409 produce_content(t);
5410 return 1;
5411}
5412
5413
Willy Tarreaubaaee002006-06-26 02:48:02 +02005414/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005415 * Print a debug line with a header
5416 */
5417void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5418{
5419 int len, max;
5420 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005421 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005422 max = end - start;
5423 UBOUND(max, sizeof(trash) - len - 1);
5424 len += strlcpy2(trash + len, start, max + 1);
5425 trash[len++] = '\n';
5426 write(1, trash, len);
5427}
5428
5429
Willy Tarreau8797c062007-05-07 00:55:35 +02005430/************************************************************************/
5431/* The code below is dedicated to ACL parsing and matching */
5432/************************************************************************/
5433
5434
5435
5436
5437/* 1. Check on METHOD
5438 * We use the pre-parsed method if it is known, and store its number as an
5439 * integer. If it is unknown, we use the pointer and the length.
5440 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005441static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005442{
5443 int len, meth;
5444
Willy Tarreauae8b7962007-06-09 23:10:04 +02005445 len = strlen(*text);
5446 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005447
5448 pattern->val.i = meth;
5449 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005450 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005451 if (!pattern->ptr.str)
5452 return 0;
5453 pattern->len = len;
5454 }
5455 return 1;
5456}
5457
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005458static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005459acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5460 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005461{
5462 int meth;
5463 struct http_txn *txn = l7;
5464
Willy Tarreaub6866442008-07-14 23:54:42 +02005465 if (!txn)
5466 return 0;
5467
Willy Tarreauc11416f2007-06-17 16:58:38 +02005468 if (txn->req.msg_state != HTTP_MSG_BODY)
5469 return 0;
5470
Willy Tarreau8797c062007-05-07 00:55:35 +02005471 meth = txn->meth;
5472 test->i = meth;
5473 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005474 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5475 /* ensure the indexes are not affected */
5476 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005477 test->len = txn->req.sl.rq.m_l;
5478 test->ptr = txn->req.sol;
5479 }
5480 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5481 return 1;
5482}
5483
5484static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5485{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005486 int icase;
5487
Willy Tarreau8797c062007-05-07 00:55:35 +02005488 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005489 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005490
5491 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005492 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005493
5494 /* Other method, we must compare the strings */
5495 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005496 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005497
5498 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5499 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5500 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005501 return ACL_PAT_FAIL;
5502 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005503}
5504
5505/* 2. Check on Request/Status Version
5506 * We simply compare strings here.
5507 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005508static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005509{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005510 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005511 if (!pattern->ptr.str)
5512 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005513 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005514 return 1;
5515}
5516
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005517static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005518acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5519 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005520{
5521 struct http_txn *txn = l7;
5522 char *ptr;
5523 int len;
5524
Willy Tarreaub6866442008-07-14 23:54:42 +02005525 if (!txn)
5526 return 0;
5527
Willy Tarreauc11416f2007-06-17 16:58:38 +02005528 if (txn->req.msg_state != HTTP_MSG_BODY)
5529 return 0;
5530
Willy Tarreau8797c062007-05-07 00:55:35 +02005531 len = txn->req.sl.rq.v_l;
5532 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5533
5534 while ((len-- > 0) && (*ptr++ != '/'));
5535 if (len <= 0)
5536 return 0;
5537
5538 test->ptr = ptr;
5539 test->len = len;
5540
5541 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5542 return 1;
5543}
5544
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005545static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005546acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5547 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005548{
5549 struct http_txn *txn = l7;
5550 char *ptr;
5551 int len;
5552
Willy Tarreaub6866442008-07-14 23:54:42 +02005553 if (!txn)
5554 return 0;
5555
Willy Tarreauc11416f2007-06-17 16:58:38 +02005556 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5557 return 0;
5558
Willy Tarreau8797c062007-05-07 00:55:35 +02005559 len = txn->rsp.sl.st.v_l;
5560 ptr = txn->rsp.sol;
5561
5562 while ((len-- > 0) && (*ptr++ != '/'));
5563 if (len <= 0)
5564 return 0;
5565
5566 test->ptr = ptr;
5567 test->len = len;
5568
5569 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5570 return 1;
5571}
5572
5573/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005574static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005575acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5576 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005577{
5578 struct http_txn *txn = l7;
5579 char *ptr;
5580 int len;
5581
Willy Tarreaub6866442008-07-14 23:54:42 +02005582 if (!txn)
5583 return 0;
5584
Willy Tarreauc11416f2007-06-17 16:58:38 +02005585 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5586 return 0;
5587
Willy Tarreau8797c062007-05-07 00:55:35 +02005588 len = txn->rsp.sl.st.c_l;
5589 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5590
5591 test->i = __strl2ui(ptr, len);
5592 test->flags = ACL_TEST_F_VOL_1ST;
5593 return 1;
5594}
5595
5596/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005597static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005598acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5599 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005600{
5601 struct http_txn *txn = l7;
5602
Willy Tarreaub6866442008-07-14 23:54:42 +02005603 if (!txn)
5604 return 0;
5605
Willy Tarreauc11416f2007-06-17 16:58:38 +02005606 if (txn->req.msg_state != HTTP_MSG_BODY)
5607 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005608
Willy Tarreauc11416f2007-06-17 16:58:38 +02005609 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5610 /* ensure the indexes are not affected */
5611 return 0;
5612
Willy Tarreau8797c062007-05-07 00:55:35 +02005613 test->len = txn->req.sl.rq.u_l;
5614 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5615
Willy Tarreauf3d25982007-05-08 22:45:09 +02005616 /* we do not need to set READ_ONLY because the data is in a buffer */
5617 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005618 return 1;
5619}
5620
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005621static int
5622acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5623 struct acl_expr *expr, struct acl_test *test)
5624{
5625 struct http_txn *txn = l7;
5626
Willy Tarreaub6866442008-07-14 23:54:42 +02005627 if (!txn)
5628 return 0;
5629
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005630 if (txn->req.msg_state != HTTP_MSG_BODY)
5631 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005632
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005633 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5634 /* ensure the indexes are not affected */
5635 return 0;
5636
5637 /* Parse HTTP request */
5638 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5639 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5640 test->i = AF_INET;
5641
5642 /*
5643 * If we are parsing url in frontend space, we prepare backend stage
5644 * to not parse again the same url ! optimization lazyness...
5645 */
5646 if (px->options & PR_O_HTTP_PROXY)
5647 l4->flags |= SN_ADDR_SET;
5648
5649 test->flags = ACL_TEST_F_READ_ONLY;
5650 return 1;
5651}
5652
5653static int
5654acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5655 struct acl_expr *expr, struct acl_test *test)
5656{
5657 struct http_txn *txn = l7;
5658
Willy Tarreaub6866442008-07-14 23:54:42 +02005659 if (!txn)
5660 return 0;
5661
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005662 if (txn->req.msg_state != HTTP_MSG_BODY)
5663 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005664
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005665 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5666 /* ensure the indexes are not affected */
5667 return 0;
5668
5669 /* Same optimization as url_ip */
5670 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5671 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5672
5673 if (px->options & PR_O_HTTP_PROXY)
5674 l4->flags |= SN_ADDR_SET;
5675
5676 test->flags = ACL_TEST_F_READ_ONLY;
5677 return 1;
5678}
5679
Willy Tarreauc11416f2007-06-17 16:58:38 +02005680/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5681 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5682 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005683static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005684acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005685 struct acl_expr *expr, struct acl_test *test)
5686{
5687 struct http_txn *txn = l7;
5688 struct hdr_idx *idx = &txn->hdr_idx;
5689 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005690
Willy Tarreaub6866442008-07-14 23:54:42 +02005691 if (!txn)
5692 return 0;
5693
Willy Tarreau33a7e692007-06-10 19:45:56 +02005694 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5695 /* search for header from the beginning */
5696 ctx->idx = 0;
5697
Willy Tarreau33a7e692007-06-10 19:45:56 +02005698 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5699 test->flags |= ACL_TEST_F_FETCH_MORE;
5700 test->flags |= ACL_TEST_F_VOL_HDR;
5701 test->len = ctx->vlen;
5702 test->ptr = (char *)ctx->line + ctx->val;
5703 return 1;
5704 }
5705
5706 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5707 test->flags |= ACL_TEST_F_VOL_HDR;
5708 return 0;
5709}
5710
Willy Tarreau33a7e692007-06-10 19:45:56 +02005711static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005712acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5713 struct acl_expr *expr, struct acl_test *test)
5714{
5715 struct http_txn *txn = l7;
5716
Willy Tarreaub6866442008-07-14 23:54:42 +02005717 if (!txn)
5718 return 0;
5719
Willy Tarreauc11416f2007-06-17 16:58:38 +02005720 if (txn->req.msg_state != HTTP_MSG_BODY)
5721 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005722
Willy Tarreauc11416f2007-06-17 16:58:38 +02005723 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5724 /* ensure the indexes are not affected */
5725 return 0;
5726
5727 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5728}
5729
5730static int
5731acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5732 struct acl_expr *expr, struct acl_test *test)
5733{
5734 struct http_txn *txn = l7;
5735
Willy Tarreaub6866442008-07-14 23:54:42 +02005736 if (!txn)
5737 return 0;
5738
Willy Tarreauc11416f2007-06-17 16:58:38 +02005739 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5740 return 0;
5741
5742 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5743}
5744
5745/* 6. Check on HTTP header count. The number of occurrences is returned.
5746 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5747 */
5748static int
5749acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005750 struct acl_expr *expr, struct acl_test *test)
5751{
5752 struct http_txn *txn = l7;
5753 struct hdr_idx *idx = &txn->hdr_idx;
5754 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005755 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005756
Willy Tarreaub6866442008-07-14 23:54:42 +02005757 if (!txn)
5758 return 0;
5759
Willy Tarreau33a7e692007-06-10 19:45:56 +02005760 ctx.idx = 0;
5761 cnt = 0;
5762 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5763 cnt++;
5764
5765 test->i = cnt;
5766 test->flags = ACL_TEST_F_VOL_HDR;
5767 return 1;
5768}
5769
Willy Tarreauc11416f2007-06-17 16:58:38 +02005770static int
5771acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5772 struct acl_expr *expr, struct acl_test *test)
5773{
5774 struct http_txn *txn = l7;
5775
Willy Tarreaub6866442008-07-14 23:54:42 +02005776 if (!txn)
5777 return 0;
5778
Willy Tarreauc11416f2007-06-17 16:58:38 +02005779 if (txn->req.msg_state != HTTP_MSG_BODY)
5780 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005781
Willy Tarreauc11416f2007-06-17 16:58:38 +02005782 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5783 /* ensure the indexes are not affected */
5784 return 0;
5785
5786 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5787}
5788
5789static int
5790acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5791 struct acl_expr *expr, struct acl_test *test)
5792{
5793 struct http_txn *txn = l7;
5794
Willy Tarreaub6866442008-07-14 23:54:42 +02005795 if (!txn)
5796 return 0;
5797
Willy Tarreauc11416f2007-06-17 16:58:38 +02005798 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5799 return 0;
5800
5801 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5802}
5803
Willy Tarreau33a7e692007-06-10 19:45:56 +02005804/* 7. Check on HTTP header's integer value. The integer value is returned.
5805 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005806 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005807 */
5808static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005809acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005810 struct acl_expr *expr, struct acl_test *test)
5811{
5812 struct http_txn *txn = l7;
5813 struct hdr_idx *idx = &txn->hdr_idx;
5814 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005815
Willy Tarreaub6866442008-07-14 23:54:42 +02005816 if (!txn)
5817 return 0;
5818
Willy Tarreau33a7e692007-06-10 19:45:56 +02005819 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5820 /* search for header from the beginning */
5821 ctx->idx = 0;
5822
Willy Tarreau33a7e692007-06-10 19:45:56 +02005823 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5824 test->flags |= ACL_TEST_F_FETCH_MORE;
5825 test->flags |= ACL_TEST_F_VOL_HDR;
5826 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5827 return 1;
5828 }
5829
5830 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5831 test->flags |= ACL_TEST_F_VOL_HDR;
5832 return 0;
5833}
5834
Willy Tarreauc11416f2007-06-17 16:58:38 +02005835static int
5836acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5837 struct acl_expr *expr, struct acl_test *test)
5838{
5839 struct http_txn *txn = l7;
5840
Willy Tarreaub6866442008-07-14 23:54:42 +02005841 if (!txn)
5842 return 0;
5843
Willy Tarreauc11416f2007-06-17 16:58:38 +02005844 if (txn->req.msg_state != HTTP_MSG_BODY)
5845 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005846
Willy Tarreauc11416f2007-06-17 16:58:38 +02005847 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5848 /* ensure the indexes are not affected */
5849 return 0;
5850
5851 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5852}
5853
5854static int
5855acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5856 struct acl_expr *expr, struct acl_test *test)
5857{
5858 struct http_txn *txn = l7;
5859
Willy Tarreaub6866442008-07-14 23:54:42 +02005860 if (!txn)
5861 return 0;
5862
Willy Tarreauc11416f2007-06-17 16:58:38 +02005863 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5864 return 0;
5865
5866 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5867}
5868
Willy Tarreau737b0c12007-06-10 21:28:46 +02005869/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5870 * the first '/' after the possible hostname, and ends before the possible '?'.
5871 */
5872static int
5873acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5874 struct acl_expr *expr, struct acl_test *test)
5875{
5876 struct http_txn *txn = l7;
5877 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005878
Willy Tarreaub6866442008-07-14 23:54:42 +02005879 if (!txn)
5880 return 0;
5881
Willy Tarreauc11416f2007-06-17 16:58:38 +02005882 if (txn->req.msg_state != HTTP_MSG_BODY)
5883 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005884
Willy Tarreauc11416f2007-06-17 16:58:38 +02005885 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5886 /* ensure the indexes are not affected */
5887 return 0;
5888
Willy Tarreau21d2af32008-02-14 20:25:24 +01005889 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5890 ptr = http_get_path(txn);
5891 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005892 return 0;
5893
5894 /* OK, we got the '/' ! */
5895 test->ptr = ptr;
5896
5897 while (ptr < end && *ptr != '?')
5898 ptr++;
5899
5900 test->len = ptr - test->ptr;
5901
5902 /* we do not need to set READ_ONLY because the data is in a buffer */
5903 test->flags = ACL_TEST_F_VOL_1ST;
5904 return 1;
5905}
5906
5907
Willy Tarreau8797c062007-05-07 00:55:35 +02005908
5909/************************************************************************/
5910/* All supported keywords must be declared here. */
5911/************************************************************************/
5912
5913/* Note: must not be declared <const> as its list will be overwritten */
5914static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005915 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5916 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5917 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5918 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005919
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005920 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5921 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5922 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5923 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5924 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5925 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5926 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5927 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5928 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005929
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005930 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5931 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5932 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5933 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5934 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5935 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5936 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5937 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5938 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5939 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005940
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005941 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5942 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5943 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5944 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5945 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5946 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5947 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5948 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5949 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005950
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005951 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5952 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5953 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5954 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5955 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5956 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5957 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005958
Willy Tarreauf3d25982007-05-08 22:45:09 +02005959 { NULL, NULL, NULL, NULL },
5960
5961#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005962 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5963 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5964 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5965 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5966 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5967 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5968 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5969
Willy Tarreau8797c062007-05-07 00:55:35 +02005970 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5971 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5972 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5973 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5974 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5975 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5976 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5977 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5978
5979 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5980 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5981 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5982 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5983 { NULL, NULL, NULL, NULL },
5984#endif
5985}};
5986
5987
5988__attribute__((constructor))
5989static void __http_protocol_init(void)
5990{
5991 acl_register_keywords(&acl_kws);
5992}
5993
5994
Willy Tarreau58f10d72006-12-04 02:26:12 +01005995/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005996 * Local variables:
5997 * c-indent-level: 8
5998 * c-basic-offset: 8
5999 * End:
6000 */