blob: 9b3d0bc6c8e433fc6e4e76e992dcafbda5cc35ff [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 Tarreaud7704b52008-09-04 11:51:16 +0200662 /* 1: Check timeouts only during data phase for now */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200663 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200664 buffer_check_timeouts(s->req);
665 buffer_check_timeouts(s->rep);
Willy Tarreaud7704b52008-09-04 11:51:16 +0200666 stream_sock_check_timeouts(&s->si[0]);
667 stream_sock_check_timeouts(&s->si[1]);
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200668
669 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
670 if (s->req->flags & BF_READ_TIMEOUT) {
671 buffer_shutw(s->req);
672 s->req->cons->shutr(s->req->prod);
673 }
674 if (s->req->flags & BF_WRITE_TIMEOUT) {
675 buffer_shutw(s->req);
676 s->req->cons->shutw(s->req->cons);
677 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200678 }
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200679
680 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
681 if (s->rep->flags & BF_READ_TIMEOUT) {
682 buffer_shutw(s->rep);
683 s->rep->cons->shutr(s->rep->prod);
684 }
685 if (s->rep->flags & BF_WRITE_TIMEOUT) {
686 buffer_shutw(s->rep);
687 s->rep->cons->shutw(s->rep->cons);
688 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200689 }
Willy Tarreau35374672008-09-03 18:11:02 +0200690 /* Note that we don't check nor indicate if we wake up because
691 * of a timeout on a stream interface.
692 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200693 }
694
Willy Tarreaud7704b52008-09-04 11:51:16 +0200695 /* 2: Check if we need to close the write side. This can only happen
Willy Tarreau48adac52008-08-30 04:58:38 +0200696 * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear
697 * from low level, and neither HIJACK nor SHUTW can disappear from low
698 * level.
699 */
700 if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
701 buffer_shutw(s->req);
702 s->req->cons->shutw(s->req->cons);
703 }
704
705 if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) {
706 buffer_shutw(s->rep);
707 s->rep->cons->shutw(s->rep->cons);
708 }
709
Willy Tarreaud7704b52008-09-04 11:51:16 +0200710 /* 3: When a server-side connection is released, we have to
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200711 * count it and check for pending connections on this server.
712 */
713 if (unlikely(s->req->cons->state == SI_ST_CLO &&
714 s->req->cons->prev_state == SI_ST_EST)) {
715 /* Count server-side errors (but not timeouts). */
716 if (s->req->flags & BF_WRITE_ERROR) {
717 s->be->failed_resp++;
718 if (s->srv)
719 s->srv->failed_resp++;
720 }
721
722 if (s->srv) {
723 s->srv->cur_sess--;
724 sess_change_server(s, NULL);
725 if (may_dequeue_tasks(s->srv, s->be))
726 process_srv_queue(s->srv);
727 }
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200728 }
729
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200730 /* Dirty trick: force one first pass everywhere */
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200731 rqf_cli = rqf_srv = ~s->req->flags;
732 rpf_cli = rpf_srv = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +0200733
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200734 /* well, the ST_CONN state is already handled properly */
735 if (s->req->prod->state == SI_ST_EST) {
736 rqf_cli = s->req->flags;
737 rpf_cli = s->rep->flags;
738 }
739
740 if (s->req->cons->state == SI_ST_EST) {
741 rqf_srv = s->req->flags;
742 rpf_srv = s->rep->flags;
743 }
744
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 do {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200746 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",
747 now_ms, __FUNCTION__,
748 t,
749 s->req, s->rep,
750 s->req->rex, s->rep->wex,
751 s->req->flags, s->rep->flags,
752 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
753
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200754 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +0200755
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200756 /* Maybe resync client FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200757 if (s->rep->cons->state != SI_ST_CLO) {
758 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
759 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200760 stream_sock_data_update(s->rep->cons->fd);
761 rqf_cli = s->req->flags;
762 rpf_cli = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +0200763 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200764 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200765
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200766 /* Maybe resync server FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200767 if (s->req->cons->state != SI_ST_CLO) {
768 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
769 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200770 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200771 process_srv_conn(s);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200772 resync = 1; /* we might have to resync */
773 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200774
775 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +0200776 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +0200777 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +0200778 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200779 /* We want to force the connection to the server to close,
780 * and the server has begun to respond. That's the right
781 * time.
782 */
783 buffer_shutw_now(s->req);
784 }
785
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200786 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +0200787
788 /* When a server-side connection is released, we have to
789 * count it and check for pending connections on this server.
790 */
791 if (s->req->cons->state == SI_ST_CLO) {
792 if (s->srv) {
793 s->srv->cur_sess--;
794 sess_change_server(s, NULL);
795 if (may_dequeue_tasks(s->srv, s->be))
796 process_srv_queue(s->srv);
797 }
798 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200799 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200800 rqf_srv = s->req->flags;
801 rpf_srv = s->rep->flags;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200802 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200803 }
804
805 /* we may have to resync because of pending connections */
806 if (resync)
807 continue;
808
809 /**** Process layer 7 below ****/
810
811 /* Analyse request */
812 if (s->req->flags & BF_MASK_ANALYSER) {
813 unsigned int flags = s->req->flags;
814
815 if (s->req->prod->state >= SI_ST_EST) {
816 /* it's up to the analysers to reset write_ena */
817 buffer_write_ena(s->req);
818 if (s->req->analysers)
819 process_request(s);
820 }
821 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
822 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
823 if (s->req->flags != flags)
824 resync = 1;
825 }
826
827 /* Analyse response */
828 if (unlikely(s->rep->flags & BF_HIJACK)) {
829 /* In inject mode, we wake up everytime something has
830 * happened on the write side of the buffer.
831 */
832 unsigned int flags = s->rep->flags;
833
834 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
835 !(s->rep->flags & BF_FULL)) {
836 produce_content(s);
837 }
838 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
839 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
840 if (s->rep->flags != flags)
841 resync = 1;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200842 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200843 else if (s->rep->flags & BF_MASK_ANALYSER) {
844 unsigned int flags = s->rep->flags;
845
846 if (s->rep->prod->state >= SI_ST_EST) {
847 /* it's up to the analysers to reset write_ena */
848 buffer_write_ena(s->rep);
849 if (s->rep->analysers)
850 process_response(s);
851 }
852 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
853 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
854 if (s->rep->flags != flags)
855 resync = 1;
856 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200857
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200858 /* For the moment, we need to clean the client and server flags that
859 * have vanished. This is just a temporary measure though.
860 */
861 rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
862 rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
863 rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
864 rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaudafde432008-08-17 01:00:46 +0200865 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866
Willy Tarreaua37095b2008-09-03 11:37:47 +0200867 /* This is needed only when debugging is enabled, to indicate
868 * client-side or server-side close. Please note that in the unlikely
869 * event where both sides would close at once, the sequence is reported
870 * on the server side first.
871 */
872 if (unlikely((global.mode & MODE_DEBUG) &&
873 (!(global.mode & MODE_QUIET) ||
874 (global.mode & MODE_VERBOSE)))) {
875 int len;
876
877 if (s->si[1].state == SI_ST_CLO &&
878 s->si[1].prev_state == SI_ST_EST) {
879 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
880 s->uniq_id, s->be->id,
881 (unsigned short)s->si[0].fd,
882 (unsigned short)s->si[1].fd);
883 write(1, trash, len);
884 }
885
886 if (s->si[0].state == SI_ST_CLO &&
887 s->si[0].prev_state == SI_ST_EST) {
888 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
889 s->uniq_id, s->be->id,
890 (unsigned short)s->si[0].fd,
891 (unsigned short)s->si[1].fd);
892 write(1, trash, len);
893 }
894 }
895
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200896 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200897 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100898
899 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
900 session_process_counters(s);
901
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200902 if (s->rep->cons->state == SI_ST_EST)
903 stream_sock_data_finish(s->rep->cons->fd);
904
905 if (s->req->cons->state == SI_ST_EST)
906 stream_sock_data_finish(s->req->cons->fd);
907
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200908 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
909 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200910 s->si[0].prev_state = s->si[0].state;
911 s->si[1].prev_state = s->si[1].state;
Willy Tarreaud7704b52008-09-04 11:51:16 +0200912 s->si[0].flags = s->si[1].flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913
Willy Tarreau7f875f62008-08-11 17:35:01 +0200914 /* Trick: if a request is being waiting for the server to respond,
915 * and if we know the server can timeout, we don't want the timeout
916 * to expire on the client side first, but we're still interested
917 * in passing data from the client to the server (eg: POST). Thus,
918 * we can cancel the client's request timeout if the server's
919 * request timeout is set and the server has not yet sent a response.
920 */
921
Willy Tarreau3da77c52008-08-29 09:58:42 +0200922 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200923 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200924 s->req->rex = TICK_ETERNITY;
925
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200926 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
927 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200928 if (s->req->analysers)
929 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930
Willy Tarreau35374672008-09-03 18:11:02 +0200931 if (s->si[0].exp)
932 t->expire = tick_first(t->expire, s->si[0].exp);
933
934 if (s->si[1].exp)
935 t->expire = tick_first(t->expire, s->si[1].exp);
936
Willy Tarreaucb651252008-08-29 13:57:30 +0200937#ifdef DEBUG_FULL
938 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n",
939 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex);
940#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941 /* restore t to its place in the task list */
942 task_queue(t);
943
Willy Tarreaua7c52762008-08-16 18:40:18 +0200944#ifdef DEBUG_DEV
945 /* this may only happen when no timeout is set or in case of an FSM bug */
946 if (!t->expire)
947 ABORT_NOW();
948#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200949 *next = t->expire;
950 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 }
952
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100953 s->fe->feconn--;
954 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200955 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956 actconn--;
957
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200958 if (unlikely((global.mode & MODE_DEBUG) &&
959 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200961 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200962 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200963 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200964 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965 write(1, trash, len);
966 }
967
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200968 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100969 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200970
971 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200972 if (s->logs.logwait &&
973 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200974 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
975 if (s->fe->to_log & LW_REQ)
976 http_sess_log(s);
977 else
978 tcp_sess_log(s);
979 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200980
981 /* the task MUST not be in the run queue anymore */
982 task_delete(t);
983 session_free(s);
984 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200985 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986}
987
988
Willy Tarreau42250582007-04-01 01:30:43 +0200989extern const char sess_term_cond[8];
990extern const char sess_fin_state[8];
991extern const char *monthname[12];
992const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
993const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
994 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
995 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200996struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200997struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100998
Willy Tarreau42250582007-04-01 01:30:43 +0200999/*
1000 * send a log for the session when we have enough info about it.
1001 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001002 */
Willy Tarreau42250582007-04-01 01:30:43 +02001003static void http_sess_log(struct session *s)
1004{
1005 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1006 struct proxy *fe = s->fe;
1007 struct proxy *be = s->be;
1008 struct proxy *prx_log;
1009 struct http_txn *txn = &s->txn;
1010 int tolog;
1011 char *uri, *h;
1012 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001013 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001014 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001015 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001016 int hdr;
1017
1018 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1019 return;
1020 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001021
Willy Tarreau42250582007-04-01 01:30:43 +02001022 if (s->cli_addr.ss_family == AF_INET)
1023 inet_ntop(AF_INET,
1024 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1025 pn, sizeof(pn));
1026 else
1027 inet_ntop(AF_INET6,
1028 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1029 pn, sizeof(pn));
1030
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001031 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001032
1033 /* FIXME: let's limit ourselves to frontend logging for now. */
1034 tolog = fe->to_log;
1035
1036 h = tmpline;
1037 if (fe->to_log & LW_REQHDR &&
1038 txn->req.cap &&
1039 (h < tmpline + sizeof(tmpline) - 10)) {
1040 *(h++) = ' ';
1041 *(h++) = '{';
1042 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1043 if (hdr)
1044 *(h++) = '|';
1045 if (txn->req.cap[hdr] != NULL)
1046 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1047 '#', hdr_encode_map, txn->req.cap[hdr]);
1048 }
1049 *(h++) = '}';
1050 }
1051
1052 if (fe->to_log & LW_RSPHDR &&
1053 txn->rsp.cap &&
1054 (h < tmpline + sizeof(tmpline) - 7)) {
1055 *(h++) = ' ';
1056 *(h++) = '{';
1057 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1058 if (hdr)
1059 *(h++) = '|';
1060 if (txn->rsp.cap[hdr] != NULL)
1061 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1062 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1063 }
1064 *(h++) = '}';
1065 }
1066
1067 if (h < tmpline + sizeof(tmpline) - 4) {
1068 *(h++) = ' ';
1069 *(h++) = '"';
1070 uri = txn->uri ? txn->uri : "<BADREQ>";
1071 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1072 '#', url_encode_map, uri);
1073 *(h++) = '"';
1074 }
1075 *h = '\0';
1076
1077 svid = (tolog & LW_SVID) ?
1078 (s->data_source != DATA_SRC_STATS) ?
1079 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1080
Willy Tarreau70089872008-06-13 21:12:51 +02001081 t_request = -1;
1082 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1083 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1084
Willy Tarreau42250582007-04-01 01:30:43 +02001085 send_log(prx_log, LOG_INFO,
1086 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
1087 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001088 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001089 pn,
1090 (s->cli_addr.ss_family == AF_INET) ?
1091 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1092 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001093 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001094 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001095 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001096 t_request,
1097 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001098 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1099 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1100 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1101 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001102 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001103 txn->cli_cookie ? txn->cli_cookie : "-",
1104 txn->srv_cookie ? txn->srv_cookie : "-",
1105 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1106 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1107 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1108 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1109 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001110 (s->flags & SN_REDISP)?"+":"",
1111 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001112 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1113
1114 s->logs.logwait = 0;
1115}
1116
Willy Tarreau117f59e2007-03-04 18:17:17 +01001117
1118/*
1119 * Capture headers from message starting at <som> according to header list
1120 * <cap_hdr>, and fill the <idx> structure appropriately.
1121 */
1122void capture_headers(char *som, struct hdr_idx *idx,
1123 char **cap, struct cap_hdr *cap_hdr)
1124{
1125 char *eol, *sol, *col, *sov;
1126 int cur_idx;
1127 struct cap_hdr *h;
1128 int len;
1129
1130 sol = som + hdr_idx_first_pos(idx);
1131 cur_idx = hdr_idx_first_idx(idx);
1132
1133 while (cur_idx) {
1134 eol = sol + idx->v[cur_idx].len;
1135
1136 col = sol;
1137 while (col < eol && *col != ':')
1138 col++;
1139
1140 sov = col + 1;
1141 while (sov < eol && http_is_lws[(unsigned char)*sov])
1142 sov++;
1143
1144 for (h = cap_hdr; h; h = h->next) {
1145 if ((h->namelen == col - sol) &&
1146 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1147 if (cap[h->index] == NULL)
1148 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001149 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001150
1151 if (cap[h->index] == NULL) {
1152 Alert("HTTP capture : out of memory.\n");
1153 continue;
1154 }
1155
1156 len = eol - sov;
1157 if (len > h->len)
1158 len = h->len;
1159
1160 memcpy(cap[h->index], sov, len);
1161 cap[h->index][len]=0;
1162 }
1163 }
1164 sol = eol + idx->v[cur_idx].cr + 1;
1165 cur_idx = idx->v[cur_idx].next;
1166 }
1167}
1168
1169
Willy Tarreau42250582007-04-01 01:30:43 +02001170/* either we find an LF at <ptr> or we jump to <bad>.
1171 */
1172#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1173
1174/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1175 * otherwise to <http_msg_ood> with <state> set to <st>.
1176 */
1177#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1178 ptr++; \
1179 if (likely(ptr < end)) \
1180 goto good; \
1181 else { \
1182 state = (st); \
1183 goto http_msg_ood; \
1184 } \
1185 } while (0)
1186
1187
Willy Tarreaubaaee002006-06-26 02:48:02 +02001188/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001189 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1191 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1192 * will give undefined results.
1193 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1194 * and that msg->sol points to the beginning of the response.
1195 * If a complete line is found (which implies that at least one CR or LF is
1196 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1197 * returned indicating an incomplete line (which does not mean that parts have
1198 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1199 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1200 * upon next call.
1201 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001202 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1204 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001205 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001207const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1208 unsigned int state, const char *ptr, const char *end,
1209 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001210{
1211 __label__
1212 http_msg_rpver,
1213 http_msg_rpver_sp,
1214 http_msg_rpcode,
1215 http_msg_rpcode_sp,
1216 http_msg_rpreason,
1217 http_msg_rpline_eol,
1218 http_msg_ood, /* out of data */
1219 http_msg_invalid;
1220
1221 switch (state) {
1222 http_msg_rpver:
1223 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001224 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001225 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1226
1227 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001228 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1230 }
1231 goto http_msg_invalid;
1232
1233 http_msg_rpver_sp:
1234 case HTTP_MSG_RPVER_SP:
1235 if (likely(!HTTP_IS_LWS(*ptr))) {
1236 msg->sl.st.c = ptr - msg_buf;
1237 goto http_msg_rpcode;
1238 }
1239 if (likely(HTTP_IS_SPHT(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1241 /* so it's a CR/LF, this is invalid */
1242 goto http_msg_invalid;
1243
1244 http_msg_rpcode:
1245 case HTTP_MSG_RPCODE:
1246 if (likely(!HTTP_IS_LWS(*ptr)))
1247 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1248
1249 if (likely(HTTP_IS_SPHT(*ptr))) {
1250 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1252 }
1253
1254 /* so it's a CR/LF, so there is no reason phrase */
1255 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1256 http_msg_rsp_reason:
1257 /* FIXME: should we support HTTP responses without any reason phrase ? */
1258 msg->sl.st.r = ptr - msg_buf;
1259 msg->sl.st.r_l = 0;
1260 goto http_msg_rpline_eol;
1261
1262 http_msg_rpcode_sp:
1263 case HTTP_MSG_RPCODE_SP:
1264 if (likely(!HTTP_IS_LWS(*ptr))) {
1265 msg->sl.st.r = ptr - msg_buf;
1266 goto http_msg_rpreason;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1270 /* so it's a CR/LF, so there is no reason phrase */
1271 goto http_msg_rsp_reason;
1272
1273 http_msg_rpreason:
1274 case HTTP_MSG_RPREASON:
1275 if (likely(!HTTP_IS_CRLF(*ptr)))
1276 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1277 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1278 http_msg_rpline_eol:
1279 /* We have seen the end of line. Note that we do not
1280 * necessarily have the \n yet, but at least we know that we
1281 * have EITHER \r OR \n, otherwise the response would not be
1282 * complete. We can then record the response length and return
1283 * to the caller which will be able to register it.
1284 */
1285 msg->sl.st.l = ptr - msg->sol;
1286 return ptr;
1287
1288#ifdef DEBUG_FULL
1289 default:
1290 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1291 exit(1);
1292#endif
1293 }
1294
1295 http_msg_ood:
1296 /* out of data */
1297 if (ret_state)
1298 *ret_state = state;
1299 if (ret_ptr)
1300 *ret_ptr = (char *)ptr;
1301 return NULL;
1302
1303 http_msg_invalid:
1304 /* invalid message */
1305 if (ret_state)
1306 *ret_state = HTTP_MSG_ERROR;
1307 return NULL;
1308}
1309
1310
1311/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 * This function parses a request line between <ptr> and <end>, starting with
1313 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1314 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1315 * will give undefined results.
1316 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1317 * and that msg->sol points to the beginning of the request.
1318 * If a complete line is found (which implies that at least one CR or LF is
1319 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1320 * returned indicating an incomplete line (which does not mean that parts have
1321 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1322 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1323 * upon next call.
1324 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001325 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1327 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001328 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001329 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001330const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1331 unsigned int state, const char *ptr, const char *end,
1332 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 __label__
1335 http_msg_rqmeth,
1336 http_msg_rqmeth_sp,
1337 http_msg_rquri,
1338 http_msg_rquri_sp,
1339 http_msg_rqver,
1340 http_msg_rqline_eol,
1341 http_msg_ood, /* out of data */
1342 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 switch (state) {
1345 http_msg_rqmeth:
1346 case HTTP_MSG_RQMETH:
1347 if (likely(HTTP_IS_TOKEN(*ptr)))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001351 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1353 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 if (likely(HTTP_IS_CRLF(*ptr))) {
1356 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001357 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_req09_uri:
1359 msg->sl.rq.u = ptr - msg_buf;
1360 http_msg_req09_uri_e:
1361 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1362 http_msg_req09_ver:
1363 msg->sl.rq.v = ptr - msg_buf;
1364 msg->sl.rq.v_l = 0;
1365 goto http_msg_rqline_eol;
1366 }
1367 goto http_msg_invalid;
1368
1369 http_msg_rqmeth_sp:
1370 case HTTP_MSG_RQMETH_SP:
1371 if (likely(!HTTP_IS_LWS(*ptr))) {
1372 msg->sl.rq.u = ptr - msg_buf;
1373 goto http_msg_rquri;
1374 }
1375 if (likely(HTTP_IS_SPHT(*ptr)))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1377 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1378 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 http_msg_rquri:
1381 case HTTP_MSG_RQURI:
1382 if (likely(!HTTP_IS_LWS(*ptr)))
1383 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 if (likely(HTTP_IS_SPHT(*ptr))) {
1386 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1387 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1391 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 http_msg_rquri_sp:
1394 case HTTP_MSG_RQURI_SP:
1395 if (likely(!HTTP_IS_LWS(*ptr))) {
1396 msg->sl.rq.v = ptr - msg_buf;
1397 goto http_msg_rqver;
1398 }
1399 if (likely(HTTP_IS_SPHT(*ptr)))
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1401 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1402 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 http_msg_rqver:
1405 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001406 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001408
1409 if (likely(HTTP_IS_CRLF(*ptr))) {
1410 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1411 http_msg_rqline_eol:
1412 /* We have seen the end of line. Note that we do not
1413 * necessarily have the \n yet, but at least we know that we
1414 * have EITHER \r OR \n, otherwise the request would not be
1415 * complete. We can then record the request length and return
1416 * to the caller which will be able to register it.
1417 */
1418 msg->sl.rq.l = ptr - msg->sol;
1419 return ptr;
1420 }
1421
1422 /* neither an HTTP_VER token nor a CRLF */
1423 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425#ifdef DEBUG_FULL
1426 default:
1427 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1428 exit(1);
1429#endif
1430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 http_msg_ood:
1433 /* out of data */
1434 if (ret_state)
1435 *ret_state = state;
1436 if (ret_ptr)
1437 *ret_ptr = (char *)ptr;
1438 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 http_msg_invalid:
1441 /* invalid message */
1442 if (ret_state)
1443 *ret_state = HTTP_MSG_ERROR;
1444 return NULL;
1445}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
1447
Willy Tarreau8973c702007-01-21 23:58:29 +01001448/*
1449 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001450 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 * when data are missing and recalled at the exact same location with no
1452 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001453 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1454 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1457{
1458 __label__
1459 http_msg_rqbefore,
1460 http_msg_rqbefore_cr,
1461 http_msg_rqmeth,
1462 http_msg_rqline_end,
1463 http_msg_hdr_first,
1464 http_msg_hdr_name,
1465 http_msg_hdr_l1_sp,
1466 http_msg_hdr_l1_lf,
1467 http_msg_hdr_l1_lws,
1468 http_msg_hdr_val,
1469 http_msg_hdr_l2_lf,
1470 http_msg_hdr_l2_lws,
1471 http_msg_complete_header,
1472 http_msg_last_lf,
1473 http_msg_ood, /* out of data */
1474 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreaue69eada2008-01-27 00:34:10 +01001476 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001478
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001479 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 ptr = buf->lr;
1481 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (unlikely(ptr >= end))
1484 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 /*
1488 * First, states that are specific to the response only.
1489 * We check them first so that request and headers are
1490 * closer to each other (accessed more often).
1491 */
1492 http_msg_rpbefore:
1493 case HTTP_MSG_RPBEFORE:
1494 if (likely(HTTP_IS_TOKEN(*ptr))) {
1495 if (likely(ptr == buf->data)) {
1496 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001497 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001498 } else {
1499#if PARSE_PRESERVE_EMPTY_LINES
1500 /* only skip empty leading lines, don't remove them */
1501 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001502 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001503#else
1504 /* Remove empty leading lines, as recommended by
1505 * RFC2616. This takes a lot of time because we
1506 * must move all the buffer backwards, but this
1507 * is rarely needed. The method above will be
1508 * cleaner when we'll be able to start sending
1509 * the request from any place in the buffer.
1510 */
1511 buf->lr = ptr;
1512 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001513 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 msg->sol = buf->data;
1515 ptr = buf->data;
1516 end = buf->r;
1517#endif
1518 }
1519 hdr_idx_init(idx);
1520 state = HTTP_MSG_RPVER;
1521 goto http_msg_rpver;
1522 }
1523
1524 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1525 goto http_msg_invalid;
1526
1527 if (unlikely(*ptr == '\n'))
1528 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1530 /* stop here */
1531
1532 http_msg_rpbefore_cr:
1533 case HTTP_MSG_RPBEFORE_CR:
1534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1536 /* stop here */
1537
1538 http_msg_rpver:
1539 case HTTP_MSG_RPVER:
1540 case HTTP_MSG_RPVER_SP:
1541 case HTTP_MSG_RPCODE:
1542 case HTTP_MSG_RPCODE_SP:
1543 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001544 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001545 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001546 if (unlikely(!ptr))
1547 return;
1548
1549 /* we have a full response and we know that we have either a CR
1550 * or an LF at <ptr>.
1551 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001552 //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 +01001553 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1554
1555 msg->sol = ptr;
1556 if (likely(*ptr == '\r'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1558 goto http_msg_rpline_end;
1559
1560 http_msg_rpline_end:
1561 case HTTP_MSG_RPLINE_END:
1562 /* msg->sol must point to the first of CR or LF. */
1563 EXPECT_LF_HERE(ptr, http_msg_invalid);
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1565 /* stop here */
1566
1567 /*
1568 * Second, states that are specific to the request only
1569 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 http_msg_rqbefore:
1571 case HTTP_MSG_RQBEFORE:
1572 if (likely(HTTP_IS_TOKEN(*ptr))) {
1573 if (likely(ptr == buf->data)) {
1574 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001575 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 } else {
1577#if PARSE_PRESERVE_EMPTY_LINES
1578 /* only skip empty leading lines, don't remove them */
1579 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* Remove empty leading lines, as recommended by
1583 * RFC2616. This takes a lot of time because we
1584 * must move all the buffer backwards, but this
1585 * is rarely needed. The method above will be
1586 * cleaner when we'll be able to start sending
1587 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 buf->lr = ptr;
1590 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001591 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 msg->sol = buf->data;
1593 ptr = buf->data;
1594 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001595#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001597 /* we will need this when keep-alive will be supported
1598 hdr_idx_init(idx);
1599 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 state = HTTP_MSG_RQMETH;
1601 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1605 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 if (unlikely(*ptr == '\n'))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1609 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001610 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 http_msg_rqbefore_cr:
1613 case HTTP_MSG_RQBEFORE_CR:
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 http_msg_rqmeth:
1619 case HTTP_MSG_RQMETH:
1620 case HTTP_MSG_RQMETH_SP:
1621 case HTTP_MSG_RQURI:
1622 case HTTP_MSG_RQURI_SP:
1623 case HTTP_MSG_RQVER:
1624 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001625 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 if (unlikely(!ptr))
1627 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 /* we have a full request and we know that we have either a CR
1630 * or an LF at <ptr>.
1631 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001632 //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 +01001633 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 msg->sol = ptr;
1636 if (likely(*ptr == '\r'))
1637 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 http_msg_rqline_end:
1641 case HTTP_MSG_RQLINE_END:
1642 /* check for HTTP/0.9 request : no version information available.
1643 * msg->sol must point to the first of CR or LF.
1644 */
1645 if (unlikely(msg->sl.rq.v_l == 0))
1646 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 EXPECT_LF_HERE(ptr, http_msg_invalid);
1649 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /*
1653 * Common states below
1654 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 http_msg_hdr_first:
1656 case HTTP_MSG_HDR_FIRST:
1657 msg->sol = ptr;
1658 if (likely(!HTTP_IS_CRLF(*ptr))) {
1659 goto http_msg_hdr_name;
1660 }
1661
1662 if (likely(*ptr == '\r'))
1663 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1664 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_hdr_name:
1667 case HTTP_MSG_HDR_NAME:
1668 /* assumes msg->sol points to the first char */
1669 if (likely(HTTP_IS_TOKEN(*ptr)))
1670 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (likely(*ptr == ':')) {
1673 msg->col = ptr - buf->data;
1674 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1675 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 http_msg_hdr_l1_sp:
1680 case HTTP_MSG_HDR_L1_SP:
1681 /* assumes msg->sol points to the first char and msg->col to the colon */
1682 if (likely(HTTP_IS_SPHT(*ptr)))
1683 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 /* header value can be basically anything except CR/LF */
1686 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 if (likely(!HTTP_IS_CRLF(*ptr))) {
1689 goto http_msg_hdr_val;
1690 }
1691
1692 if (likely(*ptr == '\r'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1694 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 http_msg_hdr_l1_lf:
1697 case HTTP_MSG_HDR_L1_LF:
1698 EXPECT_LF_HERE(ptr, http_msg_invalid);
1699 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 http_msg_hdr_l1_lws:
1702 case HTTP_MSG_HDR_L1_LWS:
1703 if (likely(HTTP_IS_SPHT(*ptr))) {
1704 /* replace HT,CR,LF with spaces */
1705 for (; buf->data+msg->sov < ptr; msg->sov++)
1706 buf->data[msg->sov] = ' ';
1707 goto http_msg_hdr_l1_sp;
1708 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001709 /* we had a header consisting only in spaces ! */
1710 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 goto http_msg_complete_header;
1712
1713 http_msg_hdr_val:
1714 case HTTP_MSG_HDR_VAL:
1715 /* assumes msg->sol points to the first char, msg->col to the
1716 * colon, and msg->sov points to the first character of the
1717 * value.
1718 */
1719 if (likely(!HTTP_IS_CRLF(*ptr)))
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 msg->eol = ptr;
1723 /* Note: we could also copy eol into ->eoh so that we have the
1724 * real header end in case it ends with lots of LWS, but is this
1725 * really needed ?
1726 */
1727 if (likely(*ptr == '\r'))
1728 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1729 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 http_msg_hdr_l2_lf:
1732 case HTTP_MSG_HDR_L2_LF:
1733 EXPECT_LF_HERE(ptr, http_msg_invalid);
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 http_msg_hdr_l2_lws:
1737 case HTTP_MSG_HDR_L2_LWS:
1738 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1739 /* LWS: replace HT,CR,LF with spaces */
1740 for (; msg->eol < ptr; msg->eol++)
1741 *msg->eol = ' ';
1742 goto http_msg_hdr_val;
1743 }
1744 http_msg_complete_header:
1745 /*
1746 * It was a new header, so the last one is finished.
1747 * Assumes msg->sol points to the first char, msg->col to the
1748 * colon, msg->sov points to the first character of the value
1749 * and msg->eol to the first CR or LF so we know how the line
1750 * ends. We insert last header into the index.
1751 */
1752 /*
1753 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1754 write(2, msg->sol, msg->eol-msg->sol);
1755 fprintf(stderr,"\n");
1756 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1759 idx, idx->tail) < 0))
1760 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 msg->sol = ptr;
1763 if (likely(!HTTP_IS_CRLF(*ptr))) {
1764 goto http_msg_hdr_name;
1765 }
1766
1767 if (likely(*ptr == '\r'))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1769 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 http_msg_last_lf:
1772 case HTTP_MSG_LAST_LF:
1773 /* Assumes msg->sol points to the first of either CR or LF */
1774 EXPECT_LF_HERE(ptr, http_msg_invalid);
1775 ptr++;
1776 buf->lr = ptr;
1777 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001778 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 return;
1780#ifdef DEBUG_FULL
1781 default:
1782 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1783 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 }
1786 http_msg_ood:
1787 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001788 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 buf->lr = ptr;
1790 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 http_msg_invalid:
1793 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001794 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 return;
1796}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001797
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001798/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001799 * It normally returns zero, but may return 1 if it absolutely needs to be
1800 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001801 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001802 * functions. Its behaviour is rather simple :
1803 * - all enabled analysers are called in turn from the lower to the higher
1804 * bit.
1805 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02001806 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001807 * that unprocessed data will not be forwarded. But that probably depends on
1808 * the protocol. Generally it is not reset in case of errors.
1809 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02001810 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001811 * analysis).
1812 * - if an analyser thinks it has no added value anymore staying here, it must
1813 * reset its bit from the analysers flags in order not to be called anymore.
1814 *
1815 * In the future, analysers should be able to indicate that they want to be
1816 * called after XXX bytes have been received (or transfered), and the min of
1817 * all's wishes will be used to ring back (unless a special condition occurs).
1818 *
1819 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001821int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 struct buffer *req = t->req;
1824 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001825
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001826 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 +02001827 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001828 t,
1829 req,
1830 req->rex, req->wex,
1831 req->flags,
1832 req->l,
1833 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001834
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001835 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001836 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001837 struct tcp_rule *rule;
1838 int partial;
1839
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001840 /* We will abort if we encounter a read error. In theory, we
1841 * should not abort if we get a close, it might be valid,
1842 * although very unlikely. FIXME: we'll abort for now, this
1843 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001844 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001845 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001846 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001847 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001848 if (!(t->flags & SN_ERR_MASK))
1849 t->flags |= SN_ERR_CLICL;
1850 if (!(t->flags & SN_FINST_MASK))
1851 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001852 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001853 }
1854
1855 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001856 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001857 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001858 t->fe->failed_req++;
1859 if (!(t->flags & SN_ERR_MASK))
1860 t->flags |= SN_ERR_CLITO;
1861 if (!(t->flags & SN_FINST_MASK))
1862 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001863 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001864 }
1865
1866 /* We don't know whether we have enough data, so must proceed
1867 * this way :
1868 * - iterate through all rules in their declaration order
1869 * - if one rule returns MISS, it means the inspect delay is
1870 * not over yet, then return immediately, otherwise consider
1871 * it as a non-match.
1872 * - if one rule returns OK, then return OK
1873 * - if one rule returns KO, then return KO
1874 */
1875
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001876 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001877 partial = 0;
1878 else
1879 partial = ACL_PARTIAL;
1880
1881 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1882 int ret = ACL_PAT_PASS;
1883
1884 if (rule->cond) {
1885 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1886 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001887 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001888 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001889 if (!tick_isset(req->analyse_exp))
1890 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001891 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001892 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001893
Willy Tarreaub6866442008-07-14 23:54:42 +02001894 ret = acl_pass(ret);
1895 if (rule->cond->pol == ACL_COND_UNLESS)
1896 ret = !ret;
1897 }
1898
1899 if (ret) {
1900 /* we have a matching rule. */
1901 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001902 buffer_abort(req);
1903 buffer_abort(rep);
1904 //FIXME: this delete this
1905 //fd_delete(t->cli_fd);
1906 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001907 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001908 t->fe->failed_req++;
1909 if (!(t->flags & SN_ERR_MASK))
1910 t->flags |= SN_ERR_PRXCOND;
1911 if (!(t->flags & SN_FINST_MASK))
1912 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001913 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001914 }
1915 /* otherwise accept */
1916 break;
1917 }
1918 }
1919
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001920 /* if we get there, it means we have no rule which matches, or
1921 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001922 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001923 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001924 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001925 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001926
Willy Tarreau2df28e82008-08-17 15:20:19 +02001927 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 /*
1929 * Now parse the partial (or complete) lines.
1930 * We will check the request syntax, and also join multi-line
1931 * headers. An index of all the lines will be elaborated while
1932 * parsing.
1933 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001934 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001937 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001938 * req->data + req->eoh = end of processed headers / start of current one
1939 * req->data + req->eol = end of current header or line (LF or CRLF)
1940 * req->lr = first non-visited byte
1941 * req->r = end of data
1942 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001945 struct http_txn *txn = &t->txn;
1946 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001948
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001950 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001952 /* 1: we might have to print this header in debug mode */
1953 if (unlikely((global.mode & MODE_DEBUG) &&
1954 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001955 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001957
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001958 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 eol = sol + msg->sl.rq.l;
1960 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001961
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001962 sol += hdr_idx_first_pos(&txn->hdr_idx);
1963 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001964
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001965 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001966 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001968 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1969 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 }
1971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001972
Willy Tarreau58f10d72006-12-04 02:26:12 +01001973
1974 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001976 * If not so, we check the FD and buffer states before leaving.
1977 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001978 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1979 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001980 *
1981 */
1982
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001983 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001984 /*
1985 * First, let's catch bad requests.
1986 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001987 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001989
1990 /* 1: Since we are in header mode, if there's no space
1991 * left for headers, we won't be able to free more
1992 * later, so the session will never terminate. We
1993 * must terminate it now.
1994 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001995 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001996 /* FIXME: check if URI is set and return Status
1997 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001998 */
Willy Tarreau06619262006-12-17 08:37:22 +01001999 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002000 }
2001
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002002 /* 2: have we encountered a read error ? */
2003 else if (req->flags & BF_READ_ERROR) {
2004 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002005 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002006 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002007 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002008 if (!(t->flags & SN_ERR_MASK))
2009 t->flags |= SN_ERR_CLICL;
2010 if (!(t->flags & SN_FINST_MASK))
2011 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002012 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002013 }
2014
2015 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002016 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002017 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002018 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01002019 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002020 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002021 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002022 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002023 if (!(t->flags & SN_ERR_MASK))
2024 t->flags |= SN_ERR_CLITO;
2025 if (!(t->flags & SN_FINST_MASK))
2026 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002027 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002028 }
2029
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002030 /* 4: have we encountered a close ? */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002031 else if (req->flags & BF_SHUTR) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002032 txn->status = 400;
2033 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002034 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002035 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002036 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002037
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002038 if (!(t->flags & SN_ERR_MASK))
2039 t->flags |= SN_ERR_CLICL;
2040 if (!(t->flags & SN_FINST_MASK))
2041 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002042 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002043 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002044
Willy Tarreau3da77c52008-08-29 09:58:42 +02002045 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002046 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002047 if (!tick_isset(req->analyse_exp))
2048 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002049
2050 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02002051 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002052 }
2053
2054
2055 /****************************************************************
2056 * More interesting part now : we know that we have a complete *
2057 * request which at least looks like HTTP. We have an indicator *
2058 * of each header's length, so we can parse them quickly. *
2059 ****************************************************************/
2060
Willy Tarreau2df28e82008-08-17 15:20:19 +02002061 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002062 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002063
Willy Tarreau9cdde232007-05-02 20:58:19 +02002064 /* ensure we keep this pointer to the beginning of the message */
2065 msg->sol = req->data + msg->som;
2066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002067 /*
2068 * 1: identify the method
2069 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002070 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002071
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002072 /* we can make use of server redirect on GET and HEAD */
2073 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2074 t->flags |= SN_REDIRECTABLE;
2075
Willy Tarreau58f10d72006-12-04 02:26:12 +01002076 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002077 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01002078 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002080 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 if (unlikely((t->fe->monitor_uri_len != 0) &&
2082 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2083 !memcmp(&req->data[msg->sl.rq.u],
2084 t->fe->monitor_uri,
2085 t->fe->monitor_uri_len))) {
2086 /*
2087 * We have found the monitor URI
2088 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01002089 struct acl_cond *cond;
2090 cur_proxy = t->fe;
2091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002092 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002093
2094 /* Check if we want to fail this monitor request or not */
2095 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
2096 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002097
2098 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01002099 if (cond->pol == ACL_COND_UNLESS)
2100 ret = !ret;
2101
2102 if (ret) {
2103 /* we fail this request, let's return 503 service unavail */
2104 txn->status = 503;
2105 client_retnclose(t, error_message(t, HTTP_ERR_503));
2106 goto return_prx_cond;
2107 }
2108 }
2109
2110 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002111 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002112 client_retnclose(t, &http_200_chunk);
2113 goto return_prx_cond;
2114 }
2115
2116 /*
2117 * 3: Maybe we have to copy the original REQURI for the logs ?
2118 * Note: we cannot log anymore if the request has been
2119 * classified as invalid.
2120 */
2121 if (unlikely(t->logs.logwait & LW_REQ)) {
2122 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002123 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002124 int urilen = msg->sl.rq.l;
2125
2126 if (urilen >= REQURI_LEN)
2127 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002128 memcpy(txn->uri, &req->data[msg->som], urilen);
2129 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130
2131 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002132 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002133 } else {
2134 Alert("HTTP logging : out of memory.\n");
2135 }
2136 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002137
Willy Tarreau06619262006-12-17 08:37:22 +01002138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002139 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2140 if (unlikely(msg->sl.rq.v_l == 0)) {
2141 int delta;
2142 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002143 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002144 cur_end = msg->sol + msg->sl.rq.l;
2145 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002147 if (msg->sl.rq.u_l == 0) {
2148 /* if no URI was set, add "/" */
2149 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2150 cur_end += delta;
2151 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002152 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002153 /* add HTTP version */
2154 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2155 msg->eoh += delta;
2156 cur_end += delta;
2157 cur_end = (char *)http_parse_reqline(msg, req->data,
2158 HTTP_MSG_RQMETH,
2159 msg->sol, cur_end + 1,
2160 NULL, NULL);
2161 if (unlikely(!cur_end))
2162 goto return_bad_req;
2163
2164 /* we have a full HTTP/1.0 request now and we know that
2165 * we have either a CR or an LF at <ptr>.
2166 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002167 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002168 }
2169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002170
2171 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002172 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002173 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002174 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002175
2176 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002177 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002178 * As opposed to version 1.2, now they will be evaluated in the
2179 * filters order and not in the header order. This means that
2180 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002181 *
2182 * We can now check whether we want to switch to another
2183 * backend, in which case we will re-check the backend's
2184 * filters and various options. In order to support 3-level
2185 * switching, here's how we should proceed :
2186 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002187 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002188 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002189 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002190 * There cannot be any switch from there, so ->be cannot be
2191 * changed anymore.
2192 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002193 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002194 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002195 * The response path will be able to apply either ->be, or
2196 * ->be then ->fe filters in order to match the reverse of
2197 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002198 */
2199
Willy Tarreau06619262006-12-17 08:37:22 +01002200 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002201 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002202 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002203 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002204 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002205
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002206 /* first check whether we have some ACLs set to redirect this request */
2207 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2208 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002209
2210 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002211 if (rule->cond->pol == ACL_COND_UNLESS)
2212 ret = !ret;
2213
2214 if (ret) {
2215 struct chunk rdr = { trash, 0 };
2216 const char *msg_fmt;
2217
2218 /* build redirect message */
2219 switch(rule->code) {
2220 case 303:
2221 rdr.len = strlen(HTTP_303);
2222 msg_fmt = HTTP_303;
2223 break;
2224 case 301:
2225 rdr.len = strlen(HTTP_301);
2226 msg_fmt = HTTP_301;
2227 break;
2228 case 302:
2229 default:
2230 rdr.len = strlen(HTTP_302);
2231 msg_fmt = HTTP_302;
2232 break;
2233 }
2234
2235 if (unlikely(rdr.len > sizeof(trash)))
2236 goto return_bad_req;
2237 memcpy(rdr.str, msg_fmt, rdr.len);
2238
2239 switch(rule->type) {
2240 case REDIRECT_TYPE_PREFIX: {
2241 const char *path;
2242 int pathlen;
2243
2244 path = http_get_path(txn);
2245 /* build message using path */
2246 if (path) {
2247 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2248 } else {
2249 path = "/";
2250 pathlen = 1;
2251 }
2252
2253 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2254 goto return_bad_req;
2255
2256 /* add prefix */
2257 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2258 rdr.len += rule->rdr_len;
2259
2260 /* add path */
2261 memcpy(rdr.str + rdr.len, path, pathlen);
2262 rdr.len += pathlen;
2263 break;
2264 }
2265 case REDIRECT_TYPE_LOCATION:
2266 default:
2267 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2268 goto return_bad_req;
2269
2270 /* add location */
2271 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2272 rdr.len += rule->rdr_len;
2273 break;
2274 }
2275
2276 /* add end of headers */
2277 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2278 rdr.len += 4;
2279
2280 txn->status = rule->code;
2281 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002282 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002283 client_retnclose(t, &rdr);
2284 goto return_prx_cond;
2285 }
2286 }
2287
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002288 /* first check whether we have some ACLs set to block this request */
2289 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002290 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002291
2292 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002293 if (cond->pol == ACL_COND_UNLESS)
2294 ret = !ret;
2295
2296 if (ret) {
2297 txn->status = 403;
2298 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002299 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002300 client_retnclose(t, error_message(t, HTTP_ERR_403));
2301 goto return_prx_cond;
2302 }
2303 }
2304
Willy Tarreau06619262006-12-17 08:37:22 +01002305 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002306 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002307 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2308 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002309 }
2310
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002311 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2312 /* to ensure correct connection accounting on
2313 * the backend, we count the connection for the
2314 * one managing the queue.
2315 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002316 t->be->beconn++;
2317 if (t->be->beconn > t->be->beconn_max)
2318 t->be->beconn_max = t->be->beconn;
2319 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002320 t->flags |= SN_BE_ASSIGNED;
2321 }
2322
Willy Tarreau06619262006-12-17 08:37:22 +01002323 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002324 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002325 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002326 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002327 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002328 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002329 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002330 goto return_prx_cond;
2331 }
2332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002333 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002334 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335 !(t->flags & SN_CONN_CLOSED)) {
2336 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002337 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002338 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002339
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002340 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002341 old_idx = 0;
2342
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002343 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2344 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002345 cur_ptr = cur_next;
2346 cur_end = cur_ptr + cur_hdr->len;
2347 cur_next = cur_end + cur_hdr->cr + 1;
2348
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002349 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2350 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002351 /* 3 possibilities :
2352 * - we have already set Connection: close,
2353 * so we remove this line.
2354 * - we have not yet set Connection: close,
2355 * but this line indicates close. We leave
2356 * it untouched and set the flag.
2357 * - we have not yet set Connection: close,
2358 * and this line indicates non-close. We
2359 * replace it.
2360 */
2361 if (t->flags & SN_CONN_CLOSED) {
2362 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002363 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002364 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002365 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2366 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002367 cur_hdr->len = 0;
2368 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002369 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2370 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2371 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002372 cur_next += delta;
2373 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002374 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002375 }
2376 t->flags |= SN_CONN_CLOSED;
2377 }
2378 }
2379 old_idx = cur_idx;
2380 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002381 }
2382 /* add request headers from the rule sets in the same order */
2383 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2384 if (unlikely(http_header_add_tail(req,
2385 &txn->req,
2386 &txn->hdr_idx,
2387 rule_set->req_add[cur_idx])) < 0)
2388 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002389 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002391 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002392 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002393 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002394 /* we have to check the URI and auth for this request.
2395 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002396 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002397 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002398 if (stats_check_uri_auth(t, rule_set))
2399 return 1;
2400 }
2401
Willy Tarreau55ea7572007-06-17 19:56:27 +02002402 /* now check whether we have some switching rules for this request */
2403 if (!(t->flags & SN_BE_ASSIGNED)) {
2404 struct switching_rule *rule;
2405
2406 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2407 int ret;
2408
2409 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002410
2411 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002412 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002413 ret = !ret;
2414
2415 if (ret) {
2416 t->be = rule->be.backend;
2417 t->be->beconn++;
2418 if (t->be->beconn > t->be->beconn_max)
2419 t->be->beconn_max = t->be->beconn;
2420 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002421
2422 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002423 t->rep->rto = t->req->wto = t->be->timeout.server;
2424 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002425 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002426 t->flags |= SN_BE_ASSIGNED;
2427 break;
2428 }
2429 }
2430 }
2431
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002432 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2433 /* No backend was set, but there was a default
2434 * backend set in the frontend, so we use it and
2435 * loop again.
2436 */
2437 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002438 t->be->beconn++;
2439 if (t->be->beconn > t->be->beconn_max)
2440 t->be->beconn_max = t->be->beconn;
2441 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002442
2443 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002444 t->rep->rto = t->req->wto = t->be->timeout.server;
2445 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002446 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002447 t->flags |= SN_BE_ASSIGNED;
2448 }
2449 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002450
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002452 if (!(t->flags & SN_BE_ASSIGNED)) {
2453 /* To ensure correct connection accounting on
2454 * the backend, we count the connection for the
2455 * one managing the queue.
2456 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002457 t->be->beconn++;
2458 if (t->be->beconn > t->be->beconn_max)
2459 t->be->beconn_max = t->be->beconn;
2460 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002461 t->flags |= SN_BE_ASSIGNED;
2462 }
2463
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002464 /*
2465 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002466 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002467 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002468 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002469 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002470
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002471 /*
2472 * If HTTP PROXY is set we simply get remote server address
2473 * parsing incoming request.
2474 */
2475 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2476 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2477 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002478
Willy Tarreau2a324282006-12-05 00:05:46 +01002479 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002480 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002481 * so let's do the same now.
2482 */
2483
2484 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002485 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002486 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002487 }
2488
2489
2490 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002491 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002492 * Note that doing so might move headers in the request, but
2493 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002494 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002495 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002496 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2497 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002498 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002499
Willy Tarreau58f10d72006-12-04 02:26:12 +01002500
Willy Tarreau2a324282006-12-05 00:05:46 +01002501 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002502 * 9: add X-Forwarded-For if either the frontend or the backend
2503 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002504 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002505 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002506 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002507 /* Add an X-Forwarded-For header unless the source IP is
2508 * in the 'except' network range.
2509 */
2510 if ((!t->fe->except_mask.s_addr ||
2511 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2512 != t->fe->except_net.s_addr) &&
2513 (!t->be->except_mask.s_addr ||
2514 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2515 != t->be->except_net.s_addr)) {
2516 int len;
2517 unsigned char *pn;
2518 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002519
Ross Westaf72a1d2008-08-03 10:51:45 +02002520 /* Note: we rely on the backend to get the header name to be used for
2521 * x-forwarded-for, because the header is really meant for the backends.
2522 * However, if the backend did not specify any option, we have to rely
2523 * on the frontend's header name.
2524 */
2525 if (t->be->fwdfor_hdr_len) {
2526 len = t->be->fwdfor_hdr_len;
2527 memcpy(trash, t->be->fwdfor_hdr_name, len);
2528 } else {
2529 len = t->fe->fwdfor_hdr_len;
2530 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2531 }
2532 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002533
Ross Westaf72a1d2008-08-03 10:51:45 +02002534 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002535 &txn->hdr_idx, trash, len)) < 0)
2536 goto return_bad_req;
2537 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002538 }
2539 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002540 /* FIXME: for the sake of completeness, we should also support
2541 * 'except' here, although it is mostly useless in this case.
2542 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002543 int len;
2544 char pn[INET6_ADDRSTRLEN];
2545 inet_ntop(AF_INET6,
2546 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2547 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002548
2549 /* Note: we rely on the backend to get the header name to be used for
2550 * x-forwarded-for, because the header is really meant for the backends.
2551 * However, if the backend did not specify any option, we have to rely
2552 * on the frontend's header name.
2553 */
2554 if (t->be->fwdfor_hdr_len) {
2555 len = t->be->fwdfor_hdr_len;
2556 memcpy(trash, t->be->fwdfor_hdr_name, len);
2557 } else {
2558 len = t->fe->fwdfor_hdr_len;
2559 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2560 }
2561 len += sprintf(trash + len, ": %s", pn);
2562
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002563 if (unlikely(http_header_add_tail2(req, &txn->req,
2564 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002565 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002566 }
2567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002568
Willy Tarreau2a324282006-12-05 00:05:46 +01002569 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002570 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002571 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002572 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002573 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002574 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002575 if ((unlikely(msg->sl.rq.v_l != 8) ||
2576 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2577 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002578 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002579 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002580 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002581 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002582 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2583 * If not assigned, perhaps we are balancing on url_param, but this is a
2584 * POST; and the parameters are in the body, maybe scan there to find our server.
2585 * (unless headers overflowed the buffer?)
2586 */
2587 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2588 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002589 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002590 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2591 /* are there enough bytes here? total == l || r || rlim ?
2592 * len is unsigned, but eoh is int,
2593 * how many bytes of body have we received?
2594 * eoh is the first empty line of the header
2595 */
2596 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002597 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 +02002598
2599 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2600 * We can't assume responsibility for the server's decision,
2601 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2602 * We also can't change our mind later, about which server to choose, so round robin.
2603 */
2604 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2605 struct hdr_ctx ctx;
2606 ctx.idx = 0;
2607 /* Expect is allowed in 1.1, look for it */
2608 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2609 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002610 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002611 /* We can't reliablly stall and wait for data, because of
2612 * .NET clients that don't conform to rfc2616; so, no need for
2613 * the next block to check length expectations.
2614 * We could send 100 status back to the client, but then we need to
2615 * re-write headers, and send the message. And this isn't the right
2616 * place for that action.
2617 * TODO: support Expect elsewhere and delete this block.
2618 */
2619 goto end_check_maybe_wait_for_body;
2620 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002621
2622 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002623 /* nothing to do, we got enough */
2624 } else {
2625 /* limit implies we are supposed to need this many bytes
2626 * to find the parameter. Let's see how many bytes we can wait for.
2627 */
2628 long long hint = len;
2629 struct hdr_ctx ctx;
2630 ctx.idx = 0;
2631 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002632 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002633 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002634 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002635 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002636 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002637 ctx.idx = 0;
2638 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2639 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002640 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002641 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002642 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002643 hint = 0; /* parse failure, untrusted client */
2644 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002645 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002646 msg->hdr_content_len = hint;
2647 else
2648 hint = 0; /* bad client, sent negative length */
2649 }
2650 }
2651 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002652 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002653 hint = t->be->url_param_post_limit;
2654 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002655 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002656 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002657 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002658 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002659 /* else... There are no body bytes to wait for */
2660 }
2661 }
2662 }
2663 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002664
Willy Tarreau2a324282006-12-05 00:05:46 +01002665 /*************************************************************
2666 * OK, that's finished for the headers. We have done what we *
2667 * could. Let's switch to the DATA state. *
2668 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002669
Willy Tarreaue393fe22008-08-16 22:18:07 +02002670 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002671 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672
Willy Tarreau1fa31262007-12-03 00:36:16 +01002673 /* When a connection is tarpitted, we use the tarpit timeout,
2674 * which may be the same as the connect timeout if unspecified.
2675 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002676 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002677 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002678 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002679 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002680 /* flush the request so that we can drop the connection early
2681 * if the client closes first.
2682 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002683 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002684 req->analysers |= AN_REQ_HTTP_TARPIT;
2685 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2686 if (!req->analyse_exp)
2687 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002688 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002690 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002691 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002692
2693 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002694 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002695 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002696 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002697 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002698 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002699 return_prx_cond:
2700 if (!(t->flags & SN_ERR_MASK))
2701 t->flags |= SN_ERR_PRXCOND;
2702 if (!(t->flags & SN_FINST_MASK))
2703 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002704 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002705 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002706 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002707 }
2708
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002709 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2710 struct http_txn *txn = &t->txn;
2711
2712 /* This connection is being tarpitted. The CLIENT side has
2713 * already set the connect expiration date to the right
2714 * timeout. We just have to check that the client is still
2715 * there and that the timeout has not expired.
2716 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002717 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002718 !tick_is_expired(req->analyse_exp, now_ms))
2719 return 0;
2720
2721 /* We will set the queue timer to the time spent, just for
2722 * logging purposes. We fake a 500 server error, so that the
2723 * attacker will not suspect his connection has been tarpitted.
2724 * It will not cause trouble to the logs because we can exclude
2725 * the tarpitted connections by filtering on the 'PT' status flags.
2726 */
2727 trace_term(t, TT_HTTP_SRV_2);
2728 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2729
2730 txn->status = 500;
2731 if (req->flags != BF_READ_ERROR)
2732 client_retnclose(t, error_message(t, HTTP_ERR_500));
2733
2734 req->analysers = 0;
2735 req->analyse_exp = TICK_ETERNITY;
2736
2737 t->fe->failed_req++;
2738 if (!(t->flags & SN_ERR_MASK))
2739 t->flags |= SN_ERR_PRXCOND;
2740 if (!(t->flags & SN_FINST_MASK))
2741 t->flags |= SN_FINST_T;
2742 return 0;
2743 }
2744
Willy Tarreau2df28e82008-08-17 15:20:19 +02002745 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002746 /* We have to parse the HTTP request body to find any required data.
2747 * "balance url_param check_post" should have been the only way to get
2748 * into this. We were brought here after HTTP header analysis, so all
2749 * related structures are ready.
2750 */
2751 struct http_msg *msg = &t->txn.req;
2752 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2753 long long limit = t->be->url_param_post_limit;
2754 struct hdr_ctx ctx;
2755
2756 ctx.idx = 0;
2757
2758 /* now if we have a length, we'll take the hint */
2759 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2760 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2761 unsigned int chunk = 0;
2762 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2763 char c = msg->sol[body];
2764 if (ishex(c)) {
2765 unsigned int hex = toupper(c) - '0';
2766 if (hex > 9)
2767 hex -= 'A' - '9' - 1;
2768 chunk = (chunk << 4) | hex;
2769 } else
2770 break;
2771 body++;
2772 }
2773 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002774 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002775
2776 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002777 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002778
2779 body += 2; // skip CRLF
2780
2781 /* if we support more then one chunk here, we have to do it again when assigning server
2782 * 1. how much entity data do we have? new var
2783 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2784 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2785 */
2786
2787 if (chunk < limit)
2788 limit = chunk; /* only reading one chunk */
2789 } else {
2790 if (msg->hdr_content_len < limit)
2791 limit = msg->hdr_content_len;
2792 }
2793
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002794 http_body_end:
2795 /* we leave once we know we have nothing left to do. This means that we have
2796 * enough bytes, or that we know we'll not get any more (buffer full, read
2797 * buffer closed).
2798 */
2799 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002800 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002801 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002802 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002803 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002804 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002805 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002806 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002807 else {
2808 /* Not enough data. We'll re-use the http-request
2809 * timeout here. Ideally, we should set the timeout
2810 * relative to the accept() date. We just set the
2811 * request timeout once at the beginning of the
2812 * request.
2813 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002814 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02002815 if (!tick_isset(req->analyse_exp))
2816 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2817 return 0;
2818 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002819 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002820
Willy Tarreau2df28e82008-08-17 15:20:19 +02002821 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2822 * probably reduce one day's debugging session.
2823 */
2824#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002825 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 +02002826 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2827 __FILE__, __LINE__, req->analysers);
2828 ABORT_NOW();
2829 }
2830#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002831 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002832 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002833}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002834
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002835/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002836 * It normally returns zero, but may return 1 if it absolutely needs to be
2837 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002838 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002839 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002840 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002841int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002842{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002843 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002844 struct buffer *req = t->req;
2845 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002846
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002847 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 +02002848 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002849 t,
2850 rep,
2851 rep->rex, rep->wex,
2852 rep->flags,
2853 rep->l,
2854 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002855
Willy Tarreau2df28e82008-08-17 15:20:19 +02002856 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002857 /*
2858 * Now parse the partial (or complete) lines.
2859 * We will check the response syntax, and also join multi-line
2860 * headers. An index of all the lines will be elaborated while
2861 * parsing.
2862 *
2863 * For the parsing, we use a 28 states FSM.
2864 *
2865 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002866 * rep->data + rep->som = beginning of response
2867 * rep->data + rep->eoh = end of processed headers / start of current one
2868 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002869 * rep->lr = first non-visited byte
2870 * rep->r = end of data
2871 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002872
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002873 int cur_idx;
2874 struct http_msg *msg = &txn->rsp;
2875 struct proxy *cur_proxy;
2876
2877 if (likely(rep->lr < rep->r))
2878 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2879
2880 /* 1: we might have to print this header in debug mode */
2881 if (unlikely((global.mode & MODE_DEBUG) &&
2882 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2883 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2884 char *eol, *sol;
2885
2886 sol = rep->data + msg->som;
2887 eol = sol + msg->sl.rq.l;
2888 debug_hdr("srvrep", t, sol, eol);
2889
2890 sol += hdr_idx_first_pos(&txn->hdr_idx);
2891 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2892
2893 while (cur_idx) {
2894 eol = sol + txn->hdr_idx.v[cur_idx].len;
2895 debug_hdr("srvhdr", t, sol, eol);
2896 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2897 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002898 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002899 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002900
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002901 /*
2902 * Now we quickly check if we have found a full valid response.
2903 * If not so, we check the FD and buffer states before leaving.
2904 * A full response is indicated by the fact that we have seen
2905 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2906 * responses are checked first.
2907 *
2908 * Depending on whether the client is still there or not, we
2909 * may send an error response back or not. Note that normally
2910 * we should only check for HTTP status there, and check I/O
2911 * errors somewhere else.
2912 */
2913
2914 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002915 /* Invalid response */
2916 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2917 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002918 //buffer_shutr(rep);
2919 //buffer_shutw(req);
2920 //fd_delete(req->cons->fd);
2921 //req->cons->state = SI_ST_CLO;
2922 buffer_shutr_now(rep);
2923 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002924 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002925 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002927 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002929 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002930 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 txn->status = 502;
2932 client_return(t, error_message(t, HTTP_ERR_502));
2933 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002934 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 if (!(t->flags & SN_FINST_MASK))
2936 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002937
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002938 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2939 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002940
Willy Tarreaudafde432008-08-17 01:00:46 +02002941 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002942 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002943 /* too large response does not fit in buffer. */
2944 else if (rep->flags & BF_FULL) {
2945 goto hdr_response_bad;
2946 }
2947 /* read error */
2948 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002949 buffer_shutr_now(rep);
2950 buffer_shutw_now(req);
2951 //fd_delete(req->cons->fd);
2952 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002953 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002954 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002955 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002956 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002957 //}
2958 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002959 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002960 txn->status = 502;
2961 client_return(t, error_message(t, HTTP_ERR_502));
2962 if (!(t->flags & SN_ERR_MASK))
2963 t->flags |= SN_ERR_SRVCL;
2964 if (!(t->flags & SN_FINST_MASK))
2965 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002966
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002967 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2968 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002969
Willy Tarreaudafde432008-08-17 01:00:46 +02002970 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002972 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002973 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002974 buffer_shutr_now(rep);
2975 buffer_shutw_now(req);
2976 //fd_delete(req->cons->fd);
2977 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002978 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002979 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002980 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002981 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002982 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002983 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002984 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002985 txn->status = 504;
2986 client_return(t, error_message(t, HTTP_ERR_504));
2987 if (!(t->flags & SN_ERR_MASK))
2988 t->flags |= SN_ERR_SRVTO;
2989 if (!(t->flags & SN_FINST_MASK))
2990 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002991
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002992 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2993 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002994 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002995 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002996 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002997 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002998 buffer_shutr_now(rep);
2999 buffer_shutw_now(req);
3000 //fd_delete(req->cons->fd);
3001 //req->cons->state = SI_ST_CLO;
3002 if (t->srv) {
3003 //t->srv->cur_sess--;
3004 t->srv->failed_resp++;
3005 //sess_change_server(t, NULL);
3006 }
3007 t->be->failed_resp++;
3008 rep->analysers = 0;
3009 txn->status = 502;
3010 client_return(t, error_message(t, HTTP_ERR_502));
3011 if (!(t->flags & SN_ERR_MASK))
3012 t->flags |= SN_ERR_SRVCL;
3013 if (!(t->flags & SN_FINST_MASK))
3014 t->flags |= SN_FINST_H;
3015
3016 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3017 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003018
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003019 return 0;
3020 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003021 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003022 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003023 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024
Willy Tarreau21d2af32008-02-14 20:25:24 +01003025
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003026 /*****************************************************************
3027 * More interesting part now : we know that we have a complete *
3028 * response which at least looks like HTTP. We have an indicator *
3029 * of each header's length, so we can parse them quickly. *
3030 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01003031
Willy Tarreau2df28e82008-08-17 15:20:19 +02003032 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /* ensure we keep this pointer to the beginning of the message */
3035 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003036
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003037 /*
3038 * 1: get the status code and check for cacheability.
3039 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01003040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003041 t->logs.logwait &= ~LW_RESP;
3042 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003043
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003044 switch (txn->status) {
3045 case 200:
3046 case 203:
3047 case 206:
3048 case 300:
3049 case 301:
3050 case 410:
3051 /* RFC2616 @13.4:
3052 * "A response received with a status code of
3053 * 200, 203, 206, 300, 301 or 410 MAY be stored
3054 * by a cache (...) unless a cache-control
3055 * directive prohibits caching."
3056 *
3057 * RFC2616 @9.5: POST method :
3058 * "Responses to this method are not cacheable,
3059 * unless the response includes appropriate
3060 * Cache-Control or Expires header fields."
3061 */
3062 if (likely(txn->meth != HTTP_METH_POST) &&
3063 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3064 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3065 break;
3066 default:
3067 break;
3068 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003069
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003070 /*
3071 * 2: we may need to capture headers
3072 */
3073 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3074 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3075 txn->rsp.cap, t->fe->rsp_cap);
3076
3077 /*
3078 * 3: we will have to evaluate the filters.
3079 * As opposed to version 1.2, now they will be evaluated in the
3080 * filters order and not in the header order. This means that
3081 * each filter has to be validated among all headers.
3082 *
3083 * Filters are tried with ->be first, then with ->fe if it is
3084 * different from ->be.
3085 */
3086
3087 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3088
3089 cur_proxy = t->be;
3090 while (1) {
3091 struct proxy *rule_set = cur_proxy;
3092
3093 /* try headers filters */
3094 if (rule_set->rsp_exp != NULL) {
3095 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3096 return_bad_resp:
3097 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003098 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003099 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003100 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003101 }
3102 cur_proxy->failed_resp++;
3103 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003104 buffer_shutr_now(rep);
3105 buffer_shutw_now(req);
3106 //fd_delete(req->cons->fd);
3107 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003108 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003109 txn->status = 502;
3110 client_return(t, error_message(t, HTTP_ERR_502));
3111 if (!(t->flags & SN_ERR_MASK))
3112 t->flags |= SN_ERR_PRXCOND;
3113 if (!(t->flags & SN_FINST_MASK))
3114 t->flags |= SN_FINST_H;
3115 /* We used to have a free connection slot. Since we'll never use it,
3116 * we have to inform the server that it may be used by another session.
3117 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003118 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3119 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003120 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003121 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003122 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003124 /* has the response been denied ? */
3125 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003126 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003127 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003129 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003130 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003131 cur_proxy->denied_resp++;
3132 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003134
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 /* We might have to check for "Connection:" */
3136 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3137 !(t->flags & SN_CONN_CLOSED)) {
3138 char *cur_ptr, *cur_end, *cur_next;
3139 int cur_idx, old_idx, delta, val;
3140 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003141
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003142 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3143 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003144
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003145 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3146 cur_hdr = &txn->hdr_idx.v[cur_idx];
3147 cur_ptr = cur_next;
3148 cur_end = cur_ptr + cur_hdr->len;
3149 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003150
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003151 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3152 if (val) {
3153 /* 3 possibilities :
3154 * - we have already set Connection: close,
3155 * so we remove this line.
3156 * - we have not yet set Connection: close,
3157 * but this line indicates close. We leave
3158 * it untouched and set the flag.
3159 * - we have not yet set Connection: close,
3160 * and this line indicates non-close. We
3161 * replace it.
3162 */
3163 if (t->flags & SN_CONN_CLOSED) {
3164 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3165 txn->rsp.eoh += delta;
3166 cur_next += delta;
3167 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3168 txn->hdr_idx.used--;
3169 cur_hdr->len = 0;
3170 } else {
3171 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3172 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3173 "close", 5);
3174 cur_next += delta;
3175 cur_hdr->len += delta;
3176 txn->rsp.eoh += delta;
3177 }
3178 t->flags |= SN_CONN_CLOSED;
3179 }
3180 }
3181 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003182 }
3183 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003185 /* add response headers from the rule sets in the same order */
3186 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3187 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3188 rule_set->rsp_add[cur_idx])) < 0)
3189 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003190 }
3191
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003192 /* check whether we're already working on the frontend */
3193 if (cur_proxy == t->fe)
3194 break;
3195 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003196 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003197
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003198 /*
3199 * 4: check for server cookie.
3200 */
3201 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3202 || (t->be->options & PR_O_CHK_CACHE))
3203 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003204
Willy Tarreaubaaee002006-06-26 02:48:02 +02003205
Willy Tarreaua15645d2007-03-18 16:22:39 +01003206 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003207 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003208 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003209 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3210 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003211
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003212 /*
3213 * 6: add server cookie in the response if needed
3214 */
3215 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3216 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3217 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003218
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 /* the server is known, it's not the one the client requested, we have to
3220 * insert a set-cookie here, except if we want to insert only on POST
3221 * requests and this one isn't. Note that servers which don't have cookies
3222 * (eg: some backup servers) will return a full cookie removal request.
3223 */
3224 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3225 t->be->cookie_name,
3226 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003227
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003228 if (t->be->cookie_domain)
3229 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003230
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003231 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3232 trash, len)) < 0)
3233 goto return_bad_resp;
3234 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003235
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003236 /* Here, we will tell an eventual cache on the client side that we don't
3237 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3238 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3239 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3240 */
3241 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003242
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003243 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3244
3245 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3246 "Cache-control: private", 22)) < 0)
3247 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003248 }
3249 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003250
Willy Tarreaubaaee002006-06-26 02:48:02 +02003251
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003252 /*
3253 * 7: check if result will be cacheable with a cookie.
3254 * We'll block the response if security checks have caught
3255 * nasty things such as a cacheable cookie.
3256 */
3257 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3258 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3259 (t->be->options & PR_O_CHK_CACHE)) {
3260
3261 /* we're in presence of a cacheable response containing
3262 * a set-cookie header. We'll block it as requested by
3263 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003264 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003265 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003266 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003267 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003268 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 }
3270 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003271
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003272 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3273 t->be->id, t->srv?t->srv->id:"<dispatch>");
3274 send_log(t->be, LOG_ALERT,
3275 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3276 t->be->id, t->srv?t->srv->id:"<dispatch>");
3277 goto return_srv_prx_502;
3278 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003279
3280 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003281 * 8: add "Connection: close" if needed and not yet set.
3282 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003283 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003284 if (!(t->flags & SN_CONN_CLOSED) &&
3285 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3286 if ((unlikely(msg->sl.st.v_l != 8) ||
3287 unlikely(req->data[msg->som + 7] != '0')) &&
3288 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3289 "Connection: close", 17)) < 0)
3290 goto return_bad_resp;
3291 t->flags |= SN_CONN_CLOSED;
3292 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003293
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003294 /*************************************************************
3295 * OK, that's finished for the headers. We have done what we *
3296 * could. Let's switch to the DATA state. *
3297 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298
Willy Tarreaue393fe22008-08-16 22:18:07 +02003299 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003300 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003301
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003302#ifdef CONFIG_HAP_TCPSPLICE
3303 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3304 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003305 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003306 }
3307#endif
3308 /* if the user wants to log as soon as possible, without counting
3309 * bytes from the server, then this is the right moment. We have
3310 * to temporarily assign bytes_out to log what we currently have.
3311 */
3312 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3313 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3314 t->logs.bytes_out = txn->rsp.eoh;
3315 if (t->fe->to_log & LW_REQ)
3316 http_sess_log(t);
3317 else
3318 tcp_sess_log(t);
3319 t->logs.bytes_out = 0;
3320 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003321
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003322 /* Note: we must not try to cheat by jumping directly to DATA,
3323 * otherwise we would not let the client side wake up.
3324 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003325
Willy Tarreaudafde432008-08-17 01:00:46 +02003326 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003327 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003328
Willy Tarreau2df28e82008-08-17 15:20:19 +02003329 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3330 * probably reduce one day's debugging session.
3331 */
3332#ifdef DEBUG_DEV
3333 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3334 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3335 __FILE__, __LINE__, rep->analysers);
3336 ABORT_NOW();
3337 }
3338#endif
3339 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003340 return 0;
3341}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003342
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003343///*
3344// * Manages the client FSM and its socket. It normally returns zero, but may
3345// * return 1 if it absolutely wants to be called again.
3346// *
3347// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3348// * but CL_STCLOSE.
3349// */
3350//int process_cli(struct session *t)
3351//{
3352// struct buffer *req = t->req;
3353// struct buffer *rep = t->rep;
3354//
3355// 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",
3356// now_ms, __FUNCTION__,
3357// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3358// cli_stnames[t->cli_state],
3359// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3360// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3361// req->rex, rep->wex,
3362// req->flags, rep->flags,
3363// req->l, rep->l);
3364//
3365// update_state:
3366// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3367// * still set this state (and will do until unix sockets are converted).
3368// */
3369// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3370// /* we can skip most of the tests at once if some conditions are not met */
3371// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3372// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3373// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3374// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3375// (!(rep->flags & BF_SHUTW) &&
3376// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3377// goto update_timeouts;
3378//
3379// /* read or write error */
3380// if (fdtab[t->cli_fd].state == FD_STERROR) {
3381// buffer_shutr(req);
3382// req->flags |= BF_READ_ERROR;
3383// buffer_shutw(rep);
3384// rep->flags |= BF_WRITE_ERROR;
3385// fd_delete(t->cli_fd);
3386// t->cli_state = CL_STCLOSE;
3387// trace_term(t, TT_HTTP_CLI_1);
3388// if (!req->analysers) {
3389// if (!(t->flags & SN_ERR_MASK))
3390// t->flags |= SN_ERR_CLICL;
3391// if (!(t->flags & SN_FINST_MASK)) {
3392// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3393// t->flags |= SN_FINST_Q;
3394// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3395// t->flags |= SN_FINST_C;
3396// else
3397// t->flags |= SN_FINST_D;
3398// }
3399// }
3400// goto update_state;
3401// }
3402// /* last read, or end of server write */
3403// else if (!(req->flags & BF_SHUTR) && /* not already done */
3404// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3405// buffer_shutr(req);
3406// if (!(rep->flags & BF_SHUTW)) {
3407// EV_FD_CLR(t->cli_fd, DIR_RD);
3408// trace_term(t, TT_HTTP_CLI_2);
3409// } else {
3410// /* output was already closed */
3411// fd_delete(t->cli_fd);
3412// t->cli_state = CL_STCLOSE;
3413// trace_term(t, TT_HTTP_CLI_3);
3414// }
3415// goto update_state;
3416// }
3417// /* last server read and buffer empty : we only check them when we're
3418// * allowed to forward the data.
3419// */
3420// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3421// ((rep->flags & BF_SHUTW_NOW) ||
3422// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3423// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3424// buffer_shutw(rep);
3425// if (!(req->flags & BF_SHUTR)) {
3426// EV_FD_CLR(t->cli_fd, DIR_WR);
3427// shutdown(t->cli_fd, SHUT_WR);
3428// trace_term(t, TT_HTTP_CLI_4);
3429// } else {
3430// fd_delete(t->cli_fd);
3431// t->cli_state = CL_STCLOSE;
3432// trace_term(t, TT_HTTP_CLI_5);
3433// }
3434// goto update_state;
3435// }
3436// /* read timeout */
3437// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3438// buffer_shutr(req);
3439// if (!(rep->flags & BF_SHUTW)) {
3440// EV_FD_CLR(t->cli_fd, DIR_RD);
3441// trace_term(t, TT_HTTP_CLI_6);
3442// } else {
3443// /* output was already closed */
3444// fd_delete(t->cli_fd);
3445// t->cli_state = CL_STCLOSE;
3446// trace_term(t, TT_HTTP_CLI_7);
3447// }
3448// if (!req->analysers) {
3449// if (!(t->flags & SN_ERR_MASK))
3450// t->flags |= SN_ERR_CLITO;
3451// if (!(t->flags & SN_FINST_MASK)) {
3452// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3453// t->flags |= SN_FINST_Q;
3454// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3455// t->flags |= SN_FINST_C;
3456// else
3457// t->flags |= SN_FINST_D;
3458// }
3459// }
3460// goto update_state;
3461// }
3462// /* write timeout */
3463// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3464// buffer_shutw(rep);
3465// if (!(req->flags & BF_SHUTR)) {
3466// EV_FD_CLR(t->cli_fd, DIR_WR);
3467// shutdown(t->cli_fd, SHUT_WR);
3468// trace_term(t, TT_HTTP_CLI_8);
3469// } else {
3470// fd_delete(t->cli_fd);
3471// t->cli_state = CL_STCLOSE;
3472// trace_term(t, TT_HTTP_CLI_9);
3473// }
3474// if (!req->analysers) {
3475// if (!(t->flags & SN_ERR_MASK))
3476// t->flags |= SN_ERR_CLITO;
3477// if (!(t->flags & SN_FINST_MASK)) {
3478// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3479// t->flags |= SN_FINST_Q;
3480// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3481// t->flags |= SN_FINST_C;
3482// else
3483// t->flags |= SN_FINST_D;
3484// }
3485// }
3486// goto update_state;
3487// }
3488//
3489// update_timeouts:
3490// /* manage read timeout */
3491// if (!(req->flags & BF_SHUTR)) {
3492// if (req->flags & BF_FULL) {
3493// /* no room to read more data */
3494// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3495// /* stop reading until we get some space */
3496// req->rex = TICK_ETERNITY;
3497// }
3498// } else {
3499// EV_FD_COND_S(t->cli_fd, DIR_RD);
3500// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3501// }
3502// }
3503//
3504// /* manage write timeout */
3505// if (!(rep->flags & BF_SHUTW)) {
3506// /* first, we may have to produce data (eg: stats).
3507// * right now, this is limited to the SHUTR state.
3508// */
3509// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3510// produce_content(t);
3511// if (rep->flags & BF_EMPTY) {
3512// buffer_shutw(rep);
3513// fd_delete(t->cli_fd);
3514// t->cli_state = CL_STCLOSE;
3515// trace_term(t, TT_HTTP_CLI_10);
3516// goto update_state;
3517// }
3518// }
3519//
3520// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3521// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3522// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3523// /* stop writing */
3524// rep->wex = TICK_ETERNITY;
3525// }
3526// } else {
3527// /* buffer not empty */
3528// EV_FD_COND_S(t->cli_fd, DIR_WR);
3529// if (!tick_isset(rep->wex)) {
3530// /* restart writing */
3531// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3532// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3533// /* FIXME: to prevent the client from expiring read timeouts during writes,
3534// * we refresh it, except if it was already infinite. */
3535// req->rex = rep->wex;
3536// }
3537// }
3538// }
3539// }
3540// return 0; /* other cases change nothing */
3541// }
3542// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3543// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3544// int len;
3545// 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);
3546// write(1, trash, len);
3547// }
3548// return 0;
3549// }
3550//#ifdef DEBUG_DEV
3551// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3552// ABORT_NOW();
3553//#endif
3554// return 0;
3555//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003556
Willy Tarreaubaaee002006-06-26 02:48:02 +02003557
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003558/* Return 1 if the pending connection has failed and should be retried,
3559 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3560 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003561 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003562int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003563{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003564 struct buffer *req = t->req;
3565 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003566 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003567
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003568 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3569 now_ms, __FUNCTION__,
3570 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003571 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003572 req->flags, rep->flags,
3573 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003574
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003575 if ((req->flags & BF_SHUTW_NOW) ||
3576 (rep->flags & BF_SHUTW) ||
3577 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003578 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003579 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3580
3581 trace_term(t, TT_HTTP_SRV_5);
3582 req->wex = TICK_ETERNITY;
3583 fd_delete(req->cons->fd);
3584 if (t->srv) {
3585 t->srv->cur_sess--;
3586 sess_change_server(t, NULL);
3587 }
3588 /* note that this must not return any error because it would be able to
3589 * overwrite the client_retnclose() output.
3590 */
3591 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3592
3593 // FIXME: should we set rep->MAY_FORWARD ?
3594 buffer_shutw(req);
3595 buffer_shutr(rep);
3596 req->cons->state = SI_ST_CLO;
3597 if (!req->cons->err_type)
3598 req->cons->err_type = SI_ET_CONN_ABRT;
3599 req->cons->err_loc = t->srv;
3600 return 0;
3601 }
3602
3603 /* check for timeouts and asynchronous connect errors */
3604 if (fdtab[req->cons->fd].state == FD_STERROR) {
3605 conn_err = SI_ET_CONN_ERR;
3606 if (!req->cons->err_type)
3607 req->cons->err_type = SI_ET_CONN_ERR;
3608 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003609 else if (!(req->flags & BF_WRITE_ACTIVITY)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003610 /* nothing happened, maybe we timed out */
3611 if (tick_is_expired(req->wex, now_ms)) {
3612 conn_err = SI_ET_CONN_TO;
3613 if (!req->cons->err_type)
3614 req->cons->err_type = SI_ET_CONN_TO;
3615 }
3616 else
3617 return 0; /* let's wait a bit more */
3618 }
3619
3620 if (conn_err) {
3621 fd_delete(req->cons->fd);
3622 req->cons->state = SI_ST_CLO;
3623
3624 if (t->srv) {
3625 t->srv->cur_sess--;
3626 sess_change_server(t, NULL);
3627 req->cons->err_loc = t->srv;
3628 }
3629
3630 /* ensure that we have enough retries left */
3631 if (srv_count_retry_down(t, conn_err))
3632 return 0;
3633
3634 if (conn_err == SI_ET_CONN_ERR) {
3635 /* we encountered an immediate connection error, and we
3636 * will have to retry connecting to the same server, most
3637 * likely leading to the same result. To avoid this, we
3638 * fake a connection timeout to retry after a turn-around
3639 * time of 1 second. We will wait in the previous if block.
3640 */
3641 req->cons->state = SI_ST_TAR;
Willy Tarreau35374672008-09-03 18:11:02 +02003642 req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003643 return 0;
3644 }
3645
3646 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3647 /* We're on our last chance, and the REDISP option was specified.
3648 * We will ignore cookie and force to balance or use the dispatcher.
3649 */
3650 /* let's try to offer this slot to anybody */
3651 if (may_dequeue_tasks(t->srv, t->be))
3652 process_srv_queue(t->srv);
3653
3654 /* it's left to the dispatcher to choose a server */
3655 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3656 t->prev_srv = t->srv;
3657 } else {
3658 /* we just want to retry */
3659 if (t->srv)
3660 t->srv->retries++;
3661 t->be->retries++;
3662
3663 /* Now we will try to either reconnect to the same server or
3664 * connect to another server. If the connection gets queued
3665 * because all servers are saturated, then we will go back to
3666 * the idle state where the buffer's consumer is marked as
3667 * unknown.
3668 */
3669 if (srv_retryable_connect(t)) {
3670 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003671 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003672 return 0;
3673 }
3674 }
3675
3676 /* We'll rely on the caller to try to get a connection again */
3677 return 1;
3678 }
3679 else {
3680 /* no error and write OK : connection succeeded */
3681 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3682 req->cons->state = SI_ST_EST;
3683 req->cons->err_type = SI_ET_NONE;
3684 req->cons->err_loc = NULL;
3685
3686 if (req->flags & BF_EMPTY) {
3687 EV_FD_CLR(req->cons->fd, DIR_WR);
3688 req->wex = TICK_ETERNITY;
3689 } else {
3690 EV_FD_SET(req->cons->fd, DIR_WR);
3691 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3692 if (tick_isset(req->wex)) {
3693 /* FIXME: to prevent the server from expiring read timeouts during writes,
3694 * we refresh it. */
3695 rep->rex = req->wex;
3696 }
3697 }
3698
3699 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3700 if (!(rep->flags & BF_HIJACK)) {
3701 EV_FD_SET(req->cons->fd, DIR_RD);
3702 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3703 }
3704 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3705
3706 /* if the user wants to log as soon as possible, without counting
3707 bytes from the server, then this is the right moment. */
3708 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3709 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3710 tcp_sess_log(t);
3711 }
3712#ifdef CONFIG_HAP_TCPSPLICE
3713 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3714 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003715 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003716 }
3717#endif
3718 }
3719 else {
3720 rep->analysers |= AN_RTR_HTTP_HDR;
3721 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3722 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3723 /* reset hdr_idx which was already initialized by the request.
3724 * right now, the http parser does it.
3725 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003726 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003727 }
3728
Willy Tarreau9a2d1542008-08-30 12:31:07 +02003729 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003730 req->wex = TICK_ETERNITY;
3731 return 0;
3732 }
3733}
3734
3735
3736/*
3737 * This function tries to assign a server to a stream_sock interface.
3738 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3739 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3740 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3741 * it returns SI_ST_ASS, otherwise zero.
3742 */
3743int stream_sock_assign_server(struct session *t)
3744{
3745 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3746 now_ms, __FUNCTION__,
3747 cli_stnames[t->cli_state],
3748 t->rep->rex, t->req->wex,
3749 t->req->flags, t->rep->flags,
3750 t->req->l, t->rep->l);
3751
3752 if (t->req->cons->state == SI_ST_TAR) {
3753 /* connection might be aborted */
3754 if ((t->req->flags & BF_SHUTW_NOW) ||
3755 (t->rep->flags & BF_SHUTW) ||
3756 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3757 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003758
Willy Tarreauf8533202008-08-16 14:55:08 +02003759 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003760 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003761
3762 // FIXME: should we set rep->MAY_FORWARD ?
3763 buffer_shutr(t->rep);
3764 buffer_shutw(t->req);
3765 if (!t->req->cons->err_type)
3766 t->req->cons->err_type = SI_ET_CONN_ABRT;
3767 t->req->cons->state = SI_ST_CLO;
3768 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003769 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003770
Willy Tarreau35374672008-09-03 18:11:02 +02003771 if (!tick_is_expired(t->req->cons->exp, now_ms))
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003772 return 0; /* still in turn-around */
3773
3774 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003775 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003776 }
3777 else if (t->req->cons->state == SI_ST_QUE) {
3778 if (t->pend_pos) {
3779 /* request still in queue... */
Willy Tarreau35374672008-09-03 18:11:02 +02003780 if (tick_is_expired(t->req->cons->exp, now_ms)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003781 /* ... and timeout expired */
3782 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreau35374672008-09-03 18:11:02 +02003783 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003784 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003785 if (t->srv)
3786 t->srv->failed_conns++;
3787 t->be->failed_conns++;
3788
3789 // FIXME: should we set rep->MAY_FORWARD ?
3790 buffer_shutr(t->rep);
3791 buffer_shutw(t->req);
3792 t->req->flags |= BF_WRITE_TIMEOUT;
3793 if (!t->req->cons->err_type)
3794 t->req->cons->err_type = SI_ET_QUEUE_TO;
3795 t->req->cons->state = SI_ST_CLO;
3796 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003797 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003798 /* connection remains in queue, check if we have to abort it */
3799 if ((t->req->flags & BF_SHUTW_NOW) ||
3800 (t->rep->flags & BF_SHUTW) ||
3801 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3802 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3803 /* give up */
3804 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreau35374672008-09-03 18:11:02 +02003805 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003806 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003807
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003808 // FIXME: should we set rep->MAY_FORWARD ?
3809 buffer_shutr(t->rep);
3810 buffer_shutw(t->req);
3811 if (!t->req->cons->err_type)
3812 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3813 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003814 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003815 return 0;
3816 }
3817 /* The connection is not in the queue anymore */
3818 t->req->cons->state = SI_ST_INI;
Willy Tarreau35374672008-09-03 18:11:02 +02003819 t->req->cons->exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003821
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003822 /* we may get here from above */
3823 if (t->req->cons->state == SI_ST_INI) {
3824 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003825
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003826 /* first, check if the connection has been aborted */
3827 if ((t->req->flags & BF_SHUTW_NOW) ||
3828 (t->rep->flags & BF_SHUTW) ||
3829 ((t->req->flags & BF_SHUTR) &&
3830 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003831
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003832 trace_term(t, TT_HTTP_SRV_1);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003833
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003834 // FIXME: should we set rep->MAY_FORWARD ?
3835 buffer_shutr(t->rep);
3836 buffer_shutw(t->req);
3837 if (!t->req->cons->err_type)
3838 t->req->cons->err_type = SI_ET_CONN_ABRT;
3839 t->req->cons->state = SI_ST_CLO;
3840 return 0;
3841 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003842
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003843 /* try to get a server assigned */
3844 if (srv_redispatch_connect(t) != 0) {
3845 /* we did not get any server, let's check the cause */
3846 if (t->req->cons->state == SI_ST_QUE) {
3847 /* the connection was queued, that's OK */
3848 return 0;
3849 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003850
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003851 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003852
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003853 // FIXME: should we set rep->MAY_FORWARD ?
3854 buffer_shutr(t->rep);
3855 buffer_shutw(t->req);
3856 t->req->flags |= BF_WRITE_ERROR;
3857 if (!t->req->cons->err_type)
3858 t->req->cons->err_type = SI_ET_CONN_OTHER;
3859 t->req->cons->state = SI_ST_CLO;
3860 return 0;
3861 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003862
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003863 t->req->cons->state = SI_ST_ASS;
3864 /* Once the server is assigned, we have to return because
3865 * the caller might be interested in checking several
3866 * things before connecting.
3867 */
3868 return 1;
3869 }
3870 return 0;
3871}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003872
Willy Tarreauf8533202008-08-16 14:55:08 +02003873
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003874/*
3875 * This function tries to establish a connection to an assigned server. It also
3876 * performs connection retries. It may only be called with t->req->cons->state
3877 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3878 * SI_ST_EST, or SI_ST_CLO.
3879 */
3880int stream_sock_connect_server(struct session *t)
3881{
3882 if (t->req->cons->state == SI_ST_ASS) {
3883 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003884
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003885 if (!srv_retryable_connect(t)) {
3886 /* we need to redispatch */
3887 t->req->cons->state = SI_ST_INI;
3888 return 0;
3889 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003890
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003891 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3892 if (t->req->cons->state != SI_ST_CON) {
3893 /* it was an error */
3894 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003895
3896 // FIXME: should we set rep->MAY_FORWARD ?
3897 buffer_shutr(t->rep);
3898 buffer_shutw(t->req);
3899 t->req->flags |= BF_WRITE_ERROR;
3900 if (!t->req->cons->err_type)
3901 t->req->cons->err_type = SI_ET_CONN_OTHER;
3902 t->req->cons->state = SI_ST_CLO;
3903 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003904 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003905 /* We have a socket and switched to SI_ST_CON */
3906 }
3907
3908 /* we may also get here from above */
3909 if (t->req->cons->state == SI_ST_CON) {
3910 /* connection in progress or just completed */
3911 if (!tcp_connection_status(t))
3912 return 0;
3913 }
3914 return 0;
3915}
3916
3917
3918/*
3919 * Tries to establish a connection to the server and associate it to the
3920 * request buffer's consumer side. It is assumed that this function will not be
Willy Tarreau3da77c52008-08-29 09:58:42 +02003921 * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003922 * returns zero, but may return 1 if it absolutely wants to be called again.
3923 */
3924int process_srv_conn(struct session *t)
3925{
3926 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3927 now_ms, __FUNCTION__,
3928 cli_stnames[t->cli_state],
3929 t->rep->rex, t->req->wex,
3930 t->req->flags, t->rep->flags,
3931 t->req->l, t->rep->l);
3932
3933 do {
3934 if (t->req->cons->state == SI_ST_INI ||
3935 t->req->cons->state == SI_ST_TAR ||
3936 t->req->cons->state == SI_ST_QUE) {
3937 /* try to assign a server */
3938 if (!stream_sock_assign_server(t))
3939 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003940 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003941
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003942 if (t->req->cons->state == SI_ST_ASS &&
3943 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3944 /* Server supporting redirection and it is possible.
3945 * Invalid requests are reported as such. It concerns all
3946 * the largest ones.
3947 */
3948 struct http_txn *txn = &t->txn;
3949 struct chunk rdr;
3950 char *path;
3951 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003952
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003953 /* 1: create the response header */
3954 rdr.len = strlen(HTTP_302);
3955 rdr.str = trash;
3956 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003957
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003958 /* 2: add the server's prefix */
3959 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3960 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003961
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003962 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3963 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003964
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003965 /* 3: add the request URI */
3966 path = http_get_path(txn);
3967 if (!path)
3968 goto cancel_redir;
3969 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3970 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3971 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003972
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003973 memcpy(rdr.str + rdr.len, path, len);
3974 rdr.len += len;
3975 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3976 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003977
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003978 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3979 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003980
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003981 /* FIXME: we should increase a counter of redirects per server and per backend. */
3982 if (t->srv)
3983 t->srv->cum_sess++;
3984
3985 t->req->cons->state = SI_ST_CLO;
3986 return 0;
3987 cancel_redir:
3988 //txn->status = 400;
3989 //t->fe->failed_req++;
3990 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3991 // 400, error_message(t, HTTP_ERR_400));
3992 trace_term(t, TT_HTTP_SRV_4);
3993
3994 // FIXME: should we set rep->MAY_FORWARD ?
3995 buffer_shutw(t->req);
3996 buffer_shutr(t->rep);
3997 if (!t->req->cons->err_type)
3998 t->req->cons->err_type = SI_ET_CONN_OTHER;
3999 t->req->cons->state = SI_ST_CLO;
4000 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004001 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004002
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004003 if (t->req->cons->state == SI_ST_CON ||
4004 t->req->cons->state == SI_ST_ASS) {
4005 stream_sock_connect_server(t);
4006 }
4007 } while (t->req->cons->state != SI_ST_CLO &&
4008 t->req->cons->state != SI_ST_CON &&
4009 t->req->cons->state != SI_ST_EST);
4010 return 0;
4011}
Willy Tarreaubaaee002006-06-26 02:48:02 +02004012
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004013
Willy Tarreaubaaee002006-06-26 02:48:02 +02004014/*
4015 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004016 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02004017 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
4018 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004019 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02004020 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004021 */
4022int produce_content(struct session *s)
4023{
Willy Tarreaubaaee002006-06-26 02:48:02 +02004024 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02004025 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004026 return 1;
4027 }
4028 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004029 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004030 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02004031 if (ret >= 0)
4032 return ret;
4033 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01004034 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004035
Willy Tarreau91861262007-10-17 17:06:05 +02004036 /* unknown data source or internal error */
4037 s->txn.status = 500;
4038 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02004039 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02004040 if (!(s->flags & SN_ERR_MASK))
4041 s->flags |= SN_ERR_PRXCOND;
4042 if (!(s->flags & SN_FINST_MASK))
4043 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02004044 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02004045 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004046}
4047
4048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004049/* Iterate the same filter through all request headers.
4050 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004051 * Since it can manage the switch to another backend, it updates the per-proxy
4052 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004053 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004054int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004055{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004056 char term;
4057 char *cur_ptr, *cur_end, *cur_next;
4058 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004059 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004060 struct hdr_idx_elem *cur_hdr;
4061 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004062
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 last_hdr = 0;
4064
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004065 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004066 old_idx = 0;
4067
4068 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004069 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004070 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004071 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004072 (exp->action == ACT_ALLOW ||
4073 exp->action == ACT_DENY ||
4074 exp->action == ACT_TARPIT))
4075 return 0;
4076
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004077 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004078 if (!cur_idx)
4079 break;
4080
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004081 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004082 cur_ptr = cur_next;
4083 cur_end = cur_ptr + cur_hdr->len;
4084 cur_next = cur_end + cur_hdr->cr + 1;
4085
4086 /* Now we have one header between cur_ptr and cur_end,
4087 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004088 */
4089
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004090 /* The annoying part is that pattern matching needs
4091 * that we modify the contents to null-terminate all
4092 * strings before testing them.
4093 */
4094
4095 term = *cur_end;
4096 *cur_end = '\0';
4097
4098 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4099 switch (exp->action) {
4100 case ACT_SETBE:
4101 /* It is not possible to jump a second time.
4102 * FIXME: should we return an HTTP/500 here so that
4103 * the admin knows there's a problem ?
4104 */
4105 if (t->be != t->fe)
4106 break;
4107
4108 /* Swithing Proxy */
4109 t->be = (struct proxy *) exp->replace;
4110
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004111 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004112 * because we have associated req_cap and rsp_cap to the
4113 * frontend, and the beconn will be updated later.
4114 */
4115
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004116 t->rep->rto = t->req->wto = t->be->timeout.server;
4117 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004118 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004119 last_hdr = 1;
4120 break;
4121
4122 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004123 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004124 last_hdr = 1;
4125 break;
4126
4127 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004128 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004130 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004131 break;
4132
4133 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004134 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004135 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004136 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004137 break;
4138
4139 case ACT_REPLACE:
4140 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4141 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4142 /* FIXME: if the user adds a newline in the replacement, the
4143 * index will not be recalculated for now, and the new line
4144 * will not be counted as a new header.
4145 */
4146
4147 cur_end += delta;
4148 cur_next += delta;
4149 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004150 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004151 break;
4152
4153 case ACT_REMOVE:
4154 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4155 cur_next += delta;
4156
4157 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004158 txn->req.eoh += delta;
4159 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4160 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004161 cur_hdr->len = 0;
4162 cur_end = NULL; /* null-term has been rewritten */
4163 break;
4164
4165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004166 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167 if (cur_end)
4168 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004170 /* keep the link from this header to next one in case of later
4171 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004172 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004173 old_idx = cur_idx;
4174 }
4175 return 0;
4176}
4177
4178
4179/* Apply the filter to the request line.
4180 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4181 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004182 * Since it can manage the switch to another backend, it updates the per-proxy
4183 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004184 */
4185int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4186{
4187 char term;
4188 char *cur_ptr, *cur_end;
4189 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004190 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004191 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004192
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193
Willy Tarreau3d300592007-03-18 18:34:41 +01004194 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004195 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004196 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004197 (exp->action == ACT_ALLOW ||
4198 exp->action == ACT_DENY ||
4199 exp->action == ACT_TARPIT))
4200 return 0;
4201 else if (exp->action == ACT_REMOVE)
4202 return 0;
4203
4204 done = 0;
4205
Willy Tarreau9cdde232007-05-02 20:58:19 +02004206 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004207 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004208
4209 /* Now we have the request line between cur_ptr and cur_end */
4210
4211 /* The annoying part is that pattern matching needs
4212 * that we modify the contents to null-terminate all
4213 * strings before testing them.
4214 */
4215
4216 term = *cur_end;
4217 *cur_end = '\0';
4218
4219 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4220 switch (exp->action) {
4221 case ACT_SETBE:
4222 /* It is not possible to jump a second time.
4223 * FIXME: should we return an HTTP/500 here so that
4224 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004226 if (t->be != t->fe)
4227 break;
4228
4229 /* Swithing Proxy */
4230 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004232 /* right now, the backend switch is not too much complicated
4233 * because we have associated req_cap and rsp_cap to the
4234 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004235 */
4236
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004237 t->rep->rto = t->req->wto = t->be->timeout.server;
4238 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004239 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004240 done = 1;
4241 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004243 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004244 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004245 done = 1;
4246 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004248 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004249 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004250 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004251 done = 1;
4252 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004253
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004254 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004255 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004256 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004257 done = 1;
4258 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004260 case ACT_REPLACE:
4261 *cur_end = term; /* restore the string terminator */
4262 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4263 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4264 /* FIXME: if the user adds a newline in the replacement, the
4265 * index will not be recalculated for now, and the new line
4266 * will not be counted as a new header.
4267 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004268
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004269 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004270 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004271
Willy Tarreau9cdde232007-05-02 20:58:19 +02004272 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004273 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004274 HTTP_MSG_RQMETH,
4275 cur_ptr, cur_end + 1,
4276 NULL, NULL);
4277 if (unlikely(!cur_end))
4278 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004280 /* we have a full request and we know that we have either a CR
4281 * or an LF at <ptr>.
4282 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004283 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4284 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004285 /* there is no point trying this regex on headers */
4286 return 1;
4287 }
4288 }
4289 *cur_end = term; /* restore the string terminator */
4290 return done;
4291}
Willy Tarreau97de6242006-12-27 17:18:38 +01004292
Willy Tarreau58f10d72006-12-04 02:26:12 +01004293
Willy Tarreau58f10d72006-12-04 02:26:12 +01004294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004295/*
4296 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4297 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004298 * unparsable request. Since it can manage the switch to another backend, it
4299 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004300 */
4301int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4302{
Willy Tarreau3d300592007-03-18 18:34:41 +01004303 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004304 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004305 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004306 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004308 /*
4309 * The interleaving of transformations and verdicts
4310 * makes it difficult to decide to continue or stop
4311 * the evaluation.
4312 */
4313
Willy Tarreau3d300592007-03-18 18:34:41 +01004314 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004315 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4316 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4317 exp = exp->next;
4318 continue;
4319 }
4320
4321 /* Apply the filter to the request line. */
4322 ret = apply_filter_to_req_line(t, req, exp);
4323 if (unlikely(ret < 0))
4324 return -1;
4325
4326 if (likely(ret == 0)) {
4327 /* The filter did not match the request, it can be
4328 * iterated through all headers.
4329 */
4330 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004331 }
4332 exp = exp->next;
4333 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004334 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004335}
4336
4337
Willy Tarreaua15645d2007-03-18 16:22:39 +01004338
Willy Tarreau58f10d72006-12-04 02:26:12 +01004339/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004340 * Manage client-side cookie. It can impact performance by about 2% so it is
4341 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004342 */
4343void manage_client_side_cookies(struct session *t, struct buffer *req)
4344{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004345 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 char *p1, *p2, *p3, *p4;
4347 char *del_colon, *del_cookie, *colon;
4348 int app_cookies;
4349
4350 appsess *asession_temp = NULL;
4351 appsess local_asession;
4352
4353 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004354 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004355
Willy Tarreau2a324282006-12-05 00:05:46 +01004356 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004357 * we start with the start line.
4358 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004359 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004360 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004361
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004362 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004363 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004364 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004365
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004366 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004367 cur_ptr = cur_next;
4368 cur_end = cur_ptr + cur_hdr->len;
4369 cur_next = cur_end + cur_hdr->cr + 1;
4370
4371 /* We have one full header between cur_ptr and cur_end, and the
4372 * next header starts at cur_next. We're only interested in
4373 * "Cookie:" headers.
4374 */
4375
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004376 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4377 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004378 old_idx = cur_idx;
4379 continue;
4380 }
4381
4382 /* Now look for cookies. Conforming to RFC2109, we have to support
4383 * attributes whose name begin with a '$', and associate them with
4384 * the right cookie, if we want to delete this cookie.
4385 * So there are 3 cases for each cookie read :
4386 * 1) it's a special attribute, beginning with a '$' : ignore it.
4387 * 2) it's a server id cookie that we *MAY* want to delete : save
4388 * some pointers on it (last semi-colon, beginning of cookie...)
4389 * 3) it's an application cookie : we *MAY* have to delete a previous
4390 * "special" cookie.
4391 * At the end of loop, if a "special" cookie remains, we may have to
4392 * remove it. If no application cookie persists in the header, we
4393 * *MUST* delete it
4394 */
4395
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004396 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004397
Willy Tarreau58f10d72006-12-04 02:26:12 +01004398 /* del_cookie == NULL => nothing to be deleted */
4399 del_colon = del_cookie = NULL;
4400 app_cookies = 0;
4401
4402 while (p1 < cur_end) {
4403 /* skip spaces and colons, but keep an eye on these ones */
4404 while (p1 < cur_end) {
4405 if (*p1 == ';' || *p1 == ',')
4406 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004407 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408 break;
4409 p1++;
4410 }
4411
4412 if (p1 == cur_end)
4413 break;
4414
4415 /* p1 is at the beginning of the cookie name */
4416 p2 = p1;
4417 while (p2 < cur_end && *p2 != '=')
4418 p2++;
4419
4420 if (p2 == cur_end)
4421 break;
4422
4423 p3 = p2 + 1; /* skips the '=' sign */
4424 if (p3 == cur_end)
4425 break;
4426
4427 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004428 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 p4++;
4430
4431 /* here, we have the cookie name between p1 and p2,
4432 * and its value between p3 and p4.
4433 * we can process it :
4434 *
4435 * Cookie: NAME=VALUE;
4436 * | || || |
4437 * | || || +--> p4
4438 * | || |+-------> p3
4439 * | || +--------> p2
4440 * | |+------------> p1
4441 * | +-------------> colon
4442 * +--------------------> cur_ptr
4443 */
4444
4445 if (*p1 == '$') {
4446 /* skip this one */
4447 }
4448 else {
4449 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004450 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004451 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004452 (p4 - p1 >= t->fe->capture_namelen) &&
4453 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004454 int log_len = p4 - p1;
4455
Willy Tarreau086b3b42007-05-13 21:45:51 +02004456 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004457 Alert("HTTP logging : out of memory.\n");
4458 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004459 if (log_len > t->fe->capture_len)
4460 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004461 memcpy(txn->cli_cookie, p1, log_len);
4462 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004463 }
4464 }
4465
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004466 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4467 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004468 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004469 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004470 char *delim;
4471
4472 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4473 * have the server ID betweek p3 and delim, and the original cookie between
4474 * delim+1 and p4. Otherwise, delim==p4 :
4475 *
4476 * Cookie: NAME=SRV~VALUE;
4477 * | || || | |
4478 * | || || | +--> p4
4479 * | || || +--------> delim
4480 * | || |+-----------> p3
4481 * | || +------------> p2
4482 * | |+----------------> p1
4483 * | +-----------------> colon
4484 * +------------------------> cur_ptr
4485 */
4486
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004487 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488 for (delim = p3; delim < p4; delim++)
4489 if (*delim == COOKIE_DELIM)
4490 break;
4491 }
4492 else
4493 delim = p4;
4494
4495
4496 /* Here, we'll look for the first running server which supports the cookie.
4497 * This allows to share a same cookie between several servers, for example
4498 * to dedicate backup servers to specific servers only.
4499 * However, to prevent clients from sticking to cookie-less backup server
4500 * when they have incidentely learned an empty cookie, we simply ignore
4501 * empty cookies and mark them as invalid.
4502 */
4503 if (delim == p3)
4504 srv = NULL;
4505
4506 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004507 if (srv->cookie && (srv->cklen == delim - p3) &&
4508 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004509 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004510 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004511 txn->flags &= ~TX_CK_MASK;
4512 txn->flags |= TX_CK_VALID;
4513 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004514 t->srv = srv;
4515 break;
4516 } else {
4517 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004518 txn->flags &= ~TX_CK_MASK;
4519 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004520 }
4521 }
4522 srv = srv->next;
4523 }
4524
Willy Tarreau3d300592007-03-18 18:34:41 +01004525 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004526 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004527 txn->flags &= ~TX_CK_MASK;
4528 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004529 }
4530
4531 /* depending on the cookie mode, we may have to either :
4532 * - delete the complete cookie if we're in insert+indirect mode, so that
4533 * the server never sees it ;
4534 * - remove the server id from the cookie value, and tag the cookie as an
4535 * application cookie so that it does not get accidentely removed later,
4536 * if we're in cookie prefix mode
4537 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004538 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004539 int delta; /* negative */
4540
4541 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4542 p4 += delta;
4543 cur_end += delta;
4544 cur_next += delta;
4545 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004546 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004547
4548 del_cookie = del_colon = NULL;
4549 app_cookies++; /* protect the header from deletion */
4550 }
4551 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004552 (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 +01004553 del_cookie = p1;
4554 del_colon = colon;
4555 }
4556 } else {
4557 /* now we know that we must keep this cookie since it's
4558 * not ours. But if we wanted to delete our cookie
4559 * earlier, we cannot remove the complete header, but we
4560 * can remove the previous block itself.
4561 */
4562 app_cookies++;
4563
4564 if (del_cookie != NULL) {
4565 int delta; /* negative */
4566
4567 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4568 p4 += delta;
4569 cur_end += delta;
4570 cur_next += delta;
4571 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004572 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004573 del_cookie = del_colon = NULL;
4574 }
4575 }
4576
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004577 if ((t->be->appsession_name != NULL) &&
4578 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004579 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004580
Willy Tarreau58f10d72006-12-04 02:26:12 +01004581 /* Cool... it's the right one */
4582
4583 asession_temp = &local_asession;
4584
Willy Tarreau63963c62007-05-13 21:29:55 +02004585 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004586 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4587 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4588 return;
4589 }
4590
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004591 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4592 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004593 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004594
Willy Tarreau58f10d72006-12-04 02:26:12 +01004595 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004596 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4597 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004598 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004599 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004600 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004601 Alert("Not enough memory process_cli():asession:calloc().\n");
4602 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4603 return;
4604 }
4605
4606 asession_temp->sessid = local_asession.sessid;
4607 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004608 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004609 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004610 } else {
4611 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004612 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004615 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004616 Alert("Found Application Session without matching server.\n");
4617 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004618 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004619 while (srv) {
4620 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004621 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004622 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004623 txn->flags &= ~TX_CK_MASK;
4624 txn->flags |= TX_CK_VALID;
4625 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004626 t->srv = srv;
4627 break;
4628 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004629 txn->flags &= ~TX_CK_MASK;
4630 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004631 }
4632 }
4633 srv = srv->next;
4634 }/* end while(srv) */
4635 }/* end else if server == NULL */
4636
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004637 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004638 asession_temp->request_count++;
4639#if defined(DEBUG_HASH)
4640 Alert("manage_client_side_cookies\n");
4641 appsession_hash_dump(&(t->be->htbl_proxy));
4642#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004643 }/* end if ((t->proxy->appsession_name != NULL) ... */
4644 }
4645
4646 /* we'll have to look for another cookie ... */
4647 p1 = p4;
4648 } /* while (p1 < cur_end) */
4649
4650 /* There's no more cookie on this line.
4651 * We may have marked the last one(s) for deletion.
4652 * We must do this now in two ways :
4653 * - if there is no app cookie, we simply delete the header ;
4654 * - if there are app cookies, we must delete the end of the
4655 * string properly, including the colon/semi-colon before
4656 * the cookie name.
4657 */
4658 if (del_cookie != NULL) {
4659 int delta;
4660 if (app_cookies) {
4661 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4662 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004663 cur_hdr->len += delta;
4664 } else {
4665 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004666
4667 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004668 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4669 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004670 cur_hdr->len = 0;
4671 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004672 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004673 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004674 }
4675
4676 /* keep the link from this header to next one */
4677 old_idx = cur_idx;
4678 } /* end of cookie processing on this header */
4679}
4680
4681
Willy Tarreaua15645d2007-03-18 16:22:39 +01004682/* Iterate the same filter through all response headers contained in <rtr>.
4683 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4684 */
4685int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4686{
4687 char term;
4688 char *cur_ptr, *cur_end, *cur_next;
4689 int cur_idx, old_idx, last_hdr;
4690 struct http_txn *txn = &t->txn;
4691 struct hdr_idx_elem *cur_hdr;
4692 int len, delta;
4693
4694 last_hdr = 0;
4695
4696 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4697 old_idx = 0;
4698
4699 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004700 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004701 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004702 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703 (exp->action == ACT_ALLOW ||
4704 exp->action == ACT_DENY))
4705 return 0;
4706
4707 cur_idx = txn->hdr_idx.v[old_idx].next;
4708 if (!cur_idx)
4709 break;
4710
4711 cur_hdr = &txn->hdr_idx.v[cur_idx];
4712 cur_ptr = cur_next;
4713 cur_end = cur_ptr + cur_hdr->len;
4714 cur_next = cur_end + cur_hdr->cr + 1;
4715
4716 /* Now we have one header between cur_ptr and cur_end,
4717 * and the next header starts at cur_next.
4718 */
4719
4720 /* The annoying part is that pattern matching needs
4721 * that we modify the contents to null-terminate all
4722 * strings before testing them.
4723 */
4724
4725 term = *cur_end;
4726 *cur_end = '\0';
4727
4728 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4729 switch (exp->action) {
4730 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004731 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 last_hdr = 1;
4733 break;
4734
4735 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004736 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004737 last_hdr = 1;
4738 break;
4739
4740 case ACT_REPLACE:
4741 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4742 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4743 /* FIXME: if the user adds a newline in the replacement, the
4744 * index will not be recalculated for now, and the new line
4745 * will not be counted as a new header.
4746 */
4747
4748 cur_end += delta;
4749 cur_next += delta;
4750 cur_hdr->len += delta;
4751 txn->rsp.eoh += delta;
4752 break;
4753
4754 case ACT_REMOVE:
4755 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4756 cur_next += delta;
4757
4758 /* FIXME: this should be a separate function */
4759 txn->rsp.eoh += delta;
4760 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4761 txn->hdr_idx.used--;
4762 cur_hdr->len = 0;
4763 cur_end = NULL; /* null-term has been rewritten */
4764 break;
4765
4766 }
4767 }
4768 if (cur_end)
4769 *cur_end = term; /* restore the string terminator */
4770
4771 /* keep the link from this header to next one in case of later
4772 * removal of next header.
4773 */
4774 old_idx = cur_idx;
4775 }
4776 return 0;
4777}
4778
4779
4780/* Apply the filter to the status line in the response buffer <rtr>.
4781 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4782 * or -1 if a replacement resulted in an invalid status line.
4783 */
4784int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4785{
4786 char term;
4787 char *cur_ptr, *cur_end;
4788 int done;
4789 struct http_txn *txn = &t->txn;
4790 int len, delta;
4791
4792
Willy Tarreau3d300592007-03-18 18:34:41 +01004793 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004794 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004795 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004796 (exp->action == ACT_ALLOW ||
4797 exp->action == ACT_DENY))
4798 return 0;
4799 else if (exp->action == ACT_REMOVE)
4800 return 0;
4801
4802 done = 0;
4803
Willy Tarreau9cdde232007-05-02 20:58:19 +02004804 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004805 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4806
4807 /* Now we have the status line between cur_ptr and cur_end */
4808
4809 /* The annoying part is that pattern matching needs
4810 * that we modify the contents to null-terminate all
4811 * strings before testing them.
4812 */
4813
4814 term = *cur_end;
4815 *cur_end = '\0';
4816
4817 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4818 switch (exp->action) {
4819 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004820 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004821 done = 1;
4822 break;
4823
4824 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004825 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004826 done = 1;
4827 break;
4828
4829 case ACT_REPLACE:
4830 *cur_end = term; /* restore the string terminator */
4831 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4832 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4833 /* FIXME: if the user adds a newline in the replacement, the
4834 * index will not be recalculated for now, and the new line
4835 * will not be counted as a new header.
4836 */
4837
4838 txn->rsp.eoh += delta;
4839 cur_end += delta;
4840
Willy Tarreau9cdde232007-05-02 20:58:19 +02004841 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004842 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004843 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004844 cur_ptr, cur_end + 1,
4845 NULL, NULL);
4846 if (unlikely(!cur_end))
4847 return -1;
4848
4849 /* we have a full respnse and we know that we have either a CR
4850 * or an LF at <ptr>.
4851 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004852 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004853 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4854 /* there is no point trying this regex on headers */
4855 return 1;
4856 }
4857 }
4858 *cur_end = term; /* restore the string terminator */
4859 return done;
4860}
4861
4862
4863
4864/*
4865 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4866 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4867 * unparsable response.
4868 */
4869int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4870{
Willy Tarreau3d300592007-03-18 18:34:41 +01004871 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004872 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004873 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004874 int ret;
4875
4876 /*
4877 * The interleaving of transformations and verdicts
4878 * makes it difficult to decide to continue or stop
4879 * the evaluation.
4880 */
4881
Willy Tarreau3d300592007-03-18 18:34:41 +01004882 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004883 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4884 exp->action == ACT_PASS)) {
4885 exp = exp->next;
4886 continue;
4887 }
4888
4889 /* Apply the filter to the status line. */
4890 ret = apply_filter_to_sts_line(t, rtr, exp);
4891 if (unlikely(ret < 0))
4892 return -1;
4893
4894 if (likely(ret == 0)) {
4895 /* The filter did not match the response, it can be
4896 * iterated through all headers.
4897 */
4898 apply_filter_to_resp_headers(t, rtr, exp);
4899 }
4900 exp = exp->next;
4901 }
4902 return 0;
4903}
4904
4905
4906
4907/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004908 * Manage server-side cookies. It can impact performance by about 2% so it is
4909 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910 */
4911void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4912{
4913 struct http_txn *txn = &t->txn;
4914 char *p1, *p2, *p3, *p4;
4915
4916 appsess *asession_temp = NULL;
4917 appsess local_asession;
4918
4919 char *cur_ptr, *cur_end, *cur_next;
4920 int cur_idx, old_idx, delta;
4921
Willy Tarreaua15645d2007-03-18 16:22:39 +01004922 /* Iterate through the headers.
4923 * we start with the start line.
4924 */
4925 old_idx = 0;
4926 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4927
4928 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4929 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004930 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004931
4932 cur_hdr = &txn->hdr_idx.v[cur_idx];
4933 cur_ptr = cur_next;
4934 cur_end = cur_ptr + cur_hdr->len;
4935 cur_next = cur_end + cur_hdr->cr + 1;
4936
4937 /* We have one full header between cur_ptr and cur_end, and the
4938 * next header starts at cur_next. We're only interested in
4939 * "Cookie:" headers.
4940 */
4941
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004942 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4943 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944 old_idx = cur_idx;
4945 continue;
4946 }
4947
4948 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004949 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004950
4951
4952 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004953 if (t->be->cookie_name == NULL &&
4954 t->be->appsession_name == NULL &&
4955 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956 return;
4957
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004958 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004959
4960 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004961 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4962 break;
4963
4964 /* p1 is at the beginning of the cookie name */
4965 p2 = p1;
4966
4967 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4968 p2++;
4969
4970 if (p2 == cur_end || *p2 == ';') /* next cookie */
4971 break;
4972
4973 p3 = p2 + 1; /* skip the '=' sign */
4974 if (p3 == cur_end)
4975 break;
4976
4977 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004978 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004979 p4++;
4980
4981 /* here, we have the cookie name between p1 and p2,
4982 * and its value between p3 and p4.
4983 * we can process it.
4984 */
4985
4986 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004987 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004988 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004989 (p4 - p1 >= t->be->capture_namelen) &&
4990 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991 int log_len = p4 - p1;
4992
Willy Tarreau086b3b42007-05-13 21:45:51 +02004993 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004994 Alert("HTTP logging : out of memory.\n");
4995 }
4996
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004997 if (log_len > t->be->capture_len)
4998 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004999 memcpy(txn->srv_cookie, p1, log_len);
5000 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005001 }
5002
5003 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005004 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5005 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005006 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005007 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005008
5009 /* If the cookie is in insert mode on a known server, we'll delete
5010 * this occurrence because we'll insert another one later.
5011 * We'll delete it too if the "indirect" option is set and we're in
5012 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005013 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5014 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005015 /* this header must be deleted */
5016 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5017 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5018 txn->hdr_idx.used--;
5019 cur_hdr->len = 0;
5020 cur_next += delta;
5021 txn->rsp.eoh += delta;
5022
Willy Tarreau3d300592007-03-18 18:34:41 +01005023 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005024 }
5025 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005026 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005027 /* replace bytes p3->p4 with the cookie name associated
5028 * with this server since we know it.
5029 */
5030 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5031 cur_hdr->len += delta;
5032 cur_next += delta;
5033 txn->rsp.eoh += delta;
5034
Willy Tarreau3d300592007-03-18 18:34:41 +01005035 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036 }
5037 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005038 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005039 /* insert the cookie name associated with this server
5040 * before existing cookie, and insert a delimitor between them..
5041 */
5042 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5043 cur_hdr->len += delta;
5044 cur_next += delta;
5045 txn->rsp.eoh += delta;
5046
5047 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005048 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005049 }
5050 }
5051 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005052 else if ((t->be->appsession_name != NULL) &&
5053 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005054
5055 /* Cool... it's the right one */
5056
5057 size_t server_id_len = strlen(t->srv->id) + 1;
5058 asession_temp = &local_asession;
5059
Willy Tarreau63963c62007-05-13 21:29:55 +02005060 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005061 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5062 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5063 return;
5064 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005065 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5066 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005067 asession_temp->serverid = NULL;
5068
5069 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005070 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5071 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005072 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073 Alert("Not enough Memory process_srv():asession:calloc().\n");
5074 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5075 return;
5076 }
5077 asession_temp->sessid = local_asession.sessid;
5078 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005079 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005080 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5081 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005082 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005083 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005084 }
5085
Willy Tarreaua15645d2007-03-18 16:22:39 +01005086 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005087 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005088 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5089 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5090 return;
5091 }
5092 asession_temp->serverid[0] = '\0';
5093 }
5094
5095 if (asession_temp->serverid[0] == '\0')
5096 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5097
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005098 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005099 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005100#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005101 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005102 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005103#endif
5104 }/* end if ((t->proxy->appsession_name != NULL) ... */
5105 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5106 } /* we're now at the end of the cookie value */
5107
5108 /* keep the link from this header to next one */
5109 old_idx = cur_idx;
5110 } /* end of cookie processing on this header */
5111}
5112
5113
5114
5115/*
5116 * Check if response is cacheable or not. Updates t->flags.
5117 */
5118void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5119{
5120 struct http_txn *txn = &t->txn;
5121 char *p1, *p2;
5122
5123 char *cur_ptr, *cur_end, *cur_next;
5124 int cur_idx;
5125
Willy Tarreau5df51872007-11-25 16:20:08 +01005126 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005127 return;
5128
5129 /* Iterate through the headers.
5130 * we start with the start line.
5131 */
5132 cur_idx = 0;
5133 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5134
5135 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5136 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005137 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005138
5139 cur_hdr = &txn->hdr_idx.v[cur_idx];
5140 cur_ptr = cur_next;
5141 cur_end = cur_ptr + cur_hdr->len;
5142 cur_next = cur_end + cur_hdr->cr + 1;
5143
5144 /* We have one full header between cur_ptr and cur_end, and the
5145 * next header starts at cur_next. We're only interested in
5146 * "Cookie:" headers.
5147 */
5148
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005149 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5150 if (val) {
5151 if ((cur_end - (cur_ptr + val) >= 8) &&
5152 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5153 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5154 return;
5155 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005156 }
5157
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005158 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5159 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160 continue;
5161
5162 /* OK, right now we know we have a cache-control header at cur_ptr */
5163
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005164 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165
5166 if (p1 >= cur_end) /* no more info */
5167 continue;
5168
5169 /* p1 is at the beginning of the value */
5170 p2 = p1;
5171
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005172 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005173 p2++;
5174
5175 /* we have a complete value between p1 and p2 */
5176 if (p2 < cur_end && *p2 == '=') {
5177 /* we have something of the form no-cache="set-cookie" */
5178 if ((cur_end - p1 >= 21) &&
5179 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5180 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005181 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005182 continue;
5183 }
5184
5185 /* OK, so we know that either p2 points to the end of string or to a comma */
5186 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5187 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5188 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5189 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005190 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005191 return;
5192 }
5193
5194 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005195 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005196 continue;
5197 }
5198 }
5199}
5200
5201
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202/*
5203 * Try to retrieve a known appsession in the URI, then the associated server.
5204 * If the server is found, it's assigned to the session.
5205 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005206void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005207{
Willy Tarreau3d300592007-03-18 18:34:41 +01005208 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005209 appsess *asession_temp = NULL;
5210 appsess local_asession;
5211 char *request_line;
5212
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005213 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005214 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005215 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005216 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217 return;
5218
5219 /* skip ';' */
5220 request_line++;
5221
5222 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005223 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 return;
5225
5226 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005227 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005228
5229 /* First try if we already have an appsession */
5230 asession_temp = &local_asession;
5231
Willy Tarreau63963c62007-05-13 21:29:55 +02005232 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5234 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5235 return;
5236 }
5237
5238 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005239 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5240 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241 asession_temp->serverid = NULL;
5242
5243 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005244 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5245 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005246 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005248 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249 Alert("Not enough memory process_cli():asession:calloc().\n");
5250 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5251 return;
5252 }
5253 asession_temp->sessid = local_asession.sessid;
5254 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005255 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005256 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005257 }
5258 else {
5259 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005260 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005262
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005263 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005264 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005265
Willy Tarreau58f10d72006-12-04 02:26:12 +01005266#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005267 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005268 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005269#endif
5270 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005271 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005272 Alert("Found Application Session without matching server.\n");
5273 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005274 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005275 while (srv) {
5276 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005277 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005278 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005279 txn->flags &= ~TX_CK_MASK;
5280 txn->flags |= TX_CK_VALID;
5281 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005282 t->srv = srv;
5283 break;
5284 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005285 txn->flags &= ~TX_CK_MASK;
5286 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005287 }
5288 }
5289 srv = srv->next;
5290 }
5291 }
5292}
5293
5294
Willy Tarreaub2513902006-12-17 14:52:38 +01005295/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005296 * In a GET or HEAD request, check if the requested URI matches the stats uri
5297 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005298 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005299 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005300 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005301 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005302 *
5303 * Returns 1 if the session's state changes, otherwise 0.
5304 */
5305int stats_check_uri_auth(struct session *t, struct proxy *backend)
5306{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005307 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005308 struct uri_auth *uri_auth = backend->uri_auth;
5309 struct user_auth *user;
5310 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005311 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005312
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005313 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005315 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005316 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005317 return 0;
5318
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005319 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005320
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005321 /* the URI is in h */
5322 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005323 return 0;
5324
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005325 h += uri_auth->uri_len;
5326 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5327 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005328 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005329 break;
5330 }
5331 h++;
5332 }
5333
5334 if (uri_auth->refresh) {
5335 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5336 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5337 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005338 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005339 break;
5340 }
5341 h++;
5342 }
5343 }
5344
Willy Tarreau55bb8452007-10-17 18:44:57 +02005345 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5346 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5347 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005348 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005349 break;
5350 }
5351 h++;
5352 }
5353
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005354 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5355
Willy Tarreaub2513902006-12-17 14:52:38 +01005356 /* we are in front of a interceptable URI. Let's check
5357 * if there's an authentication and if it's valid.
5358 */
5359 user = uri_auth->users;
5360 if (!user) {
5361 /* no user auth required, it's OK */
5362 authenticated = 1;
5363 } else {
5364 authenticated = 0;
5365
5366 /* a user list is defined, we have to check.
5367 * skip 21 chars for "Authorization: Basic ".
5368 */
5369
5370 /* FIXME: this should move to an earlier place */
5371 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005372 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5373 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5374 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005375 if (len > 14 &&
5376 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005377 txn->auth_hdr.str = h;
5378 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005379 break;
5380 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005381 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005382 }
5383
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005384 if (txn->auth_hdr.len < 21 ||
5385 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005386 user = NULL;
5387
5388 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005389 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5390 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005391 user->user_pwd, user->user_len)) {
5392 authenticated = 1;
5393 break;
5394 }
5395 user = user->next;
5396 }
5397 }
5398
5399 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005400 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005401
5402 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005403 msg.str = trash;
5404 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005405 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005406 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005407 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005408 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005409 if (!(t->flags & SN_ERR_MASK))
5410 t->flags |= SN_ERR_PRXCOND;
5411 if (!(t->flags & SN_FINST_MASK))
5412 t->flags |= SN_FINST_R;
5413 return 1;
5414 }
5415
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005416 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005417 * data.
5418 */
Willy Tarreau72b179a2008-08-28 16:01:32 +02005419 buffer_shutw_now(t->req);
5420 buffer_shutr_now(t->rep);
5421 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005422 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005423 t->data_source = DATA_SRC_STATS;
5424 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005425 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005426 produce_content(t);
5427 return 1;
5428}
5429
5430
Willy Tarreaubaaee002006-06-26 02:48:02 +02005431/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005432 * Print a debug line with a header
5433 */
5434void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5435{
5436 int len, max;
5437 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005438 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005439 max = end - start;
5440 UBOUND(max, sizeof(trash) - len - 1);
5441 len += strlcpy2(trash + len, start, max + 1);
5442 trash[len++] = '\n';
5443 write(1, trash, len);
5444}
5445
5446
Willy Tarreau8797c062007-05-07 00:55:35 +02005447/************************************************************************/
5448/* The code below is dedicated to ACL parsing and matching */
5449/************************************************************************/
5450
5451
5452
5453
5454/* 1. Check on METHOD
5455 * We use the pre-parsed method if it is known, and store its number as an
5456 * integer. If it is unknown, we use the pointer and the length.
5457 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005458static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005459{
5460 int len, meth;
5461
Willy Tarreauae8b7962007-06-09 23:10:04 +02005462 len = strlen(*text);
5463 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005464
5465 pattern->val.i = meth;
5466 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005467 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005468 if (!pattern->ptr.str)
5469 return 0;
5470 pattern->len = len;
5471 }
5472 return 1;
5473}
5474
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005475static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005476acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5477 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005478{
5479 int meth;
5480 struct http_txn *txn = l7;
5481
Willy Tarreaub6866442008-07-14 23:54:42 +02005482 if (!txn)
5483 return 0;
5484
Willy Tarreauc11416f2007-06-17 16:58:38 +02005485 if (txn->req.msg_state != HTTP_MSG_BODY)
5486 return 0;
5487
Willy Tarreau8797c062007-05-07 00:55:35 +02005488 meth = txn->meth;
5489 test->i = meth;
5490 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005491 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5492 /* ensure the indexes are not affected */
5493 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005494 test->len = txn->req.sl.rq.m_l;
5495 test->ptr = txn->req.sol;
5496 }
5497 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5498 return 1;
5499}
5500
5501static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5502{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005503 int icase;
5504
Willy Tarreau8797c062007-05-07 00:55:35 +02005505 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005506 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005507
5508 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005509 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005510
5511 /* Other method, we must compare the strings */
5512 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005513 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005514
5515 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5516 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5517 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005518 return ACL_PAT_FAIL;
5519 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005520}
5521
5522/* 2. Check on Request/Status Version
5523 * We simply compare strings here.
5524 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005525static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005526{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005527 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005528 if (!pattern->ptr.str)
5529 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005530 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005531 return 1;
5532}
5533
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005534static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005535acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5536 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005537{
5538 struct http_txn *txn = l7;
5539 char *ptr;
5540 int len;
5541
Willy Tarreaub6866442008-07-14 23:54:42 +02005542 if (!txn)
5543 return 0;
5544
Willy Tarreauc11416f2007-06-17 16:58:38 +02005545 if (txn->req.msg_state != HTTP_MSG_BODY)
5546 return 0;
5547
Willy Tarreau8797c062007-05-07 00:55:35 +02005548 len = txn->req.sl.rq.v_l;
5549 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5550
5551 while ((len-- > 0) && (*ptr++ != '/'));
5552 if (len <= 0)
5553 return 0;
5554
5555 test->ptr = ptr;
5556 test->len = len;
5557
5558 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5559 return 1;
5560}
5561
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005562static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005563acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5564 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005565{
5566 struct http_txn *txn = l7;
5567 char *ptr;
5568 int len;
5569
Willy Tarreaub6866442008-07-14 23:54:42 +02005570 if (!txn)
5571 return 0;
5572
Willy Tarreauc11416f2007-06-17 16:58:38 +02005573 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5574 return 0;
5575
Willy Tarreau8797c062007-05-07 00:55:35 +02005576 len = txn->rsp.sl.st.v_l;
5577 ptr = txn->rsp.sol;
5578
5579 while ((len-- > 0) && (*ptr++ != '/'));
5580 if (len <= 0)
5581 return 0;
5582
5583 test->ptr = ptr;
5584 test->len = len;
5585
5586 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5587 return 1;
5588}
5589
5590/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005591static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005592acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5593 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005594{
5595 struct http_txn *txn = l7;
5596 char *ptr;
5597 int len;
5598
Willy Tarreaub6866442008-07-14 23:54:42 +02005599 if (!txn)
5600 return 0;
5601
Willy Tarreauc11416f2007-06-17 16:58:38 +02005602 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5603 return 0;
5604
Willy Tarreau8797c062007-05-07 00:55:35 +02005605 len = txn->rsp.sl.st.c_l;
5606 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5607
5608 test->i = __strl2ui(ptr, len);
5609 test->flags = ACL_TEST_F_VOL_1ST;
5610 return 1;
5611}
5612
5613/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005614static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005615acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5616 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005617{
5618 struct http_txn *txn = l7;
5619
Willy Tarreaub6866442008-07-14 23:54:42 +02005620 if (!txn)
5621 return 0;
5622
Willy Tarreauc11416f2007-06-17 16:58:38 +02005623 if (txn->req.msg_state != HTTP_MSG_BODY)
5624 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005625
Willy Tarreauc11416f2007-06-17 16:58:38 +02005626 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5627 /* ensure the indexes are not affected */
5628 return 0;
5629
Willy Tarreau8797c062007-05-07 00:55:35 +02005630 test->len = txn->req.sl.rq.u_l;
5631 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5632
Willy Tarreauf3d25982007-05-08 22:45:09 +02005633 /* we do not need to set READ_ONLY because the data is in a buffer */
5634 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005635 return 1;
5636}
5637
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005638static int
5639acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5640 struct acl_expr *expr, struct acl_test *test)
5641{
5642 struct http_txn *txn = l7;
5643
Willy Tarreaub6866442008-07-14 23:54:42 +02005644 if (!txn)
5645 return 0;
5646
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005647 if (txn->req.msg_state != HTTP_MSG_BODY)
5648 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005649
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5651 /* ensure the indexes are not affected */
5652 return 0;
5653
5654 /* Parse HTTP request */
5655 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5656 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5657 test->i = AF_INET;
5658
5659 /*
5660 * If we are parsing url in frontend space, we prepare backend stage
5661 * to not parse again the same url ! optimization lazyness...
5662 */
5663 if (px->options & PR_O_HTTP_PROXY)
5664 l4->flags |= SN_ADDR_SET;
5665
5666 test->flags = ACL_TEST_F_READ_ONLY;
5667 return 1;
5668}
5669
5670static int
5671acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5672 struct acl_expr *expr, struct acl_test *test)
5673{
5674 struct http_txn *txn = l7;
5675
Willy Tarreaub6866442008-07-14 23:54:42 +02005676 if (!txn)
5677 return 0;
5678
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005679 if (txn->req.msg_state != HTTP_MSG_BODY)
5680 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005681
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005682 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5683 /* ensure the indexes are not affected */
5684 return 0;
5685
5686 /* Same optimization as url_ip */
5687 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5688 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5689
5690 if (px->options & PR_O_HTTP_PROXY)
5691 l4->flags |= SN_ADDR_SET;
5692
5693 test->flags = ACL_TEST_F_READ_ONLY;
5694 return 1;
5695}
5696
Willy Tarreauc11416f2007-06-17 16:58:38 +02005697/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5698 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5699 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005700static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005701acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005702 struct acl_expr *expr, struct acl_test *test)
5703{
5704 struct http_txn *txn = l7;
5705 struct hdr_idx *idx = &txn->hdr_idx;
5706 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005707
Willy Tarreaub6866442008-07-14 23:54:42 +02005708 if (!txn)
5709 return 0;
5710
Willy Tarreau33a7e692007-06-10 19:45:56 +02005711 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5712 /* search for header from the beginning */
5713 ctx->idx = 0;
5714
Willy Tarreau33a7e692007-06-10 19:45:56 +02005715 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5716 test->flags |= ACL_TEST_F_FETCH_MORE;
5717 test->flags |= ACL_TEST_F_VOL_HDR;
5718 test->len = ctx->vlen;
5719 test->ptr = (char *)ctx->line + ctx->val;
5720 return 1;
5721 }
5722
5723 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5724 test->flags |= ACL_TEST_F_VOL_HDR;
5725 return 0;
5726}
5727
Willy Tarreau33a7e692007-06-10 19:45:56 +02005728static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005729acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5730 struct acl_expr *expr, struct acl_test *test)
5731{
5732 struct http_txn *txn = l7;
5733
Willy Tarreaub6866442008-07-14 23:54:42 +02005734 if (!txn)
5735 return 0;
5736
Willy Tarreauc11416f2007-06-17 16:58:38 +02005737 if (txn->req.msg_state != HTTP_MSG_BODY)
5738 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005739
Willy Tarreauc11416f2007-06-17 16:58:38 +02005740 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5741 /* ensure the indexes are not affected */
5742 return 0;
5743
5744 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5745}
5746
5747static int
5748acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5749 struct acl_expr *expr, struct acl_test *test)
5750{
5751 struct http_txn *txn = l7;
5752
Willy Tarreaub6866442008-07-14 23:54:42 +02005753 if (!txn)
5754 return 0;
5755
Willy Tarreauc11416f2007-06-17 16:58:38 +02005756 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5757 return 0;
5758
5759 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5760}
5761
5762/* 6. Check on HTTP header count. The number of occurrences is returned.
5763 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5764 */
5765static int
5766acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005767 struct acl_expr *expr, struct acl_test *test)
5768{
5769 struct http_txn *txn = l7;
5770 struct hdr_idx *idx = &txn->hdr_idx;
5771 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005772 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005773
Willy Tarreaub6866442008-07-14 23:54:42 +02005774 if (!txn)
5775 return 0;
5776
Willy Tarreau33a7e692007-06-10 19:45:56 +02005777 ctx.idx = 0;
5778 cnt = 0;
5779 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5780 cnt++;
5781
5782 test->i = cnt;
5783 test->flags = ACL_TEST_F_VOL_HDR;
5784 return 1;
5785}
5786
Willy Tarreauc11416f2007-06-17 16:58:38 +02005787static int
5788acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5789 struct acl_expr *expr, struct acl_test *test)
5790{
5791 struct http_txn *txn = l7;
5792
Willy Tarreaub6866442008-07-14 23:54:42 +02005793 if (!txn)
5794 return 0;
5795
Willy Tarreauc11416f2007-06-17 16:58:38 +02005796 if (txn->req.msg_state != HTTP_MSG_BODY)
5797 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005798
Willy Tarreauc11416f2007-06-17 16:58:38 +02005799 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5800 /* ensure the indexes are not affected */
5801 return 0;
5802
5803 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5804}
5805
5806static int
5807acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5808 struct acl_expr *expr, struct acl_test *test)
5809{
5810 struct http_txn *txn = l7;
5811
Willy Tarreaub6866442008-07-14 23:54:42 +02005812 if (!txn)
5813 return 0;
5814
Willy Tarreauc11416f2007-06-17 16:58:38 +02005815 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5816 return 0;
5817
5818 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5819}
5820
Willy Tarreau33a7e692007-06-10 19:45:56 +02005821/* 7. Check on HTTP header's integer value. The integer value is returned.
5822 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005823 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005824 */
5825static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005826acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005827 struct acl_expr *expr, struct acl_test *test)
5828{
5829 struct http_txn *txn = l7;
5830 struct hdr_idx *idx = &txn->hdr_idx;
5831 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005832
Willy Tarreaub6866442008-07-14 23:54:42 +02005833 if (!txn)
5834 return 0;
5835
Willy Tarreau33a7e692007-06-10 19:45:56 +02005836 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5837 /* search for header from the beginning */
5838 ctx->idx = 0;
5839
Willy Tarreau33a7e692007-06-10 19:45:56 +02005840 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5841 test->flags |= ACL_TEST_F_FETCH_MORE;
5842 test->flags |= ACL_TEST_F_VOL_HDR;
5843 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5844 return 1;
5845 }
5846
5847 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5848 test->flags |= ACL_TEST_F_VOL_HDR;
5849 return 0;
5850}
5851
Willy Tarreauc11416f2007-06-17 16:58:38 +02005852static int
5853acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5854 struct acl_expr *expr, struct acl_test *test)
5855{
5856 struct http_txn *txn = l7;
5857
Willy Tarreaub6866442008-07-14 23:54:42 +02005858 if (!txn)
5859 return 0;
5860
Willy Tarreauc11416f2007-06-17 16:58:38 +02005861 if (txn->req.msg_state != HTTP_MSG_BODY)
5862 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005863
Willy Tarreauc11416f2007-06-17 16:58:38 +02005864 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5865 /* ensure the indexes are not affected */
5866 return 0;
5867
5868 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5869}
5870
5871static int
5872acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5873 struct acl_expr *expr, struct acl_test *test)
5874{
5875 struct http_txn *txn = l7;
5876
Willy Tarreaub6866442008-07-14 23:54:42 +02005877 if (!txn)
5878 return 0;
5879
Willy Tarreauc11416f2007-06-17 16:58:38 +02005880 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5881 return 0;
5882
5883 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5884}
5885
Willy Tarreau737b0c12007-06-10 21:28:46 +02005886/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5887 * the first '/' after the possible hostname, and ends before the possible '?'.
5888 */
5889static int
5890acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5891 struct acl_expr *expr, struct acl_test *test)
5892{
5893 struct http_txn *txn = l7;
5894 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005895
Willy Tarreaub6866442008-07-14 23:54:42 +02005896 if (!txn)
5897 return 0;
5898
Willy Tarreauc11416f2007-06-17 16:58:38 +02005899 if (txn->req.msg_state != HTTP_MSG_BODY)
5900 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005901
Willy Tarreauc11416f2007-06-17 16:58:38 +02005902 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5903 /* ensure the indexes are not affected */
5904 return 0;
5905
Willy Tarreau21d2af32008-02-14 20:25:24 +01005906 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5907 ptr = http_get_path(txn);
5908 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005909 return 0;
5910
5911 /* OK, we got the '/' ! */
5912 test->ptr = ptr;
5913
5914 while (ptr < end && *ptr != '?')
5915 ptr++;
5916
5917 test->len = ptr - test->ptr;
5918
5919 /* we do not need to set READ_ONLY because the data is in a buffer */
5920 test->flags = ACL_TEST_F_VOL_1ST;
5921 return 1;
5922}
5923
5924
Willy Tarreau8797c062007-05-07 00:55:35 +02005925
5926/************************************************************************/
5927/* All supported keywords must be declared here. */
5928/************************************************************************/
5929
5930/* Note: must not be declared <const> as its list will be overwritten */
5931static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005932 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5933 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5934 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5935 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005936
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005937 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5938 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5939 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5940 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5941 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5942 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5943 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5944 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5945 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005946
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005947 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5948 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5949 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5950 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5951 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5952 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5953 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5954 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5955 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5956 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005957
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005958 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5959 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5960 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5961 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5962 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5963 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5964 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5965 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5966 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005967
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005968 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5969 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5970 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5971 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5972 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5973 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5974 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005975
Willy Tarreauf3d25982007-05-08 22:45:09 +02005976 { NULL, NULL, NULL, NULL },
5977
5978#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005979 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5980 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5981 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5982 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5983 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5984 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5985 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5986
Willy Tarreau8797c062007-05-07 00:55:35 +02005987 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5988 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5989 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5990 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5991 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5992 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5993 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5994 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5995
5996 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5997 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5998 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5999 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6000 { NULL, NULL, NULL, NULL },
6001#endif
6002}};
6003
6004
6005__attribute__((constructor))
6006static void __http_protocol_init(void)
6007{
6008 acl_register_keywords(&acl_kws);
6009}
6010
6011
Willy Tarreau58f10d72006-12-04 02:26:12 +01006012/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006013 * Local variables:
6014 * c-indent-level: 8
6015 * c-basic-offset: 8
6016 * End:
6017 */