blob: f73964a3c59b6ae1ddc1ca5d07ed82f8bab9d87b [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 Tarreauba392ce2008-08-16 21:13:23 +0200553 buffer_shutw(t->req);
554 buffer_shutr(t->rep);
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 Tarreaucff64112008-11-03 06:26:53 +0100698 buffer_shutr(si->ib);
699 buffer_shutw(si->ob);
700 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 Tarreaucff64112008-11-03 06:26:53 +0100745 buffer_shutr(si->ib);
746 buffer_shutw(si->ob);
747 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 Tarreaucff64112008-11-03 06:26:53 +0100761 buffer_shutr(si->ib);
762 buffer_shutw(si->ob);
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 Tarreaucff64112008-11-03 06:26:53 +0100778 buffer_shutr(si->ib);
779 buffer_shutw(si->ob);
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 Tarreaucff64112008-11-03 06:26:53 +0100842 buffer_shutr(si->ib);
843 buffer_shutw(si->ob);
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 Tarreaucff64112008-11-03 06:26:53 +0100883 buffer_shutr(si->ib);
884 buffer_shutw(si->ob);
885 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) {
1031 buffer_shutr(s->req);
1032 s->req->cons->shutr(s->req->prod);
1033 }
1034 if (s->req->flags & BF_WRITE_TIMEOUT) {
1035 buffer_shutw(s->req);
1036 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) {
1042 buffer_shutr(s->rep);
1043 s->rep->cons->shutr(s->rep->prod);
1044 }
1045 if (s->rep->flags & BF_WRITE_TIMEOUT) {
1046 buffer_shutw(s->rep);
1047 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 Tarreau48adac52008-08-30 04:58:38 +02001058 buffer_shutw(s->req);
1059 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 */
1064 buffer_shutr_now(s->req);
1065 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 Tarreau48adac52008-08-30 04:58:38 +02001070 buffer_shutw(s->rep);
1071 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 */
1076 buffer_shutr_now(s->rep);
1077 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 Tarreaufa7e1022008-10-19 07:30:41 +02001301 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
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 Tarreaufa7e1022008-10-19 07:30:41 +02003795 req->wex = TICK_ETERNITY;
Willy Tarreaucff64112008-11-03 06:26:53 +01003796 fd_delete(si->fd);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003797 buffer_shutw(req);
3798 buffer_shutr(rep);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01003799 si->state = SI_ST_DIS;
Willy Tarreaucff64112008-11-03 06:26:53 +01003800 si->err_type |= SI_ET_CONN_ABRT;
3801 si->err_loc = s->srv;
3802 return 1;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003803 }
3804
Willy Tarreaucff64112008-11-03 06:26:53 +01003805 /* we need to wait a bit more if there was no activity either */
3806 if (!(req->flags & BF_WRITE_ACTIVITY))
3807 return 1;
3808
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003809 /* OK, this means that a connection succeeded. The caller will be
3810 * responsible for handling the transition from CON to EST.
3811 */
Willy Tarreaucff64112008-11-03 06:26:53 +01003812 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
3813 si->state = SI_ST_EST;
3814 si->err_type = SI_ET_NONE;
3815 si->err_loc = NULL;
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003816 return 1;
3817}
3818
3819
3820/*
3821 * This function handles the transition between the SI_ST_CON state and the
3822 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
3823 * SI_ST_EST.
3824 */
3825void sess_establish(struct session *s, struct stream_interface *si)
3826{
3827 struct buffer *req = si->ob;
3828 struct buffer *rep = si->ib;
Willy Tarreaucff64112008-11-03 06:26:53 +01003829
3830 if (req->flags & BF_EMPTY) {
3831 EV_FD_CLR(si->fd, DIR_WR);
3832 req->wex = TICK_ETERNITY;
3833 } else {
3834 EV_FD_SET(si->fd, DIR_WR);
3835 req->wex = tick_add_ifset(now_ms, s->be->timeout.server);
3836 if (tick_isset(req->wex)) {
3837 /* FIXME: to prevent the server from expiring read
3838 * timeouts during writes, we refresh it. */
3839 rep->rex = req->wex;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003840 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003841 }
3842
Willy Tarreaucff64112008-11-03 06:26:53 +01003843 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3844 if (!(rep->flags & BF_HIJACK)) {
3845 EV_FD_SET(si->fd, DIR_RD);
3846 rep->rex = tick_add_ifset(now_ms, s->be->timeout.server);
3847 }
3848 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003849
Willy Tarreaucff64112008-11-03 06:26:53 +01003850 /* if the user wants to log as soon as possible, without counting
3851 * bytes from the server, then this is the right moment. */
3852 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
3853 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
3854 tcp_sess_log(s);
3855 }
3856#ifdef CONFIG_HAP_TCPSPLICE
3857 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
3858 /* TCP splicing supported by both FE and BE */
3859 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003860 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003861#endif
3862 }
3863 else {
3864 rep->analysers |= AN_RTR_HTTP_HDR;
3865 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3866 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3867 /* reset hdr_idx which was already initialized by the request.
3868 * right now, the http parser does it.
3869 * hdr_idx_init(&s->txn.hdr_idx);
3870 */
3871 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003872
Willy Tarreaucff64112008-11-03 06:26:53 +01003873 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
3874 req->wex = TICK_ETERNITY;
Willy Tarreaucff64112008-11-03 06:26:53 +01003875}
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003876
Willy Tarreaucff64112008-11-03 06:26:53 +01003877
3878/* This function is called with (si->state == SI_ST_CER) meaning that a
3879 * previous connection attempt has failed and that the file descriptor
3880 * has already been released. Possible causes include asynchronous error
3881 * notification and time out. Possible output states are SI_ST_CLO when
3882 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
3883 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
3884 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
3885 * marked as in error state. It returns 0.
3886 */
3887int sess_update_st_cer(struct session *s, struct stream_interface *si)
3888{
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003889 /* we probably have to release last session from the server */
3890 if (s->srv) {
3891 if (s->flags & SN_CURR_SESS) {
3892 s->flags &= ~SN_CURR_SESS;
3893 s->srv->cur_sess--;
3894 }
3895 sess_change_server(s, NULL);
3896 }
3897
Willy Tarreaucff64112008-11-03 06:26:53 +01003898 /* ensure that we have enough retries left */
3899 s->conn_retries--;
3900 if (s->conn_retries < 0) {
3901 if (!si->err_type) {
3902 si->err_type = SI_ET_CONN_ERR;
3903 si->err_loc = s->srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003904 }
3905
Willy Tarreaucff64112008-11-03 06:26:53 +01003906 if (s->srv)
3907 s->srv->failed_conns++;
3908 s->be->failed_conns++;
3909 if (may_dequeue_tasks(s->srv, s->be))
3910 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003911
Willy Tarreaucff64112008-11-03 06:26:53 +01003912 buffer_shutw(si->ob);
3913 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003914
Willy Tarreaucff64112008-11-03 06:26:53 +01003915 buffer_shutr(si->ib);
3916 si->ib->flags |= BF_READ_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003917
Willy Tarreaucff64112008-11-03 06:26:53 +01003918 si->state = SI_ST_CLO;
3919 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003920 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003921
Willy Tarreaucff64112008-11-03 06:26:53 +01003922 /* If the "redispatch" option is set on the backend, we are allowed to
3923 * retry on another server for the last retry. In order to achieve this,
3924 * we must mark the session unassigned, and eventually clear the DIRECT
3925 * bit to ignore any persistence cookie. We won't count a retry nor a
3926 * redispatch yet, because this will depend on what server is selected.
3927 */
3928 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
3929 if (may_dequeue_tasks(s->srv, s->be))
3930 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003931
Willy Tarreaucff64112008-11-03 06:26:53 +01003932 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3933 s->prev_srv = s->srv;
3934 si->state = SI_ST_REQ;
3935 } else {
3936 if (s->srv)
3937 s->srv->retries++;
3938 s->be->retries++;
3939 si->state = SI_ST_ASS;
3940 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003941
Willy Tarreaucff64112008-11-03 06:26:53 +01003942 if (si->flags & SI_FL_ERR) {
3943 /* The error was an asynchronous connection error, and we will
3944 * likely have to retry connecting to the same server, most
3945 * likely leading to the same result. To avoid this, we wait
3946 * one second before retrying.
3947 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003948
Willy Tarreaucff64112008-11-03 06:26:53 +01003949 if (!si->err_type)
3950 si->err_type = SI_ET_CONN_ERR;
3951
3952 si->state = SI_ST_TAR;
3953 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003954 return 0;
3955 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003956 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003957}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003958
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003959
Willy Tarreaubaaee002006-06-26 02:48:02 +02003960/*
3961 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003962 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003963 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3964 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003965 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003966 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003967 */
3968int produce_content(struct session *s)
3969{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003970 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02003971 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003972 return 1;
3973 }
3974 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003975 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003976 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003977 if (ret >= 0)
3978 return ret;
3979 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003980 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003981
Willy Tarreau91861262007-10-17 17:06:05 +02003982 /* unknown data source or internal error */
3983 s->txn.status = 500;
3984 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003985 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003986 if (!(s->flags & SN_ERR_MASK))
3987 s->flags |= SN_ERR_PRXCOND;
3988 if (!(s->flags & SN_FINST_MASK))
3989 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02003990 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02003991 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003992}
3993
3994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003995/* Iterate the same filter through all request headers.
3996 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003997 * Since it can manage the switch to another backend, it updates the per-proxy
3998 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003999 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004000int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004001{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004002 char term;
4003 char *cur_ptr, *cur_end, *cur_next;
4004 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004005 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004006 struct hdr_idx_elem *cur_hdr;
4007 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004009 last_hdr = 0;
4010
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004011 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004012 old_idx = 0;
4013
4014 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004015 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004017 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004018 (exp->action == ACT_ALLOW ||
4019 exp->action == ACT_DENY ||
4020 exp->action == ACT_TARPIT))
4021 return 0;
4022
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004023 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024 if (!cur_idx)
4025 break;
4026
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004027 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 cur_ptr = cur_next;
4029 cur_end = cur_ptr + cur_hdr->len;
4030 cur_next = cur_end + cur_hdr->cr + 1;
4031
4032 /* Now we have one header between cur_ptr and cur_end,
4033 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004034 */
4035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004036 /* The annoying part is that pattern matching needs
4037 * that we modify the contents to null-terminate all
4038 * strings before testing them.
4039 */
4040
4041 term = *cur_end;
4042 *cur_end = '\0';
4043
4044 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4045 switch (exp->action) {
4046 case ACT_SETBE:
4047 /* It is not possible to jump a second time.
4048 * FIXME: should we return an HTTP/500 here so that
4049 * the admin knows there's a problem ?
4050 */
4051 if (t->be != t->fe)
4052 break;
4053
4054 /* Swithing Proxy */
4055 t->be = (struct proxy *) exp->replace;
4056
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004057 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 * because we have associated req_cap and rsp_cap to the
4059 * frontend, and the beconn will be updated later.
4060 */
4061
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004062 t->rep->rto = t->req->wto = t->be->timeout.server;
4063 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004064 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 last_hdr = 1;
4066 break;
4067
4068 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004069 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004070 last_hdr = 1;
4071 break;
4072
4073 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004074 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004075 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004076 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004077 break;
4078
4079 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004080 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004081 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004082 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004083 break;
4084
4085 case ACT_REPLACE:
4086 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4087 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4088 /* FIXME: if the user adds a newline in the replacement, the
4089 * index will not be recalculated for now, and the new line
4090 * will not be counted as a new header.
4091 */
4092
4093 cur_end += delta;
4094 cur_next += delta;
4095 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004096 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004097 break;
4098
4099 case ACT_REMOVE:
4100 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4101 cur_next += delta;
4102
4103 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004104 txn->req.eoh += delta;
4105 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4106 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004107 cur_hdr->len = 0;
4108 cur_end = NULL; /* null-term has been rewritten */
4109 break;
4110
4111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004112 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004113 if (cur_end)
4114 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004115
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004116 /* keep the link from this header to next one in case of later
4117 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004118 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004119 old_idx = cur_idx;
4120 }
4121 return 0;
4122}
4123
4124
4125/* Apply the filter to the request line.
4126 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4127 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004128 * Since it can manage the switch to another backend, it updates the per-proxy
4129 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004130 */
4131int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4132{
4133 char term;
4134 char *cur_ptr, *cur_end;
4135 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004136 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004137 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004138
Willy Tarreau58f10d72006-12-04 02:26:12 +01004139
Willy Tarreau3d300592007-03-18 18:34:41 +01004140 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004141 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004142 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004143 (exp->action == ACT_ALLOW ||
4144 exp->action == ACT_DENY ||
4145 exp->action == ACT_TARPIT))
4146 return 0;
4147 else if (exp->action == ACT_REMOVE)
4148 return 0;
4149
4150 done = 0;
4151
Willy Tarreau9cdde232007-05-02 20:58:19 +02004152 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004153 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004154
4155 /* Now we have the request line between cur_ptr and cur_end */
4156
4157 /* The annoying part is that pattern matching needs
4158 * that we modify the contents to null-terminate all
4159 * strings before testing them.
4160 */
4161
4162 term = *cur_end;
4163 *cur_end = '\0';
4164
4165 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4166 switch (exp->action) {
4167 case ACT_SETBE:
4168 /* It is not possible to jump a second time.
4169 * FIXME: should we return an HTTP/500 here so that
4170 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004172 if (t->be != t->fe)
4173 break;
4174
4175 /* Swithing Proxy */
4176 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004178 /* right now, the backend switch is not too much complicated
4179 * because we have associated req_cap and rsp_cap to the
4180 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004181 */
4182
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004183 t->rep->rto = t->req->wto = t->be->timeout.server;
4184 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004185 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004186 done = 1;
4187 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004190 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004191 done = 1;
4192 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004194 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004195 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004196 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004197 done = 1;
4198 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004199
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004200 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004201 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004202 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004203 done = 1;
4204 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004206 case ACT_REPLACE:
4207 *cur_end = term; /* restore the string terminator */
4208 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4209 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4210 /* FIXME: if the user adds a newline in the replacement, the
4211 * index will not be recalculated for now, and the new line
4212 * will not be counted as a new header.
4213 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004215 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004216 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004217
Willy Tarreau9cdde232007-05-02 20:58:19 +02004218 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004219 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004220 HTTP_MSG_RQMETH,
4221 cur_ptr, cur_end + 1,
4222 NULL, NULL);
4223 if (unlikely(!cur_end))
4224 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004226 /* we have a full request and we know that we have either a CR
4227 * or an LF at <ptr>.
4228 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004229 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4230 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004231 /* there is no point trying this regex on headers */
4232 return 1;
4233 }
4234 }
4235 *cur_end = term; /* restore the string terminator */
4236 return done;
4237}
Willy Tarreau97de6242006-12-27 17:18:38 +01004238
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239
Willy Tarreau58f10d72006-12-04 02:26:12 +01004240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004241/*
4242 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4243 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004244 * unparsable request. Since it can manage the switch to another backend, it
4245 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004246 */
4247int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4248{
Willy Tarreau3d300592007-03-18 18:34:41 +01004249 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004250 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004251 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004252 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004253
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004254 /*
4255 * The interleaving of transformations and verdicts
4256 * makes it difficult to decide to continue or stop
4257 * the evaluation.
4258 */
4259
Willy Tarreau3d300592007-03-18 18:34:41 +01004260 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004261 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4262 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4263 exp = exp->next;
4264 continue;
4265 }
4266
4267 /* Apply the filter to the request line. */
4268 ret = apply_filter_to_req_line(t, req, exp);
4269 if (unlikely(ret < 0))
4270 return -1;
4271
4272 if (likely(ret == 0)) {
4273 /* The filter did not match the request, it can be
4274 * iterated through all headers.
4275 */
4276 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 }
4278 exp = exp->next;
4279 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004280 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004281}
4282
4283
Willy Tarreaua15645d2007-03-18 16:22:39 +01004284
Willy Tarreau58f10d72006-12-04 02:26:12 +01004285/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004286 * Manage client-side cookie. It can impact performance by about 2% so it is
4287 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004288 */
4289void manage_client_side_cookies(struct session *t, struct buffer *req)
4290{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004291 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004292 char *p1, *p2, *p3, *p4;
4293 char *del_colon, *del_cookie, *colon;
4294 int app_cookies;
4295
4296 appsess *asession_temp = NULL;
4297 appsess local_asession;
4298
4299 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004300 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301
Willy Tarreau2a324282006-12-05 00:05:46 +01004302 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303 * we start with the start line.
4304 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004305 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004306 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004308 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004309 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004310 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004311
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004312 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004313 cur_ptr = cur_next;
4314 cur_end = cur_ptr + cur_hdr->len;
4315 cur_next = cur_end + cur_hdr->cr + 1;
4316
4317 /* We have one full header between cur_ptr and cur_end, and the
4318 * next header starts at cur_next. We're only interested in
4319 * "Cookie:" headers.
4320 */
4321
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004322 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4323 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004324 old_idx = cur_idx;
4325 continue;
4326 }
4327
4328 /* Now look for cookies. Conforming to RFC2109, we have to support
4329 * attributes whose name begin with a '$', and associate them with
4330 * the right cookie, if we want to delete this cookie.
4331 * So there are 3 cases for each cookie read :
4332 * 1) it's a special attribute, beginning with a '$' : ignore it.
4333 * 2) it's a server id cookie that we *MAY* want to delete : save
4334 * some pointers on it (last semi-colon, beginning of cookie...)
4335 * 3) it's an application cookie : we *MAY* have to delete a previous
4336 * "special" cookie.
4337 * At the end of loop, if a "special" cookie remains, we may have to
4338 * remove it. If no application cookie persists in the header, we
4339 * *MUST* delete it
4340 */
4341
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004342 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004343
Willy Tarreau58f10d72006-12-04 02:26:12 +01004344 /* del_cookie == NULL => nothing to be deleted */
4345 del_colon = del_cookie = NULL;
4346 app_cookies = 0;
4347
4348 while (p1 < cur_end) {
4349 /* skip spaces and colons, but keep an eye on these ones */
4350 while (p1 < cur_end) {
4351 if (*p1 == ';' || *p1 == ',')
4352 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004353 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354 break;
4355 p1++;
4356 }
4357
4358 if (p1 == cur_end)
4359 break;
4360
4361 /* p1 is at the beginning of the cookie name */
4362 p2 = p1;
4363 while (p2 < cur_end && *p2 != '=')
4364 p2++;
4365
4366 if (p2 == cur_end)
4367 break;
4368
4369 p3 = p2 + 1; /* skips the '=' sign */
4370 if (p3 == cur_end)
4371 break;
4372
4373 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004374 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375 p4++;
4376
4377 /* here, we have the cookie name between p1 and p2,
4378 * and its value between p3 and p4.
4379 * we can process it :
4380 *
4381 * Cookie: NAME=VALUE;
4382 * | || || |
4383 * | || || +--> p4
4384 * | || |+-------> p3
4385 * | || +--------> p2
4386 * | |+------------> p1
4387 * | +-------------> colon
4388 * +--------------------> cur_ptr
4389 */
4390
4391 if (*p1 == '$') {
4392 /* skip this one */
4393 }
4394 else {
4395 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004396 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004397 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 (p4 - p1 >= t->fe->capture_namelen) &&
4399 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004400 int log_len = p4 - p1;
4401
Willy Tarreau086b3b42007-05-13 21:45:51 +02004402 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403 Alert("HTTP logging : out of memory.\n");
4404 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004405 if (log_len > t->fe->capture_len)
4406 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004407 memcpy(txn->cli_cookie, p1, log_len);
4408 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004409 }
4410 }
4411
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004412 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4413 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004414 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004415 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004416 char *delim;
4417
4418 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4419 * have the server ID betweek p3 and delim, and the original cookie between
4420 * delim+1 and p4. Otherwise, delim==p4 :
4421 *
4422 * Cookie: NAME=SRV~VALUE;
4423 * | || || | |
4424 * | || || | +--> p4
4425 * | || || +--------> delim
4426 * | || |+-----------> p3
4427 * | || +------------> p2
4428 * | |+----------------> p1
4429 * | +-----------------> colon
4430 * +------------------------> cur_ptr
4431 */
4432
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004433 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004434 for (delim = p3; delim < p4; delim++)
4435 if (*delim == COOKIE_DELIM)
4436 break;
4437 }
4438 else
4439 delim = p4;
4440
4441
4442 /* Here, we'll look for the first running server which supports the cookie.
4443 * This allows to share a same cookie between several servers, for example
4444 * to dedicate backup servers to specific servers only.
4445 * However, to prevent clients from sticking to cookie-less backup server
4446 * when they have incidentely learned an empty cookie, we simply ignore
4447 * empty cookies and mark them as invalid.
4448 */
4449 if (delim == p3)
4450 srv = NULL;
4451
4452 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004453 if (srv->cookie && (srv->cklen == delim - p3) &&
4454 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004455 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004456 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004457 txn->flags &= ~TX_CK_MASK;
4458 txn->flags |= TX_CK_VALID;
4459 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004460 t->srv = srv;
4461 break;
4462 } else {
4463 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004464 txn->flags &= ~TX_CK_MASK;
4465 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004466 }
4467 }
4468 srv = srv->next;
4469 }
4470
Willy Tarreau3d300592007-03-18 18:34:41 +01004471 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004472 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004473 txn->flags &= ~TX_CK_MASK;
4474 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004475 }
4476
4477 /* depending on the cookie mode, we may have to either :
4478 * - delete the complete cookie if we're in insert+indirect mode, so that
4479 * the server never sees it ;
4480 * - remove the server id from the cookie value, and tag the cookie as an
4481 * application cookie so that it does not get accidentely removed later,
4482 * if we're in cookie prefix mode
4483 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004484 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004485 int delta; /* negative */
4486
4487 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4488 p4 += delta;
4489 cur_end += delta;
4490 cur_next += delta;
4491 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004492 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004493
4494 del_cookie = del_colon = NULL;
4495 app_cookies++; /* protect the header from deletion */
4496 }
4497 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004498 (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 +01004499 del_cookie = p1;
4500 del_colon = colon;
4501 }
4502 } else {
4503 /* now we know that we must keep this cookie since it's
4504 * not ours. But if we wanted to delete our cookie
4505 * earlier, we cannot remove the complete header, but we
4506 * can remove the previous block itself.
4507 */
4508 app_cookies++;
4509
4510 if (del_cookie != NULL) {
4511 int delta; /* negative */
4512
4513 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4514 p4 += delta;
4515 cur_end += delta;
4516 cur_next += delta;
4517 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004518 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519 del_cookie = del_colon = NULL;
4520 }
4521 }
4522
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004523 if ((t->be->appsession_name != NULL) &&
4524 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004525 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004526
Willy Tarreau58f10d72006-12-04 02:26:12 +01004527 /* Cool... it's the right one */
4528
4529 asession_temp = &local_asession;
4530
Willy Tarreau63963c62007-05-13 21:29:55 +02004531 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4533 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4534 return;
4535 }
4536
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004537 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4538 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004539 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004540
Willy Tarreau58f10d72006-12-04 02:26:12 +01004541 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004542 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4543 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004544 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004545 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004546 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004547 Alert("Not enough memory process_cli():asession:calloc().\n");
4548 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4549 return;
4550 }
4551
4552 asession_temp->sessid = local_asession.sessid;
4553 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004554 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004555 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004556 } else {
4557 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004558 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004561 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004562 Alert("Found Application Session without matching server.\n");
4563 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004564 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004565 while (srv) {
4566 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004567 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004568 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004569 txn->flags &= ~TX_CK_MASK;
4570 txn->flags |= TX_CK_VALID;
4571 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004572 t->srv = srv;
4573 break;
4574 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004575 txn->flags &= ~TX_CK_MASK;
4576 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004577 }
4578 }
4579 srv = srv->next;
4580 }/* end while(srv) */
4581 }/* end else if server == NULL */
4582
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004583 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004584 asession_temp->request_count++;
4585#if defined(DEBUG_HASH)
4586 Alert("manage_client_side_cookies\n");
4587 appsession_hash_dump(&(t->be->htbl_proxy));
4588#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004589 }/* end if ((t->proxy->appsession_name != NULL) ... */
4590 }
4591
4592 /* we'll have to look for another cookie ... */
4593 p1 = p4;
4594 } /* while (p1 < cur_end) */
4595
4596 /* There's no more cookie on this line.
4597 * We may have marked the last one(s) for deletion.
4598 * We must do this now in two ways :
4599 * - if there is no app cookie, we simply delete the header ;
4600 * - if there are app cookies, we must delete the end of the
4601 * string properly, including the colon/semi-colon before
4602 * the cookie name.
4603 */
4604 if (del_cookie != NULL) {
4605 int delta;
4606 if (app_cookies) {
4607 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4608 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004609 cur_hdr->len += delta;
4610 } else {
4611 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004612
4613 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004614 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4615 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004616 cur_hdr->len = 0;
4617 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004618 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004619 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004620 }
4621
4622 /* keep the link from this header to next one */
4623 old_idx = cur_idx;
4624 } /* end of cookie processing on this header */
4625}
4626
4627
Willy Tarreaua15645d2007-03-18 16:22:39 +01004628/* Iterate the same filter through all response headers contained in <rtr>.
4629 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4630 */
4631int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4632{
4633 char term;
4634 char *cur_ptr, *cur_end, *cur_next;
4635 int cur_idx, old_idx, last_hdr;
4636 struct http_txn *txn = &t->txn;
4637 struct hdr_idx_elem *cur_hdr;
4638 int len, delta;
4639
4640 last_hdr = 0;
4641
4642 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4643 old_idx = 0;
4644
4645 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004646 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004647 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004648 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004649 (exp->action == ACT_ALLOW ||
4650 exp->action == ACT_DENY))
4651 return 0;
4652
4653 cur_idx = txn->hdr_idx.v[old_idx].next;
4654 if (!cur_idx)
4655 break;
4656
4657 cur_hdr = &txn->hdr_idx.v[cur_idx];
4658 cur_ptr = cur_next;
4659 cur_end = cur_ptr + cur_hdr->len;
4660 cur_next = cur_end + cur_hdr->cr + 1;
4661
4662 /* Now we have one header between cur_ptr and cur_end,
4663 * and the next header starts at cur_next.
4664 */
4665
4666 /* The annoying part is that pattern matching needs
4667 * that we modify the contents to null-terminate all
4668 * strings before testing them.
4669 */
4670
4671 term = *cur_end;
4672 *cur_end = '\0';
4673
4674 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4675 switch (exp->action) {
4676 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004677 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004678 last_hdr = 1;
4679 break;
4680
4681 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004682 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004683 last_hdr = 1;
4684 break;
4685
4686 case ACT_REPLACE:
4687 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4688 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4689 /* FIXME: if the user adds a newline in the replacement, the
4690 * index will not be recalculated for now, and the new line
4691 * will not be counted as a new header.
4692 */
4693
4694 cur_end += delta;
4695 cur_next += delta;
4696 cur_hdr->len += delta;
4697 txn->rsp.eoh += delta;
4698 break;
4699
4700 case ACT_REMOVE:
4701 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4702 cur_next += delta;
4703
4704 /* FIXME: this should be a separate function */
4705 txn->rsp.eoh += delta;
4706 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4707 txn->hdr_idx.used--;
4708 cur_hdr->len = 0;
4709 cur_end = NULL; /* null-term has been rewritten */
4710 break;
4711
4712 }
4713 }
4714 if (cur_end)
4715 *cur_end = term; /* restore the string terminator */
4716
4717 /* keep the link from this header to next one in case of later
4718 * removal of next header.
4719 */
4720 old_idx = cur_idx;
4721 }
4722 return 0;
4723}
4724
4725
4726/* Apply the filter to the status line in the response buffer <rtr>.
4727 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4728 * or -1 if a replacement resulted in an invalid status line.
4729 */
4730int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4731{
4732 char term;
4733 char *cur_ptr, *cur_end;
4734 int done;
4735 struct http_txn *txn = &t->txn;
4736 int len, delta;
4737
4738
Willy Tarreau3d300592007-03-18 18:34:41 +01004739 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004740 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004741 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004742 (exp->action == ACT_ALLOW ||
4743 exp->action == ACT_DENY))
4744 return 0;
4745 else if (exp->action == ACT_REMOVE)
4746 return 0;
4747
4748 done = 0;
4749
Willy Tarreau9cdde232007-05-02 20:58:19 +02004750 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004751 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4752
4753 /* Now we have the status line between cur_ptr and cur_end */
4754
4755 /* The annoying part is that pattern matching needs
4756 * that we modify the contents to null-terminate all
4757 * strings before testing them.
4758 */
4759
4760 term = *cur_end;
4761 *cur_end = '\0';
4762
4763 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4764 switch (exp->action) {
4765 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004766 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767 done = 1;
4768 break;
4769
4770 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004771 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004772 done = 1;
4773 break;
4774
4775 case ACT_REPLACE:
4776 *cur_end = term; /* restore the string terminator */
4777 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4778 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4779 /* FIXME: if the user adds a newline in the replacement, the
4780 * index will not be recalculated for now, and the new line
4781 * will not be counted as a new header.
4782 */
4783
4784 txn->rsp.eoh += delta;
4785 cur_end += delta;
4786
Willy Tarreau9cdde232007-05-02 20:58:19 +02004787 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004788 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004789 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004790 cur_ptr, cur_end + 1,
4791 NULL, NULL);
4792 if (unlikely(!cur_end))
4793 return -1;
4794
4795 /* we have a full respnse and we know that we have either a CR
4796 * or an LF at <ptr>.
4797 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004798 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4800 /* there is no point trying this regex on headers */
4801 return 1;
4802 }
4803 }
4804 *cur_end = term; /* restore the string terminator */
4805 return done;
4806}
4807
4808
4809
4810/*
4811 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4812 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4813 * unparsable response.
4814 */
4815int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4816{
Willy Tarreau3d300592007-03-18 18:34:41 +01004817 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004818 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004819 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004820 int ret;
4821
4822 /*
4823 * The interleaving of transformations and verdicts
4824 * makes it difficult to decide to continue or stop
4825 * the evaluation.
4826 */
4827
Willy Tarreau3d300592007-03-18 18:34:41 +01004828 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004829 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4830 exp->action == ACT_PASS)) {
4831 exp = exp->next;
4832 continue;
4833 }
4834
4835 /* Apply the filter to the status line. */
4836 ret = apply_filter_to_sts_line(t, rtr, exp);
4837 if (unlikely(ret < 0))
4838 return -1;
4839
4840 if (likely(ret == 0)) {
4841 /* The filter did not match the response, it can be
4842 * iterated through all headers.
4843 */
4844 apply_filter_to_resp_headers(t, rtr, exp);
4845 }
4846 exp = exp->next;
4847 }
4848 return 0;
4849}
4850
4851
4852
4853/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004854 * Manage server-side cookies. It can impact performance by about 2% so it is
4855 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856 */
4857void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4858{
4859 struct http_txn *txn = &t->txn;
4860 char *p1, *p2, *p3, *p4;
4861
4862 appsess *asession_temp = NULL;
4863 appsess local_asession;
4864
4865 char *cur_ptr, *cur_end, *cur_next;
4866 int cur_idx, old_idx, delta;
4867
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868 /* Iterate through the headers.
4869 * we start with the start line.
4870 */
4871 old_idx = 0;
4872 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4873
4874 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4875 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004876 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004877
4878 cur_hdr = &txn->hdr_idx.v[cur_idx];
4879 cur_ptr = cur_next;
4880 cur_end = cur_ptr + cur_hdr->len;
4881 cur_next = cur_end + cur_hdr->cr + 1;
4882
4883 /* We have one full header between cur_ptr and cur_end, and the
4884 * next header starts at cur_next. We're only interested in
4885 * "Cookie:" headers.
4886 */
4887
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004888 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4889 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004890 old_idx = cur_idx;
4891 continue;
4892 }
4893
4894 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004895 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004896
4897
4898 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004899 if (t->be->cookie_name == NULL &&
4900 t->be->appsession_name == NULL &&
4901 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004902 return;
4903
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004904 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004905
4906 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004907 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4908 break;
4909
4910 /* p1 is at the beginning of the cookie name */
4911 p2 = p1;
4912
4913 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4914 p2++;
4915
4916 if (p2 == cur_end || *p2 == ';') /* next cookie */
4917 break;
4918
4919 p3 = p2 + 1; /* skip the '=' sign */
4920 if (p3 == cur_end)
4921 break;
4922
4923 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004924 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004925 p4++;
4926
4927 /* here, we have the cookie name between p1 and p2,
4928 * and its value between p3 and p4.
4929 * we can process it.
4930 */
4931
4932 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004933 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004934 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004935 (p4 - p1 >= t->be->capture_namelen) &&
4936 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004937 int log_len = p4 - p1;
4938
Willy Tarreau086b3b42007-05-13 21:45:51 +02004939 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 Alert("HTTP logging : out of memory.\n");
4941 }
4942
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004943 if (log_len > t->be->capture_len)
4944 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004945 memcpy(txn->srv_cookie, p1, log_len);
4946 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004947 }
4948
4949 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004950 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4951 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004952 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004953 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004954
4955 /* If the cookie is in insert mode on a known server, we'll delete
4956 * this occurrence because we'll insert another one later.
4957 * We'll delete it too if the "indirect" option is set and we're in
4958 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004959 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4960 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004961 /* this header must be deleted */
4962 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4963 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4964 txn->hdr_idx.used--;
4965 cur_hdr->len = 0;
4966 cur_next += delta;
4967 txn->rsp.eoh += delta;
4968
Willy Tarreau3d300592007-03-18 18:34:41 +01004969 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004970 }
4971 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004972 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004973 /* replace bytes p3->p4 with the cookie name associated
4974 * with this server since we know it.
4975 */
4976 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4977 cur_hdr->len += delta;
4978 cur_next += delta;
4979 txn->rsp.eoh += delta;
4980
Willy Tarreau3d300592007-03-18 18:34:41 +01004981 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004982 }
4983 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004984 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004985 /* insert the cookie name associated with this server
4986 * before existing cookie, and insert a delimitor between them..
4987 */
4988 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4989 cur_hdr->len += delta;
4990 cur_next += delta;
4991 txn->rsp.eoh += delta;
4992
4993 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004994 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995 }
4996 }
4997 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004998 else if ((t->be->appsession_name != NULL) &&
4999 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005000
5001 /* Cool... it's the right one */
5002
5003 size_t server_id_len = strlen(t->srv->id) + 1;
5004 asession_temp = &local_asession;
5005
Willy Tarreau63963c62007-05-13 21:29:55 +02005006 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005007 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5008 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5009 return;
5010 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005011 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5012 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005013 asession_temp->serverid = NULL;
5014
5015 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005016 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5017 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005018 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005019 Alert("Not enough Memory process_srv():asession:calloc().\n");
5020 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5021 return;
5022 }
5023 asession_temp->sessid = local_asession.sessid;
5024 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005025 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005026 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5027 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005028 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005029 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005030 }
5031
Willy Tarreaua15645d2007-03-18 16:22:39 +01005032 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005033 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005034 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5035 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5036 return;
5037 }
5038 asession_temp->serverid[0] = '\0';
5039 }
5040
5041 if (asession_temp->serverid[0] == '\0')
5042 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5043
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005044 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005045 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005046#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005047 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005048 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005049#endif
5050 }/* end if ((t->proxy->appsession_name != NULL) ... */
5051 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5052 } /* we're now at the end of the cookie value */
5053
5054 /* keep the link from this header to next one */
5055 old_idx = cur_idx;
5056 } /* end of cookie processing on this header */
5057}
5058
5059
5060
5061/*
5062 * Check if response is cacheable or not. Updates t->flags.
5063 */
5064void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5065{
5066 struct http_txn *txn = &t->txn;
5067 char *p1, *p2;
5068
5069 char *cur_ptr, *cur_end, *cur_next;
5070 int cur_idx;
5071
Willy Tarreau5df51872007-11-25 16:20:08 +01005072 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073 return;
5074
5075 /* Iterate through the headers.
5076 * we start with the start line.
5077 */
5078 cur_idx = 0;
5079 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5080
5081 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5082 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005083 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005084
5085 cur_hdr = &txn->hdr_idx.v[cur_idx];
5086 cur_ptr = cur_next;
5087 cur_end = cur_ptr + cur_hdr->len;
5088 cur_next = cur_end + cur_hdr->cr + 1;
5089
5090 /* We have one full header between cur_ptr and cur_end, and the
5091 * next header starts at cur_next. We're only interested in
5092 * "Cookie:" headers.
5093 */
5094
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005095 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5096 if (val) {
5097 if ((cur_end - (cur_ptr + val) >= 8) &&
5098 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5099 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5100 return;
5101 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005102 }
5103
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005104 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5105 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005106 continue;
5107
5108 /* OK, right now we know we have a cache-control header at cur_ptr */
5109
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005110 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005111
5112 if (p1 >= cur_end) /* no more info */
5113 continue;
5114
5115 /* p1 is at the beginning of the value */
5116 p2 = p1;
5117
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005118 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005119 p2++;
5120
5121 /* we have a complete value between p1 and p2 */
5122 if (p2 < cur_end && *p2 == '=') {
5123 /* we have something of the form no-cache="set-cookie" */
5124 if ((cur_end - p1 >= 21) &&
5125 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5126 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005127 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005128 continue;
5129 }
5130
5131 /* OK, so we know that either p2 points to the end of string or to a comma */
5132 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5133 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5134 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5135 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005136 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005137 return;
5138 }
5139
5140 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005141 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005142 continue;
5143 }
5144 }
5145}
5146
5147
Willy Tarreau58f10d72006-12-04 02:26:12 +01005148/*
5149 * Try to retrieve a known appsession in the URI, then the associated server.
5150 * If the server is found, it's assigned to the session.
5151 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005152void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005153{
Willy Tarreau3d300592007-03-18 18:34:41 +01005154 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155 appsess *asession_temp = NULL;
5156 appsess local_asession;
5157 char *request_line;
5158
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005159 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005160 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005161 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005162 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005163 return;
5164
5165 /* skip ';' */
5166 request_line++;
5167
5168 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005169 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005170 return;
5171
5172 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005173 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005174
5175 /* First try if we already have an appsession */
5176 asession_temp = &local_asession;
5177
Willy Tarreau63963c62007-05-13 21:29:55 +02005178 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005179 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5180 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5181 return;
5182 }
5183
5184 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005185 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5186 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005187 asession_temp->serverid = NULL;
5188
5189 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005190 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5191 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005192 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005193 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005194 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005195 Alert("Not enough memory process_cli():asession:calloc().\n");
5196 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5197 return;
5198 }
5199 asession_temp->sessid = local_asession.sessid;
5200 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005201 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005202 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005203 }
5204 else {
5205 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005206 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005207 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005208
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005209 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005210 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005211
Willy Tarreau58f10d72006-12-04 02:26:12 +01005212#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005213 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005214 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005215#endif
5216 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005217 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005218 Alert("Found Application Session without matching server.\n");
5219 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005220 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005221 while (srv) {
5222 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005223 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005225 txn->flags &= ~TX_CK_MASK;
5226 txn->flags |= TX_CK_VALID;
5227 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005228 t->srv = srv;
5229 break;
5230 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005231 txn->flags &= ~TX_CK_MASK;
5232 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 }
5234 }
5235 srv = srv->next;
5236 }
5237 }
5238}
5239
5240
Willy Tarreaub2513902006-12-17 14:52:38 +01005241/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005242 * In a GET or HEAD request, check if the requested URI matches the stats uri
5243 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005244 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005245 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005246 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005247 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005248 *
5249 * Returns 1 if the session's state changes, otherwise 0.
5250 */
5251int stats_check_uri_auth(struct session *t, struct proxy *backend)
5252{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005253 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005254 struct uri_auth *uri_auth = backend->uri_auth;
5255 struct user_auth *user;
5256 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005257 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005258
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005259 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005261 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005262 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005263 return 0;
5264
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005265 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005266
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005267 /* the URI is in h */
5268 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005269 return 0;
5270
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005271 h += uri_auth->uri_len;
5272 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5273 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005274 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005275 break;
5276 }
5277 h++;
5278 }
5279
5280 if (uri_auth->refresh) {
5281 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5282 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5283 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005284 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005285 break;
5286 }
5287 h++;
5288 }
5289 }
5290
Willy Tarreau55bb8452007-10-17 18:44:57 +02005291 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5292 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5293 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005294 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005295 break;
5296 }
5297 h++;
5298 }
5299
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005300 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5301
Willy Tarreaub2513902006-12-17 14:52:38 +01005302 /* we are in front of a interceptable URI. Let's check
5303 * if there's an authentication and if it's valid.
5304 */
5305 user = uri_auth->users;
5306 if (!user) {
5307 /* no user auth required, it's OK */
5308 authenticated = 1;
5309 } else {
5310 authenticated = 0;
5311
5312 /* a user list is defined, we have to check.
5313 * skip 21 chars for "Authorization: Basic ".
5314 */
5315
5316 /* FIXME: this should move to an earlier place */
5317 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005318 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5319 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5320 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005321 if (len > 14 &&
5322 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005323 txn->auth_hdr.str = h;
5324 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005325 break;
5326 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005327 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005328 }
5329
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005330 if (txn->auth_hdr.len < 21 ||
5331 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005332 user = NULL;
5333
5334 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005335 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5336 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005337 user->user_pwd, user->user_len)) {
5338 authenticated = 1;
5339 break;
5340 }
5341 user = user->next;
5342 }
5343 }
5344
5345 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005346 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005347
5348 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005349 msg.str = trash;
5350 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005351 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005352 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005353 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005354 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005355 if (!(t->flags & SN_ERR_MASK))
5356 t->flags |= SN_ERR_PRXCOND;
5357 if (!(t->flags & SN_FINST_MASK))
5358 t->flags |= SN_FINST_R;
5359 return 1;
5360 }
5361
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005362 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005363 * data.
5364 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01005365 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02005366 buffer_shutw_now(t->req);
5367 buffer_shutr_now(t->rep);
5368 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005369 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005370 t->data_source = DATA_SRC_STATS;
5371 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005372 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005373 produce_content(t);
5374 return 1;
5375}
5376
5377
Willy Tarreaubaaee002006-06-26 02:48:02 +02005378/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 * Print a debug line with a header
5380 */
5381void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5382{
5383 int len, max;
5384 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005385 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005386 max = end - start;
5387 UBOUND(max, sizeof(trash) - len - 1);
5388 len += strlcpy2(trash + len, start, max + 1);
5389 trash[len++] = '\n';
5390 write(1, trash, len);
5391}
5392
5393
Willy Tarreau8797c062007-05-07 00:55:35 +02005394/************************************************************************/
5395/* The code below is dedicated to ACL parsing and matching */
5396/************************************************************************/
5397
5398
5399
5400
5401/* 1. Check on METHOD
5402 * We use the pre-parsed method if it is known, and store its number as an
5403 * integer. If it is unknown, we use the pointer and the length.
5404 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005405static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005406{
5407 int len, meth;
5408
Willy Tarreauae8b7962007-06-09 23:10:04 +02005409 len = strlen(*text);
5410 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005411
5412 pattern->val.i = meth;
5413 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005414 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005415 if (!pattern->ptr.str)
5416 return 0;
5417 pattern->len = len;
5418 }
5419 return 1;
5420}
5421
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005422static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005423acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5424 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005425{
5426 int meth;
5427 struct http_txn *txn = l7;
5428
Willy Tarreaub6866442008-07-14 23:54:42 +02005429 if (!txn)
5430 return 0;
5431
Willy Tarreauc11416f2007-06-17 16:58:38 +02005432 if (txn->req.msg_state != HTTP_MSG_BODY)
5433 return 0;
5434
Willy Tarreau8797c062007-05-07 00:55:35 +02005435 meth = txn->meth;
5436 test->i = meth;
5437 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005438 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5439 /* ensure the indexes are not affected */
5440 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005441 test->len = txn->req.sl.rq.m_l;
5442 test->ptr = txn->req.sol;
5443 }
5444 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5445 return 1;
5446}
5447
5448static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5449{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005450 int icase;
5451
Willy Tarreau8797c062007-05-07 00:55:35 +02005452 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005453 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005454
5455 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005456 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005457
5458 /* Other method, we must compare the strings */
5459 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005460 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005461
5462 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5463 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5464 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005465 return ACL_PAT_FAIL;
5466 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005467}
5468
5469/* 2. Check on Request/Status Version
5470 * We simply compare strings here.
5471 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005472static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005473{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005474 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005475 if (!pattern->ptr.str)
5476 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005477 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005478 return 1;
5479}
5480
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005481static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005482acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5483 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005484{
5485 struct http_txn *txn = l7;
5486 char *ptr;
5487 int len;
5488
Willy Tarreaub6866442008-07-14 23:54:42 +02005489 if (!txn)
5490 return 0;
5491
Willy Tarreauc11416f2007-06-17 16:58:38 +02005492 if (txn->req.msg_state != HTTP_MSG_BODY)
5493 return 0;
5494
Willy Tarreau8797c062007-05-07 00:55:35 +02005495 len = txn->req.sl.rq.v_l;
5496 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5497
5498 while ((len-- > 0) && (*ptr++ != '/'));
5499 if (len <= 0)
5500 return 0;
5501
5502 test->ptr = ptr;
5503 test->len = len;
5504
5505 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5506 return 1;
5507}
5508
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005509static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005510acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5511 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005512{
5513 struct http_txn *txn = l7;
5514 char *ptr;
5515 int len;
5516
Willy Tarreaub6866442008-07-14 23:54:42 +02005517 if (!txn)
5518 return 0;
5519
Willy Tarreauc11416f2007-06-17 16:58:38 +02005520 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5521 return 0;
5522
Willy Tarreau8797c062007-05-07 00:55:35 +02005523 len = txn->rsp.sl.st.v_l;
5524 ptr = txn->rsp.sol;
5525
5526 while ((len-- > 0) && (*ptr++ != '/'));
5527 if (len <= 0)
5528 return 0;
5529
5530 test->ptr = ptr;
5531 test->len = len;
5532
5533 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5534 return 1;
5535}
5536
5537/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005538static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005539acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5540 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005541{
5542 struct http_txn *txn = l7;
5543 char *ptr;
5544 int len;
5545
Willy Tarreaub6866442008-07-14 23:54:42 +02005546 if (!txn)
5547 return 0;
5548
Willy Tarreauc11416f2007-06-17 16:58:38 +02005549 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5550 return 0;
5551
Willy Tarreau8797c062007-05-07 00:55:35 +02005552 len = txn->rsp.sl.st.c_l;
5553 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5554
5555 test->i = __strl2ui(ptr, len);
5556 test->flags = ACL_TEST_F_VOL_1ST;
5557 return 1;
5558}
5559
5560/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005561static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005562acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5563 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005564{
5565 struct http_txn *txn = l7;
5566
Willy Tarreaub6866442008-07-14 23:54:42 +02005567 if (!txn)
5568 return 0;
5569
Willy Tarreauc11416f2007-06-17 16:58:38 +02005570 if (txn->req.msg_state != HTTP_MSG_BODY)
5571 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005572
Willy Tarreauc11416f2007-06-17 16:58:38 +02005573 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5574 /* ensure the indexes are not affected */
5575 return 0;
5576
Willy Tarreau8797c062007-05-07 00:55:35 +02005577 test->len = txn->req.sl.rq.u_l;
5578 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5579
Willy Tarreauf3d25982007-05-08 22:45:09 +02005580 /* we do not need to set READ_ONLY because the data is in a buffer */
5581 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005582 return 1;
5583}
5584
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005585static int
5586acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5587 struct acl_expr *expr, struct acl_test *test)
5588{
5589 struct http_txn *txn = l7;
5590
Willy Tarreaub6866442008-07-14 23:54:42 +02005591 if (!txn)
5592 return 0;
5593
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005594 if (txn->req.msg_state != HTTP_MSG_BODY)
5595 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005596
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005597 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5598 /* ensure the indexes are not affected */
5599 return 0;
5600
5601 /* Parse HTTP request */
5602 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5603 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5604 test->i = AF_INET;
5605
5606 /*
5607 * If we are parsing url in frontend space, we prepare backend stage
5608 * to not parse again the same url ! optimization lazyness...
5609 */
5610 if (px->options & PR_O_HTTP_PROXY)
5611 l4->flags |= SN_ADDR_SET;
5612
5613 test->flags = ACL_TEST_F_READ_ONLY;
5614 return 1;
5615}
5616
5617static int
5618acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5619 struct acl_expr *expr, struct acl_test *test)
5620{
5621 struct http_txn *txn = l7;
5622
Willy Tarreaub6866442008-07-14 23:54:42 +02005623 if (!txn)
5624 return 0;
5625
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005626 if (txn->req.msg_state != HTTP_MSG_BODY)
5627 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005628
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005629 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5630 /* ensure the indexes are not affected */
5631 return 0;
5632
5633 /* Same optimization as url_ip */
5634 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5635 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5636
5637 if (px->options & PR_O_HTTP_PROXY)
5638 l4->flags |= SN_ADDR_SET;
5639
5640 test->flags = ACL_TEST_F_READ_ONLY;
5641 return 1;
5642}
5643
Willy Tarreauc11416f2007-06-17 16:58:38 +02005644/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5645 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5646 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005647static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005648acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005649 struct acl_expr *expr, struct acl_test *test)
5650{
5651 struct http_txn *txn = l7;
5652 struct hdr_idx *idx = &txn->hdr_idx;
5653 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005654
Willy Tarreaub6866442008-07-14 23:54:42 +02005655 if (!txn)
5656 return 0;
5657
Willy Tarreau33a7e692007-06-10 19:45:56 +02005658 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5659 /* search for header from the beginning */
5660 ctx->idx = 0;
5661
Willy Tarreau33a7e692007-06-10 19:45:56 +02005662 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5663 test->flags |= ACL_TEST_F_FETCH_MORE;
5664 test->flags |= ACL_TEST_F_VOL_HDR;
5665 test->len = ctx->vlen;
5666 test->ptr = (char *)ctx->line + ctx->val;
5667 return 1;
5668 }
5669
5670 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5671 test->flags |= ACL_TEST_F_VOL_HDR;
5672 return 0;
5673}
5674
Willy Tarreau33a7e692007-06-10 19:45:56 +02005675static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005676acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5677 struct acl_expr *expr, struct acl_test *test)
5678{
5679 struct http_txn *txn = l7;
5680
Willy Tarreaub6866442008-07-14 23:54:42 +02005681 if (!txn)
5682 return 0;
5683
Willy Tarreauc11416f2007-06-17 16:58:38 +02005684 if (txn->req.msg_state != HTTP_MSG_BODY)
5685 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005686
Willy Tarreauc11416f2007-06-17 16:58:38 +02005687 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5688 /* ensure the indexes are not affected */
5689 return 0;
5690
5691 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5692}
5693
5694static int
5695acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5696 struct acl_expr *expr, struct acl_test *test)
5697{
5698 struct http_txn *txn = l7;
5699
Willy Tarreaub6866442008-07-14 23:54:42 +02005700 if (!txn)
5701 return 0;
5702
Willy Tarreauc11416f2007-06-17 16:58:38 +02005703 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5704 return 0;
5705
5706 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5707}
5708
5709/* 6. Check on HTTP header count. The number of occurrences is returned.
5710 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5711 */
5712static int
5713acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005714 struct acl_expr *expr, struct acl_test *test)
5715{
5716 struct http_txn *txn = l7;
5717 struct hdr_idx *idx = &txn->hdr_idx;
5718 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005719 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005720
Willy Tarreaub6866442008-07-14 23:54:42 +02005721 if (!txn)
5722 return 0;
5723
Willy Tarreau33a7e692007-06-10 19:45:56 +02005724 ctx.idx = 0;
5725 cnt = 0;
5726 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5727 cnt++;
5728
5729 test->i = cnt;
5730 test->flags = ACL_TEST_F_VOL_HDR;
5731 return 1;
5732}
5733
Willy Tarreauc11416f2007-06-17 16:58:38 +02005734static int
5735acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5736 struct acl_expr *expr, struct acl_test *test)
5737{
5738 struct http_txn *txn = l7;
5739
Willy Tarreaub6866442008-07-14 23:54:42 +02005740 if (!txn)
5741 return 0;
5742
Willy Tarreauc11416f2007-06-17 16:58:38 +02005743 if (txn->req.msg_state != HTTP_MSG_BODY)
5744 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005745
Willy Tarreauc11416f2007-06-17 16:58:38 +02005746 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5747 /* ensure the indexes are not affected */
5748 return 0;
5749
5750 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5751}
5752
5753static int
5754acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5755 struct acl_expr *expr, struct acl_test *test)
5756{
5757 struct http_txn *txn = l7;
5758
Willy Tarreaub6866442008-07-14 23:54:42 +02005759 if (!txn)
5760 return 0;
5761
Willy Tarreauc11416f2007-06-17 16:58:38 +02005762 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5763 return 0;
5764
5765 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5766}
5767
Willy Tarreau33a7e692007-06-10 19:45:56 +02005768/* 7. Check on HTTP header's integer value. The integer value is returned.
5769 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005770 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005771 */
5772static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005773acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005774 struct acl_expr *expr, struct acl_test *test)
5775{
5776 struct http_txn *txn = l7;
5777 struct hdr_idx *idx = &txn->hdr_idx;
5778 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005779
Willy Tarreaub6866442008-07-14 23:54:42 +02005780 if (!txn)
5781 return 0;
5782
Willy Tarreau33a7e692007-06-10 19:45:56 +02005783 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5784 /* search for header from the beginning */
5785 ctx->idx = 0;
5786
Willy Tarreau33a7e692007-06-10 19:45:56 +02005787 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5788 test->flags |= ACL_TEST_F_FETCH_MORE;
5789 test->flags |= ACL_TEST_F_VOL_HDR;
5790 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5791 return 1;
5792 }
5793
5794 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5795 test->flags |= ACL_TEST_F_VOL_HDR;
5796 return 0;
5797}
5798
Willy Tarreauc11416f2007-06-17 16:58:38 +02005799static int
5800acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5801 struct acl_expr *expr, struct acl_test *test)
5802{
5803 struct http_txn *txn = l7;
5804
Willy Tarreaub6866442008-07-14 23:54:42 +02005805 if (!txn)
5806 return 0;
5807
Willy Tarreauc11416f2007-06-17 16:58:38 +02005808 if (txn->req.msg_state != HTTP_MSG_BODY)
5809 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005810
Willy Tarreauc11416f2007-06-17 16:58:38 +02005811 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5812 /* ensure the indexes are not affected */
5813 return 0;
5814
5815 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5816}
5817
5818static int
5819acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5820 struct acl_expr *expr, struct acl_test *test)
5821{
5822 struct http_txn *txn = l7;
5823
Willy Tarreaub6866442008-07-14 23:54:42 +02005824 if (!txn)
5825 return 0;
5826
Willy Tarreauc11416f2007-06-17 16:58:38 +02005827 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5828 return 0;
5829
5830 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5831}
5832
Willy Tarreau737b0c12007-06-10 21:28:46 +02005833/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5834 * the first '/' after the possible hostname, and ends before the possible '?'.
5835 */
5836static int
5837acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5838 struct acl_expr *expr, struct acl_test *test)
5839{
5840 struct http_txn *txn = l7;
5841 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005842
Willy Tarreaub6866442008-07-14 23:54:42 +02005843 if (!txn)
5844 return 0;
5845
Willy Tarreauc11416f2007-06-17 16:58:38 +02005846 if (txn->req.msg_state != HTTP_MSG_BODY)
5847 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005848
Willy Tarreauc11416f2007-06-17 16:58:38 +02005849 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5850 /* ensure the indexes are not affected */
5851 return 0;
5852
Willy Tarreau21d2af32008-02-14 20:25:24 +01005853 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5854 ptr = http_get_path(txn);
5855 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005856 return 0;
5857
5858 /* OK, we got the '/' ! */
5859 test->ptr = ptr;
5860
5861 while (ptr < end && *ptr != '?')
5862 ptr++;
5863
5864 test->len = ptr - test->ptr;
5865
5866 /* we do not need to set READ_ONLY because the data is in a buffer */
5867 test->flags = ACL_TEST_F_VOL_1ST;
5868 return 1;
5869}
5870
5871
Willy Tarreau8797c062007-05-07 00:55:35 +02005872
5873/************************************************************************/
5874/* All supported keywords must be declared here. */
5875/************************************************************************/
5876
5877/* Note: must not be declared <const> as its list will be overwritten */
5878static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005879 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5880 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5881 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5882 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005883
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005884 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5885 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5886 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5887 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5888 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5889 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5890 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5891 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5892 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005893
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005894 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5895 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5896 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5897 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5898 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5899 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5900 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5901 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5902 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5903 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005904
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005905 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5906 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5907 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5908 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5909 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5910 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5911 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5912 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5913 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005914
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005915 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5916 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5917 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5918 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5919 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5920 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5921 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005922
Willy Tarreauf3d25982007-05-08 22:45:09 +02005923 { NULL, NULL, NULL, NULL },
5924
5925#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005926 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5927 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5928 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5929 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5930 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5931 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5932 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5933
Willy Tarreau8797c062007-05-07 00:55:35 +02005934 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5935 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5936 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5937 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5938 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5939 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5940 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5941 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5942
5943 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5944 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5945 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5946 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5947 { NULL, NULL, NULL, NULL },
5948#endif
5949}};
5950
5951
5952__attribute__((constructor))
5953static void __http_protocol_init(void)
5954{
5955 acl_register_keywords(&acl_kws);
5956}
5957
5958
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005960 * Local variables:
5961 * c-indent-level: 8
5962 * c-basic-offset: 8
5963 * End:
5964 */