blob: 35f54ac6b2fc5f4fba5f438d51e63c35b06ace06 [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 Tarreaucff64112008-11-03 06:26:53 +010053#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020054#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/task.h>
56
Willy Tarreau6d1a9882007-01-07 02:03:04 +010057#ifdef CONFIG_HAP_TCPSPLICE
58#include <libtcpsplice.h>
59#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#define DEBUG_PARSE_NO_SPEEDUP
62#undef DEBUG_PARSE_NO_SPEEDUP
63
Willy Tarreau976f1ee2006-12-17 10:06:03 +010064/* This is used to perform a quick jump as an alternative to a break/continue
65 * instruction. The first argument is the label for normal operation, and the
66 * second one is the break/continue instruction in the no_speedup mode.
67 */
68
69#ifdef DEBUG_PARSE_NO_SPEEDUP
70#define QUICK_JUMP(x,y) y
71#else
72#define QUICK_JUMP(x,y) goto x
73#endif
74
Willy Tarreau1c47f852006-07-09 08:22:27 +020075/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010076const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020077 "HTTP/1.0 200 OK\r\n"
78 "Cache-Control: no-cache\r\n"
79 "Connection: close\r\n"
80 "Content-Type: text/html\r\n"
81 "\r\n"
82 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const struct chunk http_200_chunk = {
85 .str = (char *)&HTTP_200,
86 .len = sizeof(HTTP_200)-1
87};
88
Willy Tarreaub463dfb2008-06-07 23:08:56 +020089const char *HTTP_301 =
90 "HTTP/1.0 301 Moved Permantenly\r\n"
91 "Cache-Control: no-cache\r\n"
92 "Connection: close\r\n"
93 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreau0f772532006-12-23 20:51:41 +010095const char *HTTP_302 =
96 "HTTP/1.0 302 Found\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
99 "Location: "; /* not terminated since it will be concatenated with the URL */
100
101/* same as 302 except that the browser MUST retry with the GET method */
102const char *HTTP_303 =
103 "HTTP/1.0 303 See Other\r\n"
104 "Cache-Control: no-cache\r\n"
105 "Connection: close\r\n"
106 "Location: "; /* not terminated since it will be concatenated with the URL */
107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
109const char *HTTP_401_fmt =
110 "HTTP/1.0 401 Unauthorized\r\n"
111 "Cache-Control: no-cache\r\n"
112 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200113 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
115 "\r\n"
116 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
117
Willy Tarreau0f772532006-12-23 20:51:41 +0100118
119const int http_err_codes[HTTP_ERR_SIZE] = {
120 [HTTP_ERR_400] = 400,
121 [HTTP_ERR_403] = 403,
122 [HTTP_ERR_408] = 408,
123 [HTTP_ERR_500] = 500,
124 [HTTP_ERR_502] = 502,
125 [HTTP_ERR_503] = 503,
126 [HTTP_ERR_504] = 504,
127};
128
Willy Tarreau80587432006-12-24 17:47:20 +0100129static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100131 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 "Cache-Control: no-cache\r\n"
133 "Connection: close\r\n"
134 "Content-Type: text/html\r\n"
135 "\r\n"
136 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
137
138 [HTTP_ERR_403] =
139 "HTTP/1.0 403 Forbidden\r\n"
140 "Cache-Control: no-cache\r\n"
141 "Connection: close\r\n"
142 "Content-Type: text/html\r\n"
143 "\r\n"
144 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
145
146 [HTTP_ERR_408] =
147 "HTTP/1.0 408 Request Time-out\r\n"
148 "Cache-Control: no-cache\r\n"
149 "Connection: close\r\n"
150 "Content-Type: text/html\r\n"
151 "\r\n"
152 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
153
154 [HTTP_ERR_500] =
155 "HTTP/1.0 500 Server Error\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
161
162 [HTTP_ERR_502] =
163 "HTTP/1.0 502 Bad Gateway\r\n"
164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
169
170 [HTTP_ERR_503] =
171 "HTTP/1.0 503 Service Unavailable\r\n"
172 "Cache-Control: no-cache\r\n"
173 "Connection: close\r\n"
174 "Content-Type: text/html\r\n"
175 "\r\n"
176 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
177
178 [HTTP_ERR_504] =
179 "HTTP/1.0 504 Gateway Time-out\r\n"
180 "Cache-Control: no-cache\r\n"
181 "Connection: close\r\n"
182 "Content-Type: text/html\r\n"
183 "\r\n"
184 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
185
186};
187
Willy Tarreau80587432006-12-24 17:47:20 +0100188/* We must put the messages here since GCC cannot initialize consts depending
189 * on strlen().
190 */
191struct chunk http_err_chunks[HTTP_ERR_SIZE];
192
Willy Tarreau42250582007-04-01 01:30:43 +0200193#define FD_SETS_ARE_BITFIELDS
194#ifdef FD_SETS_ARE_BITFIELDS
195/*
196 * This map is used with all the FD_* macros to check whether a particular bit
197 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
198 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
199 * byte should be encoded. Be careful to always pass bytes from 0 to 255
200 * exclusively to the macros.
201 */
202fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
204
205#else
206#error "Check if your OS uses bitfields for fd_sets"
207#endif
208
Willy Tarreaucff64112008-11-03 06:26:53 +0100209int sess_update_st_con_tcp(struct session *s, struct stream_interface *si);
210int sess_update_st_cer(struct session *s, struct stream_interface *si);
Willy Tarreau4351b3a2008-11-12 01:51:41 +0100211void sess_establish(struct session *s, struct stream_interface *si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100212
Willy Tarreau80587432006-12-24 17:47:20 +0100213void init_proto_http()
214{
Willy Tarreau42250582007-04-01 01:30:43 +0200215 int i;
216 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100217 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200218
Willy Tarreau80587432006-12-24 17:47:20 +0100219 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
220 if (!http_err_msgs[msg]) {
221 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
222 abort();
223 }
224
225 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
226 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
227 }
Willy Tarreau42250582007-04-01 01:30:43 +0200228
229 /* initialize the log header encoding map : '{|}"#' should be encoded with
230 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
231 * URL encoding only requires '"', '#' to be encoded as well as non-
232 * printable characters above.
233 */
234 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
235 memset(url_encode_map, 0, sizeof(url_encode_map));
236 for (i = 0; i < 32; i++) {
237 FD_SET(i, hdr_encode_map);
238 FD_SET(i, url_encode_map);
239 }
240 for (i = 127; i < 256; i++) {
241 FD_SET(i, hdr_encode_map);
242 FD_SET(i, url_encode_map);
243 }
244
245 tmp = "\"#{|}";
246 while (*tmp) {
247 FD_SET(*tmp, hdr_encode_map);
248 tmp++;
249 }
250
251 tmp = "\"#";
252 while (*tmp) {
253 FD_SET(*tmp, url_encode_map);
254 tmp++;
255 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200256
257 /* memory allocations */
258 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200259 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100260}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261
Willy Tarreau53b6c742006-12-17 13:37:46 +0100262/*
263 * We have 26 list of methods (1 per first letter), each of which can have
264 * up to 3 entries (2 valid, 1 null).
265 */
266struct http_method_desc {
267 http_meth_t meth;
268 int len;
269 const char text[8];
270};
271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100272const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100273 ['C' - 'A'] = {
274 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
275 },
276 ['D' - 'A'] = {
277 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
278 },
279 ['G' - 'A'] = {
280 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
281 },
282 ['H' - 'A'] = {
283 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
284 },
285 ['P' - 'A'] = {
286 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
287 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
288 },
289 ['T' - 'A'] = {
290 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
291 },
292 /* rest is empty like this :
293 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
294 */
295};
296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100297/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200298 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299 * RFC2616 for those chars.
300 */
301
302const char http_is_spht[256] = {
303 [' '] = 1, ['\t'] = 1,
304};
305
306const char http_is_crlf[256] = {
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_lws[256] = {
311 [' '] = 1, ['\t'] = 1,
312 ['\r'] = 1, ['\n'] = 1,
313};
314
315const char http_is_sep[256] = {
316 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
317 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
318 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
319 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
320 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
321};
322
323const char http_is_ctl[256] = {
324 [0 ... 31] = 1,
325 [127] = 1,
326};
327
328/*
329 * A token is any ASCII char that is neither a separator nor a CTL char.
330 * Do not overwrite values in assignment since gcc-2.95 will not handle
331 * them correctly. Instead, define every non-CTL char's status.
332 */
333const char http_is_token[256] = {
334 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
335 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
336 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
337 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
338 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
339 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
340 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
341 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
342 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
343 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
344 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
345 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
346 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
347 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
348 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
349 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
350 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
351 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
352 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
353 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
354 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
355 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
356 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
357 ['|'] = 1, ['}'] = 0, ['~'] = 1,
358};
359
360
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100361/*
362 * An http ver_token is any ASCII which can be found in an HTTP version,
363 * which includes 'H', 'T', 'P', '/', '.' and any digit.
364 */
365const char http_is_ver_token[256] = {
366 ['.'] = 1, ['/'] = 1,
367 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
368 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
369 ['H'] = 1, ['P'] = 1, ['T'] = 1,
370};
371
372
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200374static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375#endif
376
Willy Tarreau42250582007-04-01 01:30:43 +0200377static void http_sess_log(struct session *s);
378
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100379/*
380 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
381 * CRLF. Text length is measured first, so it cannot be NULL.
382 * The header is also automatically added to the index <hdr_idx>, and the end
383 * of headers is automatically adjusted. The number of bytes added is returned
384 * on success, otherwise <0 is returned indicating an error.
385 */
386int http_header_add_tail(struct buffer *b, struct http_msg *msg,
387 struct hdr_idx *hdr_idx, const char *text)
388{
389 int bytes, len;
390
391 len = strlen(text);
392 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
393 if (!bytes)
394 return -1;
395 msg->eoh += bytes;
396 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
397}
398
399/*
400 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
401 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
402 * the buffer is only opened and the space reserved, but nothing is copied.
403 * The header is also automatically added to the index <hdr_idx>, and the end
404 * of headers is automatically adjusted. The number of bytes added is returned
405 * on success, otherwise <0 is returned indicating an error.
406 */
407int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
408 struct hdr_idx *hdr_idx, const char *text, int len)
409{
410 int bytes;
411
412 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
413 if (!bytes)
414 return -1;
415 msg->eoh += bytes;
416 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
417}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418
419/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100420 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
421 * If so, returns the position of the first non-space character relative to
422 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
423 * to return a pointer to the place after the first space. Returns 0 if the
424 * header name does not match. Checks are case-insensitive.
425 */
426int http_header_match2(const char *hdr, const char *end,
427 const char *name, int len)
428{
429 const char *val;
430
431 if (hdr + len >= end)
432 return 0;
433 if (hdr[len] != ':')
434 return 0;
435 if (strncasecmp(hdr, name, len) != 0)
436 return 0;
437 val = hdr + len + 1;
438 while (val < end && HTTP_IS_SPHT(*val))
439 val++;
440 if ((val >= end) && (len + 2 <= end - hdr))
441 return len + 2; /* we may replace starting from second space */
442 return val - hdr;
443}
444
Willy Tarreau33a7e692007-06-10 19:45:56 +0200445/* Find the end of the header value contained between <s> and <e>.
446 * See RFC2616, par 2.2 for more information. Note that it requires
447 * a valid header to return a valid result.
448 */
449const char *find_hdr_value_end(const char *s, const char *e)
450{
451 int quoted, qdpair;
452
453 quoted = qdpair = 0;
454 for (; s < e; s++) {
455 if (qdpair) qdpair = 0;
456 else if (quoted && *s == '\\') qdpair = 1;
457 else if (quoted && *s == '"') quoted = 0;
458 else if (*s == '"') quoted = 1;
459 else if (*s == ',') return s;
460 }
461 return s;
462}
463
464/* Find the first or next occurrence of header <name> in message buffer <sol>
465 * using headers index <idx>, and return it in the <ctx> structure. This
466 * structure holds everything necessary to use the header and find next
467 * occurrence. If its <idx> member is 0, the header is searched from the
468 * beginning. Otherwise, the next occurrence is returned. The function returns
469 * 1 when it finds a value, and 0 when there is no more.
470 */
471int http_find_header2(const char *name, int len,
472 const char *sol, struct hdr_idx *idx,
473 struct hdr_ctx *ctx)
474{
475 __label__ return_hdr, next_hdr;
476 const char *eol, *sov;
477 int cur_idx;
478
479 if (ctx->idx) {
480 /* We have previously returned a value, let's search
481 * another one on the same line.
482 */
483 cur_idx = ctx->idx;
484 sol = ctx->line;
485 sov = sol + ctx->val + ctx->vlen;
486 eol = sol + idx->v[cur_idx].len;
487
488 if (sov >= eol)
489 /* no more values in this header */
490 goto next_hdr;
491
492 /* values remaining for this header, skip the comma */
493 sov++;
494 while (sov < eol && http_is_lws[(unsigned char)*sov])
495 sov++;
496
497 goto return_hdr;
498 }
499
500 /* first request for this header */
501 sol += hdr_idx_first_pos(idx);
502 cur_idx = hdr_idx_first_idx(idx);
503
504 while (cur_idx) {
505 eol = sol + idx->v[cur_idx].len;
506
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200507 if (len == 0) {
508 /* No argument was passed, we want any header.
509 * To achieve this, we simply build a fake request. */
510 while (sol + len < eol && sol[len] != ':')
511 len++;
512 name = sol;
513 }
514
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515 if ((len < eol - sol) &&
516 (sol[len] == ':') &&
517 (strncasecmp(sol, name, len) == 0)) {
518
519 sov = sol + len + 1;
520 while (sov < eol && http_is_lws[(unsigned char)*sov])
521 sov++;
522 return_hdr:
523 ctx->line = sol;
524 ctx->idx = cur_idx;
525 ctx->val = sov - sol;
526
527 eol = find_hdr_value_end(sov, eol);
528 ctx->vlen = eol - sov;
529 return 1;
530 }
531 next_hdr:
532 sol = eol + idx->v[cur_idx].cr + 1;
533 cur_idx = idx->v[cur_idx].next;
534 }
535 return 0;
536}
537
538int http_find_header(const char *name,
539 const char *sol, struct hdr_idx *idx,
540 struct hdr_ctx *ctx)
541{
542 return http_find_header2(name, strlen(name), sol, idx, ctx);
543}
544
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200545/* This function shuts down the buffers on the server side, and sets indicators
546 * accordingly. The server's fd is supposed to already be closed. Note that if
547 * <status> is 0, or if the message pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548 */
549void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100550 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551{
Willy Tarreau3da77c52008-08-29 09:58:42 +0200552 buffer_write_ena(t->rep);
Willy Tarreau99126c32008-11-27 10:30:51 +0100553 t->req->cons->shutw(t->req->cons);
554 t->req->cons->shutr(t->req->cons);
Willy Tarreau0f772532006-12-23 20:51:41 +0100555 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100556 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100557 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100558 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 }
560 if (!(t->flags & SN_ERR_MASK))
561 t->flags |= err;
562 if (!(t->flags & SN_FINST_MASK))
563 t->flags |= finst;
564}
565
Willy Tarreau80587432006-12-24 17:47:20 +0100566/* This function returns the appropriate error location for the given session
567 * and message.
568 */
569
570struct chunk *error_message(struct session *s, int msgnum)
571{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200572 if (s->be->errmsg[msgnum].str)
573 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100574 else if (s->fe->errmsg[msgnum].str)
575 return &s->fe->errmsg[msgnum];
576 else
577 return &http_err_chunks[msgnum];
578}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579
Willy Tarreau53b6c742006-12-17 13:37:46 +0100580/*
581 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
582 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
583 */
584static http_meth_t find_http_meth(const char *str, const int len)
585{
586 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588
589 m = ((unsigned)*str - 'A');
590
591 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100592 for (h = http_methods[m]; h->len > 0; h++) {
593 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100594 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100595 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100596 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100597 };
598 return HTTP_METH_OTHER;
599 }
600 return HTTP_METH_NONE;
601
602}
603
Willy Tarreau21d2af32008-02-14 20:25:24 +0100604/* Parse the URI from the given transaction (which is assumed to be in request
605 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
606 * It is returned otherwise.
607 */
608static char *
609http_get_path(struct http_txn *txn)
610{
611 char *ptr, *end;
612
613 ptr = txn->req.sol + txn->req.sl.rq.u;
614 end = ptr + txn->req.sl.rq.u_l;
615
616 if (ptr >= end)
617 return NULL;
618
619 /* RFC2616, par. 5.1.2 :
620 * Request-URI = "*" | absuri | abspath | authority
621 */
622
623 if (*ptr == '*')
624 return NULL;
625
626 if (isalpha((unsigned char)*ptr)) {
627 /* this is a scheme as described by RFC3986, par. 3.1 */
628 ptr++;
629 while (ptr < end &&
630 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
631 ptr++;
632 /* skip '://' */
633 if (ptr == end || *ptr++ != ':')
634 return NULL;
635 if (ptr == end || *ptr++ != '/')
636 return NULL;
637 if (ptr == end || *ptr++ != '/')
638 return NULL;
639 }
640 /* skip [user[:passwd]@]host[:[port]] */
641
642 while (ptr < end && *ptr != '/')
643 ptr++;
644
645 if (ptr == end)
646 return NULL;
647
648 /* OK, we got the '/' ! */
649 return ptr;
650}
651
Willy Tarreauefb453c2008-10-26 20:49:47 +0100652
653/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
654 * Other input states are simply ignored.
Willy Tarreaucff64112008-11-03 06:26:53 +0100655 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100656 * Flags must have previously been updated for timeouts and other conditions.
657 */
658void sess_update_stream_int(struct session *s, struct stream_interface *si)
659{
660 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
661 now_ms, __FUNCTION__,
662 s,
663 s->req, s->rep,
664 s->req->rex, s->rep->wex,
665 s->req->flags, s->rep->flags,
666 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
667
668 if (si->state == SI_ST_ASS) {
669 /* Server assigned to connection request, we have to try to connect now */
670 int conn_err;
671
672 conn_err = connect_server(s);
673 if (conn_err == SN_ERR_NONE) {
674 /* state = SI_ST_CON now */
675 return;
676 }
677
678 /* We have received a synchronous error. We might have to
679 * abort, retry immediately or redispatch.
680 */
681 if (conn_err == SN_ERR_INTERNAL) {
682 if (!si->err_type) {
683 si->err_type = SI_ET_CONN_OTHER;
684 si->err_loc = s->srv;
685 }
686
687 if (s->srv)
688 s->srv->cum_sess++;
689 if (s->srv)
690 s->srv->failed_conns++;
691 s->be->failed_conns++;
692
693 /* release other sessions waiting for this server */
694 if (may_dequeue_tasks(s->srv, s->be))
695 process_srv_queue(s->srv);
696
697 /* Failed and not retryable. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100698 si->shutr(si);
699 si->shutw(si);
Willy Tarreaucff64112008-11-03 06:26:53 +0100700 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701
702 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100703
704 /* no session was ever accounted for this server */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 si->state = SI_ST_CLO;
706 return;
707 }
708
Willy Tarreaucff64112008-11-03 06:26:53 +0100709 /* We are facing a retryable error, but we don't want to run a
710 * turn-around now, as the problem is likely a source port
711 * allocation problem, so we want to retry now.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100712 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100713 si->state = SI_ST_CER;
714 si->flags &= ~SI_FL_ERR;
715 sess_update_st_cer(s, si);
716 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717 return;
718 }
719 else if (si->state == SI_ST_QUE) {
720 /* connection request was queued, check for any update */
721 if (!s->pend_pos) {
722 /* The connection is not in the queue anymore. Either
723 * we have a server connection slot available and we
724 * go directly to the assigned state, or we need to
725 * load-balance first and go to the INI state.
726 */
727 si->exp = TICK_ETERNITY;
728 if (unlikely(!(s->flags & SN_ASSIGNED)))
729 si->state = SI_ST_REQ;
730 else {
731 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
732 si->state = SI_ST_ASS;
733 }
734 return;
735 }
736
737 /* Connection request still in queue... */
738 if (si->flags & SI_FL_EXP) {
739 /* ... and timeout expired */
740 si->exp = TICK_ETERNITY;
741 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
742 if (s->srv)
743 s->srv->failed_conns++;
744 s->be->failed_conns++;
Willy Tarreau99126c32008-11-27 10:30:51 +0100745 si->shutr(si);
746 si->shutw(si);
Willy Tarreaucff64112008-11-03 06:26:53 +0100747 si->ob->flags |= BF_WRITE_TIMEOUT;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100748 if (!si->err_type)
749 si->err_type = SI_ET_QUEUE_TO;
750 si->state = SI_ST_CLO;
751 return;
752 }
753
754 /* Connection remains in queue, check if we have to abort it */
Willy Tarreaucff64112008-11-03 06:26:53 +0100755 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
756 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
757 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 /* give up */
759 si->exp = TICK_ETERNITY;
760 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau99126c32008-11-27 10:30:51 +0100761 si->shutr(si);
762 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 si->err_type |= SI_ET_QUEUE_ABRT;
764 si->state = SI_ST_CLO;
765 return;
766 }
767
768 /* Nothing changed */
769 return;
770 }
771 else if (si->state == SI_ST_TAR) {
772 /* Connection request might be aborted */
Willy Tarreaucff64112008-11-03 06:26:53 +0100773 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
774 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
775 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 /* give up */
777 si->exp = TICK_ETERNITY;
Willy Tarreau99126c32008-11-27 10:30:51 +0100778 si->shutr(si);
779 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780 si->err_type |= SI_ET_CONN_ABRT;
781 si->state = SI_ST_CLO;
782 return;
783 }
784
785 if (!(si->flags & SI_FL_EXP))
786 return; /* still in turn-around */
787
788 si->exp = TICK_ETERNITY;
789
790 /* we keep trying on the same server as long as the session is
791 * marked "assigned".
792 * FIXME: Should we force a redispatch attempt when the server is down ?
793 */
794 if (s->flags & SN_ASSIGNED)
795 si->state = SI_ST_ASS;
796 else
797 si->state = SI_ST_REQ;
798 return;
799 }
800}
801
802
803/* Returns a 302 for a redirectable request. This may only be called just after
804 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
805 * left unchanged and will follow normal proxy processing.
806 */
807static void perform_http_redirect(struct session *s, struct stream_interface *si)
808{
809 struct http_txn *txn;
810 struct chunk rdr;
811 char *path;
812 int len;
813
814 /* 1: create the response header */
815 rdr.len = strlen(HTTP_302);
816 rdr.str = trash;
817 memcpy(rdr.str, HTTP_302, rdr.len);
818
819 /* 2: add the server's prefix */
820 if (rdr.len + s->srv->rdr_len > sizeof(trash))
821 return;
822
823 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
824 rdr.len += s->srv->rdr_len;
825
826 /* 3: add the request URI */
827 txn = &s->txn;
828 path = http_get_path(txn);
829 if (!path)
830 return;
831
832 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
833 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
834 return;
835
836 memcpy(rdr.str + rdr.len, path, len);
837 rdr.len += len;
838 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
839 rdr.len += 4;
840
841 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100842 si->shutr(si);
843 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 si->err_type = SI_ET_NONE;
845 si->err_loc = NULL;
846 si->state = SI_ST_CLO;
847
848 /* send the message */
849 srv_close_with_err(s, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
850
851 /* FIXME: we should increase a counter of redirects per server and per backend. */
852 if (s->srv)
853 s->srv->cum_sess++;
854}
855
856
857/* This function initiates a server connection request on a stream interface
858 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
859 * indicating that a server has been assigned. It may also return SI_ST_QUE,
860 * or SI_ST_CLO upon error.
861 */
862static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
863 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
864 now_ms, __FUNCTION__,
865 s,
866 s->req, s->rep,
867 s->req->rex, s->rep->wex,
868 s->req->flags, s->rep->flags,
869 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
870
871 if (si->state != SI_ST_REQ)
872 return;
873
874 /* Try to assign a server */
875 if (srv_redispatch_connect(s) != 0) {
876 /* We did not get a server. Either we queued the
877 * connection request, or we encountered an error.
878 */
879 if (si->state == SI_ST_QUE)
880 return;
881
882 /* we did not get any server, let's check the cause */
Willy Tarreau99126c32008-11-27 10:30:51 +0100883 si->shutr(si);
884 si->shutw(si);
Willy Tarreaucff64112008-11-03 06:26:53 +0100885 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100886 if (!si->err_type)
887 si->err_type = SI_ET_CONN_OTHER;
888 si->state = SI_ST_CLO;
889 return;
890 }
891
892 /* The server is assigned */
893 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
894 si->state = SI_ST_ASS;
895}
896
897
898/* Return the error message corresponding to err_type. It is assumed
899 * that the server side is closed. Note that err_type is actually a
900 * bitmask, where almost only aborts may be cumulated with other
901 * values. We consider that aborted operations are more important
902 * than timeouts or errors due to the fact that nobody else in the
903 * logs might explain incomplete retries. All others should avoid
904 * being cumulated. It should normally not be possible to have multiple
905 * aborts at once, but just in case, the first one in sequence is reported.
906 */
907void return_srv_error(struct session *s, int err_type)
908{
909 s->req->wex = TICK_ETERNITY;
910
911 if (err_type & SI_ET_QUEUE_ABRT)
912 srv_close_with_err(s, SN_ERR_CLICL, SN_FINST_Q,
913 503, error_message(s, HTTP_ERR_503));
914 else if (err_type & SI_ET_CONN_ABRT)
915 srv_close_with_err(s, SN_ERR_CLICL, SN_FINST_C,
916 503, error_message(s, HTTP_ERR_503));
917 else if (err_type & SI_ET_QUEUE_TO)
918 srv_close_with_err(s, SN_ERR_SRVTO, SN_FINST_Q,
919 503, error_message(s, HTTP_ERR_503));
920 else if (err_type & SI_ET_QUEUE_ERR)
921 srv_close_with_err(s, SN_ERR_SRVCL, SN_FINST_Q,
922 503, error_message(s, HTTP_ERR_503));
923 else if (err_type & SI_ET_CONN_TO)
924 srv_close_with_err(s, SN_ERR_SRVTO, SN_FINST_C,
925 503, error_message(s, HTTP_ERR_503));
926 else if (err_type & SI_ET_CONN_ERR)
927 srv_close_with_err(s, SN_ERR_SRVCL, SN_FINST_C,
928 503, error_message(s, HTTP_ERR_503));
929 else /* SI_ET_CONN_OTHER and others */
930 srv_close_with_err(s, SN_ERR_INTERNAL, SN_FINST_C,
931 500, error_message(s, HTTP_ERR_500));
932}
933
Willy Tarreaudafde432008-08-17 01:00:46 +0200934/* Processes the client, server, request and response jobs of a session task,
935 * then puts it back to the wait queue in a clean state, or cleans up its
936 * resources if it must be deleted. Returns in <next> the date the task wants
937 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
938 * nothing too many times, the request and response buffers flags are monitored
939 * and each function is called only if at least another function has changed at
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200940 * least one flag it is interested in.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200942void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943{
944 struct session *s = t->context;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200945 int resync;
946 unsigned int rqf_cli, rpf_cli;
947 unsigned int rqf_srv, rpf_srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948
Willy Tarreaucff64112008-11-03 06:26:53 +0100949 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
950 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951
Willy Tarreaucff64112008-11-03 06:26:53 +0100952 /* 1a: Check for low level timeouts if needed. We just set a flag on
Willy Tarreau4351b3a2008-11-12 01:51:41 +0100953 * stream interfaces when their timeouts have expired.
Willy Tarreaucff64112008-11-03 06:26:53 +0100954 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100955 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreaucff64112008-11-03 06:26:53 +0100956 stream_int_check_timeouts(&s->si[0]);
957 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaucff64112008-11-03 06:26:53 +0100958 }
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200959
Willy Tarreaucff64112008-11-03 06:26:53 +0100960 /* 1b: check for low-level errors reported at the stream interface.
961 * First we check if it's a retryable error (in which case we don't
962 * want to tell the buffer). Otherwise we report the error one level
963 * upper by setting flags into the buffers. Note that the side towards
964 * the client cannot have connect (hence retryable) errors.
965 */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100966 if (s->si[0].state == SI_ST_EST) {
967 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100968 s->si[0].state = SI_ST_DIS;
Willy Tarreaucff64112008-11-03 06:26:53 +0100969 fd_delete(s->si[0].fd);
970 stream_int_report_error(&s->si[0]);
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200971 }
Willy Tarreaucff64112008-11-03 06:26:53 +0100972 }
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200973
Willy Tarreaucff64112008-11-03 06:26:53 +0100974 if (s->si[1].state == SI_ST_EST) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100975 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100976 s->si[1].state = SI_ST_DIS;
Willy Tarreaucff64112008-11-03 06:26:53 +0100977 fd_delete(s->si[1].fd);
978 stream_int_report_error(&s->si[1]);
Willy Tarreau4351b3a2008-11-12 01:51:41 +0100979 /////////// FIXME: the following must move somewhere else
Willy Tarreau1e62de62008-11-11 20:20:02 +0100980 s->be->failed_resp++;
981 if (s->srv)
982 s->srv->failed_resp++;
Willy Tarreau4ffd51a2008-08-30 13:36:43 +0200983 }
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200984 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100985 else if (s->si[1].state >= SI_ST_QUE && s->si[1].state <= SI_ST_CON) {
Willy Tarreaucff64112008-11-03 06:26:53 +0100986 /* Maybe we were trying to establish a connection on the server side ? */
Willy Tarreau4351b3a2008-11-12 01:51:41 +0100987 if (s->si[1].state == SI_ST_CON) {
988 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
989 sess_update_st_cer(s, &s->si[1]);
990 else if (s->si[1].state == SI_ST_EST)
991 sess_establish(s, &s->si[1]);
992 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100993
Willy Tarreaucff64112008-11-03 06:26:53 +0100994 /* now try to complete any initiated connection setup */
995 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
996 do {
997 /* nb: step 1 might switch from QUE to ASS, but we first want
998 * to give a chance to step 2 to perform a redirect if needed.
999 */
1000 sess_update_stream_int(s, &s->si[1]);
1001 if (s->si[1].state == SI_ST_REQ)
1002 sess_prepare_conn_req(s, &s->si[1]);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
Willy Tarreau4351b3a2008-11-12 01:51:41 +01001004 ///// FIXME: redirect should be handled later
Willy Tarreaucff64112008-11-03 06:26:53 +01001005 if (s->si[1].state == SI_ST_ASS && s->srv &&
1006 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1007 perform_http_redirect(s, &s->si[1]);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001008
Willy Tarreaucff64112008-11-03 06:26:53 +01001009 } while (s->si[1].state == SI_ST_ASS);
1010 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 }
1012
Willy Tarreau4351b3a2008-11-12 01:51:41 +01001013 /////// FIXME: do that later
1014 /* FIXME: we might have got errors above, and we should process them below */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01001015 if ((s->si[1].state == SI_ST_DIS || s->si[1].state == SI_ST_CLO) &&
1016 s->si[1].prev_state != SI_ST_CLO && s->si[1].err_type != SI_ET_NONE)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 return_srv_error(s, s->si[1].err_type);
1018
Willy Tarreaucff64112008-11-03 06:26:53 +01001019
Willy Tarreau4351b3a2008-11-12 01:51:41 +01001020 /* 1a: Check for low level timeouts if needed. We just set a flag on
1021 * buffers and/or stream interfaces when their timeouts have expired.
1022 */
1023 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1024 buffer_check_timeouts(s->req);
1025 buffer_check_timeouts(s->rep);
1026 }
1027
Willy Tarreaucff64112008-11-03 06:26:53 +01001028 /* 1c: Manage buffer timeouts. */
1029 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
1030 if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001031 //buffer_shutr(s->req);
Willy Tarreaucff64112008-11-03 06:26:53 +01001032 s->req->cons->shutr(s->req->prod);
1033 }
1034 if (s->req->flags & BF_WRITE_TIMEOUT) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001035 //buffer_shutw(s->req);
Willy Tarreaucff64112008-11-03 06:26:53 +01001036 s->req->cons->shutw(s->req->cons);
1037 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001038 }
1039
Willy Tarreaucff64112008-11-03 06:26:53 +01001040 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
1041 if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001042 //buffer_shutr(s->rep);
Willy Tarreaucff64112008-11-03 06:26:53 +01001043 s->rep->cons->shutr(s->rep->prod);
1044 }
1045 if (s->rep->flags & BF_WRITE_TIMEOUT) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001046 //buffer_shutw(s->rep);
Willy Tarreaucff64112008-11-03 06:26:53 +01001047 s->rep->cons->shutw(s->rep->cons);
1048 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001049 }
1050
Willy Tarreaud7704b52008-09-04 11:51:16 +02001051 /* 2: Check if we need to close the write side. This can only happen
Willy Tarreau48adac52008-08-30 04:58:38 +02001052 * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear
1053 * from low level, and neither HIJACK nor SHUTW can disappear from low
1054 * level.
1055 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056 if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) ||
1057 unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001058 //buffer_shutw(s->req);
Willy Tarreau48adac52008-08-30 04:58:38 +02001059 s->req->cons->shutw(s->req->cons);
1060 }
1061
Willy Tarreau1e62de62008-11-11 20:20:02 +01001062 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) {
1063 /* write closed on server side, let's forward it to the client */
Willy Tarreau99126c32008-11-27 10:30:51 +01001064 //buffer_shutr_now(s->req);
Willy Tarreau1e62de62008-11-11 20:20:02 +01001065 s->req->prod->shutr(s->req->prod);
1066 }
1067
Willy Tarreauefb453c2008-10-26 20:49:47 +01001068 if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) ||
1069 unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) {
Willy Tarreau99126c32008-11-27 10:30:51 +01001070 //buffer_shutw(s->rep);
Willy Tarreau48adac52008-08-30 04:58:38 +02001071 s->rep->cons->shutw(s->rep->cons);
1072 }
1073
Willy Tarreau1e62de62008-11-11 20:20:02 +01001074 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) {
1075 /* write closed on client side, let's forward it to the server */
Willy Tarreau99126c32008-11-27 10:30:51 +01001076 //buffer_shutr_now(s->rep);
Willy Tarreau1e62de62008-11-11 20:20:02 +01001077 s->rep->prod->shutr(s->rep->prod);
1078 }
1079
Willy Tarreaud7704b52008-09-04 11:51:16 +02001080 /* 3: When a server-side connection is released, we have to
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001081 * count it and check for pending connections on this server.
1082 */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01001083 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1084 s->req->cons->state = SI_ST_CLO;
1085 if (s->srv) {
1086 if (s->flags & SN_CURR_SESS) {
1087 s->flags &= ~SN_CURR_SESS;
1088 s->srv->cur_sess--;
1089 }
1090 sess_change_server(s, NULL);
1091 if (may_dequeue_tasks(s->srv, s->be))
1092 process_srv_queue(s->srv);
1093 }
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001094 }
1095
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01001096 /* nothing special to be done on client side */
1097 if (unlikely(s->req->prod->state == SI_ST_DIS))
1098 s->req->prod->state = SI_ST_CLO;
1099
1100 /*
1101 * Note: all transient states (REQ, CER, DIS) have been eliminated at
1102 * this point.
1103 */
1104
1105
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001106 /* Dirty trick: force one first pass everywhere */
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001107 rqf_cli = rqf_srv = ~s->req->flags;
1108 rpf_cli = rpf_srv = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +02001109
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 /* well, SI_ST_EST state is already handled properly */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001111 if (s->req->prod->state == SI_ST_EST) {
1112 rqf_cli = s->req->flags;
1113 rpf_cli = s->rep->flags;
1114 }
1115
1116 if (s->req->cons->state == SI_ST_EST) {
1117 rqf_srv = s->req->flags;
1118 rpf_srv = s->rep->flags;
1119 }
1120
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121 do {
Willy Tarreauefb453c2008-10-26 20:49:47 +01001122 DPRINTF(stderr,"[%u] %s: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001123 now_ms, __FUNCTION__,
1124 t,
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125 s, s->flags,
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001126 s->req, s->rep,
1127 s->req->rex, s->rep->wex,
1128 s->req->flags, s->rep->flags,
Willy Tarreauefb453c2008-10-26 20:49:47 +01001129 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1130 s->rep->cons->err_type, s->req->cons->err_type,
1131 s->conn_retries);
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001132
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001133 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +02001134
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001135 /* Maybe resync client FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001136 if (s->rep->cons->state != SI_ST_CLO) {
1137 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
1138 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +01001139
1140 if (!(s->rep->flags & BF_SHUTW))
1141 buffer_check_shutw(s->rep);
1142 if (!(s->req->flags & BF_SHUTR))
1143 buffer_check_shutr(s->req);
1144
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001145 rqf_cli = s->req->flags;
1146 rpf_cli = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +02001147 }
Willy Tarreaudafde432008-08-17 01:00:46 +02001148 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001149
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001150 /* Maybe resync server FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001151 if (s->req->cons->state != SI_ST_CLO) {
1152 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
1153 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreaucff64112008-11-03 06:26:53 +01001154 if (s->req->cons->state == SI_ST_INI &&
1155 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA) {
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156 s->req->cons->state = SI_ST_REQ;
1157 do {
1158 sess_prepare_conn_req(s, &s->si[1]);
1159 if (s->si[1].state != SI_ST_ASS)
1160 break;
1161 if (s->srv && s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1162 perform_http_redirect(s, &s->si[1]);
1163 sess_update_stream_int(s, &s->si[1]);
1164 } while (s->si[1].state == SI_ST_REQ || s->si[1].state == SI_ST_ASS);
1165
1166 /* FIXME: how to process this type of errors ? */
1167 if (s->si[1].state == SI_ST_CLO && s->si[1].err_type != SI_ET_NONE)
1168 return_srv_error(s, s->si[1].err_type);
1169 resync = 1;
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001170 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001171
1172 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001173 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +02001174 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +02001175 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +02001176 /* We want to force the connection to the server to close,
1177 * and the server has begun to respond. That's the right
1178 * time.
1179 */
1180 buffer_shutw_now(s->req);
1181 }
1182
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +01001183 if (!(s->req->flags & BF_SHUTW))
1184 buffer_check_shutw(s->req);
1185 if (!(s->rep->flags & BF_SHUTR))
1186 buffer_check_shutr(s->rep);
Willy Tarreau376580a2008-08-27 18:52:22 +02001187
1188 /* When a server-side connection is released, we have to
1189 * count it and check for pending connections on this server.
1190 */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01001191 if (s->req->cons->state == SI_ST_DIS) {
1192 s->req->cons->state = SI_ST_CLO;
Willy Tarreau4351b3a2008-11-12 01:51:41 +01001193 if (s->srv) {
1194 if (s->flags & SN_CURR_SESS) {
1195 s->flags &= ~SN_CURR_SESS;
1196 s->srv->cur_sess--;
1197 }
Willy Tarreau376580a2008-08-27 18:52:22 +02001198 sess_change_server(s, NULL);
1199 if (may_dequeue_tasks(s->srv, s->be))
1200 process_srv_queue(s->srv);
1201 }
1202 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001203 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001204 rqf_srv = s->req->flags;
1205 rpf_srv = s->rep->flags;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001206 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001207 }
1208
1209 /* we may have to resync because of pending connections */
1210 if (resync)
1211 continue;
1212
1213 /**** Process layer 7 below ****/
1214
1215 /* Analyse request */
1216 if (s->req->flags & BF_MASK_ANALYSER) {
1217 unsigned int flags = s->req->flags;
1218
1219 if (s->req->prod->state >= SI_ST_EST) {
1220 /* it's up to the analysers to reset write_ena */
1221 buffer_write_ena(s->req);
1222 if (s->req->analysers)
1223 process_request(s);
1224 }
1225 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1226 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1227 if (s->req->flags != flags)
1228 resync = 1;
1229 }
1230
1231 /* Analyse response */
1232 if (unlikely(s->rep->flags & BF_HIJACK)) {
1233 /* In inject mode, we wake up everytime something has
1234 * happened on the write side of the buffer.
1235 */
1236 unsigned int flags = s->rep->flags;
1237
1238 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1239 !(s->rep->flags & BF_FULL)) {
1240 produce_content(s);
1241 }
1242 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1243 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1244 if (s->rep->flags != flags)
1245 resync = 1;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001246 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001247 else if (s->rep->flags & BF_MASK_ANALYSER) {
1248 unsigned int flags = s->rep->flags;
1249
1250 if (s->rep->prod->state >= SI_ST_EST) {
1251 /* it's up to the analysers to reset write_ena */
1252 buffer_write_ena(s->rep);
1253 if (s->rep->analysers)
1254 process_response(s);
1255 }
1256 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1257 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1258 if (s->rep->flags != flags)
1259 resync = 1;
1260 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001261
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001262 /* For the moment, we need to clean the client and server flags that
1263 * have vanished. This is just a temporary measure though.
1264 */
1265 rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1266 rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1267 rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1268 rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaudafde432008-08-17 01:00:46 +02001269 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270
Willy Tarreaua37095b2008-09-03 11:37:47 +02001271 /* This is needed only when debugging is enabled, to indicate
1272 * client-side or server-side close. Please note that in the unlikely
1273 * event where both sides would close at once, the sequence is reported
1274 * on the server side first.
1275 */
1276 if (unlikely((global.mode & MODE_DEBUG) &&
1277 (!(global.mode & MODE_QUIET) ||
1278 (global.mode & MODE_VERBOSE)))) {
1279 int len;
1280
1281 if (s->si[1].state == SI_ST_CLO &&
1282 s->si[1].prev_state == SI_ST_EST) {
1283 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1284 s->uniq_id, s->be->id,
1285 (unsigned short)s->si[0].fd,
1286 (unsigned short)s->si[1].fd);
1287 write(1, trash, len);
1288 }
1289
1290 if (s->si[0].state == SI_ST_CLO &&
1291 s->si[0].prev_state == SI_ST_EST) {
1292 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1293 s->uniq_id, s->be->id,
1294 (unsigned short)s->si[0].fd,
1295 (unsigned short)s->si[1].fd);
1296 write(1, trash, len);
1297 }
1298 }
1299
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001300 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreau8bfa4262008-11-27 09:25:45 +01001301 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001302
1303 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1304 session_process_counters(s);
1305
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001306 if (s->rep->cons->state == SI_ST_EST)
1307 stream_sock_data_finish(s->rep->cons->fd);
1308
1309 if (s->req->cons->state == SI_ST_EST)
1310 stream_sock_data_finish(s->req->cons->fd);
1311
Willy Tarreau9a2d1542008-08-30 12:31:07 +02001312 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1313 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001314 s->si[0].prev_state = s->si[0].state;
1315 s->si[1].prev_state = s->si[1].state;
Willy Tarreaucff64112008-11-03 06:26:53 +01001316 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317
Willy Tarreau7f875f62008-08-11 17:35:01 +02001318 /* Trick: if a request is being waiting for the server to respond,
1319 * and if we know the server can timeout, we don't want the timeout
1320 * to expire on the client side first, but we're still interested
1321 * in passing data from the client to the server (eg: POST). Thus,
1322 * we can cancel the client's request timeout if the server's
1323 * request timeout is set and the server has not yet sent a response.
1324 */
1325
Willy Tarreau3da77c52008-08-29 09:58:42 +02001326 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +02001327 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +02001328 s->req->rex = TICK_ETERNITY;
1329
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001330 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1331 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +02001332 if (s->req->analysers)
1333 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001334
Willy Tarreau35374672008-09-03 18:11:02 +02001335 if (s->si[0].exp)
1336 t->expire = tick_first(t->expire, s->si[0].exp);
1337
1338 if (s->si[1].exp)
1339 t->expire = tick_first(t->expire, s->si[1].exp);
1340
Willy Tarreaucb651252008-08-29 13:57:30 +02001341#ifdef DEBUG_FULL
Willy Tarreauefb453c2008-10-26 20:49:47 +01001342 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u, cs=%d, ss=%d\n",
1343 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex, s->si[0].state, s->si[1].state);
Willy Tarreaucb651252008-08-29 13:57:30 +02001344#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345 /* restore t to its place in the task list */
1346 task_queue(t);
1347
Willy Tarreaua7c52762008-08-16 18:40:18 +02001348#ifdef DEBUG_DEV
1349 /* this may only happen when no timeout is set or in case of an FSM bug */
1350 if (!t->expire)
1351 ABORT_NOW();
1352#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +02001353 *next = t->expire;
1354 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001355 }
1356
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001357 s->fe->feconn--;
1358 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001359 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001360 actconn--;
1361
Willy Tarreauf41d4b12007-04-28 23:26:14 +02001362 if (unlikely((global.mode & MODE_DEBUG) &&
1363 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001364 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001365 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001366 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001367 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001368 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369 write(1, trash, len);
1370 }
1371
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001372 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001373 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001374
1375 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +02001376 if (s->logs.logwait &&
1377 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +02001378 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
1379 if (s->fe->to_log & LW_REQ)
1380 http_sess_log(s);
1381 else
1382 tcp_sess_log(s);
1383 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384
1385 /* the task MUST not be in the run queue anymore */
1386 task_delete(t);
1387 session_free(s);
1388 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001389 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001390}
1391
1392
Willy Tarreau42250582007-04-01 01:30:43 +02001393extern const char sess_term_cond[8];
1394extern const char sess_fin_state[8];
1395extern const char *monthname[12];
1396const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
1397const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
1398 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
1399 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001400struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +02001401struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402
Willy Tarreau42250582007-04-01 01:30:43 +02001403/*
1404 * send a log for the session when we have enough info about it.
1405 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 */
Willy Tarreau42250582007-04-01 01:30:43 +02001407static void http_sess_log(struct session *s)
1408{
1409 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1410 struct proxy *fe = s->fe;
1411 struct proxy *be = s->be;
1412 struct proxy *prx_log;
1413 struct http_txn *txn = &s->txn;
1414 int tolog;
1415 char *uri, *h;
1416 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001417 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001418 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001419 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001420 int hdr;
1421
1422 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1423 return;
1424 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425
Willy Tarreau42250582007-04-01 01:30:43 +02001426 if (s->cli_addr.ss_family == AF_INET)
1427 inet_ntop(AF_INET,
1428 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1429 pn, sizeof(pn));
1430 else
1431 inet_ntop(AF_INET6,
1432 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1433 pn, sizeof(pn));
1434
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001435 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001436
1437 /* FIXME: let's limit ourselves to frontend logging for now. */
1438 tolog = fe->to_log;
1439
1440 h = tmpline;
1441 if (fe->to_log & LW_REQHDR &&
1442 txn->req.cap &&
1443 (h < tmpline + sizeof(tmpline) - 10)) {
1444 *(h++) = ' ';
1445 *(h++) = '{';
1446 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1447 if (hdr)
1448 *(h++) = '|';
1449 if (txn->req.cap[hdr] != NULL)
1450 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1451 '#', hdr_encode_map, txn->req.cap[hdr]);
1452 }
1453 *(h++) = '}';
1454 }
1455
1456 if (fe->to_log & LW_RSPHDR &&
1457 txn->rsp.cap &&
1458 (h < tmpline + sizeof(tmpline) - 7)) {
1459 *(h++) = ' ';
1460 *(h++) = '{';
1461 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1462 if (hdr)
1463 *(h++) = '|';
1464 if (txn->rsp.cap[hdr] != NULL)
1465 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1466 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1467 }
1468 *(h++) = '}';
1469 }
1470
1471 if (h < tmpline + sizeof(tmpline) - 4) {
1472 *(h++) = ' ';
1473 *(h++) = '"';
1474 uri = txn->uri ? txn->uri : "<BADREQ>";
1475 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1476 '#', url_encode_map, uri);
1477 *(h++) = '"';
1478 }
1479 *h = '\0';
1480
1481 svid = (tolog & LW_SVID) ?
1482 (s->data_source != DATA_SRC_STATS) ?
1483 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1484
Willy Tarreau70089872008-06-13 21:12:51 +02001485 t_request = -1;
1486 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1487 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1488
Willy Tarreau42250582007-04-01 01:30:43 +02001489 send_log(prx_log, LOG_INFO,
1490 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
1491 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001492 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001493 pn,
1494 (s->cli_addr.ss_family == AF_INET) ?
1495 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1496 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001497 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001498 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001499 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001500 t_request,
1501 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001502 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1503 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1504 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1505 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001506 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001507 txn->cli_cookie ? txn->cli_cookie : "-",
1508 txn->srv_cookie ? txn->srv_cookie : "-",
1509 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1510 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1511 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1512 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1513 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001514 (s->flags & SN_REDISP)?"+":"",
1515 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001516 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1517
1518 s->logs.logwait = 0;
1519}
1520
Willy Tarreau117f59e2007-03-04 18:17:17 +01001521
1522/*
1523 * Capture headers from message starting at <som> according to header list
1524 * <cap_hdr>, and fill the <idx> structure appropriately.
1525 */
1526void capture_headers(char *som, struct hdr_idx *idx,
1527 char **cap, struct cap_hdr *cap_hdr)
1528{
1529 char *eol, *sol, *col, *sov;
1530 int cur_idx;
1531 struct cap_hdr *h;
1532 int len;
1533
1534 sol = som + hdr_idx_first_pos(idx);
1535 cur_idx = hdr_idx_first_idx(idx);
1536
1537 while (cur_idx) {
1538 eol = sol + idx->v[cur_idx].len;
1539
1540 col = sol;
1541 while (col < eol && *col != ':')
1542 col++;
1543
1544 sov = col + 1;
1545 while (sov < eol && http_is_lws[(unsigned char)*sov])
1546 sov++;
1547
1548 for (h = cap_hdr; h; h = h->next) {
1549 if ((h->namelen == col - sol) &&
1550 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1551 if (cap[h->index] == NULL)
1552 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001553 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001554
1555 if (cap[h->index] == NULL) {
1556 Alert("HTTP capture : out of memory.\n");
1557 continue;
1558 }
1559
1560 len = eol - sov;
1561 if (len > h->len)
1562 len = h->len;
1563
1564 memcpy(cap[h->index], sov, len);
1565 cap[h->index][len]=0;
1566 }
1567 }
1568 sol = eol + idx->v[cur_idx].cr + 1;
1569 cur_idx = idx->v[cur_idx].next;
1570 }
1571}
1572
1573
Willy Tarreau42250582007-04-01 01:30:43 +02001574/* either we find an LF at <ptr> or we jump to <bad>.
1575 */
1576#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1577
1578/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1579 * otherwise to <http_msg_ood> with <state> set to <st>.
1580 */
1581#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1582 ptr++; \
1583 if (likely(ptr < end)) \
1584 goto good; \
1585 else { \
1586 state = (st); \
1587 goto http_msg_ood; \
1588 } \
1589 } while (0)
1590
1591
Willy Tarreaubaaee002006-06-26 02:48:02 +02001592/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001593 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001594 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1595 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1596 * will give undefined results.
1597 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1598 * and that msg->sol points to the beginning of the response.
1599 * If a complete line is found (which implies that at least one CR or LF is
1600 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1601 * returned indicating an incomplete line (which does not mean that parts have
1602 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1603 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1604 * upon next call.
1605 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001606 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1608 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001609 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001610 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001611const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1612 unsigned int state, const char *ptr, const char *end,
1613 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001614{
1615 __label__
1616 http_msg_rpver,
1617 http_msg_rpver_sp,
1618 http_msg_rpcode,
1619 http_msg_rpcode_sp,
1620 http_msg_rpreason,
1621 http_msg_rpline_eol,
1622 http_msg_ood, /* out of data */
1623 http_msg_invalid;
1624
1625 switch (state) {
1626 http_msg_rpver:
1627 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001628 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001629 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1630
1631 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001632 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001633 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1634 }
1635 goto http_msg_invalid;
1636
1637 http_msg_rpver_sp:
1638 case HTTP_MSG_RPVER_SP:
1639 if (likely(!HTTP_IS_LWS(*ptr))) {
1640 msg->sl.st.c = ptr - msg_buf;
1641 goto http_msg_rpcode;
1642 }
1643 if (likely(HTTP_IS_SPHT(*ptr)))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1645 /* so it's a CR/LF, this is invalid */
1646 goto http_msg_invalid;
1647
1648 http_msg_rpcode:
1649 case HTTP_MSG_RPCODE:
1650 if (likely(!HTTP_IS_LWS(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1652
1653 if (likely(HTTP_IS_SPHT(*ptr))) {
1654 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1655 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1656 }
1657
1658 /* so it's a CR/LF, so there is no reason phrase */
1659 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1660 http_msg_rsp_reason:
1661 /* FIXME: should we support HTTP responses without any reason phrase ? */
1662 msg->sl.st.r = ptr - msg_buf;
1663 msg->sl.st.r_l = 0;
1664 goto http_msg_rpline_eol;
1665
1666 http_msg_rpcode_sp:
1667 case HTTP_MSG_RPCODE_SP:
1668 if (likely(!HTTP_IS_LWS(*ptr))) {
1669 msg->sl.st.r = ptr - msg_buf;
1670 goto http_msg_rpreason;
1671 }
1672 if (likely(HTTP_IS_SPHT(*ptr)))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1674 /* so it's a CR/LF, so there is no reason phrase */
1675 goto http_msg_rsp_reason;
1676
1677 http_msg_rpreason:
1678 case HTTP_MSG_RPREASON:
1679 if (likely(!HTTP_IS_CRLF(*ptr)))
1680 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1681 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1682 http_msg_rpline_eol:
1683 /* We have seen the end of line. Note that we do not
1684 * necessarily have the \n yet, but at least we know that we
1685 * have EITHER \r OR \n, otherwise the response would not be
1686 * complete. We can then record the response length and return
1687 * to the caller which will be able to register it.
1688 */
1689 msg->sl.st.l = ptr - msg->sol;
1690 return ptr;
1691
1692#ifdef DEBUG_FULL
1693 default:
1694 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1695 exit(1);
1696#endif
1697 }
1698
1699 http_msg_ood:
1700 /* out of data */
1701 if (ret_state)
1702 *ret_state = state;
1703 if (ret_ptr)
1704 *ret_ptr = (char *)ptr;
1705 return NULL;
1706
1707 http_msg_invalid:
1708 /* invalid message */
1709 if (ret_state)
1710 *ret_state = HTTP_MSG_ERROR;
1711 return NULL;
1712}
1713
1714
1715/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 * This function parses a request line between <ptr> and <end>, starting with
1717 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1718 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1719 * will give undefined results.
1720 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1721 * and that msg->sol points to the beginning of the request.
1722 * If a complete line is found (which implies that at least one CR or LF is
1723 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1724 * returned indicating an incomplete line (which does not mean that parts have
1725 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1726 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1727 * upon next call.
1728 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001729 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1731 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001732 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001733 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001734const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1735 unsigned int state, const char *ptr, const char *end,
1736 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001737{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 __label__
1739 http_msg_rqmeth,
1740 http_msg_rqmeth_sp,
1741 http_msg_rquri,
1742 http_msg_rquri_sp,
1743 http_msg_rqver,
1744 http_msg_rqline_eol,
1745 http_msg_ood, /* out of data */
1746 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 switch (state) {
1749 http_msg_rqmeth:
1750 case HTTP_MSG_RQMETH:
1751 if (likely(HTTP_IS_TOKEN(*ptr)))
1752 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 if (likely(HTTP_IS_CRLF(*ptr))) {
1760 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001761 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 http_msg_req09_uri:
1763 msg->sl.rq.u = ptr - msg_buf;
1764 http_msg_req09_uri_e:
1765 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1766 http_msg_req09_ver:
1767 msg->sl.rq.v = ptr - msg_buf;
1768 msg->sl.rq.v_l = 0;
1769 goto http_msg_rqline_eol;
1770 }
1771 goto http_msg_invalid;
1772
1773 http_msg_rqmeth_sp:
1774 case HTTP_MSG_RQMETH_SP:
1775 if (likely(!HTTP_IS_LWS(*ptr))) {
1776 msg->sl.rq.u = ptr - msg_buf;
1777 goto http_msg_rquri;
1778 }
1779 if (likely(HTTP_IS_SPHT(*ptr)))
1780 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1781 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1782 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 http_msg_rquri:
1785 case HTTP_MSG_RQURI:
1786 if (likely(!HTTP_IS_LWS(*ptr)))
1787 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 if (likely(HTTP_IS_SPHT(*ptr))) {
1790 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1791 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1792 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1795 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 http_msg_rquri_sp:
1798 case HTTP_MSG_RQURI_SP:
1799 if (likely(!HTTP_IS_LWS(*ptr))) {
1800 msg->sl.rq.v = ptr - msg_buf;
1801 goto http_msg_rqver;
1802 }
1803 if (likely(HTTP_IS_SPHT(*ptr)))
1804 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1805 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1806 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 http_msg_rqver:
1809 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001810 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001812
1813 if (likely(HTTP_IS_CRLF(*ptr))) {
1814 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1815 http_msg_rqline_eol:
1816 /* We have seen the end of line. Note that we do not
1817 * necessarily have the \n yet, but at least we know that we
1818 * have EITHER \r OR \n, otherwise the request would not be
1819 * complete. We can then record the request length and return
1820 * to the caller which will be able to register it.
1821 */
1822 msg->sl.rq.l = ptr - msg->sol;
1823 return ptr;
1824 }
1825
1826 /* neither an HTTP_VER token nor a CRLF */
1827 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829#ifdef DEBUG_FULL
1830 default:
1831 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1832 exit(1);
1833#endif
1834 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 http_msg_ood:
1837 /* out of data */
1838 if (ret_state)
1839 *ret_state = state;
1840 if (ret_ptr)
1841 *ret_ptr = (char *)ptr;
1842 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 http_msg_invalid:
1845 /* invalid message */
1846 if (ret_state)
1847 *ret_state = HTTP_MSG_ERROR;
1848 return NULL;
1849}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001850
1851
Willy Tarreau8973c702007-01-21 23:58:29 +01001852/*
1853 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001854 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001855 * when data are missing and recalled at the exact same location with no
1856 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001857 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1858 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001859 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1861{
1862 __label__
1863 http_msg_rqbefore,
1864 http_msg_rqbefore_cr,
1865 http_msg_rqmeth,
1866 http_msg_rqline_end,
1867 http_msg_hdr_first,
1868 http_msg_hdr_name,
1869 http_msg_hdr_l1_sp,
1870 http_msg_hdr_l1_lf,
1871 http_msg_hdr_l1_lws,
1872 http_msg_hdr_val,
1873 http_msg_hdr_l2_lf,
1874 http_msg_hdr_l2_lws,
1875 http_msg_complete_header,
1876 http_msg_last_lf,
1877 http_msg_ood, /* out of data */
1878 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001879
Willy Tarreaue69eada2008-01-27 00:34:10 +01001880 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001882
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001883 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 ptr = buf->lr;
1885 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 if (unlikely(ptr >= end))
1888 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001891 /*
1892 * First, states that are specific to the response only.
1893 * We check them first so that request and headers are
1894 * closer to each other (accessed more often).
1895 */
1896 http_msg_rpbefore:
1897 case HTTP_MSG_RPBEFORE:
1898 if (likely(HTTP_IS_TOKEN(*ptr))) {
1899 if (likely(ptr == buf->data)) {
1900 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001901 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001902 } else {
1903#if PARSE_PRESERVE_EMPTY_LINES
1904 /* only skip empty leading lines, don't remove them */
1905 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001906 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001907#else
1908 /* Remove empty leading lines, as recommended by
1909 * RFC2616. This takes a lot of time because we
1910 * must move all the buffer backwards, but this
1911 * is rarely needed. The method above will be
1912 * cleaner when we'll be able to start sending
1913 * the request from any place in the buffer.
1914 */
1915 buf->lr = ptr;
1916 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001917 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001918 msg->sol = buf->data;
1919 ptr = buf->data;
1920 end = buf->r;
1921#endif
1922 }
1923 hdr_idx_init(idx);
1924 state = HTTP_MSG_RPVER;
1925 goto http_msg_rpver;
1926 }
1927
1928 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1929 goto http_msg_invalid;
1930
1931 if (unlikely(*ptr == '\n'))
1932 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1933 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1934 /* stop here */
1935
1936 http_msg_rpbefore_cr:
1937 case HTTP_MSG_RPBEFORE_CR:
1938 EXPECT_LF_HERE(ptr, http_msg_invalid);
1939 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1940 /* stop here */
1941
1942 http_msg_rpver:
1943 case HTTP_MSG_RPVER:
1944 case HTTP_MSG_RPVER_SP:
1945 case HTTP_MSG_RPCODE:
1946 case HTTP_MSG_RPCODE_SP:
1947 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001948 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001949 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001950 if (unlikely(!ptr))
1951 return;
1952
1953 /* we have a full response and we know that we have either a CR
1954 * or an LF at <ptr>.
1955 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001956 //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 +01001957 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1958
1959 msg->sol = ptr;
1960 if (likely(*ptr == '\r'))
1961 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1962 goto http_msg_rpline_end;
1963
1964 http_msg_rpline_end:
1965 case HTTP_MSG_RPLINE_END:
1966 /* msg->sol must point to the first of CR or LF. */
1967 EXPECT_LF_HERE(ptr, http_msg_invalid);
1968 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1969 /* stop here */
1970
1971 /*
1972 * Second, states that are specific to the request only
1973 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 http_msg_rqbefore:
1975 case HTTP_MSG_RQBEFORE:
1976 if (likely(HTTP_IS_TOKEN(*ptr))) {
1977 if (likely(ptr == buf->data)) {
1978 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001979 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001980 } else {
1981#if PARSE_PRESERVE_EMPTY_LINES
1982 /* only skip empty leading lines, don't remove them */
1983 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001984 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001985#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001986 /* Remove empty leading lines, as recommended by
1987 * RFC2616. This takes a lot of time because we
1988 * must move all the buffer backwards, but this
1989 * is rarely needed. The method above will be
1990 * cleaner when we'll be able to start sending
1991 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001992 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001993 buf->lr = ptr;
1994 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001995 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001996 msg->sol = buf->data;
1997 ptr = buf->data;
1998 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001999#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002000 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01002001 /* we will need this when keep-alive will be supported
2002 hdr_idx_init(idx);
2003 */
Willy Tarreau8973c702007-01-21 23:58:29 +01002004 state = HTTP_MSG_RQMETH;
2005 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002008 if (unlikely(!HTTP_IS_CRLF(*ptr)))
2009 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002010
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002011 if (unlikely(*ptr == '\n'))
2012 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
2013 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01002014 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002015
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002016 http_msg_rqbefore_cr:
2017 case HTTP_MSG_RQBEFORE_CR:
2018 EXPECT_LF_HERE(ptr, http_msg_invalid);
2019 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01002020 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002022 http_msg_rqmeth:
2023 case HTTP_MSG_RQMETH:
2024 case HTTP_MSG_RQMETH_SP:
2025 case HTTP_MSG_RQURI:
2026 case HTTP_MSG_RQURI_SP:
2027 case HTTP_MSG_RQVER:
2028 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002029 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002030 if (unlikely(!ptr))
2031 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 /* we have a full request and we know that we have either a CR
2034 * or an LF at <ptr>.
2035 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002036 //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 +01002037 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002039 msg->sol = ptr;
2040 if (likely(*ptr == '\r'))
2041 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002042 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 http_msg_rqline_end:
2045 case HTTP_MSG_RQLINE_END:
2046 /* check for HTTP/0.9 request : no version information available.
2047 * msg->sol must point to the first of CR or LF.
2048 */
2049 if (unlikely(msg->sl.rq.v_l == 0))
2050 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002052 EXPECT_LF_HERE(ptr, http_msg_invalid);
2053 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01002054 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002055
Willy Tarreau8973c702007-01-21 23:58:29 +01002056 /*
2057 * Common states below
2058 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059 http_msg_hdr_first:
2060 case HTTP_MSG_HDR_FIRST:
2061 msg->sol = ptr;
2062 if (likely(!HTTP_IS_CRLF(*ptr))) {
2063 goto http_msg_hdr_name;
2064 }
2065
2066 if (likely(*ptr == '\r'))
2067 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2068 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002070 http_msg_hdr_name:
2071 case HTTP_MSG_HDR_NAME:
2072 /* assumes msg->sol points to the first char */
2073 if (likely(HTTP_IS_TOKEN(*ptr)))
2074 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002076 if (likely(*ptr == ':')) {
2077 msg->col = ptr - buf->data;
2078 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
2079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002083 http_msg_hdr_l1_sp:
2084 case HTTP_MSG_HDR_L1_SP:
2085 /* assumes msg->sol points to the first char and msg->col to the colon */
2086 if (likely(HTTP_IS_SPHT(*ptr)))
2087 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002089 /* header value can be basically anything except CR/LF */
2090 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002092 if (likely(!HTTP_IS_CRLF(*ptr))) {
2093 goto http_msg_hdr_val;
2094 }
2095
2096 if (likely(*ptr == '\r'))
2097 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
2098 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 http_msg_hdr_l1_lf:
2101 case HTTP_MSG_HDR_L1_LF:
2102 EXPECT_LF_HERE(ptr, http_msg_invalid);
2103 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002105 http_msg_hdr_l1_lws:
2106 case HTTP_MSG_HDR_L1_LWS:
2107 if (likely(HTTP_IS_SPHT(*ptr))) {
2108 /* replace HT,CR,LF with spaces */
2109 for (; buf->data+msg->sov < ptr; msg->sov++)
2110 buf->data[msg->sov] = ' ';
2111 goto http_msg_hdr_l1_sp;
2112 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002113 /* we had a header consisting only in spaces ! */
2114 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002115 goto http_msg_complete_header;
2116
2117 http_msg_hdr_val:
2118 case HTTP_MSG_HDR_VAL:
2119 /* assumes msg->sol points to the first char, msg->col to the
2120 * colon, and msg->sov points to the first character of the
2121 * value.
2122 */
2123 if (likely(!HTTP_IS_CRLF(*ptr)))
2124 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002126 msg->eol = ptr;
2127 /* Note: we could also copy eol into ->eoh so that we have the
2128 * real header end in case it ends with lots of LWS, but is this
2129 * really needed ?
2130 */
2131 if (likely(*ptr == '\r'))
2132 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2133 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002135 http_msg_hdr_l2_lf:
2136 case HTTP_MSG_HDR_L2_LF:
2137 EXPECT_LF_HERE(ptr, http_msg_invalid);
2138 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002140 http_msg_hdr_l2_lws:
2141 case HTTP_MSG_HDR_L2_LWS:
2142 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2143 /* LWS: replace HT,CR,LF with spaces */
2144 for (; msg->eol < ptr; msg->eol++)
2145 *msg->eol = ' ';
2146 goto http_msg_hdr_val;
2147 }
2148 http_msg_complete_header:
2149 /*
2150 * It was a new header, so the last one is finished.
2151 * Assumes msg->sol points to the first char, msg->col to the
2152 * colon, msg->sov points to the first character of the value
2153 * and msg->eol to the first CR or LF so we know how the line
2154 * ends. We insert last header into the index.
2155 */
2156 /*
2157 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
2158 write(2, msg->sol, msg->eol-msg->sol);
2159 fprintf(stderr,"\n");
2160 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002162 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
2163 idx, idx->tail) < 0))
2164 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002166 msg->sol = ptr;
2167 if (likely(!HTTP_IS_CRLF(*ptr))) {
2168 goto http_msg_hdr_name;
2169 }
2170
2171 if (likely(*ptr == '\r'))
2172 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2173 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002175 http_msg_last_lf:
2176 case HTTP_MSG_LAST_LF:
2177 /* Assumes msg->sol points to the first of either CR or LF */
2178 EXPECT_LF_HERE(ptr, http_msg_invalid);
2179 ptr++;
2180 buf->lr = ptr;
2181 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002182 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002183 return;
2184#ifdef DEBUG_FULL
2185 default:
2186 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2187 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002188#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002189 }
2190 http_msg_ood:
2191 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002192 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002193 buf->lr = ptr;
2194 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002196 http_msg_invalid:
2197 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002198 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002199 return;
2200}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002201
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002202/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02002203 * It normally returns zero, but may return 1 if it absolutely needs to be
2204 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002205 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002206 * functions. Its behaviour is rather simple :
2207 * - all enabled analysers are called in turn from the lower to the higher
2208 * bit.
2209 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02002210 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002211 * that unprocessed data will not be forwarded. But that probably depends on
2212 * the protocol. Generally it is not reset in case of errors.
2213 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02002214 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002215 * analysis).
2216 * - if an analyser thinks it has no added value anymore staying here, it must
2217 * reset its bit from the analysers flags in order not to be called anymore.
2218 *
2219 * In the future, analysers should be able to indicate that they want to be
2220 * called after XXX bytes have been received (or transfered), and the min of
2221 * all's wishes will be used to ring back (unless a special condition occurs).
2222 *
2223 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002224 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002225int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002226{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002227 struct buffer *req = t->req;
2228 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002229
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002230 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 +02002231 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002232 t,
2233 req,
2234 req->rex, req->wex,
2235 req->flags,
2236 req->l,
2237 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002238
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002239 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002240 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02002241 struct tcp_rule *rule;
2242 int partial;
2243
Willy Tarreauf495ddf2008-08-17 14:38:41 +02002244 /* We will abort if we encounter a read error. In theory, we
2245 * should not abort if we get a close, it might be valid,
2246 * although very unlikely. FIXME: we'll abort for now, this
2247 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02002248 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002249 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002250 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002251 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02002252 if (!(t->flags & SN_ERR_MASK))
2253 t->flags |= SN_ERR_CLICL;
2254 if (!(t->flags & SN_FINST_MASK))
2255 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002256 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002257 }
2258
2259 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002260 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002261 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002262 t->fe->failed_req++;
2263 if (!(t->flags & SN_ERR_MASK))
2264 t->flags |= SN_ERR_CLITO;
2265 if (!(t->flags & SN_FINST_MASK))
2266 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002267 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002268 }
2269
2270 /* We don't know whether we have enough data, so must proceed
2271 * this way :
2272 * - iterate through all rules in their declaration order
2273 * - if one rule returns MISS, it means the inspect delay is
2274 * not over yet, then return immediately, otherwise consider
2275 * it as a non-match.
2276 * - if one rule returns OK, then return OK
2277 * - if one rule returns KO, then return KO
2278 */
2279
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002280 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02002281 partial = 0;
2282 else
2283 partial = ACL_PARTIAL;
2284
2285 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
2286 int ret = ACL_PAT_PASS;
2287
2288 if (rule->cond) {
2289 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
2290 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002291 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002292 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002293 if (!tick_isset(req->analyse_exp))
2294 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02002295 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002296 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002297
Willy Tarreaub6866442008-07-14 23:54:42 +02002298 ret = acl_pass(ret);
2299 if (rule->cond->pol == ACL_COND_UNLESS)
2300 ret = !ret;
2301 }
2302
2303 if (ret) {
2304 /* we have a matching rule. */
2305 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002306 buffer_abort(req);
2307 buffer_abort(rep);
2308 //FIXME: this delete this
2309 //fd_delete(t->cli_fd);
2310 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002311 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002312 t->fe->failed_req++;
2313 if (!(t->flags & SN_ERR_MASK))
2314 t->flags |= SN_ERR_PRXCOND;
2315 if (!(t->flags & SN_FINST_MASK))
2316 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002317 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002318 }
2319 /* otherwise accept */
2320 break;
2321 }
2322 }
2323
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002324 /* if we get there, it means we have no rule which matches, or
2325 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02002326 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002327 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002328 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02002329 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002330
Willy Tarreau2df28e82008-08-17 15:20:19 +02002331 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002332 /*
2333 * Now parse the partial (or complete) lines.
2334 * We will check the request syntax, and also join multi-line
2335 * headers. An index of all the lines will be elaborated while
2336 * parsing.
2337 *
Willy Tarreau8973c702007-01-21 23:58:29 +01002338 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002339 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002340 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002341 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002342 * req->data + req->eoh = end of processed headers / start of current one
2343 * req->data + req->eol = end of current header or line (LF or CRLF)
2344 * req->lr = first non-visited byte
2345 * req->r = end of data
2346 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002348 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002349 struct http_txn *txn = &t->txn;
2350 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002351 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002352
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002353 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002354 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002356 /* 1: we might have to print this header in debug mode */
2357 if (unlikely((global.mode & MODE_DEBUG) &&
2358 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002359 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002360 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002361
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002362 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 eol = sol + msg->sl.rq.l;
2364 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002365
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002366 sol += hdr_idx_first_pos(&txn->hdr_idx);
2367 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002369 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002370 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002371 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002372 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2373 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002374 }
2375 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002376
Willy Tarreau58f10d72006-12-04 02:26:12 +01002377
2378 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002379 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002380 * If not so, we check the FD and buffer states before leaving.
2381 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002382 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2383 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384 *
2385 */
2386
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002387 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002388 /*
2389 * First, let's catch bad requests.
2390 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002391 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002392 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002393
2394 /* 1: Since we are in header mode, if there's no space
2395 * left for headers, we won't be able to free more
2396 * later, so the session will never terminate. We
2397 * must terminate it now.
2398 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002399 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002400 /* FIXME: check if URI is set and return Status
2401 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002402 */
Willy Tarreau06619262006-12-17 08:37:22 +01002403 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002404 }
2405
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002406 /* 2: have we encountered a read error ? */
2407 else if (req->flags & BF_READ_ERROR) {
2408 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002409 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002410 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002411 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002412 if (!(t->flags & SN_ERR_MASK))
2413 t->flags |= SN_ERR_CLICL;
2414 if (!(t->flags & SN_FINST_MASK))
2415 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002416 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002417 }
2418
2419 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002420 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002421 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002422 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01002423 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002424 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002425 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002426 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002427 if (!(t->flags & SN_ERR_MASK))
2428 t->flags |= SN_ERR_CLITO;
2429 if (!(t->flags & SN_FINST_MASK))
2430 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002431 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002432 }
2433
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002434 /* 4: have we encountered a close ? */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002435 else if (req->flags & BF_SHUTR) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002436 txn->status = 400;
2437 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002438 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002439 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002440 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002441
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002442 if (!(t->flags & SN_ERR_MASK))
2443 t->flags |= SN_ERR_CLICL;
2444 if (!(t->flags & SN_FINST_MASK))
2445 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002446 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002447 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002448
Willy Tarreau3da77c52008-08-29 09:58:42 +02002449 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002450 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002451 if (!tick_isset(req->analyse_exp))
2452 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002453
2454 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02002455 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002456 }
2457
2458
2459 /****************************************************************
2460 * More interesting part now : we know that we have a complete *
2461 * request which at least looks like HTTP. We have an indicator *
2462 * of each header's length, so we can parse them quickly. *
2463 ****************************************************************/
2464
Willy Tarreau2df28e82008-08-17 15:20:19 +02002465 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002466 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002467
Willy Tarreau9cdde232007-05-02 20:58:19 +02002468 /* ensure we keep this pointer to the beginning of the message */
2469 msg->sol = req->data + msg->som;
2470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002471 /*
2472 * 1: identify the method
2473 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002474 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002475
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002476 /* we can make use of server redirect on GET and HEAD */
2477 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2478 t->flags |= SN_REDIRECTABLE;
2479
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002481 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01002482 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002483 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002484 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002485 if (unlikely((t->fe->monitor_uri_len != 0) &&
2486 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2487 !memcmp(&req->data[msg->sl.rq.u],
2488 t->fe->monitor_uri,
2489 t->fe->monitor_uri_len))) {
2490 /*
2491 * We have found the monitor URI
2492 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01002493 struct acl_cond *cond;
2494 cur_proxy = t->fe;
2495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002496 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002497
2498 /* Check if we want to fail this monitor request or not */
2499 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
2500 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002501
2502 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01002503 if (cond->pol == ACL_COND_UNLESS)
2504 ret = !ret;
2505
2506 if (ret) {
2507 /* we fail this request, let's return 503 service unavail */
2508 txn->status = 503;
2509 client_retnclose(t, error_message(t, HTTP_ERR_503));
2510 goto return_prx_cond;
2511 }
2512 }
2513
2514 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002515 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002516 client_retnclose(t, &http_200_chunk);
2517 goto return_prx_cond;
2518 }
2519
2520 /*
2521 * 3: Maybe we have to copy the original REQURI for the logs ?
2522 * Note: we cannot log anymore if the request has been
2523 * classified as invalid.
2524 */
2525 if (unlikely(t->logs.logwait & LW_REQ)) {
2526 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002527 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002528 int urilen = msg->sl.rq.l;
2529
2530 if (urilen >= REQURI_LEN)
2531 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002532 memcpy(txn->uri, &req->data[msg->som], urilen);
2533 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002534
2535 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002536 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002537 } else {
2538 Alert("HTTP logging : out of memory.\n");
2539 }
2540 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541
Willy Tarreau06619262006-12-17 08:37:22 +01002542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002543 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2544 if (unlikely(msg->sl.rq.v_l == 0)) {
2545 int delta;
2546 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002547 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002548 cur_end = msg->sol + msg->sl.rq.l;
2549 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002551 if (msg->sl.rq.u_l == 0) {
2552 /* if no URI was set, add "/" */
2553 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2554 cur_end += delta;
2555 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002556 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002557 /* add HTTP version */
2558 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2559 msg->eoh += delta;
2560 cur_end += delta;
2561 cur_end = (char *)http_parse_reqline(msg, req->data,
2562 HTTP_MSG_RQMETH,
2563 msg->sol, cur_end + 1,
2564 NULL, NULL);
2565 if (unlikely(!cur_end))
2566 goto return_bad_req;
2567
2568 /* we have a full HTTP/1.0 request now and we know that
2569 * we have either a CR or an LF at <ptr>.
2570 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002571 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002572 }
2573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002574
2575 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002576 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002577 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002578 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002579
2580 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002581 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 * As opposed to version 1.2, now they will be evaluated in the
2583 * filters order and not in the header order. This means that
2584 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002585 *
2586 * We can now check whether we want to switch to another
2587 * backend, in which case we will re-check the backend's
2588 * filters and various options. In order to support 3-level
2589 * switching, here's how we should proceed :
2590 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002591 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002592 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002593 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002594 * There cannot be any switch from there, so ->be cannot be
2595 * changed anymore.
2596 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002597 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002598 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002599 * The response path will be able to apply either ->be, or
2600 * ->be then ->fe filters in order to match the reverse of
2601 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002602 */
2603
Willy Tarreau06619262006-12-17 08:37:22 +01002604 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002605 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002606 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002607 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002608 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002609
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002610 /* first check whether we have some ACLs set to redirect this request */
2611 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2612 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002613
2614 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002615 if (rule->cond->pol == ACL_COND_UNLESS)
2616 ret = !ret;
2617
2618 if (ret) {
2619 struct chunk rdr = { trash, 0 };
2620 const char *msg_fmt;
2621
2622 /* build redirect message */
2623 switch(rule->code) {
2624 case 303:
2625 rdr.len = strlen(HTTP_303);
2626 msg_fmt = HTTP_303;
2627 break;
2628 case 301:
2629 rdr.len = strlen(HTTP_301);
2630 msg_fmt = HTTP_301;
2631 break;
2632 case 302:
2633 default:
2634 rdr.len = strlen(HTTP_302);
2635 msg_fmt = HTTP_302;
2636 break;
2637 }
2638
2639 if (unlikely(rdr.len > sizeof(trash)))
2640 goto return_bad_req;
2641 memcpy(rdr.str, msg_fmt, rdr.len);
2642
2643 switch(rule->type) {
2644 case REDIRECT_TYPE_PREFIX: {
2645 const char *path;
2646 int pathlen;
2647
2648 path = http_get_path(txn);
2649 /* build message using path */
2650 if (path) {
2651 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2652 } else {
2653 path = "/";
2654 pathlen = 1;
2655 }
2656
2657 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2658 goto return_bad_req;
2659
2660 /* add prefix */
2661 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2662 rdr.len += rule->rdr_len;
2663
2664 /* add path */
2665 memcpy(rdr.str + rdr.len, path, pathlen);
2666 rdr.len += pathlen;
2667 break;
2668 }
2669 case REDIRECT_TYPE_LOCATION:
2670 default:
2671 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2672 goto return_bad_req;
2673
2674 /* add location */
2675 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2676 rdr.len += rule->rdr_len;
2677 break;
2678 }
2679
2680 /* add end of headers */
2681 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2682 rdr.len += 4;
2683
2684 txn->status = rule->code;
2685 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002686 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002687 client_retnclose(t, &rdr);
2688 goto return_prx_cond;
2689 }
2690 }
2691
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002692 /* first check whether we have some ACLs set to block this request */
2693 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002694 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002695
2696 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002697 if (cond->pol == ACL_COND_UNLESS)
2698 ret = !ret;
2699
2700 if (ret) {
2701 txn->status = 403;
2702 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002703 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002704 client_retnclose(t, error_message(t, HTTP_ERR_403));
2705 goto return_prx_cond;
2706 }
2707 }
2708
Willy Tarreau06619262006-12-17 08:37:22 +01002709 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002710 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002711 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2712 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002713 }
2714
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002715 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2716 /* to ensure correct connection accounting on
2717 * the backend, we count the connection for the
2718 * one managing the queue.
2719 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002720 t->be->beconn++;
2721 if (t->be->beconn > t->be->beconn_max)
2722 t->be->beconn_max = t->be->beconn;
2723 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002724 t->flags |= SN_BE_ASSIGNED;
2725 }
2726
Willy Tarreau06619262006-12-17 08:37:22 +01002727 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002728 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002729 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002730 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002731 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002732 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002733 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002734 goto return_prx_cond;
2735 }
2736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002737 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002738 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002739 !(t->flags & SN_CONN_CLOSED)) {
2740 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002741 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002742 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002743
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002744 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002745 old_idx = 0;
2746
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002747 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2748 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002749 cur_ptr = cur_next;
2750 cur_end = cur_ptr + cur_hdr->len;
2751 cur_next = cur_end + cur_hdr->cr + 1;
2752
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002753 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2754 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002755 /* 3 possibilities :
2756 * - we have already set Connection: close,
2757 * so we remove this line.
2758 * - we have not yet set Connection: close,
2759 * but this line indicates close. We leave
2760 * it untouched and set the flag.
2761 * - we have not yet set Connection: close,
2762 * and this line indicates non-close. We
2763 * replace it.
2764 */
2765 if (t->flags & SN_CONN_CLOSED) {
2766 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002767 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002768 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002769 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2770 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002771 cur_hdr->len = 0;
2772 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002773 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2774 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2775 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002776 cur_next += delta;
2777 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002778 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002779 }
2780 t->flags |= SN_CONN_CLOSED;
2781 }
2782 }
2783 old_idx = cur_idx;
2784 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002785 }
2786 /* add request headers from the rule sets in the same order */
2787 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2788 if (unlikely(http_header_add_tail(req,
2789 &txn->req,
2790 &txn->hdr_idx,
2791 rule_set->req_add[cur_idx])) < 0)
2792 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002793 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002794
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002795 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002796 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002797 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002798 /* we have to check the URI and auth for this request.
2799 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002800 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002801 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002802 if (stats_check_uri_auth(t, rule_set))
2803 return 1;
2804 }
2805
Willy Tarreau55ea7572007-06-17 19:56:27 +02002806 /* now check whether we have some switching rules for this request */
2807 if (!(t->flags & SN_BE_ASSIGNED)) {
2808 struct switching_rule *rule;
2809
2810 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2811 int ret;
2812
2813 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002814
2815 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002816 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002817 ret = !ret;
2818
2819 if (ret) {
2820 t->be = rule->be.backend;
2821 t->be->beconn++;
2822 if (t->be->beconn > t->be->beconn_max)
2823 t->be->beconn_max = t->be->beconn;
2824 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002825
2826 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002827 t->rep->rto = t->req->wto = t->be->timeout.server;
2828 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002829 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002830 t->flags |= SN_BE_ASSIGNED;
2831 break;
2832 }
2833 }
2834 }
2835
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002836 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2837 /* No backend was set, but there was a default
2838 * backend set in the frontend, so we use it and
2839 * loop again.
2840 */
2841 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002842 t->be->beconn++;
2843 if (t->be->beconn > t->be->beconn_max)
2844 t->be->beconn_max = t->be->beconn;
2845 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002846
2847 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002848 t->rep->rto = t->req->wto = t->be->timeout.server;
2849 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002850 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002851 t->flags |= SN_BE_ASSIGNED;
2852 }
2853 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002854
Willy Tarreau58f10d72006-12-04 02:26:12 +01002855
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002856 if (!(t->flags & SN_BE_ASSIGNED)) {
2857 /* To ensure correct connection accounting on
2858 * the backend, we count the connection for the
2859 * one managing the queue.
2860 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002861 t->be->beconn++;
2862 if (t->be->beconn > t->be->beconn_max)
2863 t->be->beconn_max = t->be->beconn;
2864 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002865 t->flags |= SN_BE_ASSIGNED;
2866 }
2867
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002868 /*
2869 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002870 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002871 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002872 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002873 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002874
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002875 /*
2876 * If HTTP PROXY is set we simply get remote server address
2877 * parsing incoming request.
2878 */
2879 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2880 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2881 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002882
Willy Tarreau2a324282006-12-05 00:05:46 +01002883 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002884 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002885 * so let's do the same now.
2886 */
2887
2888 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002889 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002890 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002891 }
2892
2893
2894 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002895 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002896 * Note that doing so might move headers in the request, but
2897 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002898 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002899 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002900 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2901 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002902 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002903
Willy Tarreau58f10d72006-12-04 02:26:12 +01002904
Willy Tarreau2a324282006-12-05 00:05:46 +01002905 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002906 * 9: add X-Forwarded-For if either the frontend or the backend
2907 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002908 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002909 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002910 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002911 /* Add an X-Forwarded-For header unless the source IP is
2912 * in the 'except' network range.
2913 */
2914 if ((!t->fe->except_mask.s_addr ||
2915 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2916 != t->fe->except_net.s_addr) &&
2917 (!t->be->except_mask.s_addr ||
2918 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2919 != t->be->except_net.s_addr)) {
2920 int len;
2921 unsigned char *pn;
2922 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002923
Ross Westaf72a1d2008-08-03 10:51:45 +02002924 /* Note: we rely on the backend to get the header name to be used for
2925 * x-forwarded-for, because the header is really meant for the backends.
2926 * However, if the backend did not specify any option, we have to rely
2927 * on the frontend's header name.
2928 */
2929 if (t->be->fwdfor_hdr_len) {
2930 len = t->be->fwdfor_hdr_len;
2931 memcpy(trash, t->be->fwdfor_hdr_name, len);
2932 } else {
2933 len = t->fe->fwdfor_hdr_len;
2934 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2935 }
2936 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002937
Ross Westaf72a1d2008-08-03 10:51:45 +02002938 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002939 &txn->hdr_idx, trash, len)) < 0)
2940 goto return_bad_req;
2941 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002942 }
2943 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002944 /* FIXME: for the sake of completeness, we should also support
2945 * 'except' here, although it is mostly useless in this case.
2946 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002947 int len;
2948 char pn[INET6_ADDRSTRLEN];
2949 inet_ntop(AF_INET6,
2950 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2951 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002952
2953 /* Note: we rely on the backend to get the header name to be used for
2954 * x-forwarded-for, because the header is really meant for the backends.
2955 * However, if the backend did not specify any option, we have to rely
2956 * on the frontend's header name.
2957 */
2958 if (t->be->fwdfor_hdr_len) {
2959 len = t->be->fwdfor_hdr_len;
2960 memcpy(trash, t->be->fwdfor_hdr_name, len);
2961 } else {
2962 len = t->fe->fwdfor_hdr_len;
2963 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2964 }
2965 len += sprintf(trash + len, ": %s", pn);
2966
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002967 if (unlikely(http_header_add_tail2(req, &txn->req,
2968 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002969 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002970 }
2971 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002972
Willy Tarreau2a324282006-12-05 00:05:46 +01002973 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002974 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002975 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002976 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002977 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002978 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002979 if ((unlikely(msg->sl.rq.v_l != 8) ||
2980 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2981 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002982 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002983 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002984 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002985 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002986 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2987 * If not assigned, perhaps we are balancing on url_param, but this is a
2988 * POST; and the parameters are in the body, maybe scan there to find our server.
2989 * (unless headers overflowed the buffer?)
2990 */
2991 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2992 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002993 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002994 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2995 /* are there enough bytes here? total == l || r || rlim ?
2996 * len is unsigned, but eoh is int,
2997 * how many bytes of body have we received?
2998 * eoh is the first empty line of the header
2999 */
3000 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02003001 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 +02003002
3003 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
3004 * We can't assume responsibility for the server's decision,
3005 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
3006 * We also can't change our mind later, about which server to choose, so round robin.
3007 */
3008 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
3009 struct hdr_ctx ctx;
3010 ctx.idx = 0;
3011 /* Expect is allowed in 1.1, look for it */
3012 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
3013 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02003014 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003015 /* We can't reliablly stall and wait for data, because of
3016 * .NET clients that don't conform to rfc2616; so, no need for
3017 * the next block to check length expectations.
3018 * We could send 100 status back to the client, but then we need to
3019 * re-write headers, and send the message. And this isn't the right
3020 * place for that action.
3021 * TODO: support Expect elsewhere and delete this block.
3022 */
3023 goto end_check_maybe_wait_for_body;
3024 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003025
3026 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003027 /* nothing to do, we got enough */
3028 } else {
3029 /* limit implies we are supposed to need this many bytes
3030 * to find the parameter. Let's see how many bytes we can wait for.
3031 */
3032 long long hint = len;
3033 struct hdr_ctx ctx;
3034 ctx.idx = 0;
3035 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003036 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02003037 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003038 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003039 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003040 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003041 ctx.idx = 0;
3042 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
3043 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003044 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003045 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003046 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003047 hint = 0; /* parse failure, untrusted client */
3048 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003049 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003050 msg->hdr_content_len = hint;
3051 else
3052 hint = 0; /* bad client, sent negative length */
3053 }
3054 }
3055 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003056 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003057 hint = t->be->url_param_post_limit;
3058 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003059 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02003060 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003061 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003062 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003063 /* else... There are no body bytes to wait for */
3064 }
3065 }
3066 }
3067 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003068
Willy Tarreau2a324282006-12-05 00:05:46 +01003069 /*************************************************************
3070 * OK, that's finished for the headers. We have done what we *
3071 * could. Let's switch to the DATA state. *
3072 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073
Willy Tarreaue393fe22008-08-16 22:18:07 +02003074 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02003075 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003076
Willy Tarreau1fa31262007-12-03 00:36:16 +01003077 /* When a connection is tarpitted, we use the tarpit timeout,
3078 * which may be the same as the connect timeout if unspecified.
3079 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003080 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01003081 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003082 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003083 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01003084 /* flush the request so that we can drop the connection early
3085 * if the client closes first.
3086 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003087 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003088 req->analysers |= AN_REQ_HTTP_TARPIT;
3089 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
3090 if (!req->analyse_exp)
3091 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01003092 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003094 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003095 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01003096
3097 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003098 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003099 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003100 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01003101 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003102 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01003103 return_prx_cond:
3104 if (!(t->flags & SN_ERR_MASK))
3105 t->flags |= SN_ERR_PRXCOND;
3106 if (!(t->flags & SN_FINST_MASK))
3107 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003108 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003109 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003110 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02003111 }
3112
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003113 if (req->analysers & AN_REQ_HTTP_TARPIT) {
3114 struct http_txn *txn = &t->txn;
3115
3116 /* This connection is being tarpitted. The CLIENT side has
3117 * already set the connect expiration date to the right
3118 * timeout. We just have to check that the client is still
3119 * there and that the timeout has not expired.
3120 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003121 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003122 !tick_is_expired(req->analyse_exp, now_ms))
3123 return 0;
3124
3125 /* We will set the queue timer to the time spent, just for
3126 * logging purposes. We fake a 500 server error, so that the
3127 * attacker will not suspect his connection has been tarpitted.
3128 * It will not cause trouble to the logs because we can exclude
3129 * the tarpitted connections by filtering on the 'PT' status flags.
3130 */
3131 trace_term(t, TT_HTTP_SRV_2);
3132 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3133
3134 txn->status = 500;
3135 if (req->flags != BF_READ_ERROR)
3136 client_retnclose(t, error_message(t, HTTP_ERR_500));
3137
3138 req->analysers = 0;
3139 req->analyse_exp = TICK_ETERNITY;
3140
3141 t->fe->failed_req++;
3142 if (!(t->flags & SN_ERR_MASK))
3143 t->flags |= SN_ERR_PRXCOND;
3144 if (!(t->flags & SN_FINST_MASK))
3145 t->flags |= SN_FINST_T;
3146 return 0;
3147 }
3148
Willy Tarreau2df28e82008-08-17 15:20:19 +02003149 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003150 /* We have to parse the HTTP request body to find any required data.
3151 * "balance url_param check_post" should have been the only way to get
3152 * into this. We were brought here after HTTP header analysis, so all
3153 * related structures are ready.
3154 */
3155 struct http_msg *msg = &t->txn.req;
3156 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
3157 long long limit = t->be->url_param_post_limit;
3158 struct hdr_ctx ctx;
3159
3160 ctx.idx = 0;
3161
3162 /* now if we have a length, we'll take the hint */
3163 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
3164 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
3165 unsigned int chunk = 0;
3166 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
3167 char c = msg->sol[body];
3168 if (ishex(c)) {
3169 unsigned int hex = toupper(c) - '0';
3170 if (hex > 9)
3171 hex -= 'A' - '9' - 1;
3172 chunk = (chunk << 4) | hex;
3173 } else
3174 break;
3175 body++;
3176 }
3177 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003178 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003179
3180 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003181 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003182
3183 body += 2; // skip CRLF
3184
3185 /* if we support more then one chunk here, we have to do it again when assigning server
3186 * 1. how much entity data do we have? new var
3187 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
3188 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
3189 */
3190
3191 if (chunk < limit)
3192 limit = chunk; /* only reading one chunk */
3193 } else {
3194 if (msg->hdr_content_len < limit)
3195 limit = msg->hdr_content_len;
3196 }
3197
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003198 http_body_end:
3199 /* we leave once we know we have nothing left to do. This means that we have
3200 * enough bytes, or that we know we'll not get any more (buffer full, read
3201 * buffer closed).
3202 */
3203 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003204 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02003205 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003206 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003207 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02003208 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02003209 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003210 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02003211 else {
3212 /* Not enough data. We'll re-use the http-request
3213 * timeout here. Ideally, we should set the timeout
3214 * relative to the accept() date. We just set the
3215 * request timeout once at the beginning of the
3216 * request.
3217 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003218 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02003219 if (!tick_isset(req->analyse_exp))
3220 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
3221 return 0;
3222 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003223 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003224
Willy Tarreau2df28e82008-08-17 15:20:19 +02003225 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3226 * probably reduce one day's debugging session.
3227 */
3228#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003229 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 +02003230 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3231 __FILE__, __LINE__, req->analysers);
3232 ABORT_NOW();
3233 }
3234#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003235 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003236 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003237}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003238
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003239/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02003240 * It normally returns zero, but may return 1 if it absolutely needs to be
3241 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02003242 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003243 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003244 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003245int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003246{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003247 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003248 struct buffer *req = t->req;
3249 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003250
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003251 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 +02003252 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003253 t,
3254 rep,
3255 rep->rex, rep->wex,
3256 rep->flags,
3257 rep->l,
3258 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003259
Willy Tarreau2df28e82008-08-17 15:20:19 +02003260 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003261 /*
3262 * Now parse the partial (or complete) lines.
3263 * We will check the response syntax, and also join multi-line
3264 * headers. An index of all the lines will be elaborated while
3265 * parsing.
3266 *
3267 * For the parsing, we use a 28 states FSM.
3268 *
3269 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02003270 * rep->data + rep->som = beginning of response
3271 * rep->data + rep->eoh = end of processed headers / start of current one
3272 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003273 * rep->lr = first non-visited byte
3274 * rep->r = end of data
3275 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02003276
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003277 int cur_idx;
3278 struct http_msg *msg = &txn->rsp;
3279 struct proxy *cur_proxy;
3280
3281 if (likely(rep->lr < rep->r))
3282 http_msg_analyzer(rep, msg, &txn->hdr_idx);
3283
3284 /* 1: we might have to print this header in debug mode */
3285 if (unlikely((global.mode & MODE_DEBUG) &&
3286 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3287 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
3288 char *eol, *sol;
3289
3290 sol = rep->data + msg->som;
3291 eol = sol + msg->sl.rq.l;
3292 debug_hdr("srvrep", t, sol, eol);
3293
3294 sol += hdr_idx_first_pos(&txn->hdr_idx);
3295 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
3296
3297 while (cur_idx) {
3298 eol = sol + txn->hdr_idx.v[cur_idx].len;
3299 debug_hdr("srvhdr", t, sol, eol);
3300 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3301 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003302 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003305 /*
3306 * Now we quickly check if we have found a full valid response.
3307 * If not so, we check the FD and buffer states before leaving.
3308 * A full response is indicated by the fact that we have seen
3309 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
3310 * responses are checked first.
3311 *
3312 * Depending on whether the client is still there or not, we
3313 * may send an error response back or not. Note that normally
3314 * we should only check for HTTP status there, and check I/O
3315 * errors somewhere else.
3316 */
3317
3318 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003319 /* Invalid response */
3320 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3321 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003322 //buffer_shutr(rep);
3323 //buffer_shutw(req);
3324 //fd_delete(req->cons->fd);
3325 //req->cons->state = SI_ST_CLO;
3326 buffer_shutr_now(rep);
3327 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003328 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003329 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003330 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003331 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003332 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003333 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003334 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003335 txn->status = 502;
3336 client_return(t, error_message(t, HTTP_ERR_502));
3337 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003338 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003339 if (!(t->flags & SN_FINST_MASK))
3340 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003341
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003342 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3343 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003344
Willy Tarreaudafde432008-08-17 01:00:46 +02003345 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003346 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003347 /* too large response does not fit in buffer. */
3348 else if (rep->flags & BF_FULL) {
3349 goto hdr_response_bad;
3350 }
3351 /* read error */
3352 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003353 buffer_shutr_now(rep);
3354 buffer_shutw_now(req);
3355 //fd_delete(req->cons->fd);
3356 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003357 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003358 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003359 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003360 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003361 //}
3362 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003363 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003364 txn->status = 502;
3365 client_return(t, error_message(t, HTTP_ERR_502));
3366 if (!(t->flags & SN_ERR_MASK))
3367 t->flags |= SN_ERR_SRVCL;
3368 if (!(t->flags & SN_FINST_MASK))
3369 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003370
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003371 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3372 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003373
Willy Tarreaudafde432008-08-17 01:00:46 +02003374 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003375 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003376 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003377 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003378 buffer_shutr_now(rep);
3379 buffer_shutw_now(req);
3380 //fd_delete(req->cons->fd);
3381 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003382 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003383 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003384 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003385 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003386 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003387 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003388 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003389 txn->status = 504;
3390 client_return(t, error_message(t, HTTP_ERR_504));
3391 if (!(t->flags & SN_ERR_MASK))
3392 t->flags |= SN_ERR_SRVTO;
3393 if (!(t->flags & SN_FINST_MASK))
3394 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003395
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003396 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3397 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003398 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003399 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003400 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003401 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003402 buffer_shutr_now(rep);
3403 buffer_shutw_now(req);
3404 //fd_delete(req->cons->fd);
3405 //req->cons->state = SI_ST_CLO;
3406 if (t->srv) {
3407 //t->srv->cur_sess--;
3408 t->srv->failed_resp++;
3409 //sess_change_server(t, NULL);
3410 }
3411 t->be->failed_resp++;
3412 rep->analysers = 0;
3413 txn->status = 502;
3414 client_return(t, error_message(t, HTTP_ERR_502));
3415 if (!(t->flags & SN_ERR_MASK))
3416 t->flags |= SN_ERR_SRVCL;
3417 if (!(t->flags & SN_FINST_MASK))
3418 t->flags |= SN_FINST_H;
3419
3420 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3421 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003422
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003423 return 0;
3424 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003425 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003426 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003427 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003428
Willy Tarreau21d2af32008-02-14 20:25:24 +01003429
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003430 /*****************************************************************
3431 * More interesting part now : we know that we have a complete *
3432 * response which at least looks like HTTP. We have an indicator *
3433 * of each header's length, so we can parse them quickly. *
3434 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01003435
Willy Tarreau2df28e82008-08-17 15:20:19 +02003436 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02003437
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003438 /* ensure we keep this pointer to the beginning of the message */
3439 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003440
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003441 /*
3442 * 1: get the status code and check for cacheability.
3443 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01003444
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003445 t->logs.logwait &= ~LW_RESP;
3446 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003447
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003448 switch (txn->status) {
3449 case 200:
3450 case 203:
3451 case 206:
3452 case 300:
3453 case 301:
3454 case 410:
3455 /* RFC2616 @13.4:
3456 * "A response received with a status code of
3457 * 200, 203, 206, 300, 301 or 410 MAY be stored
3458 * by a cache (...) unless a cache-control
3459 * directive prohibits caching."
3460 *
3461 * RFC2616 @9.5: POST method :
3462 * "Responses to this method are not cacheable,
3463 * unless the response includes appropriate
3464 * Cache-Control or Expires header fields."
3465 */
3466 if (likely(txn->meth != HTTP_METH_POST) &&
3467 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3468 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3469 break;
3470 default:
3471 break;
3472 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003473
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003474 /*
3475 * 2: we may need to capture headers
3476 */
3477 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3478 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3479 txn->rsp.cap, t->fe->rsp_cap);
3480
3481 /*
3482 * 3: we will have to evaluate the filters.
3483 * As opposed to version 1.2, now they will be evaluated in the
3484 * filters order and not in the header order. This means that
3485 * each filter has to be validated among all headers.
3486 *
3487 * Filters are tried with ->be first, then with ->fe if it is
3488 * different from ->be.
3489 */
3490
3491 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3492
3493 cur_proxy = t->be;
3494 while (1) {
3495 struct proxy *rule_set = cur_proxy;
3496
3497 /* try headers filters */
3498 if (rule_set->rsp_exp != NULL) {
3499 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3500 return_bad_resp:
3501 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003502 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003503 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003504 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003505 }
3506 cur_proxy->failed_resp++;
3507 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003508 buffer_shutr_now(rep);
3509 buffer_shutw_now(req);
3510 //fd_delete(req->cons->fd);
3511 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003512 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003513 txn->status = 502;
3514 client_return(t, error_message(t, HTTP_ERR_502));
3515 if (!(t->flags & SN_ERR_MASK))
3516 t->flags |= SN_ERR_PRXCOND;
3517 if (!(t->flags & SN_FINST_MASK))
3518 t->flags |= SN_FINST_H;
3519 /* We used to have a free connection slot. Since we'll never use it,
3520 * we have to inform the server that it may be used by another session.
3521 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003522 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3523 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003524 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003525 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003526 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003527
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003528 /* has the response been denied ? */
3529 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003530 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003531 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003532 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003533 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003534 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003535 cur_proxy->denied_resp++;
3536 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003537 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003538
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003539 /* We might have to check for "Connection:" */
3540 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3541 !(t->flags & SN_CONN_CLOSED)) {
3542 char *cur_ptr, *cur_end, *cur_next;
3543 int cur_idx, old_idx, delta, val;
3544 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003545
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003546 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3547 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003548
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003549 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3550 cur_hdr = &txn->hdr_idx.v[cur_idx];
3551 cur_ptr = cur_next;
3552 cur_end = cur_ptr + cur_hdr->len;
3553 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003554
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003555 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3556 if (val) {
3557 /* 3 possibilities :
3558 * - we have already set Connection: close,
3559 * so we remove this line.
3560 * - we have not yet set Connection: close,
3561 * but this line indicates close. We leave
3562 * it untouched and set the flag.
3563 * - we have not yet set Connection: close,
3564 * and this line indicates non-close. We
3565 * replace it.
3566 */
3567 if (t->flags & SN_CONN_CLOSED) {
3568 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3569 txn->rsp.eoh += delta;
3570 cur_next += delta;
3571 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3572 txn->hdr_idx.used--;
3573 cur_hdr->len = 0;
3574 } else {
3575 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3576 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3577 "close", 5);
3578 cur_next += delta;
3579 cur_hdr->len += delta;
3580 txn->rsp.eoh += delta;
3581 }
3582 t->flags |= SN_CONN_CLOSED;
3583 }
3584 }
3585 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003586 }
3587 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003589 /* add response headers from the rule sets in the same order */
3590 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3591 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3592 rule_set->rsp_add[cur_idx])) < 0)
3593 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003594 }
3595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003596 /* check whether we're already working on the frontend */
3597 if (cur_proxy == t->fe)
3598 break;
3599 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003600 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003601
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003602 /*
3603 * 4: check for server cookie.
3604 */
3605 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3606 || (t->be->options & PR_O_CHK_CACHE))
3607 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003608
Willy Tarreaubaaee002006-06-26 02:48:02 +02003609
Willy Tarreaua15645d2007-03-18 16:22:39 +01003610 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003611 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003612 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003613 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3614 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003615
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003616 /*
3617 * 6: add server cookie in the response if needed
3618 */
3619 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3620 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3621 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003622
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003623 /* the server is known, it's not the one the client requested, we have to
3624 * insert a set-cookie here, except if we want to insert only on POST
3625 * requests and this one isn't. Note that servers which don't have cookies
3626 * (eg: some backup servers) will return a full cookie removal request.
3627 */
3628 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3629 t->be->cookie_name,
3630 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003631
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003632 if (t->be->cookie_domain)
3633 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003634
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003635 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3636 trash, len)) < 0)
3637 goto return_bad_resp;
3638 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003639
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003640 /* Here, we will tell an eventual cache on the client side that we don't
3641 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3642 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3643 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3644 */
3645 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003646
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003647 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3648
3649 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3650 "Cache-control: private", 22)) < 0)
3651 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003652 }
3653 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003654
Willy Tarreaubaaee002006-06-26 02:48:02 +02003655
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003656 /*
3657 * 7: check if result will be cacheable with a cookie.
3658 * We'll block the response if security checks have caught
3659 * nasty things such as a cacheable cookie.
3660 */
3661 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3662 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3663 (t->be->options & PR_O_CHK_CACHE)) {
3664
3665 /* we're in presence of a cacheable response containing
3666 * a set-cookie header. We'll block it as requested by
3667 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003668 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003669 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003670 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003671 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003672 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003673 }
3674 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003675
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003676 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3677 t->be->id, t->srv?t->srv->id:"<dispatch>");
3678 send_log(t->be, LOG_ALERT,
3679 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3680 t->be->id, t->srv?t->srv->id:"<dispatch>");
3681 goto return_srv_prx_502;
3682 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003683
3684 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003685 * 8: add "Connection: close" if needed and not yet set.
3686 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003687 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003688 if (!(t->flags & SN_CONN_CLOSED) &&
3689 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3690 if ((unlikely(msg->sl.st.v_l != 8) ||
3691 unlikely(req->data[msg->som + 7] != '0')) &&
3692 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3693 "Connection: close", 17)) < 0)
3694 goto return_bad_resp;
3695 t->flags |= SN_CONN_CLOSED;
3696 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003698 /*************************************************************
3699 * OK, that's finished for the headers. We have done what we *
3700 * could. Let's switch to the DATA state. *
3701 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003702
Willy Tarreaue393fe22008-08-16 22:18:07 +02003703 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003704 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003705
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003706#ifdef CONFIG_HAP_TCPSPLICE
3707 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3708 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003709 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003710 }
3711#endif
3712 /* if the user wants to log as soon as possible, without counting
3713 * bytes from the server, then this is the right moment. We have
3714 * to temporarily assign bytes_out to log what we currently have.
3715 */
3716 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3717 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3718 t->logs.bytes_out = txn->rsp.eoh;
3719 if (t->fe->to_log & LW_REQ)
3720 http_sess_log(t);
3721 else
3722 tcp_sess_log(t);
3723 t->logs.bytes_out = 0;
3724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003725
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003726 /* Note: we must not try to cheat by jumping directly to DATA,
3727 * otherwise we would not let the client side wake up.
3728 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003729
Willy Tarreaudafde432008-08-17 01:00:46 +02003730 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003731 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003732
Willy Tarreau2df28e82008-08-17 15:20:19 +02003733 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3734 * probably reduce one day's debugging session.
3735 */
3736#ifdef DEBUG_DEV
3737 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3738 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3739 __FILE__, __LINE__, rep->analysers);
3740 ABORT_NOW();
3741 }
3742#endif
3743 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003744 return 0;
3745}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003746
Willy Tarreaubaaee002006-06-26 02:48:02 +02003747
Willy Tarreaucff64112008-11-03 06:26:53 +01003748/* This function is called with (si->state == SI_ST_CON) meaning that a
3749 * connection was attempted and that the file descriptor is already allocated.
3750 * We must check for establishment, error and abort. Possible output states
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01003751 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
Willy Tarreaucff64112008-11-03 06:26:53 +01003752 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
3753 * otherwise 1.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003754 */
Willy Tarreaucff64112008-11-03 06:26:53 +01003755int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003756{
Willy Tarreaucff64112008-11-03 06:26:53 +01003757 struct buffer *req = si->ob;
3758 struct buffer *rep = si->ib;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759
Willy Tarreauefb453c2008-10-26 20:49:47 +01003760 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d, fds=%d\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003761 now_ms, __FUNCTION__,
Willy Tarreaucff64112008-11-03 06:26:53 +01003762 cli_stnames[s->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003763 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003764 req->flags, rep->flags,
Willy Tarreauefb453c2008-10-26 20:49:47 +01003765 req->l, rep->l,
Willy Tarreaucff64112008-11-03 06:26:53 +01003766 fdtab[si->fd].state);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003767
Willy Tarreaucff64112008-11-03 06:26:53 +01003768
3769 /* If we got an error, or if nothing happened and the connection timed
3770 * out, we must give up. The CER state handler will take care of retry
3771 * attempts and error reports.
3772 */
3773 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
3774 si->state = SI_ST_CER;
3775 fd_delete(si->fd);
3776
Willy Tarreaucff64112008-11-03 06:26:53 +01003777 if (si->err_type)
3778 return 0;
3779
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003780 si->err_loc = s->srv;
Willy Tarreaucff64112008-11-03 06:26:53 +01003781 if (si->flags & SI_FL_ERR)
3782 si->err_type = SI_ET_CONN_ERR;
3783 else
3784 si->err_type = SI_ET_CONN_TO;
3785 return 0;
3786 }
3787
3788 /* OK, maybe we want to abort */
3789 if (unlikely((req->flags & BF_SHUTW_NOW) ||
3790 (rep->flags & BF_SHUTW) ||
3791 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3792 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
3793 s->be->options & PR_O_ABRT_CLOSE)))) {
Willy Tarreauefb453c2008-10-26 20:49:47 +01003794 /* give up */
Willy Tarreau99126c32008-11-27 10:30:51 +01003795 si->shutw(si);
Willy Tarreaucff64112008-11-03 06:26:53 +01003796 si->err_type |= SI_ET_CONN_ABRT;
3797 si->err_loc = s->srv;
3798 return 1;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003799 }
3800
Willy Tarreaucff64112008-11-03 06:26:53 +01003801 /* we need to wait a bit more if there was no activity either */
3802 if (!(req->flags & BF_WRITE_ACTIVITY))
3803 return 1;
3804
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003805 /* OK, this means that a connection succeeded. The caller will be
3806 * responsible for handling the transition from CON to EST.
3807 */
Willy Tarreaucff64112008-11-03 06:26:53 +01003808 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
3809 si->state = SI_ST_EST;
3810 si->err_type = SI_ET_NONE;
3811 si->err_loc = NULL;
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003812 return 1;
3813}
3814
3815
3816/*
3817 * This function handles the transition between the SI_ST_CON state and the
3818 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
3819 * SI_ST_EST.
3820 */
3821void sess_establish(struct session *s, struct stream_interface *si)
3822{
3823 struct buffer *req = si->ob;
3824 struct buffer *rep = si->ib;
Willy Tarreaucff64112008-11-03 06:26:53 +01003825
3826 if (req->flags & BF_EMPTY) {
3827 EV_FD_CLR(si->fd, DIR_WR);
3828 req->wex = TICK_ETERNITY;
3829 } else {
3830 EV_FD_SET(si->fd, DIR_WR);
3831 req->wex = tick_add_ifset(now_ms, s->be->timeout.server);
3832 if (tick_isset(req->wex)) {
3833 /* FIXME: to prevent the server from expiring read
3834 * timeouts during writes, we refresh it. */
3835 rep->rex = req->wex;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003836 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003837 }
3838
Willy Tarreaucff64112008-11-03 06:26:53 +01003839 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3840 if (!(rep->flags & BF_HIJACK)) {
3841 EV_FD_SET(si->fd, DIR_RD);
3842 rep->rex = tick_add_ifset(now_ms, s->be->timeout.server);
3843 }
3844 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003845
Willy Tarreaucff64112008-11-03 06:26:53 +01003846 /* if the user wants to log as soon as possible, without counting
3847 * bytes from the server, then this is the right moment. */
3848 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
3849 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
3850 tcp_sess_log(s);
3851 }
3852#ifdef CONFIG_HAP_TCPSPLICE
3853 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
3854 /* TCP splicing supported by both FE and BE */
3855 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003856 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003857#endif
3858 }
3859 else {
3860 rep->analysers |= AN_RTR_HTTP_HDR;
3861 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3862 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3863 /* reset hdr_idx which was already initialized by the request.
3864 * right now, the http parser does it.
3865 * hdr_idx_init(&s->txn.hdr_idx);
3866 */
3867 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003868
Willy Tarreaucff64112008-11-03 06:26:53 +01003869 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
3870 req->wex = TICK_ETERNITY;
Willy Tarreaucff64112008-11-03 06:26:53 +01003871}
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003872
Willy Tarreaucff64112008-11-03 06:26:53 +01003873
3874/* This function is called with (si->state == SI_ST_CER) meaning that a
3875 * previous connection attempt has failed and that the file descriptor
3876 * has already been released. Possible causes include asynchronous error
3877 * notification and time out. Possible output states are SI_ST_CLO when
3878 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
3879 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
3880 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
3881 * marked as in error state. It returns 0.
3882 */
3883int sess_update_st_cer(struct session *s, struct stream_interface *si)
3884{
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003885 /* we probably have to release last session from the server */
3886 if (s->srv) {
3887 if (s->flags & SN_CURR_SESS) {
3888 s->flags &= ~SN_CURR_SESS;
3889 s->srv->cur_sess--;
3890 }
3891 sess_change_server(s, NULL);
3892 }
3893
Willy Tarreaucff64112008-11-03 06:26:53 +01003894 /* ensure that we have enough retries left */
3895 s->conn_retries--;
3896 if (s->conn_retries < 0) {
3897 if (!si->err_type) {
3898 si->err_type = SI_ET_CONN_ERR;
3899 si->err_loc = s->srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003900 }
3901
Willy Tarreaucff64112008-11-03 06:26:53 +01003902 if (s->srv)
3903 s->srv->failed_conns++;
3904 s->be->failed_conns++;
3905 if (may_dequeue_tasks(s->srv, s->be))
3906 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003907
Willy Tarreau99126c32008-11-27 10:30:51 +01003908 si->shutw(si);
Willy Tarreaucff64112008-11-03 06:26:53 +01003909 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003910
Willy Tarreau99126c32008-11-27 10:30:51 +01003911 si->shutr(si);
Willy Tarreaucff64112008-11-03 06:26:53 +01003912 si->ib->flags |= BF_READ_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003913
Willy Tarreaucff64112008-11-03 06:26:53 +01003914 si->state = SI_ST_CLO;
3915 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003916 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003917
Willy Tarreaucff64112008-11-03 06:26:53 +01003918 /* If the "redispatch" option is set on the backend, we are allowed to
3919 * retry on another server for the last retry. In order to achieve this,
3920 * we must mark the session unassigned, and eventually clear the DIRECT
3921 * bit to ignore any persistence cookie. We won't count a retry nor a
3922 * redispatch yet, because this will depend on what server is selected.
3923 */
3924 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
3925 if (may_dequeue_tasks(s->srv, s->be))
3926 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003927
Willy Tarreaucff64112008-11-03 06:26:53 +01003928 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3929 s->prev_srv = s->srv;
3930 si->state = SI_ST_REQ;
3931 } else {
3932 if (s->srv)
3933 s->srv->retries++;
3934 s->be->retries++;
3935 si->state = SI_ST_ASS;
3936 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003937
Willy Tarreaucff64112008-11-03 06:26:53 +01003938 if (si->flags & SI_FL_ERR) {
3939 /* The error was an asynchronous connection error, and we will
3940 * likely have to retry connecting to the same server, most
3941 * likely leading to the same result. To avoid this, we wait
3942 * one second before retrying.
3943 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003944
Willy Tarreaucff64112008-11-03 06:26:53 +01003945 if (!si->err_type)
3946 si->err_type = SI_ET_CONN_ERR;
3947
3948 si->state = SI_ST_TAR;
3949 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003950 return 0;
3951 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003952 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003953}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003954
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003955
Willy Tarreaubaaee002006-06-26 02:48:02 +02003956/*
3957 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003958 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003959 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3960 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003961 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003962 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003963 */
3964int produce_content(struct session *s)
3965{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003966 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02003967 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003968 return 1;
3969 }
3970 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003971 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003972 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003973 if (ret >= 0)
3974 return ret;
3975 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003976 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003977
Willy Tarreau91861262007-10-17 17:06:05 +02003978 /* unknown data source or internal error */
3979 s->txn.status = 500;
3980 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003981 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003982 if (!(s->flags & SN_ERR_MASK))
3983 s->flags |= SN_ERR_PRXCOND;
3984 if (!(s->flags & SN_FINST_MASK))
3985 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02003986 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02003987 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003988}
3989
3990
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991/* Iterate the same filter through all request headers.
3992 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003993 * Since it can manage the switch to another backend, it updates the per-proxy
3994 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003995 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003996int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003997{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003998 char term;
3999 char *cur_ptr, *cur_end, *cur_next;
4000 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004001 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004002 struct hdr_idx_elem *cur_hdr;
4003 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004004
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004005 last_hdr = 0;
4006
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004007 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008 old_idx = 0;
4009
4010 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004011 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004012 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004013 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004014 (exp->action == ACT_ALLOW ||
4015 exp->action == ACT_DENY ||
4016 exp->action == ACT_TARPIT))
4017 return 0;
4018
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004019 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 if (!cur_idx)
4021 break;
4022
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004023 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024 cur_ptr = cur_next;
4025 cur_end = cur_ptr + cur_hdr->len;
4026 cur_next = cur_end + cur_hdr->cr + 1;
4027
4028 /* Now we have one header between cur_ptr and cur_end,
4029 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004030 */
4031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004032 /* The annoying part is that pattern matching needs
4033 * that we modify the contents to null-terminate all
4034 * strings before testing them.
4035 */
4036
4037 term = *cur_end;
4038 *cur_end = '\0';
4039
4040 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4041 switch (exp->action) {
4042 case ACT_SETBE:
4043 /* It is not possible to jump a second time.
4044 * FIXME: should we return an HTTP/500 here so that
4045 * the admin knows there's a problem ?
4046 */
4047 if (t->be != t->fe)
4048 break;
4049
4050 /* Swithing Proxy */
4051 t->be = (struct proxy *) exp->replace;
4052
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004053 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004054 * because we have associated req_cap and rsp_cap to the
4055 * frontend, and the beconn will be updated later.
4056 */
4057
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004058 t->rep->rto = t->req->wto = t->be->timeout.server;
4059 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004060 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 last_hdr = 1;
4062 break;
4063
4064 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004065 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004066 last_hdr = 1;
4067 break;
4068
4069 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004070 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004071 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004072 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 break;
4074
4075 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004076 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004077 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004078 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004079 break;
4080
4081 case ACT_REPLACE:
4082 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4083 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4084 /* FIXME: if the user adds a newline in the replacement, the
4085 * index will not be recalculated for now, and the new line
4086 * will not be counted as a new header.
4087 */
4088
4089 cur_end += delta;
4090 cur_next += delta;
4091 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004092 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 break;
4094
4095 case ACT_REMOVE:
4096 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4097 cur_next += delta;
4098
4099 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004100 txn->req.eoh += delta;
4101 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4102 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004103 cur_hdr->len = 0;
4104 cur_end = NULL; /* null-term has been rewritten */
4105 break;
4106
4107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004108 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004109 if (cur_end)
4110 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004112 /* keep the link from this header to next one in case of later
4113 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004115 old_idx = cur_idx;
4116 }
4117 return 0;
4118}
4119
4120
4121/* Apply the filter to the request line.
4122 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4123 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004124 * Since it can manage the switch to another backend, it updates the per-proxy
4125 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004126 */
4127int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4128{
4129 char term;
4130 char *cur_ptr, *cur_end;
4131 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004132 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004133 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004134
Willy Tarreau58f10d72006-12-04 02:26:12 +01004135
Willy Tarreau3d300592007-03-18 18:34:41 +01004136 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004137 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004138 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004139 (exp->action == ACT_ALLOW ||
4140 exp->action == ACT_DENY ||
4141 exp->action == ACT_TARPIT))
4142 return 0;
4143 else if (exp->action == ACT_REMOVE)
4144 return 0;
4145
4146 done = 0;
4147
Willy Tarreau9cdde232007-05-02 20:58:19 +02004148 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004149 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004150
4151 /* Now we have the request line between cur_ptr and cur_end */
4152
4153 /* The annoying part is that pattern matching needs
4154 * that we modify the contents to null-terminate all
4155 * strings before testing them.
4156 */
4157
4158 term = *cur_end;
4159 *cur_end = '\0';
4160
4161 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4162 switch (exp->action) {
4163 case ACT_SETBE:
4164 /* It is not possible to jump a second time.
4165 * FIXME: should we return an HTTP/500 here so that
4166 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004168 if (t->be != t->fe)
4169 break;
4170
4171 /* Swithing Proxy */
4172 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004173
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004174 /* right now, the backend switch is not too much complicated
4175 * because we have associated req_cap and rsp_cap to the
4176 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004177 */
4178
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004179 t->rep->rto = t->req->wto = t->be->timeout.server;
4180 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004181 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004182 done = 1;
4183 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004185 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004186 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004187 done = 1;
4188 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004190 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004191 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004192 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004193 done = 1;
4194 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004196 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004197 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004198 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004199 done = 1;
4200 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004202 case ACT_REPLACE:
4203 *cur_end = term; /* restore the string terminator */
4204 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4205 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4206 /* FIXME: if the user adds a newline in the replacement, the
4207 * index will not be recalculated for now, and the new line
4208 * will not be counted as a new header.
4209 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004210
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004211 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004212 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004213
Willy Tarreau9cdde232007-05-02 20:58:19 +02004214 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004215 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004216 HTTP_MSG_RQMETH,
4217 cur_ptr, cur_end + 1,
4218 NULL, NULL);
4219 if (unlikely(!cur_end))
4220 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004222 /* we have a full request and we know that we have either a CR
4223 * or an LF at <ptr>.
4224 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004225 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4226 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004227 /* there is no point trying this regex on headers */
4228 return 1;
4229 }
4230 }
4231 *cur_end = term; /* restore the string terminator */
4232 return done;
4233}
Willy Tarreau97de6242006-12-27 17:18:38 +01004234
Willy Tarreau58f10d72006-12-04 02:26:12 +01004235
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004237/*
4238 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4239 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004240 * unparsable request. Since it can manage the switch to another backend, it
4241 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004242 */
4243int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4244{
Willy Tarreau3d300592007-03-18 18:34:41 +01004245 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004246 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004247 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004248 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004249
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004250 /*
4251 * The interleaving of transformations and verdicts
4252 * makes it difficult to decide to continue or stop
4253 * the evaluation.
4254 */
4255
Willy Tarreau3d300592007-03-18 18:34:41 +01004256 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004257 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4258 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4259 exp = exp->next;
4260 continue;
4261 }
4262
4263 /* Apply the filter to the request line. */
4264 ret = apply_filter_to_req_line(t, req, exp);
4265 if (unlikely(ret < 0))
4266 return -1;
4267
4268 if (likely(ret == 0)) {
4269 /* The filter did not match the request, it can be
4270 * iterated through all headers.
4271 */
4272 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 }
4274 exp = exp->next;
4275 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004276 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277}
4278
4279
Willy Tarreaua15645d2007-03-18 16:22:39 +01004280
Willy Tarreau58f10d72006-12-04 02:26:12 +01004281/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004282 * Manage client-side cookie. It can impact performance by about 2% so it is
4283 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004284 */
4285void manage_client_side_cookies(struct session *t, struct buffer *req)
4286{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004287 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004288 char *p1, *p2, *p3, *p4;
4289 char *del_colon, *del_cookie, *colon;
4290 int app_cookies;
4291
4292 appsess *asession_temp = NULL;
4293 appsess local_asession;
4294
4295 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004296 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004297
Willy Tarreau2a324282006-12-05 00:05:46 +01004298 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004299 * we start with the start line.
4300 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004301 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004302 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004304 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004305 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004306 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004308 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004309 cur_ptr = cur_next;
4310 cur_end = cur_ptr + cur_hdr->len;
4311 cur_next = cur_end + cur_hdr->cr + 1;
4312
4313 /* We have one full header between cur_ptr and cur_end, and the
4314 * next header starts at cur_next. We're only interested in
4315 * "Cookie:" headers.
4316 */
4317
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004318 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4319 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004320 old_idx = cur_idx;
4321 continue;
4322 }
4323
4324 /* Now look for cookies. Conforming to RFC2109, we have to support
4325 * attributes whose name begin with a '$', and associate them with
4326 * the right cookie, if we want to delete this cookie.
4327 * So there are 3 cases for each cookie read :
4328 * 1) it's a special attribute, beginning with a '$' : ignore it.
4329 * 2) it's a server id cookie that we *MAY* want to delete : save
4330 * some pointers on it (last semi-colon, beginning of cookie...)
4331 * 3) it's an application cookie : we *MAY* have to delete a previous
4332 * "special" cookie.
4333 * At the end of loop, if a "special" cookie remains, we may have to
4334 * remove it. If no application cookie persists in the header, we
4335 * *MUST* delete it
4336 */
4337
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004338 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004339
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 /* del_cookie == NULL => nothing to be deleted */
4341 del_colon = del_cookie = NULL;
4342 app_cookies = 0;
4343
4344 while (p1 < cur_end) {
4345 /* skip spaces and colons, but keep an eye on these ones */
4346 while (p1 < cur_end) {
4347 if (*p1 == ';' || *p1 == ',')
4348 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004349 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004350 break;
4351 p1++;
4352 }
4353
4354 if (p1 == cur_end)
4355 break;
4356
4357 /* p1 is at the beginning of the cookie name */
4358 p2 = p1;
4359 while (p2 < cur_end && *p2 != '=')
4360 p2++;
4361
4362 if (p2 == cur_end)
4363 break;
4364
4365 p3 = p2 + 1; /* skips the '=' sign */
4366 if (p3 == cur_end)
4367 break;
4368
4369 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004370 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004371 p4++;
4372
4373 /* here, we have the cookie name between p1 and p2,
4374 * and its value between p3 and p4.
4375 * we can process it :
4376 *
4377 * Cookie: NAME=VALUE;
4378 * | || || |
4379 * | || || +--> p4
4380 * | || |+-------> p3
4381 * | || +--------> p2
4382 * | |+------------> p1
4383 * | +-------------> colon
4384 * +--------------------> cur_ptr
4385 */
4386
4387 if (*p1 == '$') {
4388 /* skip this one */
4389 }
4390 else {
4391 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004392 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004393 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004394 (p4 - p1 >= t->fe->capture_namelen) &&
4395 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004396 int log_len = p4 - p1;
4397
Willy Tarreau086b3b42007-05-13 21:45:51 +02004398 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 Alert("HTTP logging : out of memory.\n");
4400 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004401 if (log_len > t->fe->capture_len)
4402 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004403 memcpy(txn->cli_cookie, p1, log_len);
4404 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 }
4406 }
4407
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004408 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4409 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004410 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004411 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004412 char *delim;
4413
4414 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4415 * have the server ID betweek p3 and delim, and the original cookie between
4416 * delim+1 and p4. Otherwise, delim==p4 :
4417 *
4418 * Cookie: NAME=SRV~VALUE;
4419 * | || || | |
4420 * | || || | +--> p4
4421 * | || || +--------> delim
4422 * | || |+-----------> p3
4423 * | || +------------> p2
4424 * | |+----------------> p1
4425 * | +-----------------> colon
4426 * +------------------------> cur_ptr
4427 */
4428
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004429 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004430 for (delim = p3; delim < p4; delim++)
4431 if (*delim == COOKIE_DELIM)
4432 break;
4433 }
4434 else
4435 delim = p4;
4436
4437
4438 /* Here, we'll look for the first running server which supports the cookie.
4439 * This allows to share a same cookie between several servers, for example
4440 * to dedicate backup servers to specific servers only.
4441 * However, to prevent clients from sticking to cookie-less backup server
4442 * when they have incidentely learned an empty cookie, we simply ignore
4443 * empty cookies and mark them as invalid.
4444 */
4445 if (delim == p3)
4446 srv = NULL;
4447
4448 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004449 if (srv->cookie && (srv->cklen == delim - p3) &&
4450 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004451 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004452 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004453 txn->flags &= ~TX_CK_MASK;
4454 txn->flags |= TX_CK_VALID;
4455 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004456 t->srv = srv;
4457 break;
4458 } else {
4459 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004460 txn->flags &= ~TX_CK_MASK;
4461 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 }
4463 }
4464 srv = srv->next;
4465 }
4466
Willy Tarreau3d300592007-03-18 18:34:41 +01004467 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004468 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004469 txn->flags &= ~TX_CK_MASK;
4470 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004471 }
4472
4473 /* depending on the cookie mode, we may have to either :
4474 * - delete the complete cookie if we're in insert+indirect mode, so that
4475 * the server never sees it ;
4476 * - remove the server id from the cookie value, and tag the cookie as an
4477 * application cookie so that it does not get accidentely removed later,
4478 * if we're in cookie prefix mode
4479 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004480 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004481 int delta; /* negative */
4482
4483 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4484 p4 += delta;
4485 cur_end += delta;
4486 cur_next += delta;
4487 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004488 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004489
4490 del_cookie = del_colon = NULL;
4491 app_cookies++; /* protect the header from deletion */
4492 }
4493 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004494 (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 +01004495 del_cookie = p1;
4496 del_colon = colon;
4497 }
4498 } else {
4499 /* now we know that we must keep this cookie since it's
4500 * not ours. But if we wanted to delete our cookie
4501 * earlier, we cannot remove the complete header, but we
4502 * can remove the previous block itself.
4503 */
4504 app_cookies++;
4505
4506 if (del_cookie != NULL) {
4507 int delta; /* negative */
4508
4509 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4510 p4 += delta;
4511 cur_end += delta;
4512 cur_next += delta;
4513 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004514 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004515 del_cookie = del_colon = NULL;
4516 }
4517 }
4518
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004519 if ((t->be->appsession_name != NULL) &&
4520 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004521 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004522
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523 /* Cool... it's the right one */
4524
4525 asession_temp = &local_asession;
4526
Willy Tarreau63963c62007-05-13 21:29:55 +02004527 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004528 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4529 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4530 return;
4531 }
4532
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004533 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4534 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004535 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004536
Willy Tarreau58f10d72006-12-04 02:26:12 +01004537 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004538 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4539 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004540 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004541 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004542 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004543 Alert("Not enough memory process_cli():asession:calloc().\n");
4544 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4545 return;
4546 }
4547
4548 asession_temp->sessid = local_asession.sessid;
4549 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004550 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004551 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004552 } else {
4553 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004554 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004555 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004556 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004557 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004558 Alert("Found Application Session without matching server.\n");
4559 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004560 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004561 while (srv) {
4562 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004563 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004564 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004565 txn->flags &= ~TX_CK_MASK;
4566 txn->flags |= TX_CK_VALID;
4567 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004568 t->srv = srv;
4569 break;
4570 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004571 txn->flags &= ~TX_CK_MASK;
4572 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004573 }
4574 }
4575 srv = srv->next;
4576 }/* end while(srv) */
4577 }/* end else if server == NULL */
4578
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004579 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004580 asession_temp->request_count++;
4581#if defined(DEBUG_HASH)
4582 Alert("manage_client_side_cookies\n");
4583 appsession_hash_dump(&(t->be->htbl_proxy));
4584#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004585 }/* end if ((t->proxy->appsession_name != NULL) ... */
4586 }
4587
4588 /* we'll have to look for another cookie ... */
4589 p1 = p4;
4590 } /* while (p1 < cur_end) */
4591
4592 /* There's no more cookie on this line.
4593 * We may have marked the last one(s) for deletion.
4594 * We must do this now in two ways :
4595 * - if there is no app cookie, we simply delete the header ;
4596 * - if there are app cookies, we must delete the end of the
4597 * string properly, including the colon/semi-colon before
4598 * the cookie name.
4599 */
4600 if (del_cookie != NULL) {
4601 int delta;
4602 if (app_cookies) {
4603 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4604 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004605 cur_hdr->len += delta;
4606 } else {
4607 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004608
4609 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004610 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4611 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004612 cur_hdr->len = 0;
4613 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004614 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004615 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004616 }
4617
4618 /* keep the link from this header to next one */
4619 old_idx = cur_idx;
4620 } /* end of cookie processing on this header */
4621}
4622
4623
Willy Tarreaua15645d2007-03-18 16:22:39 +01004624/* Iterate the same filter through all response headers contained in <rtr>.
4625 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4626 */
4627int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4628{
4629 char term;
4630 char *cur_ptr, *cur_end, *cur_next;
4631 int cur_idx, old_idx, last_hdr;
4632 struct http_txn *txn = &t->txn;
4633 struct hdr_idx_elem *cur_hdr;
4634 int len, delta;
4635
4636 last_hdr = 0;
4637
4638 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4639 old_idx = 0;
4640
4641 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004642 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004643 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004644 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004645 (exp->action == ACT_ALLOW ||
4646 exp->action == ACT_DENY))
4647 return 0;
4648
4649 cur_idx = txn->hdr_idx.v[old_idx].next;
4650 if (!cur_idx)
4651 break;
4652
4653 cur_hdr = &txn->hdr_idx.v[cur_idx];
4654 cur_ptr = cur_next;
4655 cur_end = cur_ptr + cur_hdr->len;
4656 cur_next = cur_end + cur_hdr->cr + 1;
4657
4658 /* Now we have one header between cur_ptr and cur_end,
4659 * and the next header starts at cur_next.
4660 */
4661
4662 /* The annoying part is that pattern matching needs
4663 * that we modify the contents to null-terminate all
4664 * strings before testing them.
4665 */
4666
4667 term = *cur_end;
4668 *cur_end = '\0';
4669
4670 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4671 switch (exp->action) {
4672 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004673 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674 last_hdr = 1;
4675 break;
4676
4677 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004678 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004679 last_hdr = 1;
4680 break;
4681
4682 case ACT_REPLACE:
4683 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4684 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4685 /* FIXME: if the user adds a newline in the replacement, the
4686 * index will not be recalculated for now, and the new line
4687 * will not be counted as a new header.
4688 */
4689
4690 cur_end += delta;
4691 cur_next += delta;
4692 cur_hdr->len += delta;
4693 txn->rsp.eoh += delta;
4694 break;
4695
4696 case ACT_REMOVE:
4697 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4698 cur_next += delta;
4699
4700 /* FIXME: this should be a separate function */
4701 txn->rsp.eoh += delta;
4702 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4703 txn->hdr_idx.used--;
4704 cur_hdr->len = 0;
4705 cur_end = NULL; /* null-term has been rewritten */
4706 break;
4707
4708 }
4709 }
4710 if (cur_end)
4711 *cur_end = term; /* restore the string terminator */
4712
4713 /* keep the link from this header to next one in case of later
4714 * removal of next header.
4715 */
4716 old_idx = cur_idx;
4717 }
4718 return 0;
4719}
4720
4721
4722/* Apply the filter to the status line in the response buffer <rtr>.
4723 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4724 * or -1 if a replacement resulted in an invalid status line.
4725 */
4726int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4727{
4728 char term;
4729 char *cur_ptr, *cur_end;
4730 int done;
4731 struct http_txn *txn = &t->txn;
4732 int len, delta;
4733
4734
Willy Tarreau3d300592007-03-18 18:34:41 +01004735 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004736 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004737 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004738 (exp->action == ACT_ALLOW ||
4739 exp->action == ACT_DENY))
4740 return 0;
4741 else if (exp->action == ACT_REMOVE)
4742 return 0;
4743
4744 done = 0;
4745
Willy Tarreau9cdde232007-05-02 20:58:19 +02004746 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004747 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4748
4749 /* Now we have the status line between cur_ptr and cur_end */
4750
4751 /* The annoying part is that pattern matching needs
4752 * that we modify the contents to null-terminate all
4753 * strings before testing them.
4754 */
4755
4756 term = *cur_end;
4757 *cur_end = '\0';
4758
4759 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4760 switch (exp->action) {
4761 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004762 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 done = 1;
4764 break;
4765
4766 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004767 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004768 done = 1;
4769 break;
4770
4771 case ACT_REPLACE:
4772 *cur_end = term; /* restore the string terminator */
4773 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4774 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4775 /* FIXME: if the user adds a newline in the replacement, the
4776 * index will not be recalculated for now, and the new line
4777 * will not be counted as a new header.
4778 */
4779
4780 txn->rsp.eoh += delta;
4781 cur_end += delta;
4782
Willy Tarreau9cdde232007-05-02 20:58:19 +02004783 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004784 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004785 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004786 cur_ptr, cur_end + 1,
4787 NULL, NULL);
4788 if (unlikely(!cur_end))
4789 return -1;
4790
4791 /* we have a full respnse and we know that we have either a CR
4792 * or an LF at <ptr>.
4793 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004794 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004795 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4796 /* there is no point trying this regex on headers */
4797 return 1;
4798 }
4799 }
4800 *cur_end = term; /* restore the string terminator */
4801 return done;
4802}
4803
4804
4805
4806/*
4807 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4808 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4809 * unparsable response.
4810 */
4811int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4812{
Willy Tarreau3d300592007-03-18 18:34:41 +01004813 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004814 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004815 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816 int ret;
4817
4818 /*
4819 * The interleaving of transformations and verdicts
4820 * makes it difficult to decide to continue or stop
4821 * the evaluation.
4822 */
4823
Willy Tarreau3d300592007-03-18 18:34:41 +01004824 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004825 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4826 exp->action == ACT_PASS)) {
4827 exp = exp->next;
4828 continue;
4829 }
4830
4831 /* Apply the filter to the status line. */
4832 ret = apply_filter_to_sts_line(t, rtr, exp);
4833 if (unlikely(ret < 0))
4834 return -1;
4835
4836 if (likely(ret == 0)) {
4837 /* The filter did not match the response, it can be
4838 * iterated through all headers.
4839 */
4840 apply_filter_to_resp_headers(t, rtr, exp);
4841 }
4842 exp = exp->next;
4843 }
4844 return 0;
4845}
4846
4847
4848
4849/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004850 * Manage server-side cookies. It can impact performance by about 2% so it is
4851 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004852 */
4853void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4854{
4855 struct http_txn *txn = &t->txn;
4856 char *p1, *p2, *p3, *p4;
4857
4858 appsess *asession_temp = NULL;
4859 appsess local_asession;
4860
4861 char *cur_ptr, *cur_end, *cur_next;
4862 int cur_idx, old_idx, delta;
4863
Willy Tarreaua15645d2007-03-18 16:22:39 +01004864 /* Iterate through the headers.
4865 * we start with the start line.
4866 */
4867 old_idx = 0;
4868 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4869
4870 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4871 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004872 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004873
4874 cur_hdr = &txn->hdr_idx.v[cur_idx];
4875 cur_ptr = cur_next;
4876 cur_end = cur_ptr + cur_hdr->len;
4877 cur_next = cur_end + cur_hdr->cr + 1;
4878
4879 /* We have one full header between cur_ptr and cur_end, and the
4880 * next header starts at cur_next. We're only interested in
4881 * "Cookie:" headers.
4882 */
4883
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004884 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4885 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004886 old_idx = cur_idx;
4887 continue;
4888 }
4889
4890 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004891 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004892
4893
4894 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004895 if (t->be->cookie_name == NULL &&
4896 t->be->appsession_name == NULL &&
4897 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004898 return;
4899
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004900 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004901
4902 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4904 break;
4905
4906 /* p1 is at the beginning of the cookie name */
4907 p2 = p1;
4908
4909 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4910 p2++;
4911
4912 if (p2 == cur_end || *p2 == ';') /* next cookie */
4913 break;
4914
4915 p3 = p2 + 1; /* skip the '=' sign */
4916 if (p3 == cur_end)
4917 break;
4918
4919 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004920 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004921 p4++;
4922
4923 /* here, we have the cookie name between p1 and p2,
4924 * and its value between p3 and p4.
4925 * we can process it.
4926 */
4927
4928 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004929 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004930 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004931 (p4 - p1 >= t->be->capture_namelen) &&
4932 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004933 int log_len = p4 - p1;
4934
Willy Tarreau086b3b42007-05-13 21:45:51 +02004935 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004936 Alert("HTTP logging : out of memory.\n");
4937 }
4938
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004939 if (log_len > t->be->capture_len)
4940 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004941 memcpy(txn->srv_cookie, p1, log_len);
4942 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004943 }
4944
4945 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004946 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4947 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004949 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004950
4951 /* If the cookie is in insert mode on a known server, we'll delete
4952 * this occurrence because we'll insert another one later.
4953 * We'll delete it too if the "indirect" option is set and we're in
4954 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004955 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4956 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004957 /* this header must be deleted */
4958 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4959 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4960 txn->hdr_idx.used--;
4961 cur_hdr->len = 0;
4962 cur_next += delta;
4963 txn->rsp.eoh += delta;
4964
Willy Tarreau3d300592007-03-18 18:34:41 +01004965 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004966 }
4967 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004968 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004969 /* replace bytes p3->p4 with the cookie name associated
4970 * with this server since we know it.
4971 */
4972 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4973 cur_hdr->len += delta;
4974 cur_next += delta;
4975 txn->rsp.eoh += delta;
4976
Willy Tarreau3d300592007-03-18 18:34:41 +01004977 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004978 }
4979 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004980 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004981 /* insert the cookie name associated with this server
4982 * before existing cookie, and insert a delimitor between them..
4983 */
4984 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4985 cur_hdr->len += delta;
4986 cur_next += delta;
4987 txn->rsp.eoh += delta;
4988
4989 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004990 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991 }
4992 }
4993 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004994 else if ((t->be->appsession_name != NULL) &&
4995 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004996
4997 /* Cool... it's the right one */
4998
4999 size_t server_id_len = strlen(t->srv->id) + 1;
5000 asession_temp = &local_asession;
5001
Willy Tarreau63963c62007-05-13 21:29:55 +02005002 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005003 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5004 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5005 return;
5006 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005007 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5008 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005009 asession_temp->serverid = NULL;
5010
5011 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005012 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5013 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005014 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005015 Alert("Not enough Memory process_srv():asession:calloc().\n");
5016 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5017 return;
5018 }
5019 asession_temp->sessid = local_asession.sessid;
5020 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005021 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005022 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5023 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005024 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005025 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005026 }
5027
Willy Tarreaua15645d2007-03-18 16:22:39 +01005028 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005029 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005030 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5031 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5032 return;
5033 }
5034 asession_temp->serverid[0] = '\0';
5035 }
5036
5037 if (asession_temp->serverid[0] == '\0')
5038 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5039
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005040 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005041 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005043 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005044 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005045#endif
5046 }/* end if ((t->proxy->appsession_name != NULL) ... */
5047 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5048 } /* we're now at the end of the cookie value */
5049
5050 /* keep the link from this header to next one */
5051 old_idx = cur_idx;
5052 } /* end of cookie processing on this header */
5053}
5054
5055
5056
5057/*
5058 * Check if response is cacheable or not. Updates t->flags.
5059 */
5060void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5061{
5062 struct http_txn *txn = &t->txn;
5063 char *p1, *p2;
5064
5065 char *cur_ptr, *cur_end, *cur_next;
5066 int cur_idx;
5067
Willy Tarreau5df51872007-11-25 16:20:08 +01005068 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069 return;
5070
5071 /* Iterate through the headers.
5072 * we start with the start line.
5073 */
5074 cur_idx = 0;
5075 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5076
5077 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5078 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005079 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005080
5081 cur_hdr = &txn->hdr_idx.v[cur_idx];
5082 cur_ptr = cur_next;
5083 cur_end = cur_ptr + cur_hdr->len;
5084 cur_next = cur_end + cur_hdr->cr + 1;
5085
5086 /* We have one full header between cur_ptr and cur_end, and the
5087 * next header starts at cur_next. We're only interested in
5088 * "Cookie:" headers.
5089 */
5090
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005091 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5092 if (val) {
5093 if ((cur_end - (cur_ptr + val) >= 8) &&
5094 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5095 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5096 return;
5097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005098 }
5099
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005100 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5101 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005102 continue;
5103
5104 /* OK, right now we know we have a cache-control header at cur_ptr */
5105
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005106 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005107
5108 if (p1 >= cur_end) /* no more info */
5109 continue;
5110
5111 /* p1 is at the beginning of the value */
5112 p2 = p1;
5113
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005114 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005115 p2++;
5116
5117 /* we have a complete value between p1 and p2 */
5118 if (p2 < cur_end && *p2 == '=') {
5119 /* we have something of the form no-cache="set-cookie" */
5120 if ((cur_end - p1 >= 21) &&
5121 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5122 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005123 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005124 continue;
5125 }
5126
5127 /* OK, so we know that either p2 points to the end of string or to a comma */
5128 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5129 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5130 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5131 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005132 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005133 return;
5134 }
5135
5136 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005137 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005138 continue;
5139 }
5140 }
5141}
5142
5143
Willy Tarreau58f10d72006-12-04 02:26:12 +01005144/*
5145 * Try to retrieve a known appsession in the URI, then the associated server.
5146 * If the server is found, it's assigned to the session.
5147 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005148void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005149{
Willy Tarreau3d300592007-03-18 18:34:41 +01005150 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005151 appsess *asession_temp = NULL;
5152 appsess local_asession;
5153 char *request_line;
5154
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005155 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005156 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005158 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159 return;
5160
5161 /* skip ';' */
5162 request_line++;
5163
5164 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005165 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005166 return;
5167
5168 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005169 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005170
5171 /* First try if we already have an appsession */
5172 asession_temp = &local_asession;
5173
Willy Tarreau63963c62007-05-13 21:29:55 +02005174 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005175 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5176 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5177 return;
5178 }
5179
5180 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005181 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5182 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005183 asession_temp->serverid = NULL;
5184
5185 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005186 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5187 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005188 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005189 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005190 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005191 Alert("Not enough memory process_cli():asession:calloc().\n");
5192 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5193 return;
5194 }
5195 asession_temp->sessid = local_asession.sessid;
5196 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005197 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005198 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005199 }
5200 else {
5201 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005202 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005203 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005204
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005205 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005207
Willy Tarreau58f10d72006-12-04 02:26:12 +01005208#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005209 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005210 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005211#endif
5212 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005213 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 Alert("Found Application Session without matching server.\n");
5215 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005216 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217 while (srv) {
5218 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005219 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005220 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005221 txn->flags &= ~TX_CK_MASK;
5222 txn->flags |= TX_CK_VALID;
5223 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 t->srv = srv;
5225 break;
5226 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005227 txn->flags &= ~TX_CK_MASK;
5228 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005229 }
5230 }
5231 srv = srv->next;
5232 }
5233 }
5234}
5235
5236
Willy Tarreaub2513902006-12-17 14:52:38 +01005237/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005238 * In a GET or HEAD request, check if the requested URI matches the stats uri
5239 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005240 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005241 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005242 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005243 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005244 *
5245 * Returns 1 if the session's state changes, otherwise 0.
5246 */
5247int stats_check_uri_auth(struct session *t, struct proxy *backend)
5248{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005249 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005250 struct uri_auth *uri_auth = backend->uri_auth;
5251 struct user_auth *user;
5252 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005253 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005254
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005255 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5256
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005257 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005258 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005259 return 0;
5260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005261 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005262
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005263 /* the URI is in h */
5264 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005265 return 0;
5266
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005267 h += uri_auth->uri_len;
5268 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5269 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005270 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005271 break;
5272 }
5273 h++;
5274 }
5275
5276 if (uri_auth->refresh) {
5277 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5278 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5279 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005280 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005281 break;
5282 }
5283 h++;
5284 }
5285 }
5286
Willy Tarreau55bb8452007-10-17 18:44:57 +02005287 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5288 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5289 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005290 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005291 break;
5292 }
5293 h++;
5294 }
5295
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005296 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5297
Willy Tarreaub2513902006-12-17 14:52:38 +01005298 /* we are in front of a interceptable URI. Let's check
5299 * if there's an authentication and if it's valid.
5300 */
5301 user = uri_auth->users;
5302 if (!user) {
5303 /* no user auth required, it's OK */
5304 authenticated = 1;
5305 } else {
5306 authenticated = 0;
5307
5308 /* a user list is defined, we have to check.
5309 * skip 21 chars for "Authorization: Basic ".
5310 */
5311
5312 /* FIXME: this should move to an earlier place */
5313 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005314 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5315 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5316 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005317 if (len > 14 &&
5318 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005319 txn->auth_hdr.str = h;
5320 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005321 break;
5322 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005323 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005324 }
5325
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005326 if (txn->auth_hdr.len < 21 ||
5327 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005328 user = NULL;
5329
5330 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005331 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5332 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005333 user->user_pwd, user->user_len)) {
5334 authenticated = 1;
5335 break;
5336 }
5337 user = user->next;
5338 }
5339 }
5340
5341 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005342 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005343
5344 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005345 msg.str = trash;
5346 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005347 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005348 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005349 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005350 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005351 if (!(t->flags & SN_ERR_MASK))
5352 t->flags |= SN_ERR_PRXCOND;
5353 if (!(t->flags & SN_FINST_MASK))
5354 t->flags |= SN_FINST_R;
5355 return 1;
5356 }
5357
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005358 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005359 * data.
5360 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01005361 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02005362 buffer_shutw_now(t->req);
5363 buffer_shutr_now(t->rep);
5364 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005365 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005366 t->data_source = DATA_SRC_STATS;
5367 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005368 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005369 produce_content(t);
5370 return 1;
5371}
5372
5373
Willy Tarreaubaaee002006-06-26 02:48:02 +02005374/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005375 * Print a debug line with a header
5376 */
5377void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5378{
5379 int len, max;
5380 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005381 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005382 max = end - start;
5383 UBOUND(max, sizeof(trash) - len - 1);
5384 len += strlcpy2(trash + len, start, max + 1);
5385 trash[len++] = '\n';
5386 write(1, trash, len);
5387}
5388
5389
Willy Tarreau8797c062007-05-07 00:55:35 +02005390/************************************************************************/
5391/* The code below is dedicated to ACL parsing and matching */
5392/************************************************************************/
5393
5394
5395
5396
5397/* 1. Check on METHOD
5398 * We use the pre-parsed method if it is known, and store its number as an
5399 * integer. If it is unknown, we use the pointer and the length.
5400 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005401static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005402{
5403 int len, meth;
5404
Willy Tarreauae8b7962007-06-09 23:10:04 +02005405 len = strlen(*text);
5406 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005407
5408 pattern->val.i = meth;
5409 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005410 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005411 if (!pattern->ptr.str)
5412 return 0;
5413 pattern->len = len;
5414 }
5415 return 1;
5416}
5417
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005418static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005419acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5420 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005421{
5422 int meth;
5423 struct http_txn *txn = l7;
5424
Willy Tarreaub6866442008-07-14 23:54:42 +02005425 if (!txn)
5426 return 0;
5427
Willy Tarreauc11416f2007-06-17 16:58:38 +02005428 if (txn->req.msg_state != HTTP_MSG_BODY)
5429 return 0;
5430
Willy Tarreau8797c062007-05-07 00:55:35 +02005431 meth = txn->meth;
5432 test->i = meth;
5433 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005434 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5435 /* ensure the indexes are not affected */
5436 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005437 test->len = txn->req.sl.rq.m_l;
5438 test->ptr = txn->req.sol;
5439 }
5440 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5441 return 1;
5442}
5443
5444static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5445{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005446 int icase;
5447
Willy Tarreau8797c062007-05-07 00:55:35 +02005448 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005449 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005450
5451 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005452 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005453
5454 /* Other method, we must compare the strings */
5455 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005456 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005457
5458 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5459 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5460 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005461 return ACL_PAT_FAIL;
5462 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005463}
5464
5465/* 2. Check on Request/Status Version
5466 * We simply compare strings here.
5467 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005468static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005469{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005470 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005471 if (!pattern->ptr.str)
5472 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005473 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005474 return 1;
5475}
5476
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005477static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005478acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5479 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005480{
5481 struct http_txn *txn = l7;
5482 char *ptr;
5483 int len;
5484
Willy Tarreaub6866442008-07-14 23:54:42 +02005485 if (!txn)
5486 return 0;
5487
Willy Tarreauc11416f2007-06-17 16:58:38 +02005488 if (txn->req.msg_state != HTTP_MSG_BODY)
5489 return 0;
5490
Willy Tarreau8797c062007-05-07 00:55:35 +02005491 len = txn->req.sl.rq.v_l;
5492 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5493
5494 while ((len-- > 0) && (*ptr++ != '/'));
5495 if (len <= 0)
5496 return 0;
5497
5498 test->ptr = ptr;
5499 test->len = len;
5500
5501 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5502 return 1;
5503}
5504
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005505static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005506acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5507 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005508{
5509 struct http_txn *txn = l7;
5510 char *ptr;
5511 int len;
5512
Willy Tarreaub6866442008-07-14 23:54:42 +02005513 if (!txn)
5514 return 0;
5515
Willy Tarreauc11416f2007-06-17 16:58:38 +02005516 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5517 return 0;
5518
Willy Tarreau8797c062007-05-07 00:55:35 +02005519 len = txn->rsp.sl.st.v_l;
5520 ptr = txn->rsp.sol;
5521
5522 while ((len-- > 0) && (*ptr++ != '/'));
5523 if (len <= 0)
5524 return 0;
5525
5526 test->ptr = ptr;
5527 test->len = len;
5528
5529 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5530 return 1;
5531}
5532
5533/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005534static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005535acl_fetch_stcode(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->rsp.msg_state != HTTP_MSG_BODY)
5546 return 0;
5547
Willy Tarreau8797c062007-05-07 00:55:35 +02005548 len = txn->rsp.sl.st.c_l;
5549 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5550
5551 test->i = __strl2ui(ptr, len);
5552 test->flags = ACL_TEST_F_VOL_1ST;
5553 return 1;
5554}
5555
5556/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005557static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005558acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5559 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005560{
5561 struct http_txn *txn = l7;
5562
Willy Tarreaub6866442008-07-14 23:54:42 +02005563 if (!txn)
5564 return 0;
5565
Willy Tarreauc11416f2007-06-17 16:58:38 +02005566 if (txn->req.msg_state != HTTP_MSG_BODY)
5567 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005568
Willy Tarreauc11416f2007-06-17 16:58:38 +02005569 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5570 /* ensure the indexes are not affected */
5571 return 0;
5572
Willy Tarreau8797c062007-05-07 00:55:35 +02005573 test->len = txn->req.sl.rq.u_l;
5574 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5575
Willy Tarreauf3d25982007-05-08 22:45:09 +02005576 /* we do not need to set READ_ONLY because the data is in a buffer */
5577 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005578 return 1;
5579}
5580
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005581static int
5582acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5583 struct acl_expr *expr, struct acl_test *test)
5584{
5585 struct http_txn *txn = l7;
5586
Willy Tarreaub6866442008-07-14 23:54:42 +02005587 if (!txn)
5588 return 0;
5589
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005590 if (txn->req.msg_state != HTTP_MSG_BODY)
5591 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005592
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005593 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5594 /* ensure the indexes are not affected */
5595 return 0;
5596
5597 /* Parse HTTP request */
5598 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5599 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5600 test->i = AF_INET;
5601
5602 /*
5603 * If we are parsing url in frontend space, we prepare backend stage
5604 * to not parse again the same url ! optimization lazyness...
5605 */
5606 if (px->options & PR_O_HTTP_PROXY)
5607 l4->flags |= SN_ADDR_SET;
5608
5609 test->flags = ACL_TEST_F_READ_ONLY;
5610 return 1;
5611}
5612
5613static int
5614acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5615 struct acl_expr *expr, struct acl_test *test)
5616{
5617 struct http_txn *txn = l7;
5618
Willy Tarreaub6866442008-07-14 23:54:42 +02005619 if (!txn)
5620 return 0;
5621
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005622 if (txn->req.msg_state != HTTP_MSG_BODY)
5623 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005624
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005625 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5626 /* ensure the indexes are not affected */
5627 return 0;
5628
5629 /* Same optimization as url_ip */
5630 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5631 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5632
5633 if (px->options & PR_O_HTTP_PROXY)
5634 l4->flags |= SN_ADDR_SET;
5635
5636 test->flags = ACL_TEST_F_READ_ONLY;
5637 return 1;
5638}
5639
Willy Tarreauc11416f2007-06-17 16:58:38 +02005640/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5641 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5642 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005643static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005644acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005645 struct acl_expr *expr, struct acl_test *test)
5646{
5647 struct http_txn *txn = l7;
5648 struct hdr_idx *idx = &txn->hdr_idx;
5649 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005650
Willy Tarreaub6866442008-07-14 23:54:42 +02005651 if (!txn)
5652 return 0;
5653
Willy Tarreau33a7e692007-06-10 19:45:56 +02005654 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5655 /* search for header from the beginning */
5656 ctx->idx = 0;
5657
Willy Tarreau33a7e692007-06-10 19:45:56 +02005658 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5659 test->flags |= ACL_TEST_F_FETCH_MORE;
5660 test->flags |= ACL_TEST_F_VOL_HDR;
5661 test->len = ctx->vlen;
5662 test->ptr = (char *)ctx->line + ctx->val;
5663 return 1;
5664 }
5665
5666 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5667 test->flags |= ACL_TEST_F_VOL_HDR;
5668 return 0;
5669}
5670
Willy Tarreau33a7e692007-06-10 19:45:56 +02005671static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005672acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5673 struct acl_expr *expr, struct acl_test *test)
5674{
5675 struct http_txn *txn = l7;
5676
Willy Tarreaub6866442008-07-14 23:54:42 +02005677 if (!txn)
5678 return 0;
5679
Willy Tarreauc11416f2007-06-17 16:58:38 +02005680 if (txn->req.msg_state != HTTP_MSG_BODY)
5681 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005682
Willy Tarreauc11416f2007-06-17 16:58:38 +02005683 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5684 /* ensure the indexes are not affected */
5685 return 0;
5686
5687 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5688}
5689
5690static int
5691acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5692 struct acl_expr *expr, struct acl_test *test)
5693{
5694 struct http_txn *txn = l7;
5695
Willy Tarreaub6866442008-07-14 23:54:42 +02005696 if (!txn)
5697 return 0;
5698
Willy Tarreauc11416f2007-06-17 16:58:38 +02005699 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5700 return 0;
5701
5702 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5703}
5704
5705/* 6. Check on HTTP header count. The number of occurrences is returned.
5706 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5707 */
5708static int
5709acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005710 struct acl_expr *expr, struct acl_test *test)
5711{
5712 struct http_txn *txn = l7;
5713 struct hdr_idx *idx = &txn->hdr_idx;
5714 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005715 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005716
Willy Tarreaub6866442008-07-14 23:54:42 +02005717 if (!txn)
5718 return 0;
5719
Willy Tarreau33a7e692007-06-10 19:45:56 +02005720 ctx.idx = 0;
5721 cnt = 0;
5722 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5723 cnt++;
5724
5725 test->i = cnt;
5726 test->flags = ACL_TEST_F_VOL_HDR;
5727 return 1;
5728}
5729
Willy Tarreauc11416f2007-06-17 16:58:38 +02005730static int
5731acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5732 struct acl_expr *expr, struct acl_test *test)
5733{
5734 struct http_txn *txn = l7;
5735
Willy Tarreaub6866442008-07-14 23:54:42 +02005736 if (!txn)
5737 return 0;
5738
Willy Tarreauc11416f2007-06-17 16:58:38 +02005739 if (txn->req.msg_state != HTTP_MSG_BODY)
5740 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005741
Willy Tarreauc11416f2007-06-17 16:58:38 +02005742 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5743 /* ensure the indexes are not affected */
5744 return 0;
5745
5746 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5747}
5748
5749static int
5750acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5751 struct acl_expr *expr, struct acl_test *test)
5752{
5753 struct http_txn *txn = l7;
5754
Willy Tarreaub6866442008-07-14 23:54:42 +02005755 if (!txn)
5756 return 0;
5757
Willy Tarreauc11416f2007-06-17 16:58:38 +02005758 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5759 return 0;
5760
5761 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5762}
5763
Willy Tarreau33a7e692007-06-10 19:45:56 +02005764/* 7. Check on HTTP header's integer value. The integer value is returned.
5765 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005766 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005767 */
5768static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005769acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005770 struct acl_expr *expr, struct acl_test *test)
5771{
5772 struct http_txn *txn = l7;
5773 struct hdr_idx *idx = &txn->hdr_idx;
5774 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005775
Willy Tarreaub6866442008-07-14 23:54:42 +02005776 if (!txn)
5777 return 0;
5778
Willy Tarreau33a7e692007-06-10 19:45:56 +02005779 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5780 /* search for header from the beginning */
5781 ctx->idx = 0;
5782
Willy Tarreau33a7e692007-06-10 19:45:56 +02005783 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5784 test->flags |= ACL_TEST_F_FETCH_MORE;
5785 test->flags |= ACL_TEST_F_VOL_HDR;
5786 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5787 return 1;
5788 }
5789
5790 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5791 test->flags |= ACL_TEST_F_VOL_HDR;
5792 return 0;
5793}
5794
Willy Tarreauc11416f2007-06-17 16:58:38 +02005795static int
5796acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5797 struct acl_expr *expr, struct acl_test *test)
5798{
5799 struct http_txn *txn = l7;
5800
Willy Tarreaub6866442008-07-14 23:54:42 +02005801 if (!txn)
5802 return 0;
5803
Willy Tarreauc11416f2007-06-17 16:58:38 +02005804 if (txn->req.msg_state != HTTP_MSG_BODY)
5805 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005806
Willy Tarreauc11416f2007-06-17 16:58:38 +02005807 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5808 /* ensure the indexes are not affected */
5809 return 0;
5810
5811 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5812}
5813
5814static int
5815acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5816 struct acl_expr *expr, struct acl_test *test)
5817{
5818 struct http_txn *txn = l7;
5819
Willy Tarreaub6866442008-07-14 23:54:42 +02005820 if (!txn)
5821 return 0;
5822
Willy Tarreauc11416f2007-06-17 16:58:38 +02005823 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5824 return 0;
5825
5826 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5827}
5828
Willy Tarreau737b0c12007-06-10 21:28:46 +02005829/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5830 * the first '/' after the possible hostname, and ends before the possible '?'.
5831 */
5832static int
5833acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5834 struct acl_expr *expr, struct acl_test *test)
5835{
5836 struct http_txn *txn = l7;
5837 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005838
Willy Tarreaub6866442008-07-14 23:54:42 +02005839 if (!txn)
5840 return 0;
5841
Willy Tarreauc11416f2007-06-17 16:58:38 +02005842 if (txn->req.msg_state != HTTP_MSG_BODY)
5843 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005844
Willy Tarreauc11416f2007-06-17 16:58:38 +02005845 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5846 /* ensure the indexes are not affected */
5847 return 0;
5848
Willy Tarreau21d2af32008-02-14 20:25:24 +01005849 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5850 ptr = http_get_path(txn);
5851 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005852 return 0;
5853
5854 /* OK, we got the '/' ! */
5855 test->ptr = ptr;
5856
5857 while (ptr < end && *ptr != '?')
5858 ptr++;
5859
5860 test->len = ptr - test->ptr;
5861
5862 /* we do not need to set READ_ONLY because the data is in a buffer */
5863 test->flags = ACL_TEST_F_VOL_1ST;
5864 return 1;
5865}
5866
5867
Willy Tarreau8797c062007-05-07 00:55:35 +02005868
5869/************************************************************************/
5870/* All supported keywords must be declared here. */
5871/************************************************************************/
5872
5873/* Note: must not be declared <const> as its list will be overwritten */
5874static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005875 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5876 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5877 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5878 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005879
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005880 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5881 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5882 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5883 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5884 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5885 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5886 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5887 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5888 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005889
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005890 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5891 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5892 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5893 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5894 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5895 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5896 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5897 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5898 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5899 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005900
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005901 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5902 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5903 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5904 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5905 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5906 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5907 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5908 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5909 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005910
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005911 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5912 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5913 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5914 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5915 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5916 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5917 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005918
Willy Tarreauf3d25982007-05-08 22:45:09 +02005919 { NULL, NULL, NULL, NULL },
5920
5921#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005922 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5923 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5924 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5925 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5926 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5927 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5928 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5929
Willy Tarreau8797c062007-05-07 00:55:35 +02005930 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5931 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5932 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5933 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5934 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5935 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5936 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5937 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5938
5939 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5940 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5941 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5942 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5943 { NULL, NULL, NULL, NULL },
5944#endif
5945}};
5946
5947
5948__attribute__((constructor))
5949static void __http_protocol_init(void)
5950{
5951 acl_register_keywords(&acl_kws);
5952}
5953
5954
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005956 * Local variables:
5957 * c-indent-level: 8
5958 * c-basic-offset: 8
5959 * End:
5960 */