blob: df905e27138b155f78355fd104e717d4d25325a8 [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 Tarreau3a16b2c2008-08-28 08:54:27 +02001139 stream_sock_data_update(s->rep->cons->fd);
1140 rqf_cli = s->req->flags;
1141 rpf_cli = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +02001142 }
Willy Tarreaudafde432008-08-17 01:00:46 +02001143 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001144
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001145 /* Maybe resync server FD state */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001146 if (s->req->cons->state != SI_ST_CLO) {
1147 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
1148 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
Willy Tarreaucff64112008-11-03 06:26:53 +01001149 if (s->req->cons->state == SI_ST_INI &&
1150 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA) {
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151 s->req->cons->state = SI_ST_REQ;
1152 do {
1153 sess_prepare_conn_req(s, &s->si[1]);
1154 if (s->si[1].state != SI_ST_ASS)
1155 break;
1156 if (s->srv && s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1157 perform_http_redirect(s, &s->si[1]);
1158 sess_update_stream_int(s, &s->si[1]);
1159 } while (s->si[1].state == SI_ST_REQ || s->si[1].state == SI_ST_ASS);
1160
1161 /* FIXME: how to process this type of errors ? */
1162 if (s->si[1].state == SI_ST_CLO && s->si[1].err_type != SI_ET_NONE)
1163 return_srv_error(s, s->si[1].err_type);
1164 resync = 1;
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001165 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001166
1167 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001168 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +02001169 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +02001170 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +02001171 /* We want to force the connection to the server to close,
1172 * and the server has begun to respond. That's the right
1173 * time.
1174 */
1175 buffer_shutw_now(s->req);
1176 }
1177
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001178 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +02001179
1180 /* When a server-side connection is released, we have to
1181 * count it and check for pending connections on this server.
1182 */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01001183 if (s->req->cons->state == SI_ST_DIS) {
1184 s->req->cons->state = SI_ST_CLO;
Willy Tarreau4351b3a2008-11-12 01:51:41 +01001185 if (s->srv) {
1186 if (s->flags & SN_CURR_SESS) {
1187 s->flags &= ~SN_CURR_SESS;
1188 s->srv->cur_sess--;
1189 }
Willy Tarreau376580a2008-08-27 18:52:22 +02001190 sess_change_server(s, NULL);
1191 if (may_dequeue_tasks(s->srv, s->be))
1192 process_srv_queue(s->srv);
1193 }
1194 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001195 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001196 rqf_srv = s->req->flags;
1197 rpf_srv = s->rep->flags;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001198 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001199 }
1200
1201 /* we may have to resync because of pending connections */
1202 if (resync)
1203 continue;
1204
1205 /**** Process layer 7 below ****/
1206
1207 /* Analyse request */
1208 if (s->req->flags & BF_MASK_ANALYSER) {
1209 unsigned int flags = s->req->flags;
1210
1211 if (s->req->prod->state >= SI_ST_EST) {
1212 /* it's up to the analysers to reset write_ena */
1213 buffer_write_ena(s->req);
1214 if (s->req->analysers)
1215 process_request(s);
1216 }
1217 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1218 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1219 if (s->req->flags != flags)
1220 resync = 1;
1221 }
1222
1223 /* Analyse response */
1224 if (unlikely(s->rep->flags & BF_HIJACK)) {
1225 /* In inject mode, we wake up everytime something has
1226 * happened on the write side of the buffer.
1227 */
1228 unsigned int flags = s->rep->flags;
1229
1230 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1231 !(s->rep->flags & BF_FULL)) {
1232 produce_content(s);
1233 }
1234 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1235 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1236 if (s->rep->flags != flags)
1237 resync = 1;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001238 }
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001239 else if (s->rep->flags & BF_MASK_ANALYSER) {
1240 unsigned int flags = s->rep->flags;
1241
1242 if (s->rep->prod->state >= SI_ST_EST) {
1243 /* it's up to the analysers to reset write_ena */
1244 buffer_write_ena(s->rep);
1245 if (s->rep->analysers)
1246 process_response(s);
1247 }
1248 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1249 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1250 if (s->rep->flags != flags)
1251 resync = 1;
1252 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001253
Willy Tarreau4ffd51a2008-08-30 13:36:43 +02001254 /* For the moment, we need to clean the client and server flags that
1255 * have vanished. This is just a temporary measure though.
1256 */
1257 rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1258 rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1259 rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1260 rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaudafde432008-08-17 01:00:46 +02001261 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001262
Willy Tarreaua37095b2008-09-03 11:37:47 +02001263 /* This is needed only when debugging is enabled, to indicate
1264 * client-side or server-side close. Please note that in the unlikely
1265 * event where both sides would close at once, the sequence is reported
1266 * on the server side first.
1267 */
1268 if (unlikely((global.mode & MODE_DEBUG) &&
1269 (!(global.mode & MODE_QUIET) ||
1270 (global.mode & MODE_VERBOSE)))) {
1271 int len;
1272
1273 if (s->si[1].state == SI_ST_CLO &&
1274 s->si[1].prev_state == SI_ST_EST) {
1275 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1276 s->uniq_id, s->be->id,
1277 (unsigned short)s->si[0].fd,
1278 (unsigned short)s->si[1].fd);
1279 write(1, trash, len);
1280 }
1281
1282 if (s->si[0].state == SI_ST_CLO &&
1283 s->si[0].prev_state == SI_ST_EST) {
1284 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1285 s->uniq_id, s->be->id,
1286 (unsigned short)s->si[0].fd,
1287 (unsigned short)s->si[1].fd);
1288 write(1, trash, len);
1289 }
1290 }
1291
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001292 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001293 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001294
1295 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1296 session_process_counters(s);
1297
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001298 if (s->rep->cons->state == SI_ST_EST)
1299 stream_sock_data_finish(s->rep->cons->fd);
1300
1301 if (s->req->cons->state == SI_ST_EST)
1302 stream_sock_data_finish(s->req->cons->fd);
1303
Willy Tarreau9a2d1542008-08-30 12:31:07 +02001304 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1305 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreaue5ed4062008-08-30 03:17:31 +02001306 s->si[0].prev_state = s->si[0].state;
1307 s->si[1].prev_state = s->si[1].state;
Willy Tarreaucff64112008-11-03 06:26:53 +01001308 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309
Willy Tarreau7f875f62008-08-11 17:35:01 +02001310 /* Trick: if a request is being waiting for the server to respond,
1311 * and if we know the server can timeout, we don't want the timeout
1312 * to expire on the client side first, but we're still interested
1313 * in passing data from the client to the server (eg: POST). Thus,
1314 * we can cancel the client's request timeout if the server's
1315 * request timeout is set and the server has not yet sent a response.
1316 */
1317
Willy Tarreau3da77c52008-08-29 09:58:42 +02001318 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +02001319 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +02001320 s->req->rex = TICK_ETERNITY;
1321
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001322 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1323 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +02001324 if (s->req->analysers)
1325 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001326
Willy Tarreau35374672008-09-03 18:11:02 +02001327 if (s->si[0].exp)
1328 t->expire = tick_first(t->expire, s->si[0].exp);
1329
1330 if (s->si[1].exp)
1331 t->expire = tick_first(t->expire, s->si[1].exp);
1332
Willy Tarreaucb651252008-08-29 13:57:30 +02001333#ifdef DEBUG_FULL
Willy Tarreauefb453c2008-10-26 20:49:47 +01001334 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",
1335 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 +02001336#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001337 /* restore t to its place in the task list */
1338 task_queue(t);
1339
Willy Tarreaua7c52762008-08-16 18:40:18 +02001340#ifdef DEBUG_DEV
1341 /* this may only happen when no timeout is set or in case of an FSM bug */
1342 if (!t->expire)
1343 ABORT_NOW();
1344#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +02001345 *next = t->expire;
1346 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347 }
1348
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001349 s->fe->feconn--;
1350 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001351 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001352 actconn--;
1353
Willy Tarreauf41d4b12007-04-28 23:26:14 +02001354 if (unlikely((global.mode & MODE_DEBUG) &&
1355 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001357 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001358 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001359 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001360 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001361 write(1, trash, len);
1362 }
1363
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001364 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01001365 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001366
1367 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +02001368 if (s->logs.logwait &&
1369 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +02001370 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
1371 if (s->fe->to_log & LW_REQ)
1372 http_sess_log(s);
1373 else
1374 tcp_sess_log(s);
1375 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376
1377 /* the task MUST not be in the run queue anymore */
1378 task_delete(t);
1379 session_free(s);
1380 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001381 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382}
1383
1384
Willy Tarreau42250582007-04-01 01:30:43 +02001385extern const char sess_term_cond[8];
1386extern const char sess_fin_state[8];
1387extern const char *monthname[12];
1388const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
1389const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
1390 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
1391 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001392struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +02001393struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394
Willy Tarreau42250582007-04-01 01:30:43 +02001395/*
1396 * send a log for the session when we have enough info about it.
1397 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 */
Willy Tarreau42250582007-04-01 01:30:43 +02001399static void http_sess_log(struct session *s)
1400{
1401 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1402 struct proxy *fe = s->fe;
1403 struct proxy *be = s->be;
1404 struct proxy *prx_log;
1405 struct http_txn *txn = &s->txn;
1406 int tolog;
1407 char *uri, *h;
1408 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001409 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001410 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001411 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001412 int hdr;
1413
1414 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1415 return;
1416 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417
Willy Tarreau42250582007-04-01 01:30:43 +02001418 if (s->cli_addr.ss_family == AF_INET)
1419 inet_ntop(AF_INET,
1420 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1421 pn, sizeof(pn));
1422 else
1423 inet_ntop(AF_INET6,
1424 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1425 pn, sizeof(pn));
1426
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001427 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001428
1429 /* FIXME: let's limit ourselves to frontend logging for now. */
1430 tolog = fe->to_log;
1431
1432 h = tmpline;
1433 if (fe->to_log & LW_REQHDR &&
1434 txn->req.cap &&
1435 (h < tmpline + sizeof(tmpline) - 10)) {
1436 *(h++) = ' ';
1437 *(h++) = '{';
1438 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1439 if (hdr)
1440 *(h++) = '|';
1441 if (txn->req.cap[hdr] != NULL)
1442 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1443 '#', hdr_encode_map, txn->req.cap[hdr]);
1444 }
1445 *(h++) = '}';
1446 }
1447
1448 if (fe->to_log & LW_RSPHDR &&
1449 txn->rsp.cap &&
1450 (h < tmpline + sizeof(tmpline) - 7)) {
1451 *(h++) = ' ';
1452 *(h++) = '{';
1453 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1454 if (hdr)
1455 *(h++) = '|';
1456 if (txn->rsp.cap[hdr] != NULL)
1457 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1458 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1459 }
1460 *(h++) = '}';
1461 }
1462
1463 if (h < tmpline + sizeof(tmpline) - 4) {
1464 *(h++) = ' ';
1465 *(h++) = '"';
1466 uri = txn->uri ? txn->uri : "<BADREQ>";
1467 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1468 '#', url_encode_map, uri);
1469 *(h++) = '"';
1470 }
1471 *h = '\0';
1472
1473 svid = (tolog & LW_SVID) ?
1474 (s->data_source != DATA_SRC_STATS) ?
1475 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1476
Willy Tarreau70089872008-06-13 21:12:51 +02001477 t_request = -1;
1478 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1479 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1480
Willy Tarreau42250582007-04-01 01:30:43 +02001481 send_log(prx_log, LOG_INFO,
1482 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
1483 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001484 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001485 pn,
1486 (s->cli_addr.ss_family == AF_INET) ?
1487 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1488 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001489 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001490 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001491 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001492 t_request,
1493 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001494 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1495 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1496 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1497 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001498 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001499 txn->cli_cookie ? txn->cli_cookie : "-",
1500 txn->srv_cookie ? txn->srv_cookie : "-",
1501 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1502 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1503 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1504 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1505 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001506 (s->flags & SN_REDISP)?"+":"",
1507 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001508 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1509
1510 s->logs.logwait = 0;
1511}
1512
Willy Tarreau117f59e2007-03-04 18:17:17 +01001513
1514/*
1515 * Capture headers from message starting at <som> according to header list
1516 * <cap_hdr>, and fill the <idx> structure appropriately.
1517 */
1518void capture_headers(char *som, struct hdr_idx *idx,
1519 char **cap, struct cap_hdr *cap_hdr)
1520{
1521 char *eol, *sol, *col, *sov;
1522 int cur_idx;
1523 struct cap_hdr *h;
1524 int len;
1525
1526 sol = som + hdr_idx_first_pos(idx);
1527 cur_idx = hdr_idx_first_idx(idx);
1528
1529 while (cur_idx) {
1530 eol = sol + idx->v[cur_idx].len;
1531
1532 col = sol;
1533 while (col < eol && *col != ':')
1534 col++;
1535
1536 sov = col + 1;
1537 while (sov < eol && http_is_lws[(unsigned char)*sov])
1538 sov++;
1539
1540 for (h = cap_hdr; h; h = h->next) {
1541 if ((h->namelen == col - sol) &&
1542 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1543 if (cap[h->index] == NULL)
1544 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001545 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001546
1547 if (cap[h->index] == NULL) {
1548 Alert("HTTP capture : out of memory.\n");
1549 continue;
1550 }
1551
1552 len = eol - sov;
1553 if (len > h->len)
1554 len = h->len;
1555
1556 memcpy(cap[h->index], sov, len);
1557 cap[h->index][len]=0;
1558 }
1559 }
1560 sol = eol + idx->v[cur_idx].cr + 1;
1561 cur_idx = idx->v[cur_idx].next;
1562 }
1563}
1564
1565
Willy Tarreau42250582007-04-01 01:30:43 +02001566/* either we find an LF at <ptr> or we jump to <bad>.
1567 */
1568#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1569
1570/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1571 * otherwise to <http_msg_ood> with <state> set to <st>.
1572 */
1573#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1574 ptr++; \
1575 if (likely(ptr < end)) \
1576 goto good; \
1577 else { \
1578 state = (st); \
1579 goto http_msg_ood; \
1580 } \
1581 } while (0)
1582
1583
Willy Tarreaubaaee002006-06-26 02:48:02 +02001584/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001585 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001586 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1587 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1588 * will give undefined results.
1589 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1590 * and that msg->sol points to the beginning of the response.
1591 * If a complete line is found (which implies that at least one CR or LF is
1592 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1593 * returned indicating an incomplete line (which does not mean that parts have
1594 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1595 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1596 * upon next call.
1597 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001598 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001599 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1600 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001601 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001603const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1604 unsigned int state, const char *ptr, const char *end,
1605 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001606{
1607 __label__
1608 http_msg_rpver,
1609 http_msg_rpver_sp,
1610 http_msg_rpcode,
1611 http_msg_rpcode_sp,
1612 http_msg_rpreason,
1613 http_msg_rpline_eol,
1614 http_msg_ood, /* out of data */
1615 http_msg_invalid;
1616
1617 switch (state) {
1618 http_msg_rpver:
1619 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001620 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001621 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1622
1623 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1626 }
1627 goto http_msg_invalid;
1628
1629 http_msg_rpver_sp:
1630 case HTTP_MSG_RPVER_SP:
1631 if (likely(!HTTP_IS_LWS(*ptr))) {
1632 msg->sl.st.c = ptr - msg_buf;
1633 goto http_msg_rpcode;
1634 }
1635 if (likely(HTTP_IS_SPHT(*ptr)))
1636 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1637 /* so it's a CR/LF, this is invalid */
1638 goto http_msg_invalid;
1639
1640 http_msg_rpcode:
1641 case HTTP_MSG_RPCODE:
1642 if (likely(!HTTP_IS_LWS(*ptr)))
1643 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1644
1645 if (likely(HTTP_IS_SPHT(*ptr))) {
1646 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1647 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1648 }
1649
1650 /* so it's a CR/LF, so there is no reason phrase */
1651 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1652 http_msg_rsp_reason:
1653 /* FIXME: should we support HTTP responses without any reason phrase ? */
1654 msg->sl.st.r = ptr - msg_buf;
1655 msg->sl.st.r_l = 0;
1656 goto http_msg_rpline_eol;
1657
1658 http_msg_rpcode_sp:
1659 case HTTP_MSG_RPCODE_SP:
1660 if (likely(!HTTP_IS_LWS(*ptr))) {
1661 msg->sl.st.r = ptr - msg_buf;
1662 goto http_msg_rpreason;
1663 }
1664 if (likely(HTTP_IS_SPHT(*ptr)))
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1666 /* so it's a CR/LF, so there is no reason phrase */
1667 goto http_msg_rsp_reason;
1668
1669 http_msg_rpreason:
1670 case HTTP_MSG_RPREASON:
1671 if (likely(!HTTP_IS_CRLF(*ptr)))
1672 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1673 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1674 http_msg_rpline_eol:
1675 /* We have seen the end of line. Note that we do not
1676 * necessarily have the \n yet, but at least we know that we
1677 * have EITHER \r OR \n, otherwise the response would not be
1678 * complete. We can then record the response length and return
1679 * to the caller which will be able to register it.
1680 */
1681 msg->sl.st.l = ptr - msg->sol;
1682 return ptr;
1683
1684#ifdef DEBUG_FULL
1685 default:
1686 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1687 exit(1);
1688#endif
1689 }
1690
1691 http_msg_ood:
1692 /* out of data */
1693 if (ret_state)
1694 *ret_state = state;
1695 if (ret_ptr)
1696 *ret_ptr = (char *)ptr;
1697 return NULL;
1698
1699 http_msg_invalid:
1700 /* invalid message */
1701 if (ret_state)
1702 *ret_state = HTTP_MSG_ERROR;
1703 return NULL;
1704}
1705
1706
1707/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 * This function parses a request line between <ptr> and <end>, starting with
1709 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1710 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1711 * will give undefined results.
1712 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1713 * and that msg->sol points to the beginning of the request.
1714 * If a complete line is found (which implies that at least one CR or LF is
1715 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1716 * returned indicating an incomplete line (which does not mean that parts have
1717 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1718 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1719 * upon next call.
1720 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001721 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1723 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001724 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001725 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001726const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1727 unsigned int state, const char *ptr, const char *end,
1728 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001729{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 __label__
1731 http_msg_rqmeth,
1732 http_msg_rqmeth_sp,
1733 http_msg_rquri,
1734 http_msg_rquri_sp,
1735 http_msg_rqver,
1736 http_msg_rqline_eol,
1737 http_msg_ood, /* out of data */
1738 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 switch (state) {
1741 http_msg_rqmeth:
1742 case HTTP_MSG_RQMETH:
1743 if (likely(HTTP_IS_TOKEN(*ptr)))
1744 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001747 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1749 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 if (likely(HTTP_IS_CRLF(*ptr))) {
1752 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001753 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 http_msg_req09_uri:
1755 msg->sl.rq.u = ptr - msg_buf;
1756 http_msg_req09_uri_e:
1757 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1758 http_msg_req09_ver:
1759 msg->sl.rq.v = ptr - msg_buf;
1760 msg->sl.rq.v_l = 0;
1761 goto http_msg_rqline_eol;
1762 }
1763 goto http_msg_invalid;
1764
1765 http_msg_rqmeth_sp:
1766 case HTTP_MSG_RQMETH_SP:
1767 if (likely(!HTTP_IS_LWS(*ptr))) {
1768 msg->sl.rq.u = ptr - msg_buf;
1769 goto http_msg_rquri;
1770 }
1771 if (likely(HTTP_IS_SPHT(*ptr)))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1773 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1774 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 http_msg_rquri:
1777 case HTTP_MSG_RQURI:
1778 if (likely(!HTTP_IS_LWS(*ptr)))
1779 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 if (likely(HTTP_IS_SPHT(*ptr))) {
1782 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1783 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1787 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 http_msg_rquri_sp:
1790 case HTTP_MSG_RQURI_SP:
1791 if (likely(!HTTP_IS_LWS(*ptr))) {
1792 msg->sl.rq.v = ptr - msg_buf;
1793 goto http_msg_rqver;
1794 }
1795 if (likely(HTTP_IS_SPHT(*ptr)))
1796 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1797 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1798 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001799
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 http_msg_rqver:
1801 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001802 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001804
1805 if (likely(HTTP_IS_CRLF(*ptr))) {
1806 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1807 http_msg_rqline_eol:
1808 /* We have seen the end of line. Note that we do not
1809 * necessarily have the \n yet, but at least we know that we
1810 * have EITHER \r OR \n, otherwise the request would not be
1811 * complete. We can then record the request length and return
1812 * to the caller which will be able to register it.
1813 */
1814 msg->sl.rq.l = ptr - msg->sol;
1815 return ptr;
1816 }
1817
1818 /* neither an HTTP_VER token nor a CRLF */
1819 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821#ifdef DEBUG_FULL
1822 default:
1823 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1824 exit(1);
1825#endif
1826 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 http_msg_ood:
1829 /* out of data */
1830 if (ret_state)
1831 *ret_state = state;
1832 if (ret_ptr)
1833 *ret_ptr = (char *)ptr;
1834 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 http_msg_invalid:
1837 /* invalid message */
1838 if (ret_state)
1839 *ret_state = HTTP_MSG_ERROR;
1840 return NULL;
1841}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001842
1843
Willy Tarreau8973c702007-01-21 23:58:29 +01001844/*
1845 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001846 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001847 * when data are missing and recalled at the exact same location with no
1848 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001849 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1850 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001851 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1853{
1854 __label__
1855 http_msg_rqbefore,
1856 http_msg_rqbefore_cr,
1857 http_msg_rqmeth,
1858 http_msg_rqline_end,
1859 http_msg_hdr_first,
1860 http_msg_hdr_name,
1861 http_msg_hdr_l1_sp,
1862 http_msg_hdr_l1_lf,
1863 http_msg_hdr_l1_lws,
1864 http_msg_hdr_val,
1865 http_msg_hdr_l2_lf,
1866 http_msg_hdr_l2_lws,
1867 http_msg_complete_header,
1868 http_msg_last_lf,
1869 http_msg_ood, /* out of data */
1870 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001871
Willy Tarreaue69eada2008-01-27 00:34:10 +01001872 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001874
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001875 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 ptr = buf->lr;
1877 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001878
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 if (unlikely(ptr >= end))
1880 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001881
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001883 /*
1884 * First, states that are specific to the response only.
1885 * We check them first so that request and headers are
1886 * closer to each other (accessed more often).
1887 */
1888 http_msg_rpbefore:
1889 case HTTP_MSG_RPBEFORE:
1890 if (likely(HTTP_IS_TOKEN(*ptr))) {
1891 if (likely(ptr == buf->data)) {
1892 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001893 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001894 } else {
1895#if PARSE_PRESERVE_EMPTY_LINES
1896 /* only skip empty leading lines, don't remove them */
1897 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001898 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001899#else
1900 /* Remove empty leading lines, as recommended by
1901 * RFC2616. This takes a lot of time because we
1902 * must move all the buffer backwards, but this
1903 * is rarely needed. The method above will be
1904 * cleaner when we'll be able to start sending
1905 * the request from any place in the buffer.
1906 */
1907 buf->lr = ptr;
1908 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001909 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001910 msg->sol = buf->data;
1911 ptr = buf->data;
1912 end = buf->r;
1913#endif
1914 }
1915 hdr_idx_init(idx);
1916 state = HTTP_MSG_RPVER;
1917 goto http_msg_rpver;
1918 }
1919
1920 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1921 goto http_msg_invalid;
1922
1923 if (unlikely(*ptr == '\n'))
1924 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1925 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1926 /* stop here */
1927
1928 http_msg_rpbefore_cr:
1929 case HTTP_MSG_RPBEFORE_CR:
1930 EXPECT_LF_HERE(ptr, http_msg_invalid);
1931 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1932 /* stop here */
1933
1934 http_msg_rpver:
1935 case HTTP_MSG_RPVER:
1936 case HTTP_MSG_RPVER_SP:
1937 case HTTP_MSG_RPCODE:
1938 case HTTP_MSG_RPCODE_SP:
1939 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001940 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001941 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001942 if (unlikely(!ptr))
1943 return;
1944
1945 /* we have a full response and we know that we have either a CR
1946 * or an LF at <ptr>.
1947 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001948 //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 +01001949 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1950
1951 msg->sol = ptr;
1952 if (likely(*ptr == '\r'))
1953 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1954 goto http_msg_rpline_end;
1955
1956 http_msg_rpline_end:
1957 case HTTP_MSG_RPLINE_END:
1958 /* msg->sol must point to the first of CR or LF. */
1959 EXPECT_LF_HERE(ptr, http_msg_invalid);
1960 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1961 /* stop here */
1962
1963 /*
1964 * Second, states that are specific to the request only
1965 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 http_msg_rqbefore:
1967 case HTTP_MSG_RQBEFORE:
1968 if (likely(HTTP_IS_TOKEN(*ptr))) {
1969 if (likely(ptr == buf->data)) {
1970 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001971 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 } else {
1973#if PARSE_PRESERVE_EMPTY_LINES
1974 /* only skip empty leading lines, don't remove them */
1975 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001976 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001977#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001978 /* Remove empty leading lines, as recommended by
1979 * RFC2616. This takes a lot of time because we
1980 * must move all the buffer backwards, but this
1981 * is rarely needed. The method above will be
1982 * cleaner when we'll be able to start sending
1983 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001984 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985 buf->lr = ptr;
1986 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001987 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 msg->sol = buf->data;
1989 ptr = buf->data;
1990 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001991#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001992 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001993 /* we will need this when keep-alive will be supported
1994 hdr_idx_init(idx);
1995 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001996 state = HTTP_MSG_RQMETH;
1997 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001998 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001999
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002000 if (unlikely(!HTTP_IS_CRLF(*ptr)))
2001 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 if (unlikely(*ptr == '\n'))
2004 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
2005 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01002006 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002008 http_msg_rqbefore_cr:
2009 case HTTP_MSG_RQBEFORE_CR:
2010 EXPECT_LF_HERE(ptr, http_msg_invalid);
2011 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01002012 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002014 http_msg_rqmeth:
2015 case HTTP_MSG_RQMETH:
2016 case HTTP_MSG_RQMETH_SP:
2017 case HTTP_MSG_RQURI:
2018 case HTTP_MSG_RQURI_SP:
2019 case HTTP_MSG_RQVER:
2020 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002021 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002022 if (unlikely(!ptr))
2023 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002024
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 /* we have a full request and we know that we have either a CR
2026 * or an LF at <ptr>.
2027 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002028 //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 +01002029 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002031 msg->sol = ptr;
2032 if (likely(*ptr == '\r'))
2033 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002034 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002036 http_msg_rqline_end:
2037 case HTTP_MSG_RQLINE_END:
2038 /* check for HTTP/0.9 request : no version information available.
2039 * msg->sol must point to the first of CR or LF.
2040 */
2041 if (unlikely(msg->sl.rq.v_l == 0))
2042 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 EXPECT_LF_HERE(ptr, http_msg_invalid);
2045 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01002046 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002047
Willy Tarreau8973c702007-01-21 23:58:29 +01002048 /*
2049 * Common states below
2050 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002051 http_msg_hdr_first:
2052 case HTTP_MSG_HDR_FIRST:
2053 msg->sol = ptr;
2054 if (likely(!HTTP_IS_CRLF(*ptr))) {
2055 goto http_msg_hdr_name;
2056 }
2057
2058 if (likely(*ptr == '\r'))
2059 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2060 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002062 http_msg_hdr_name:
2063 case HTTP_MSG_HDR_NAME:
2064 /* assumes msg->sol points to the first char */
2065 if (likely(HTTP_IS_TOKEN(*ptr)))
2066 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002068 if (likely(*ptr == ':')) {
2069 msg->col = ptr - buf->data;
2070 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
2071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002075 http_msg_hdr_l1_sp:
2076 case HTTP_MSG_HDR_L1_SP:
2077 /* assumes msg->sol points to the first char and msg->col to the colon */
2078 if (likely(HTTP_IS_SPHT(*ptr)))
2079 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 /* header value can be basically anything except CR/LF */
2082 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002084 if (likely(!HTTP_IS_CRLF(*ptr))) {
2085 goto http_msg_hdr_val;
2086 }
2087
2088 if (likely(*ptr == '\r'))
2089 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
2090 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002092 http_msg_hdr_l1_lf:
2093 case HTTP_MSG_HDR_L1_LF:
2094 EXPECT_LF_HERE(ptr, http_msg_invalid);
2095 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002097 http_msg_hdr_l1_lws:
2098 case HTTP_MSG_HDR_L1_LWS:
2099 if (likely(HTTP_IS_SPHT(*ptr))) {
2100 /* replace HT,CR,LF with spaces */
2101 for (; buf->data+msg->sov < ptr; msg->sov++)
2102 buf->data[msg->sov] = ' ';
2103 goto http_msg_hdr_l1_sp;
2104 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002105 /* we had a header consisting only in spaces ! */
2106 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002107 goto http_msg_complete_header;
2108
2109 http_msg_hdr_val:
2110 case HTTP_MSG_HDR_VAL:
2111 /* assumes msg->sol points to the first char, msg->col to the
2112 * colon, and msg->sov points to the first character of the
2113 * value.
2114 */
2115 if (likely(!HTTP_IS_CRLF(*ptr)))
2116 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002118 msg->eol = ptr;
2119 /* Note: we could also copy eol into ->eoh so that we have the
2120 * real header end in case it ends with lots of LWS, but is this
2121 * really needed ?
2122 */
2123 if (likely(*ptr == '\r'))
2124 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2125 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002127 http_msg_hdr_l2_lf:
2128 case HTTP_MSG_HDR_L2_LF:
2129 EXPECT_LF_HERE(ptr, http_msg_invalid);
2130 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002132 http_msg_hdr_l2_lws:
2133 case HTTP_MSG_HDR_L2_LWS:
2134 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2135 /* LWS: replace HT,CR,LF with spaces */
2136 for (; msg->eol < ptr; msg->eol++)
2137 *msg->eol = ' ';
2138 goto http_msg_hdr_val;
2139 }
2140 http_msg_complete_header:
2141 /*
2142 * It was a new header, so the last one is finished.
2143 * Assumes msg->sol points to the first char, msg->col to the
2144 * colon, msg->sov points to the first character of the value
2145 * and msg->eol to the first CR or LF so we know how the line
2146 * ends. We insert last header into the index.
2147 */
2148 /*
2149 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
2150 write(2, msg->sol, msg->eol-msg->sol);
2151 fprintf(stderr,"\n");
2152 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002154 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
2155 idx, idx->tail) < 0))
2156 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002158 msg->sol = ptr;
2159 if (likely(!HTTP_IS_CRLF(*ptr))) {
2160 goto http_msg_hdr_name;
2161 }
2162
2163 if (likely(*ptr == '\r'))
2164 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2165 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002167 http_msg_last_lf:
2168 case HTTP_MSG_LAST_LF:
2169 /* Assumes msg->sol points to the first of either CR or LF */
2170 EXPECT_LF_HERE(ptr, http_msg_invalid);
2171 ptr++;
2172 buf->lr = ptr;
2173 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002174 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002175 return;
2176#ifdef DEBUG_FULL
2177 default:
2178 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2179 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002180#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002181 }
2182 http_msg_ood:
2183 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002184 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002185 buf->lr = ptr;
2186 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002188 http_msg_invalid:
2189 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002190 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002191 return;
2192}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002193
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002194/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02002195 * It normally returns zero, but may return 1 if it absolutely needs to be
2196 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002197 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002198 * functions. Its behaviour is rather simple :
2199 * - all enabled analysers are called in turn from the lower to the higher
2200 * bit.
2201 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02002202 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002203 * that unprocessed data will not be forwarded. But that probably depends on
2204 * the protocol. Generally it is not reset in case of errors.
2205 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02002206 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002207 * analysis).
2208 * - if an analyser thinks it has no added value anymore staying here, it must
2209 * reset its bit from the analysers flags in order not to be called anymore.
2210 *
2211 * In the future, analysers should be able to indicate that they want to be
2212 * called after XXX bytes have been received (or transfered), and the min of
2213 * all's wishes will be used to ring back (unless a special condition occurs).
2214 *
2215 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002216 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002217int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002218{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002219 struct buffer *req = t->req;
2220 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002221
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002222 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 +02002223 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002224 t,
2225 req,
2226 req->rex, req->wex,
2227 req->flags,
2228 req->l,
2229 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002230
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002231 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002232 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02002233 struct tcp_rule *rule;
2234 int partial;
2235
Willy Tarreauf495ddf2008-08-17 14:38:41 +02002236 /* We will abort if we encounter a read error. In theory, we
2237 * should not abort if we get a close, it might be valid,
2238 * although very unlikely. FIXME: we'll abort for now, this
2239 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02002240 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002241 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002242 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002243 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02002244 if (!(t->flags & SN_ERR_MASK))
2245 t->flags |= SN_ERR_CLICL;
2246 if (!(t->flags & SN_FINST_MASK))
2247 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002248 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002249 }
2250
2251 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002252 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002253 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002254 t->fe->failed_req++;
2255 if (!(t->flags & SN_ERR_MASK))
2256 t->flags |= SN_ERR_CLITO;
2257 if (!(t->flags & SN_FINST_MASK))
2258 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002259 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002260 }
2261
2262 /* We don't know whether we have enough data, so must proceed
2263 * this way :
2264 * - iterate through all rules in their declaration order
2265 * - if one rule returns MISS, it means the inspect delay is
2266 * not over yet, then return immediately, otherwise consider
2267 * it as a non-match.
2268 * - if one rule returns OK, then return OK
2269 * - if one rule returns KO, then return KO
2270 */
2271
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002272 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02002273 partial = 0;
2274 else
2275 partial = ACL_PARTIAL;
2276
2277 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
2278 int ret = ACL_PAT_PASS;
2279
2280 if (rule->cond) {
2281 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
2282 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002283 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002284 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002285 if (!tick_isset(req->analyse_exp))
2286 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02002287 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002288 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002289
Willy Tarreaub6866442008-07-14 23:54:42 +02002290 ret = acl_pass(ret);
2291 if (rule->cond->pol == ACL_COND_UNLESS)
2292 ret = !ret;
2293 }
2294
2295 if (ret) {
2296 /* we have a matching rule. */
2297 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002298 buffer_abort(req);
2299 buffer_abort(rep);
2300 //FIXME: this delete this
2301 //fd_delete(t->cli_fd);
2302 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002303 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002304 t->fe->failed_req++;
2305 if (!(t->flags & SN_ERR_MASK))
2306 t->flags |= SN_ERR_PRXCOND;
2307 if (!(t->flags & SN_FINST_MASK))
2308 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002309 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02002310 }
2311 /* otherwise accept */
2312 break;
2313 }
2314 }
2315
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002316 /* if we get there, it means we have no rule which matches, or
2317 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02002318 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002319 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002320 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02002321 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002322
Willy Tarreau2df28e82008-08-17 15:20:19 +02002323 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002324 /*
2325 * Now parse the partial (or complete) lines.
2326 * We will check the request syntax, and also join multi-line
2327 * headers. An index of all the lines will be elaborated while
2328 * parsing.
2329 *
Willy Tarreau8973c702007-01-21 23:58:29 +01002330 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002331 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002332 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002333 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002334 * req->data + req->eoh = end of processed headers / start of current one
2335 * req->data + req->eol = end of current header or line (LF or CRLF)
2336 * req->lr = first non-visited byte
2337 * req->r = end of data
2338 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002340 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002341 struct http_txn *txn = &t->txn;
2342 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002343 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002345 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002346 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002348 /* 1: we might have to print this header in debug mode */
2349 if (unlikely((global.mode & MODE_DEBUG) &&
2350 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002351 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002352 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002353
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002354 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002355 eol = sol + msg->sl.rq.l;
2356 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002357
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002358 sol += hdr_idx_first_pos(&txn->hdr_idx);
2359 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002361 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002362 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002364 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2365 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002366 }
2367 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002368
Willy Tarreau58f10d72006-12-04 02:26:12 +01002369
2370 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002371 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372 * If not so, we check the FD and buffer states before leaving.
2373 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002374 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2375 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002376 *
2377 */
2378
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002379 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380 /*
2381 * First, let's catch bad requests.
2382 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002385
2386 /* 1: Since we are in header mode, if there's no space
2387 * left for headers, we won't be able to free more
2388 * later, so the session will never terminate. We
2389 * must terminate it now.
2390 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002391 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002392 /* FIXME: check if URI is set and return Status
2393 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002394 */
Willy Tarreau06619262006-12-17 08:37:22 +01002395 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002396 }
2397
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002398 /* 2: have we encountered a read error ? */
2399 else if (req->flags & BF_READ_ERROR) {
2400 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002401 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002402 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002403 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002404 if (!(t->flags & SN_ERR_MASK))
2405 t->flags |= SN_ERR_CLICL;
2406 if (!(t->flags & SN_FINST_MASK))
2407 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002408 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002409 }
2410
2411 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002412 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002413 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002414 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01002415 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002416 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002417 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002418 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002419 if (!(t->flags & SN_ERR_MASK))
2420 t->flags |= SN_ERR_CLITO;
2421 if (!(t->flags & SN_FINST_MASK))
2422 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002423 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002424 }
2425
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002426 /* 4: have we encountered a close ? */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002427 else if (req->flags & BF_SHUTR) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002428 txn->status = 400;
2429 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002430 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002431 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002432 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002433
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002434 if (!(t->flags & SN_ERR_MASK))
2435 t->flags |= SN_ERR_CLICL;
2436 if (!(t->flags & SN_FINST_MASK))
2437 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002438 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002439 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002440
Willy Tarreau3da77c52008-08-29 09:58:42 +02002441 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002442 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02002443 if (!tick_isset(req->analyse_exp))
2444 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002445
2446 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02002447 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002448 }
2449
2450
2451 /****************************************************************
2452 * More interesting part now : we know that we have a complete *
2453 * request which at least looks like HTTP. We have an indicator *
2454 * of each header's length, so we can parse them quickly. *
2455 ****************************************************************/
2456
Willy Tarreau2df28e82008-08-17 15:20:19 +02002457 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002458 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002459
Willy Tarreau9cdde232007-05-02 20:58:19 +02002460 /* ensure we keep this pointer to the beginning of the message */
2461 msg->sol = req->data + msg->som;
2462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002463 /*
2464 * 1: identify the method
2465 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002466 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002467
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002468 /* we can make use of server redirect on GET and HEAD */
2469 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2470 t->flags |= SN_REDIRECTABLE;
2471
Willy Tarreau58f10d72006-12-04 02:26:12 +01002472 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002473 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01002474 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002475 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002476 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002477 if (unlikely((t->fe->monitor_uri_len != 0) &&
2478 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2479 !memcmp(&req->data[msg->sl.rq.u],
2480 t->fe->monitor_uri,
2481 t->fe->monitor_uri_len))) {
2482 /*
2483 * We have found the monitor URI
2484 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01002485 struct acl_cond *cond;
2486 cur_proxy = t->fe;
2487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002488 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002489
2490 /* Check if we want to fail this monitor request or not */
2491 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
2492 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002493
2494 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01002495 if (cond->pol == ACL_COND_UNLESS)
2496 ret = !ret;
2497
2498 if (ret) {
2499 /* we fail this request, let's return 503 service unavail */
2500 txn->status = 503;
2501 client_retnclose(t, error_message(t, HTTP_ERR_503));
2502 goto return_prx_cond;
2503 }
2504 }
2505
2506 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002507 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002508 client_retnclose(t, &http_200_chunk);
2509 goto return_prx_cond;
2510 }
2511
2512 /*
2513 * 3: Maybe we have to copy the original REQURI for the logs ?
2514 * Note: we cannot log anymore if the request has been
2515 * classified as invalid.
2516 */
2517 if (unlikely(t->logs.logwait & LW_REQ)) {
2518 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002519 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002520 int urilen = msg->sl.rq.l;
2521
2522 if (urilen >= REQURI_LEN)
2523 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002524 memcpy(txn->uri, &req->data[msg->som], urilen);
2525 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002526
2527 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002528 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002529 } else {
2530 Alert("HTTP logging : out of memory.\n");
2531 }
2532 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002533
Willy Tarreau06619262006-12-17 08:37:22 +01002534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002535 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2536 if (unlikely(msg->sl.rq.v_l == 0)) {
2537 int delta;
2538 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002539 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002540 cur_end = msg->sol + msg->sl.rq.l;
2541 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002543 if (msg->sl.rq.u_l == 0) {
2544 /* if no URI was set, add "/" */
2545 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2546 cur_end += delta;
2547 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002548 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002549 /* add HTTP version */
2550 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2551 msg->eoh += delta;
2552 cur_end += delta;
2553 cur_end = (char *)http_parse_reqline(msg, req->data,
2554 HTTP_MSG_RQMETH,
2555 msg->sol, cur_end + 1,
2556 NULL, NULL);
2557 if (unlikely(!cur_end))
2558 goto return_bad_req;
2559
2560 /* we have a full HTTP/1.0 request now and we know that
2561 * we have either a CR or an LF at <ptr>.
2562 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002563 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002564 }
2565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002566
2567 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002568 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002569 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002570 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
2572 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002573 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002574 * As opposed to version 1.2, now they will be evaluated in the
2575 * filters order and not in the header order. This means that
2576 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002577 *
2578 * We can now check whether we want to switch to another
2579 * backend, in which case we will re-check the backend's
2580 * filters and various options. In order to support 3-level
2581 * switching, here's how we should proceed :
2582 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002583 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002584 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002585 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002586 * There cannot be any switch from there, so ->be cannot be
2587 * changed anymore.
2588 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002589 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002590 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002591 * The response path will be able to apply either ->be, or
2592 * ->be then ->fe filters in order to match the reverse of
2593 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002594 */
2595
Willy Tarreau06619262006-12-17 08:37:22 +01002596 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002597 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002598 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002599 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002600 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002601
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002602 /* first check whether we have some ACLs set to redirect this request */
2603 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2604 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002605
2606 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002607 if (rule->cond->pol == ACL_COND_UNLESS)
2608 ret = !ret;
2609
2610 if (ret) {
2611 struct chunk rdr = { trash, 0 };
2612 const char *msg_fmt;
2613
2614 /* build redirect message */
2615 switch(rule->code) {
2616 case 303:
2617 rdr.len = strlen(HTTP_303);
2618 msg_fmt = HTTP_303;
2619 break;
2620 case 301:
2621 rdr.len = strlen(HTTP_301);
2622 msg_fmt = HTTP_301;
2623 break;
2624 case 302:
2625 default:
2626 rdr.len = strlen(HTTP_302);
2627 msg_fmt = HTTP_302;
2628 break;
2629 }
2630
2631 if (unlikely(rdr.len > sizeof(trash)))
2632 goto return_bad_req;
2633 memcpy(rdr.str, msg_fmt, rdr.len);
2634
2635 switch(rule->type) {
2636 case REDIRECT_TYPE_PREFIX: {
2637 const char *path;
2638 int pathlen;
2639
2640 path = http_get_path(txn);
2641 /* build message using path */
2642 if (path) {
2643 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2644 } else {
2645 path = "/";
2646 pathlen = 1;
2647 }
2648
2649 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2650 goto return_bad_req;
2651
2652 /* add prefix */
2653 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2654 rdr.len += rule->rdr_len;
2655
2656 /* add path */
2657 memcpy(rdr.str + rdr.len, path, pathlen);
2658 rdr.len += pathlen;
2659 break;
2660 }
2661 case REDIRECT_TYPE_LOCATION:
2662 default:
2663 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2664 goto return_bad_req;
2665
2666 /* add location */
2667 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2668 rdr.len += rule->rdr_len;
2669 break;
2670 }
2671
2672 /* add end of headers */
2673 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2674 rdr.len += 4;
2675
2676 txn->status = rule->code;
2677 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002678 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002679 client_retnclose(t, &rdr);
2680 goto return_prx_cond;
2681 }
2682 }
2683
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002684 /* first check whether we have some ACLs set to block this request */
2685 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002686 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002687
2688 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002689 if (cond->pol == ACL_COND_UNLESS)
2690 ret = !ret;
2691
2692 if (ret) {
2693 txn->status = 403;
2694 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002695 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002696 client_retnclose(t, error_message(t, HTTP_ERR_403));
2697 goto return_prx_cond;
2698 }
2699 }
2700
Willy Tarreau06619262006-12-17 08:37:22 +01002701 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002702 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002703 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2704 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002705 }
2706
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002707 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2708 /* to ensure correct connection accounting on
2709 * the backend, we count the connection for the
2710 * one managing the queue.
2711 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002712 t->be->beconn++;
2713 if (t->be->beconn > t->be->beconn_max)
2714 t->be->beconn_max = t->be->beconn;
2715 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002716 t->flags |= SN_BE_ASSIGNED;
2717 }
2718
Willy Tarreau06619262006-12-17 08:37:22 +01002719 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002720 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002721 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002722 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002723 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002724 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002725 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002726 goto return_prx_cond;
2727 }
2728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002729 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002730 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002731 !(t->flags & SN_CONN_CLOSED)) {
2732 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002733 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002734 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002735
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002736 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002737 old_idx = 0;
2738
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002739 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2740 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002741 cur_ptr = cur_next;
2742 cur_end = cur_ptr + cur_hdr->len;
2743 cur_next = cur_end + cur_hdr->cr + 1;
2744
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002745 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2746 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002747 /* 3 possibilities :
2748 * - we have already set Connection: close,
2749 * so we remove this line.
2750 * - we have not yet set Connection: close,
2751 * but this line indicates close. We leave
2752 * it untouched and set the flag.
2753 * - we have not yet set Connection: close,
2754 * and this line indicates non-close. We
2755 * replace it.
2756 */
2757 if (t->flags & SN_CONN_CLOSED) {
2758 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002759 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002760 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002761 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2762 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002763 cur_hdr->len = 0;
2764 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002765 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2766 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2767 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002768 cur_next += delta;
2769 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002770 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002771 }
2772 t->flags |= SN_CONN_CLOSED;
2773 }
2774 }
2775 old_idx = cur_idx;
2776 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002777 }
2778 /* add request headers from the rule sets in the same order */
2779 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2780 if (unlikely(http_header_add_tail(req,
2781 &txn->req,
2782 &txn->hdr_idx,
2783 rule_set->req_add[cur_idx])) < 0)
2784 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002785 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002787 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002788 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002789 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002790 /* we have to check the URI and auth for this request.
2791 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002792 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002793 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002794 if (stats_check_uri_auth(t, rule_set))
2795 return 1;
2796 }
2797
Willy Tarreau55ea7572007-06-17 19:56:27 +02002798 /* now check whether we have some switching rules for this request */
2799 if (!(t->flags & SN_BE_ASSIGNED)) {
2800 struct switching_rule *rule;
2801
2802 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2803 int ret;
2804
2805 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002806
2807 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002808 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002809 ret = !ret;
2810
2811 if (ret) {
2812 t->be = rule->be.backend;
2813 t->be->beconn++;
2814 if (t->be->beconn > t->be->beconn_max)
2815 t->be->beconn_max = t->be->beconn;
2816 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002817
2818 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002819 t->rep->rto = t->req->wto = t->be->timeout.server;
2820 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002821 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002822 t->flags |= SN_BE_ASSIGNED;
2823 break;
2824 }
2825 }
2826 }
2827
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002828 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2829 /* No backend was set, but there was a default
2830 * backend set in the frontend, so we use it and
2831 * loop again.
2832 */
2833 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002834 t->be->beconn++;
2835 if (t->be->beconn > t->be->beconn_max)
2836 t->be->beconn_max = t->be->beconn;
2837 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002838
2839 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002840 t->rep->rto = t->req->wto = t->be->timeout.server;
2841 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002842 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002843 t->flags |= SN_BE_ASSIGNED;
2844 }
2845 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002846
Willy Tarreau58f10d72006-12-04 02:26:12 +01002847
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002848 if (!(t->flags & SN_BE_ASSIGNED)) {
2849 /* To ensure correct connection accounting on
2850 * the backend, we count the connection for the
2851 * one managing the queue.
2852 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002853 t->be->beconn++;
2854 if (t->be->beconn > t->be->beconn_max)
2855 t->be->beconn_max = t->be->beconn;
2856 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002857 t->flags |= SN_BE_ASSIGNED;
2858 }
2859
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002860 /*
2861 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002862 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002863 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002864 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002865 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002866
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002867 /*
2868 * If HTTP PROXY is set we simply get remote server address
2869 * parsing incoming request.
2870 */
2871 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2872 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2873 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002874
Willy Tarreau2a324282006-12-05 00:05:46 +01002875 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002876 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002877 * so let's do the same now.
2878 */
2879
2880 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002881 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002882 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002883 }
2884
2885
2886 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002887 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002888 * Note that doing so might move headers in the request, but
2889 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002890 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002891 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002892 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2893 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002894 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002895
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896
Willy Tarreau2a324282006-12-05 00:05:46 +01002897 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002898 * 9: add X-Forwarded-For if either the frontend or the backend
2899 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002900 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002901 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002902 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002903 /* Add an X-Forwarded-For header unless the source IP is
2904 * in the 'except' network range.
2905 */
2906 if ((!t->fe->except_mask.s_addr ||
2907 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2908 != t->fe->except_net.s_addr) &&
2909 (!t->be->except_mask.s_addr ||
2910 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2911 != t->be->except_net.s_addr)) {
2912 int len;
2913 unsigned char *pn;
2914 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002915
Ross Westaf72a1d2008-08-03 10:51:45 +02002916 /* Note: we rely on the backend to get the header name to be used for
2917 * x-forwarded-for, because the header is really meant for the backends.
2918 * However, if the backend did not specify any option, we have to rely
2919 * on the frontend's header name.
2920 */
2921 if (t->be->fwdfor_hdr_len) {
2922 len = t->be->fwdfor_hdr_len;
2923 memcpy(trash, t->be->fwdfor_hdr_name, len);
2924 } else {
2925 len = t->fe->fwdfor_hdr_len;
2926 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2927 }
2928 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002929
Ross Westaf72a1d2008-08-03 10:51:45 +02002930 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002931 &txn->hdr_idx, trash, len)) < 0)
2932 goto return_bad_req;
2933 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002934 }
2935 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002936 /* FIXME: for the sake of completeness, we should also support
2937 * 'except' here, although it is mostly useless in this case.
2938 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002939 int len;
2940 char pn[INET6_ADDRSTRLEN];
2941 inet_ntop(AF_INET6,
2942 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2943 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002944
2945 /* Note: we rely on the backend to get the header name to be used for
2946 * x-forwarded-for, because the header is really meant for the backends.
2947 * However, if the backend did not specify any option, we have to rely
2948 * on the frontend's header name.
2949 */
2950 if (t->be->fwdfor_hdr_len) {
2951 len = t->be->fwdfor_hdr_len;
2952 memcpy(trash, t->be->fwdfor_hdr_name, len);
2953 } else {
2954 len = t->fe->fwdfor_hdr_len;
2955 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2956 }
2957 len += sprintf(trash + len, ": %s", pn);
2958
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002959 if (unlikely(http_header_add_tail2(req, &txn->req,
2960 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002961 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002962 }
2963 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964
Willy Tarreau2a324282006-12-05 00:05:46 +01002965 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002966 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002967 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002968 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002969 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002970 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002971 if ((unlikely(msg->sl.rq.v_l != 8) ||
2972 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2973 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002974 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002975 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002977 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002978 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2979 * If not assigned, perhaps we are balancing on url_param, but this is a
2980 * POST; and the parameters are in the body, maybe scan there to find our server.
2981 * (unless headers overflowed the buffer?)
2982 */
2983 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2984 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002985 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002986 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2987 /* are there enough bytes here? total == l || r || rlim ?
2988 * len is unsigned, but eoh is int,
2989 * how many bytes of body have we received?
2990 * eoh is the first empty line of the header
2991 */
2992 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002993 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 +02002994
2995 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2996 * We can't assume responsibility for the server's decision,
2997 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2998 * We also can't change our mind later, about which server to choose, so round robin.
2999 */
3000 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
3001 struct hdr_ctx ctx;
3002 ctx.idx = 0;
3003 /* Expect is allowed in 1.1, look for it */
3004 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
3005 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02003006 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003007 /* We can't reliablly stall and wait for data, because of
3008 * .NET clients that don't conform to rfc2616; so, no need for
3009 * the next block to check length expectations.
3010 * We could send 100 status back to the client, but then we need to
3011 * re-write headers, and send the message. And this isn't the right
3012 * place for that action.
3013 * TODO: support Expect elsewhere and delete this block.
3014 */
3015 goto end_check_maybe_wait_for_body;
3016 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003017
3018 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003019 /* nothing to do, we got enough */
3020 } else {
3021 /* limit implies we are supposed to need this many bytes
3022 * to find the parameter. Let's see how many bytes we can wait for.
3023 */
3024 long long hint = len;
3025 struct hdr_ctx ctx;
3026 ctx.idx = 0;
3027 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003028 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02003029 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003030 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003031 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003032 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003033 ctx.idx = 0;
3034 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
3035 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003036 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003037 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003038 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003039 hint = 0; /* parse failure, untrusted client */
3040 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003041 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003042 msg->hdr_content_len = hint;
3043 else
3044 hint = 0; /* bad client, sent negative length */
3045 }
3046 }
3047 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003048 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003049 hint = t->be->url_param_post_limit;
3050 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003051 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02003052 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003053 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003054 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003055 /* else... There are no body bytes to wait for */
3056 }
3057 }
3058 }
3059 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003060
Willy Tarreau2a324282006-12-05 00:05:46 +01003061 /*************************************************************
3062 * OK, that's finished for the headers. We have done what we *
3063 * could. Let's switch to the DATA state. *
3064 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065
Willy Tarreaue393fe22008-08-16 22:18:07 +02003066 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02003067 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003068
Willy Tarreau1fa31262007-12-03 00:36:16 +01003069 /* When a connection is tarpitted, we use the tarpit timeout,
3070 * which may be the same as the connect timeout if unspecified.
3071 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003072 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01003073 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003074 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003075 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01003076 /* flush the request so that we can drop the connection early
3077 * if the client closes first.
3078 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003079 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003080 req->analysers |= AN_REQ_HTTP_TARPIT;
3081 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
3082 if (!req->analyse_exp)
3083 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01003084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003086 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003087 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01003088
3089 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003090 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003091 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003092 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01003093 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003094 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01003095 return_prx_cond:
3096 if (!(t->flags & SN_ERR_MASK))
3097 t->flags |= SN_ERR_PRXCOND;
3098 if (!(t->flags & SN_FINST_MASK))
3099 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003100 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003101 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003102 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02003103 }
3104
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003105 if (req->analysers & AN_REQ_HTTP_TARPIT) {
3106 struct http_txn *txn = &t->txn;
3107
3108 /* This connection is being tarpitted. The CLIENT side has
3109 * already set the connect expiration date to the right
3110 * timeout. We just have to check that the client is still
3111 * there and that the timeout has not expired.
3112 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003113 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003114 !tick_is_expired(req->analyse_exp, now_ms))
3115 return 0;
3116
3117 /* We will set the queue timer to the time spent, just for
3118 * logging purposes. We fake a 500 server error, so that the
3119 * attacker will not suspect his connection has been tarpitted.
3120 * It will not cause trouble to the logs because we can exclude
3121 * the tarpitted connections by filtering on the 'PT' status flags.
3122 */
3123 trace_term(t, TT_HTTP_SRV_2);
3124 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3125
3126 txn->status = 500;
3127 if (req->flags != BF_READ_ERROR)
3128 client_retnclose(t, error_message(t, HTTP_ERR_500));
3129
3130 req->analysers = 0;
3131 req->analyse_exp = TICK_ETERNITY;
3132
3133 t->fe->failed_req++;
3134 if (!(t->flags & SN_ERR_MASK))
3135 t->flags |= SN_ERR_PRXCOND;
3136 if (!(t->flags & SN_FINST_MASK))
3137 t->flags |= SN_FINST_T;
3138 return 0;
3139 }
3140
Willy Tarreau2df28e82008-08-17 15:20:19 +02003141 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003142 /* We have to parse the HTTP request body to find any required data.
3143 * "balance url_param check_post" should have been the only way to get
3144 * into this. We were brought here after HTTP header analysis, so all
3145 * related structures are ready.
3146 */
3147 struct http_msg *msg = &t->txn.req;
3148 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
3149 long long limit = t->be->url_param_post_limit;
3150 struct hdr_ctx ctx;
3151
3152 ctx.idx = 0;
3153
3154 /* now if we have a length, we'll take the hint */
3155 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
3156 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
3157 unsigned int chunk = 0;
3158 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
3159 char c = msg->sol[body];
3160 if (ishex(c)) {
3161 unsigned int hex = toupper(c) - '0';
3162 if (hex > 9)
3163 hex -= 'A' - '9' - 1;
3164 chunk = (chunk << 4) | hex;
3165 } else
3166 break;
3167 body++;
3168 }
3169 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003170 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003171
3172 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003173 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003174
3175 body += 2; // skip CRLF
3176
3177 /* if we support more then one chunk here, we have to do it again when assigning server
3178 * 1. how much entity data do we have? new var
3179 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
3180 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
3181 */
3182
3183 if (chunk < limit)
3184 limit = chunk; /* only reading one chunk */
3185 } else {
3186 if (msg->hdr_content_len < limit)
3187 limit = msg->hdr_content_len;
3188 }
3189
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003190 http_body_end:
3191 /* we leave once we know we have nothing left to do. This means that we have
3192 * enough bytes, or that we know we'll not get any more (buffer full, read
3193 * buffer closed).
3194 */
3195 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003196 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02003197 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003198 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003199 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02003200 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02003201 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003202 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02003203 else {
3204 /* Not enough data. We'll re-use the http-request
3205 * timeout here. Ideally, we should set the timeout
3206 * relative to the accept() date. We just set the
3207 * request timeout once at the beginning of the
3208 * request.
3209 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003210 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02003211 if (!tick_isset(req->analyse_exp))
3212 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
3213 return 0;
3214 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003215 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02003216
Willy Tarreau2df28e82008-08-17 15:20:19 +02003217 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3218 * probably reduce one day's debugging session.
3219 */
3220#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003221 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 +02003222 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3223 __FILE__, __LINE__, req->analysers);
3224 ABORT_NOW();
3225 }
3226#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003227 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003228 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003229}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003230
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003231/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02003232 * It normally returns zero, but may return 1 if it absolutely needs to be
3233 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02003234 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003235 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003236 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003237int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003238{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003239 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003240 struct buffer *req = t->req;
3241 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003242
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003243 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 +02003244 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003245 t,
3246 rep,
3247 rep->rex, rep->wex,
3248 rep->flags,
3249 rep->l,
3250 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003251
Willy Tarreau2df28e82008-08-17 15:20:19 +02003252 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003253 /*
3254 * Now parse the partial (or complete) lines.
3255 * We will check the response syntax, and also join multi-line
3256 * headers. An index of all the lines will be elaborated while
3257 * parsing.
3258 *
3259 * For the parsing, we use a 28 states FSM.
3260 *
3261 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02003262 * rep->data + rep->som = beginning of response
3263 * rep->data + rep->eoh = end of processed headers / start of current one
3264 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003265 * rep->lr = first non-visited byte
3266 * rep->r = end of data
3267 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02003268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 int cur_idx;
3270 struct http_msg *msg = &txn->rsp;
3271 struct proxy *cur_proxy;
3272
3273 if (likely(rep->lr < rep->r))
3274 http_msg_analyzer(rep, msg, &txn->hdr_idx);
3275
3276 /* 1: we might have to print this header in debug mode */
3277 if (unlikely((global.mode & MODE_DEBUG) &&
3278 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3279 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
3280 char *eol, *sol;
3281
3282 sol = rep->data + msg->som;
3283 eol = sol + msg->sl.rq.l;
3284 debug_hdr("srvrep", t, sol, eol);
3285
3286 sol += hdr_idx_first_pos(&txn->hdr_idx);
3287 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
3288
3289 while (cur_idx) {
3290 eol = sol + txn->hdr_idx.v[cur_idx].len;
3291 debug_hdr("srvhdr", t, sol, eol);
3292 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3293 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003294 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003295 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003296
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003297 /*
3298 * Now we quickly check if we have found a full valid response.
3299 * If not so, we check the FD and buffer states before leaving.
3300 * A full response is indicated by the fact that we have seen
3301 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
3302 * responses are checked first.
3303 *
3304 * Depending on whether the client is still there or not, we
3305 * may send an error response back or not. Note that normally
3306 * we should only check for HTTP status there, and check I/O
3307 * errors somewhere else.
3308 */
3309
3310 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003311 /* Invalid response */
3312 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3313 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003314 //buffer_shutr(rep);
3315 //buffer_shutw(req);
3316 //fd_delete(req->cons->fd);
3317 //req->cons->state = SI_ST_CLO;
3318 buffer_shutr_now(rep);
3319 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003320 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003321 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003322 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003323 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003325 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003326 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003327 txn->status = 502;
3328 client_return(t, error_message(t, HTTP_ERR_502));
3329 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003330 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003331 if (!(t->flags & SN_FINST_MASK))
3332 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003333
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003334 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3335 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003336
Willy Tarreaudafde432008-08-17 01:00:46 +02003337 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003338 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003339 /* too large response does not fit in buffer. */
3340 else if (rep->flags & BF_FULL) {
3341 goto hdr_response_bad;
3342 }
3343 /* read error */
3344 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003345 buffer_shutr_now(rep);
3346 buffer_shutw_now(req);
3347 //fd_delete(req->cons->fd);
3348 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003349 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003350 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003351 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003352 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003353 //}
3354 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003355 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003356 txn->status = 502;
3357 client_return(t, error_message(t, HTTP_ERR_502));
3358 if (!(t->flags & SN_ERR_MASK))
3359 t->flags |= SN_ERR_SRVCL;
3360 if (!(t->flags & SN_FINST_MASK))
3361 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003362
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003363 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3364 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003365
Willy Tarreaudafde432008-08-17 01:00:46 +02003366 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003367 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003368 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003369 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003370 buffer_shutr_now(rep);
3371 buffer_shutw_now(req);
3372 //fd_delete(req->cons->fd);
3373 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003374 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003375 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003376 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003377 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003378 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003379 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003380 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003381 txn->status = 504;
3382 client_return(t, error_message(t, HTTP_ERR_504));
3383 if (!(t->flags & SN_ERR_MASK))
3384 t->flags |= SN_ERR_SRVTO;
3385 if (!(t->flags & SN_FINST_MASK))
3386 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02003387
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003388 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3389 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003390 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003392 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02003393 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003394 buffer_shutr_now(rep);
3395 buffer_shutw_now(req);
3396 //fd_delete(req->cons->fd);
3397 //req->cons->state = SI_ST_CLO;
3398 if (t->srv) {
3399 //t->srv->cur_sess--;
3400 t->srv->failed_resp++;
3401 //sess_change_server(t, NULL);
3402 }
3403 t->be->failed_resp++;
3404 rep->analysers = 0;
3405 txn->status = 502;
3406 client_return(t, error_message(t, HTTP_ERR_502));
3407 if (!(t->flags & SN_ERR_MASK))
3408 t->flags |= SN_ERR_SRVCL;
3409 if (!(t->flags & SN_FINST_MASK))
3410 t->flags |= SN_FINST_H;
3411
3412 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
3413 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02003414
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003415 return 0;
3416 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003417 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003418 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003419 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003420
Willy Tarreau21d2af32008-02-14 20:25:24 +01003421
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003422 /*****************************************************************
3423 * More interesting part now : we know that we have a complete *
3424 * response which at least looks like HTTP. We have an indicator *
3425 * of each header's length, so we can parse them quickly. *
3426 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01003427
Willy Tarreau2df28e82008-08-17 15:20:19 +02003428 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02003429
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003430 /* ensure we keep this pointer to the beginning of the message */
3431 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003432
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003433 /*
3434 * 1: get the status code and check for cacheability.
3435 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01003436
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003437 t->logs.logwait &= ~LW_RESP;
3438 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003439
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003440 switch (txn->status) {
3441 case 200:
3442 case 203:
3443 case 206:
3444 case 300:
3445 case 301:
3446 case 410:
3447 /* RFC2616 @13.4:
3448 * "A response received with a status code of
3449 * 200, 203, 206, 300, 301 or 410 MAY be stored
3450 * by a cache (...) unless a cache-control
3451 * directive prohibits caching."
3452 *
3453 * RFC2616 @9.5: POST method :
3454 * "Responses to this method are not cacheable,
3455 * unless the response includes appropriate
3456 * Cache-Control or Expires header fields."
3457 */
3458 if (likely(txn->meth != HTTP_METH_POST) &&
3459 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3460 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3461 break;
3462 default:
3463 break;
3464 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003465
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003466 /*
3467 * 2: we may need to capture headers
3468 */
3469 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3470 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3471 txn->rsp.cap, t->fe->rsp_cap);
3472
3473 /*
3474 * 3: we will have to evaluate the filters.
3475 * As opposed to version 1.2, now they will be evaluated in the
3476 * filters order and not in the header order. This means that
3477 * each filter has to be validated among all headers.
3478 *
3479 * Filters are tried with ->be first, then with ->fe if it is
3480 * different from ->be.
3481 */
3482
3483 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3484
3485 cur_proxy = t->be;
3486 while (1) {
3487 struct proxy *rule_set = cur_proxy;
3488
3489 /* try headers filters */
3490 if (rule_set->rsp_exp != NULL) {
3491 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3492 return_bad_resp:
3493 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003494 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003495 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003496 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003497 }
3498 cur_proxy->failed_resp++;
3499 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003500 buffer_shutr_now(rep);
3501 buffer_shutw_now(req);
3502 //fd_delete(req->cons->fd);
3503 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003504 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003505 txn->status = 502;
3506 client_return(t, error_message(t, HTTP_ERR_502));
3507 if (!(t->flags & SN_ERR_MASK))
3508 t->flags |= SN_ERR_PRXCOND;
3509 if (!(t->flags & SN_FINST_MASK))
3510 t->flags |= SN_FINST_H;
3511 /* We used to have a free connection slot. Since we'll never use it,
3512 * we have to inform the server that it may be used by another session.
3513 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003514 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3515 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003516 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003517 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003518 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003519
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003520 /* has the response been denied ? */
3521 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003522 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003523 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003524 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003525 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003526 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003527 cur_proxy->denied_resp++;
3528 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003529 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003530
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003531 /* We might have to check for "Connection:" */
3532 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3533 !(t->flags & SN_CONN_CLOSED)) {
3534 char *cur_ptr, *cur_end, *cur_next;
3535 int cur_idx, old_idx, delta, val;
3536 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003537
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003538 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3539 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003541 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3542 cur_hdr = &txn->hdr_idx.v[cur_idx];
3543 cur_ptr = cur_next;
3544 cur_end = cur_ptr + cur_hdr->len;
3545 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003546
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003547 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3548 if (val) {
3549 /* 3 possibilities :
3550 * - we have already set Connection: close,
3551 * so we remove this line.
3552 * - we have not yet set Connection: close,
3553 * but this line indicates close. We leave
3554 * it untouched and set the flag.
3555 * - we have not yet set Connection: close,
3556 * and this line indicates non-close. We
3557 * replace it.
3558 */
3559 if (t->flags & SN_CONN_CLOSED) {
3560 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3561 txn->rsp.eoh += delta;
3562 cur_next += delta;
3563 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3564 txn->hdr_idx.used--;
3565 cur_hdr->len = 0;
3566 } else {
3567 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3568 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3569 "close", 5);
3570 cur_next += delta;
3571 cur_hdr->len += delta;
3572 txn->rsp.eoh += delta;
3573 }
3574 t->flags |= SN_CONN_CLOSED;
3575 }
3576 }
3577 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003578 }
3579 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003580
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003581 /* add response headers from the rule sets in the same order */
3582 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3583 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3584 rule_set->rsp_add[cur_idx])) < 0)
3585 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003586 }
3587
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003588 /* check whether we're already working on the frontend */
3589 if (cur_proxy == t->fe)
3590 break;
3591 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003592 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003593
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003594 /*
3595 * 4: check for server cookie.
3596 */
3597 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3598 || (t->be->options & PR_O_CHK_CACHE))
3599 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003600
Willy Tarreaubaaee002006-06-26 02:48:02 +02003601
Willy Tarreaua15645d2007-03-18 16:22:39 +01003602 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003603 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003604 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003605 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3606 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003607
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003608 /*
3609 * 6: add server cookie in the response if needed
3610 */
3611 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3612 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3613 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003614
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003615 /* the server is known, it's not the one the client requested, we have to
3616 * insert a set-cookie here, except if we want to insert only on POST
3617 * requests and this one isn't. Note that servers which don't have cookies
3618 * (eg: some backup servers) will return a full cookie removal request.
3619 */
3620 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3621 t->be->cookie_name,
3622 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003623
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003624 if (t->be->cookie_domain)
3625 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003626
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003627 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3628 trash, len)) < 0)
3629 goto return_bad_resp;
3630 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003631
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003632 /* Here, we will tell an eventual cache on the client side that we don't
3633 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3634 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3635 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3636 */
3637 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003638
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003639 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3640
3641 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3642 "Cache-control: private", 22)) < 0)
3643 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003644 }
3645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003646
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003648 /*
3649 * 7: check if result will be cacheable with a cookie.
3650 * We'll block the response if security checks have caught
3651 * nasty things such as a cacheable cookie.
3652 */
3653 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3654 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3655 (t->be->options & PR_O_CHK_CACHE)) {
3656
3657 /* we're in presence of a cacheable response containing
3658 * a set-cookie header. We'll block it as requested by
3659 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003660 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003661 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003662 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003663 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003664 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003665 }
3666 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003667
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003668 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3669 t->be->id, t->srv?t->srv->id:"<dispatch>");
3670 send_log(t->be, LOG_ALERT,
3671 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3672 t->be->id, t->srv?t->srv->id:"<dispatch>");
3673 goto return_srv_prx_502;
3674 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003675
3676 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003677 * 8: add "Connection: close" if needed and not yet set.
3678 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003679 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003680 if (!(t->flags & SN_CONN_CLOSED) &&
3681 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3682 if ((unlikely(msg->sl.st.v_l != 8) ||
3683 unlikely(req->data[msg->som + 7] != '0')) &&
3684 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3685 "Connection: close", 17)) < 0)
3686 goto return_bad_resp;
3687 t->flags |= SN_CONN_CLOSED;
3688 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003689
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003690 /*************************************************************
3691 * OK, that's finished for the headers. We have done what we *
3692 * could. Let's switch to the DATA state. *
3693 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003694
Willy Tarreaue393fe22008-08-16 22:18:07 +02003695 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003696 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003698#ifdef CONFIG_HAP_TCPSPLICE
3699 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3700 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003701 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003702 }
3703#endif
3704 /* if the user wants to log as soon as possible, without counting
3705 * bytes from the server, then this is the right moment. We have
3706 * to temporarily assign bytes_out to log what we currently have.
3707 */
3708 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3709 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3710 t->logs.bytes_out = txn->rsp.eoh;
3711 if (t->fe->to_log & LW_REQ)
3712 http_sess_log(t);
3713 else
3714 tcp_sess_log(t);
3715 t->logs.bytes_out = 0;
3716 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003717
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003718 /* Note: we must not try to cheat by jumping directly to DATA,
3719 * otherwise we would not let the client side wake up.
3720 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003721
Willy Tarreaudafde432008-08-17 01:00:46 +02003722 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003723 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003724
Willy Tarreau2df28e82008-08-17 15:20:19 +02003725 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3726 * probably reduce one day's debugging session.
3727 */
3728#ifdef DEBUG_DEV
3729 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3730 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3731 __FILE__, __LINE__, rep->analysers);
3732 ABORT_NOW();
3733 }
3734#endif
3735 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003736 return 0;
3737}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003738
Willy Tarreaubaaee002006-06-26 02:48:02 +02003739
Willy Tarreaucff64112008-11-03 06:26:53 +01003740/* This function is called with (si->state == SI_ST_CON) meaning that a
3741 * connection was attempted and that the file descriptor is already allocated.
3742 * We must check for establishment, error and abort. Possible output states
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01003743 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
Willy Tarreaucff64112008-11-03 06:26:53 +01003744 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
3745 * otherwise 1.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003746 */
Willy Tarreaucff64112008-11-03 06:26:53 +01003747int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003748{
Willy Tarreaucff64112008-11-03 06:26:53 +01003749 struct buffer *req = si->ob;
3750 struct buffer *rep = si->ib;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003751
Willy Tarreauefb453c2008-10-26 20:49:47 +01003752 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 +02003753 now_ms, __FUNCTION__,
Willy Tarreaucff64112008-11-03 06:26:53 +01003754 cli_stnames[s->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003755 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003756 req->flags, rep->flags,
Willy Tarreauefb453c2008-10-26 20:49:47 +01003757 req->l, rep->l,
Willy Tarreaucff64112008-11-03 06:26:53 +01003758 fdtab[si->fd].state);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759
Willy Tarreaucff64112008-11-03 06:26:53 +01003760
3761 /* If we got an error, or if nothing happened and the connection timed
3762 * out, we must give up. The CER state handler will take care of retry
3763 * attempts and error reports.
3764 */
3765 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
3766 si->state = SI_ST_CER;
3767 fd_delete(si->fd);
3768
Willy Tarreaucff64112008-11-03 06:26:53 +01003769 if (si->err_type)
3770 return 0;
3771
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003772 si->err_loc = s->srv;
Willy Tarreaucff64112008-11-03 06:26:53 +01003773 if (si->flags & SI_FL_ERR)
3774 si->err_type = SI_ET_CONN_ERR;
3775 else
3776 si->err_type = SI_ET_CONN_TO;
3777 return 0;
3778 }
3779
3780 /* OK, maybe we want to abort */
3781 if (unlikely((req->flags & BF_SHUTW_NOW) ||
3782 (rep->flags & BF_SHUTW) ||
3783 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3784 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
3785 s->be->options & PR_O_ABRT_CLOSE)))) {
Willy Tarreauefb453c2008-10-26 20:49:47 +01003786 /* give up */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003787 req->wex = TICK_ETERNITY;
Willy Tarreaucff64112008-11-03 06:26:53 +01003788 fd_delete(si->fd);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003789 buffer_shutw(req);
3790 buffer_shutr(rep);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +01003791 si->state = SI_ST_DIS;
Willy Tarreaucff64112008-11-03 06:26:53 +01003792 si->err_type |= SI_ET_CONN_ABRT;
3793 si->err_loc = s->srv;
3794 return 1;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003795 }
3796
Willy Tarreaucff64112008-11-03 06:26:53 +01003797 /* we need to wait a bit more if there was no activity either */
3798 if (!(req->flags & BF_WRITE_ACTIVITY))
3799 return 1;
3800
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003801 /* OK, this means that a connection succeeded. The caller will be
3802 * responsible for handling the transition from CON to EST.
3803 */
Willy Tarreaucff64112008-11-03 06:26:53 +01003804 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
3805 si->state = SI_ST_EST;
3806 si->err_type = SI_ET_NONE;
3807 si->err_loc = NULL;
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003808 return 1;
3809}
3810
3811
3812/*
3813 * This function handles the transition between the SI_ST_CON state and the
3814 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
3815 * SI_ST_EST.
3816 */
3817void sess_establish(struct session *s, struct stream_interface *si)
3818{
3819 struct buffer *req = si->ob;
3820 struct buffer *rep = si->ib;
Willy Tarreaucff64112008-11-03 06:26:53 +01003821
3822 if (req->flags & BF_EMPTY) {
3823 EV_FD_CLR(si->fd, DIR_WR);
3824 req->wex = TICK_ETERNITY;
3825 } else {
3826 EV_FD_SET(si->fd, DIR_WR);
3827 req->wex = tick_add_ifset(now_ms, s->be->timeout.server);
3828 if (tick_isset(req->wex)) {
3829 /* FIXME: to prevent the server from expiring read
3830 * timeouts during writes, we refresh it. */
3831 rep->rex = req->wex;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003832 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003833 }
3834
Willy Tarreaucff64112008-11-03 06:26:53 +01003835 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3836 if (!(rep->flags & BF_HIJACK)) {
3837 EV_FD_SET(si->fd, DIR_RD);
3838 rep->rex = tick_add_ifset(now_ms, s->be->timeout.server);
3839 }
3840 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003841
Willy Tarreaucff64112008-11-03 06:26:53 +01003842 /* if the user wants to log as soon as possible, without counting
3843 * bytes from the server, then this is the right moment. */
3844 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
3845 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
3846 tcp_sess_log(s);
3847 }
3848#ifdef CONFIG_HAP_TCPSPLICE
3849 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
3850 /* TCP splicing supported by both FE and BE */
3851 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003852 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003853#endif
3854 }
3855 else {
3856 rep->analysers |= AN_RTR_HTTP_HDR;
3857 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3858 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3859 /* reset hdr_idx which was already initialized by the request.
3860 * right now, the http parser does it.
3861 * hdr_idx_init(&s->txn.hdr_idx);
3862 */
3863 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003864
Willy Tarreaucff64112008-11-03 06:26:53 +01003865 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
3866 req->wex = TICK_ETERNITY;
Willy Tarreaucff64112008-11-03 06:26:53 +01003867}
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003868
Willy Tarreaucff64112008-11-03 06:26:53 +01003869
3870/* This function is called with (si->state == SI_ST_CER) meaning that a
3871 * previous connection attempt has failed and that the file descriptor
3872 * has already been released. Possible causes include asynchronous error
3873 * notification and time out. Possible output states are SI_ST_CLO when
3874 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
3875 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
3876 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
3877 * marked as in error state. It returns 0.
3878 */
3879int sess_update_st_cer(struct session *s, struct stream_interface *si)
3880{
Willy Tarreau4351b3a2008-11-12 01:51:41 +01003881 /* we probably have to release last session from the server */
3882 if (s->srv) {
3883 if (s->flags & SN_CURR_SESS) {
3884 s->flags &= ~SN_CURR_SESS;
3885 s->srv->cur_sess--;
3886 }
3887 sess_change_server(s, NULL);
3888 }
3889
Willy Tarreaucff64112008-11-03 06:26:53 +01003890 /* ensure that we have enough retries left */
3891 s->conn_retries--;
3892 if (s->conn_retries < 0) {
3893 if (!si->err_type) {
3894 si->err_type = SI_ET_CONN_ERR;
3895 si->err_loc = s->srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003896 }
3897
Willy Tarreaucff64112008-11-03 06:26:53 +01003898 if (s->srv)
3899 s->srv->failed_conns++;
3900 s->be->failed_conns++;
3901 if (may_dequeue_tasks(s->srv, s->be))
3902 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003903
Willy Tarreaucff64112008-11-03 06:26:53 +01003904 buffer_shutw(si->ob);
3905 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003906
Willy Tarreaucff64112008-11-03 06:26:53 +01003907 buffer_shutr(si->ib);
3908 si->ib->flags |= BF_READ_ERROR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003909
Willy Tarreaucff64112008-11-03 06:26:53 +01003910 si->state = SI_ST_CLO;
3911 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003912 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003913
Willy Tarreaucff64112008-11-03 06:26:53 +01003914 /* If the "redispatch" option is set on the backend, we are allowed to
3915 * retry on another server for the last retry. In order to achieve this,
3916 * we must mark the session unassigned, and eventually clear the DIRECT
3917 * bit to ignore any persistence cookie. We won't count a retry nor a
3918 * redispatch yet, because this will depend on what server is selected.
3919 */
3920 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
3921 if (may_dequeue_tasks(s->srv, s->be))
3922 process_srv_queue(s->srv);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003923
Willy Tarreaucff64112008-11-03 06:26:53 +01003924 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3925 s->prev_srv = s->srv;
3926 si->state = SI_ST_REQ;
3927 } else {
3928 if (s->srv)
3929 s->srv->retries++;
3930 s->be->retries++;
3931 si->state = SI_ST_ASS;
3932 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003933
Willy Tarreaucff64112008-11-03 06:26:53 +01003934 if (si->flags & SI_FL_ERR) {
3935 /* The error was an asynchronous connection error, and we will
3936 * likely have to retry connecting to the same server, most
3937 * likely leading to the same result. To avoid this, we wait
3938 * one second before retrying.
3939 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003940
Willy Tarreaucff64112008-11-03 06:26:53 +01003941 if (!si->err_type)
3942 si->err_type = SI_ET_CONN_ERR;
3943
3944 si->state = SI_ST_TAR;
3945 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003946 return 0;
3947 }
Willy Tarreaucff64112008-11-03 06:26:53 +01003948 return 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003949}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003950
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003951
Willy Tarreaubaaee002006-06-26 02:48:02 +02003952/*
3953 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003954 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003955 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3956 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003957 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003958 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003959 */
3960int produce_content(struct session *s)
3961{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003962 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02003963 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003964 return 1;
3965 }
3966 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003967 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003968 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003969 if (ret >= 0)
3970 return ret;
3971 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003973
Willy Tarreau91861262007-10-17 17:06:05 +02003974 /* unknown data source or internal error */
3975 s->txn.status = 500;
3976 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003977 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003978 if (!(s->flags & SN_ERR_MASK))
3979 s->flags |= SN_ERR_PRXCOND;
3980 if (!(s->flags & SN_FINST_MASK))
3981 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02003982 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02003983 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003984}
3985
3986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003987/* Iterate the same filter through all request headers.
3988 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003989 * Since it can manage the switch to another backend, it updates the per-proxy
3990 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003991 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003992int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003993{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003994 char term;
3995 char *cur_ptr, *cur_end, *cur_next;
3996 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003997 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003998 struct hdr_idx_elem *cur_hdr;
3999 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004000
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004001 last_hdr = 0;
4002
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004003 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004004 old_idx = 0;
4005
4006 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004007 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004009 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 (exp->action == ACT_ALLOW ||
4011 exp->action == ACT_DENY ||
4012 exp->action == ACT_TARPIT))
4013 return 0;
4014
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004015 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 if (!cur_idx)
4017 break;
4018
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004019 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 cur_ptr = cur_next;
4021 cur_end = cur_ptr + cur_hdr->len;
4022 cur_next = cur_end + cur_hdr->cr + 1;
4023
4024 /* Now we have one header between cur_ptr and cur_end,
4025 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004026 */
4027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 /* The annoying part is that pattern matching needs
4029 * that we modify the contents to null-terminate all
4030 * strings before testing them.
4031 */
4032
4033 term = *cur_end;
4034 *cur_end = '\0';
4035
4036 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4037 switch (exp->action) {
4038 case ACT_SETBE:
4039 /* It is not possible to jump a second time.
4040 * FIXME: should we return an HTTP/500 here so that
4041 * the admin knows there's a problem ?
4042 */
4043 if (t->be != t->fe)
4044 break;
4045
4046 /* Swithing Proxy */
4047 t->be = (struct proxy *) exp->replace;
4048
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004049 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004050 * because we have associated req_cap and rsp_cap to the
4051 * frontend, and the beconn will be updated later.
4052 */
4053
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004054 t->rep->rto = t->req->wto = t->be->timeout.server;
4055 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004056 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004057 last_hdr = 1;
4058 break;
4059
4060 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004061 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004062 last_hdr = 1;
4063 break;
4064
4065 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004066 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004068 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004069 break;
4070
4071 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004072 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004074 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004075 break;
4076
4077 case ACT_REPLACE:
4078 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4079 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4080 /* FIXME: if the user adds a newline in the replacement, the
4081 * index will not be recalculated for now, and the new line
4082 * will not be counted as a new header.
4083 */
4084
4085 cur_end += delta;
4086 cur_next += delta;
4087 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004088 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004089 break;
4090
4091 case ACT_REMOVE:
4092 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4093 cur_next += delta;
4094
4095 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004096 txn->req.eoh += delta;
4097 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4098 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004099 cur_hdr->len = 0;
4100 cur_end = NULL; /* null-term has been rewritten */
4101 break;
4102
4103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004104 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004105 if (cur_end)
4106 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004108 /* keep the link from this header to next one in case of later
4109 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004110 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004111 old_idx = cur_idx;
4112 }
4113 return 0;
4114}
4115
4116
4117/* Apply the filter to the request line.
4118 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4119 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004120 * Since it can manage the switch to another backend, it updates the per-proxy
4121 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004122 */
4123int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4124{
4125 char term;
4126 char *cur_ptr, *cur_end;
4127 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004128 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004130
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131
Willy Tarreau3d300592007-03-18 18:34:41 +01004132 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004133 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004134 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004135 (exp->action == ACT_ALLOW ||
4136 exp->action == ACT_DENY ||
4137 exp->action == ACT_TARPIT))
4138 return 0;
4139 else if (exp->action == ACT_REMOVE)
4140 return 0;
4141
4142 done = 0;
4143
Willy Tarreau9cdde232007-05-02 20:58:19 +02004144 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004145 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004146
4147 /* Now we have the request line between cur_ptr and cur_end */
4148
4149 /* The annoying part is that pattern matching needs
4150 * that we modify the contents to null-terminate all
4151 * strings before testing them.
4152 */
4153
4154 term = *cur_end;
4155 *cur_end = '\0';
4156
4157 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4158 switch (exp->action) {
4159 case ACT_SETBE:
4160 /* It is not possible to jump a second time.
4161 * FIXME: should we return an HTTP/500 here so that
4162 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 if (t->be != t->fe)
4165 break;
4166
4167 /* Swithing Proxy */
4168 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004170 /* right now, the backend switch is not too much complicated
4171 * because we have associated req_cap and rsp_cap to the
4172 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004173 */
4174
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004175 t->rep->rto = t->req->wto = t->be->timeout.server;
4176 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004177 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004178 done = 1;
4179 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004181 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004182 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004183 done = 1;
4184 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004186 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004187 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004188 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 done = 1;
4190 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004191
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004192 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004193 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004194 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004195 done = 1;
4196 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004198 case ACT_REPLACE:
4199 *cur_end = term; /* restore the string terminator */
4200 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4201 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4202 /* FIXME: if the user adds a newline in the replacement, the
4203 * index will not be recalculated for now, and the new line
4204 * will not be counted as a new header.
4205 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004206
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004207 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004208 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004209
Willy Tarreau9cdde232007-05-02 20:58:19 +02004210 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004211 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004212 HTTP_MSG_RQMETH,
4213 cur_ptr, cur_end + 1,
4214 NULL, NULL);
4215 if (unlikely(!cur_end))
4216 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004217
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004218 /* we have a full request and we know that we have either a CR
4219 * or an LF at <ptr>.
4220 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004221 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4222 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004223 /* there is no point trying this regex on headers */
4224 return 1;
4225 }
4226 }
4227 *cur_end = term; /* restore the string terminator */
4228 return done;
4229}
Willy Tarreau97de6242006-12-27 17:18:38 +01004230
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231
Willy Tarreau58f10d72006-12-04 02:26:12 +01004232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004233/*
4234 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4235 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004236 * unparsable request. Since it can manage the switch to another backend, it
4237 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004238 */
4239int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4240{
Willy Tarreau3d300592007-03-18 18:34:41 +01004241 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004242 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004243 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004244 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004246 /*
4247 * The interleaving of transformations and verdicts
4248 * makes it difficult to decide to continue or stop
4249 * the evaluation.
4250 */
4251
Willy Tarreau3d300592007-03-18 18:34:41 +01004252 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004253 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4254 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4255 exp = exp->next;
4256 continue;
4257 }
4258
4259 /* Apply the filter to the request line. */
4260 ret = apply_filter_to_req_line(t, req, exp);
4261 if (unlikely(ret < 0))
4262 return -1;
4263
4264 if (likely(ret == 0)) {
4265 /* The filter did not match the request, it can be
4266 * iterated through all headers.
4267 */
4268 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 }
4270 exp = exp->next;
4271 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004272 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273}
4274
4275
Willy Tarreaua15645d2007-03-18 16:22:39 +01004276
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004278 * Manage client-side cookie. It can impact performance by about 2% so it is
4279 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004280 */
4281void manage_client_side_cookies(struct session *t, struct buffer *req)
4282{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004283 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004284 char *p1, *p2, *p3, *p4;
4285 char *del_colon, *del_cookie, *colon;
4286 int app_cookies;
4287
4288 appsess *asession_temp = NULL;
4289 appsess local_asession;
4290
4291 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004292 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004293
Willy Tarreau2a324282006-12-05 00:05:46 +01004294 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004295 * we start with the start line.
4296 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004297 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004298 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004299
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004300 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004302 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004304 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004305 cur_ptr = cur_next;
4306 cur_end = cur_ptr + cur_hdr->len;
4307 cur_next = cur_end + cur_hdr->cr + 1;
4308
4309 /* We have one full header between cur_ptr and cur_end, and the
4310 * next header starts at cur_next. We're only interested in
4311 * "Cookie:" headers.
4312 */
4313
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004314 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4315 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004316 old_idx = cur_idx;
4317 continue;
4318 }
4319
4320 /* Now look for cookies. Conforming to RFC2109, we have to support
4321 * attributes whose name begin with a '$', and associate them with
4322 * the right cookie, if we want to delete this cookie.
4323 * So there are 3 cases for each cookie read :
4324 * 1) it's a special attribute, beginning with a '$' : ignore it.
4325 * 2) it's a server id cookie that we *MAY* want to delete : save
4326 * some pointers on it (last semi-colon, beginning of cookie...)
4327 * 3) it's an application cookie : we *MAY* have to delete a previous
4328 * "special" cookie.
4329 * At the end of loop, if a "special" cookie remains, we may have to
4330 * remove it. If no application cookie persists in the header, we
4331 * *MUST* delete it
4332 */
4333
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004334 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004335
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 /* del_cookie == NULL => nothing to be deleted */
4337 del_colon = del_cookie = NULL;
4338 app_cookies = 0;
4339
4340 while (p1 < cur_end) {
4341 /* skip spaces and colons, but keep an eye on these ones */
4342 while (p1 < cur_end) {
4343 if (*p1 == ';' || *p1 == ',')
4344 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004345 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 break;
4347 p1++;
4348 }
4349
4350 if (p1 == cur_end)
4351 break;
4352
4353 /* p1 is at the beginning of the cookie name */
4354 p2 = p1;
4355 while (p2 < cur_end && *p2 != '=')
4356 p2++;
4357
4358 if (p2 == cur_end)
4359 break;
4360
4361 p3 = p2 + 1; /* skips the '=' sign */
4362 if (p3 == cur_end)
4363 break;
4364
4365 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004366 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004367 p4++;
4368
4369 /* here, we have the cookie name between p1 and p2,
4370 * and its value between p3 and p4.
4371 * we can process it :
4372 *
4373 * Cookie: NAME=VALUE;
4374 * | || || |
4375 * | || || +--> p4
4376 * | || |+-------> p3
4377 * | || +--------> p2
4378 * | |+------------> p1
4379 * | +-------------> colon
4380 * +--------------------> cur_ptr
4381 */
4382
4383 if (*p1 == '$') {
4384 /* skip this one */
4385 }
4386 else {
4387 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004388 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004389 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004390 (p4 - p1 >= t->fe->capture_namelen) &&
4391 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004392 int log_len = p4 - p1;
4393
Willy Tarreau086b3b42007-05-13 21:45:51 +02004394 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004395 Alert("HTTP logging : out of memory.\n");
4396 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004397 if (log_len > t->fe->capture_len)
4398 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004399 memcpy(txn->cli_cookie, p1, log_len);
4400 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004401 }
4402 }
4403
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004404 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4405 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004407 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408 char *delim;
4409
4410 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4411 * have the server ID betweek p3 and delim, and the original cookie between
4412 * delim+1 and p4. Otherwise, delim==p4 :
4413 *
4414 * Cookie: NAME=SRV~VALUE;
4415 * | || || | |
4416 * | || || | +--> p4
4417 * | || || +--------> delim
4418 * | || |+-----------> p3
4419 * | || +------------> p2
4420 * | |+----------------> p1
4421 * | +-----------------> colon
4422 * +------------------------> cur_ptr
4423 */
4424
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004425 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 for (delim = p3; delim < p4; delim++)
4427 if (*delim == COOKIE_DELIM)
4428 break;
4429 }
4430 else
4431 delim = p4;
4432
4433
4434 /* Here, we'll look for the first running server which supports the cookie.
4435 * This allows to share a same cookie between several servers, for example
4436 * to dedicate backup servers to specific servers only.
4437 * However, to prevent clients from sticking to cookie-less backup server
4438 * when they have incidentely learned an empty cookie, we simply ignore
4439 * empty cookies and mark them as invalid.
4440 */
4441 if (delim == p3)
4442 srv = NULL;
4443
4444 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004445 if (srv->cookie && (srv->cklen == delim - p3) &&
4446 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004447 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004448 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004449 txn->flags &= ~TX_CK_MASK;
4450 txn->flags |= TX_CK_VALID;
4451 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004452 t->srv = srv;
4453 break;
4454 } else {
4455 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004456 txn->flags &= ~TX_CK_MASK;
4457 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004458 }
4459 }
4460 srv = srv->next;
4461 }
4462
Willy Tarreau3d300592007-03-18 18:34:41 +01004463 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004464 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004465 txn->flags &= ~TX_CK_MASK;
4466 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004467 }
4468
4469 /* depending on the cookie mode, we may have to either :
4470 * - delete the complete cookie if we're in insert+indirect mode, so that
4471 * the server never sees it ;
4472 * - remove the server id from the cookie value, and tag the cookie as an
4473 * application cookie so that it does not get accidentely removed later,
4474 * if we're in cookie prefix mode
4475 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004476 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004477 int delta; /* negative */
4478
4479 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4480 p4 += delta;
4481 cur_end += delta;
4482 cur_next += delta;
4483 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004484 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004485
4486 del_cookie = del_colon = NULL;
4487 app_cookies++; /* protect the header from deletion */
4488 }
4489 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004490 (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 +01004491 del_cookie = p1;
4492 del_colon = colon;
4493 }
4494 } else {
4495 /* now we know that we must keep this cookie since it's
4496 * not ours. But if we wanted to delete our cookie
4497 * earlier, we cannot remove the complete header, but we
4498 * can remove the previous block itself.
4499 */
4500 app_cookies++;
4501
4502 if (del_cookie != NULL) {
4503 int delta; /* negative */
4504
4505 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4506 p4 += delta;
4507 cur_end += delta;
4508 cur_next += delta;
4509 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004510 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004511 del_cookie = del_colon = NULL;
4512 }
4513 }
4514
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004515 if ((t->be->appsession_name != NULL) &&
4516 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004517 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004518
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519 /* Cool... it's the right one */
4520
4521 asession_temp = &local_asession;
4522
Willy Tarreau63963c62007-05-13 21:29:55 +02004523 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004524 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4525 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4526 return;
4527 }
4528
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004529 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4530 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004531 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004532
Willy Tarreau58f10d72006-12-04 02:26:12 +01004533 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004534 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4535 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004536 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004537 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004538 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004539 Alert("Not enough memory process_cli():asession:calloc().\n");
4540 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4541 return;
4542 }
4543
4544 asession_temp->sessid = local_asession.sessid;
4545 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004546 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004547 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004548 } else {
4549 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004550 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004552 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004553 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004554 Alert("Found Application Session without matching server.\n");
4555 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004556 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004557 while (srv) {
4558 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004559 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004561 txn->flags &= ~TX_CK_MASK;
4562 txn->flags |= TX_CK_VALID;
4563 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004564 t->srv = srv;
4565 break;
4566 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004567 txn->flags &= ~TX_CK_MASK;
4568 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004569 }
4570 }
4571 srv = srv->next;
4572 }/* end while(srv) */
4573 }/* end else if server == NULL */
4574
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004575 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004576 asession_temp->request_count++;
4577#if defined(DEBUG_HASH)
4578 Alert("manage_client_side_cookies\n");
4579 appsession_hash_dump(&(t->be->htbl_proxy));
4580#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004581 }/* end if ((t->proxy->appsession_name != NULL) ... */
4582 }
4583
4584 /* we'll have to look for another cookie ... */
4585 p1 = p4;
4586 } /* while (p1 < cur_end) */
4587
4588 /* There's no more cookie on this line.
4589 * We may have marked the last one(s) for deletion.
4590 * We must do this now in two ways :
4591 * - if there is no app cookie, we simply delete the header ;
4592 * - if there are app cookies, we must delete the end of the
4593 * string properly, including the colon/semi-colon before
4594 * the cookie name.
4595 */
4596 if (del_cookie != NULL) {
4597 int delta;
4598 if (app_cookies) {
4599 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4600 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004601 cur_hdr->len += delta;
4602 } else {
4603 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004604
4605 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004606 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4607 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004608 cur_hdr->len = 0;
4609 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004610 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004611 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004612 }
4613
4614 /* keep the link from this header to next one */
4615 old_idx = cur_idx;
4616 } /* end of cookie processing on this header */
4617}
4618
4619
Willy Tarreaua15645d2007-03-18 16:22:39 +01004620/* Iterate the same filter through all response headers contained in <rtr>.
4621 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4622 */
4623int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4624{
4625 char term;
4626 char *cur_ptr, *cur_end, *cur_next;
4627 int cur_idx, old_idx, last_hdr;
4628 struct http_txn *txn = &t->txn;
4629 struct hdr_idx_elem *cur_hdr;
4630 int len, delta;
4631
4632 last_hdr = 0;
4633
4634 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4635 old_idx = 0;
4636
4637 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004638 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004639 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004640 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004641 (exp->action == ACT_ALLOW ||
4642 exp->action == ACT_DENY))
4643 return 0;
4644
4645 cur_idx = txn->hdr_idx.v[old_idx].next;
4646 if (!cur_idx)
4647 break;
4648
4649 cur_hdr = &txn->hdr_idx.v[cur_idx];
4650 cur_ptr = cur_next;
4651 cur_end = cur_ptr + cur_hdr->len;
4652 cur_next = cur_end + cur_hdr->cr + 1;
4653
4654 /* Now we have one header between cur_ptr and cur_end,
4655 * and the next header starts at cur_next.
4656 */
4657
4658 /* The annoying part is that pattern matching needs
4659 * that we modify the contents to null-terminate all
4660 * strings before testing them.
4661 */
4662
4663 term = *cur_end;
4664 *cur_end = '\0';
4665
4666 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4667 switch (exp->action) {
4668 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004669 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004670 last_hdr = 1;
4671 break;
4672
4673 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004674 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004675 last_hdr = 1;
4676 break;
4677
4678 case ACT_REPLACE:
4679 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4680 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4681 /* FIXME: if the user adds a newline in the replacement, the
4682 * index will not be recalculated for now, and the new line
4683 * will not be counted as a new header.
4684 */
4685
4686 cur_end += delta;
4687 cur_next += delta;
4688 cur_hdr->len += delta;
4689 txn->rsp.eoh += delta;
4690 break;
4691
4692 case ACT_REMOVE:
4693 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4694 cur_next += delta;
4695
4696 /* FIXME: this should be a separate function */
4697 txn->rsp.eoh += delta;
4698 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4699 txn->hdr_idx.used--;
4700 cur_hdr->len = 0;
4701 cur_end = NULL; /* null-term has been rewritten */
4702 break;
4703
4704 }
4705 }
4706 if (cur_end)
4707 *cur_end = term; /* restore the string terminator */
4708
4709 /* keep the link from this header to next one in case of later
4710 * removal of next header.
4711 */
4712 old_idx = cur_idx;
4713 }
4714 return 0;
4715}
4716
4717
4718/* Apply the filter to the status line in the response buffer <rtr>.
4719 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4720 * or -1 if a replacement resulted in an invalid status line.
4721 */
4722int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4723{
4724 char term;
4725 char *cur_ptr, *cur_end;
4726 int done;
4727 struct http_txn *txn = &t->txn;
4728 int len, delta;
4729
4730
Willy Tarreau3d300592007-03-18 18:34:41 +01004731 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004733 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 (exp->action == ACT_ALLOW ||
4735 exp->action == ACT_DENY))
4736 return 0;
4737 else if (exp->action == ACT_REMOVE)
4738 return 0;
4739
4740 done = 0;
4741
Willy Tarreau9cdde232007-05-02 20:58:19 +02004742 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004743 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4744
4745 /* Now we have the status line between cur_ptr and cur_end */
4746
4747 /* The annoying part is that pattern matching needs
4748 * that we modify the contents to null-terminate all
4749 * strings before testing them.
4750 */
4751
4752 term = *cur_end;
4753 *cur_end = '\0';
4754
4755 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4756 switch (exp->action) {
4757 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004758 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004759 done = 1;
4760 break;
4761
4762 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004763 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004764 done = 1;
4765 break;
4766
4767 case ACT_REPLACE:
4768 *cur_end = term; /* restore the string terminator */
4769 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4770 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4771 /* FIXME: if the user adds a newline in the replacement, the
4772 * index will not be recalculated for now, and the new line
4773 * will not be counted as a new header.
4774 */
4775
4776 txn->rsp.eoh += delta;
4777 cur_end += delta;
4778
Willy Tarreau9cdde232007-05-02 20:58:19 +02004779 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004780 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004781 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004782 cur_ptr, cur_end + 1,
4783 NULL, NULL);
4784 if (unlikely(!cur_end))
4785 return -1;
4786
4787 /* we have a full respnse and we know that we have either a CR
4788 * or an LF at <ptr>.
4789 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004790 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004791 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4792 /* there is no point trying this regex on headers */
4793 return 1;
4794 }
4795 }
4796 *cur_end = term; /* restore the string terminator */
4797 return done;
4798}
4799
4800
4801
4802/*
4803 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4804 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4805 * unparsable response.
4806 */
4807int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4808{
Willy Tarreau3d300592007-03-18 18:34:41 +01004809 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004810 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004811 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812 int ret;
4813
4814 /*
4815 * The interleaving of transformations and verdicts
4816 * makes it difficult to decide to continue or stop
4817 * the evaluation.
4818 */
4819
Willy Tarreau3d300592007-03-18 18:34:41 +01004820 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004821 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4822 exp->action == ACT_PASS)) {
4823 exp = exp->next;
4824 continue;
4825 }
4826
4827 /* Apply the filter to the status line. */
4828 ret = apply_filter_to_sts_line(t, rtr, exp);
4829 if (unlikely(ret < 0))
4830 return -1;
4831
4832 if (likely(ret == 0)) {
4833 /* The filter did not match the response, it can be
4834 * iterated through all headers.
4835 */
4836 apply_filter_to_resp_headers(t, rtr, exp);
4837 }
4838 exp = exp->next;
4839 }
4840 return 0;
4841}
4842
4843
4844
4845/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004846 * Manage server-side cookies. It can impact performance by about 2% so it is
4847 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004848 */
4849void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4850{
4851 struct http_txn *txn = &t->txn;
4852 char *p1, *p2, *p3, *p4;
4853
4854 appsess *asession_temp = NULL;
4855 appsess local_asession;
4856
4857 char *cur_ptr, *cur_end, *cur_next;
4858 int cur_idx, old_idx, delta;
4859
Willy Tarreaua15645d2007-03-18 16:22:39 +01004860 /* Iterate through the headers.
4861 * we start with the start line.
4862 */
4863 old_idx = 0;
4864 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4865
4866 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4867 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004868 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004869
4870 cur_hdr = &txn->hdr_idx.v[cur_idx];
4871 cur_ptr = cur_next;
4872 cur_end = cur_ptr + cur_hdr->len;
4873 cur_next = cur_end + cur_hdr->cr + 1;
4874
4875 /* We have one full header between cur_ptr and cur_end, and the
4876 * next header starts at cur_next. We're only interested in
4877 * "Cookie:" headers.
4878 */
4879
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004880 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4881 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004882 old_idx = cur_idx;
4883 continue;
4884 }
4885
4886 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004887 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004888
4889
4890 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004891 if (t->be->cookie_name == NULL &&
4892 t->be->appsession_name == NULL &&
4893 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004894 return;
4895
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004896 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004897
4898 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4900 break;
4901
4902 /* p1 is at the beginning of the cookie name */
4903 p2 = p1;
4904
4905 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4906 p2++;
4907
4908 if (p2 == cur_end || *p2 == ';') /* next cookie */
4909 break;
4910
4911 p3 = p2 + 1; /* skip the '=' sign */
4912 if (p3 == cur_end)
4913 break;
4914
4915 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004916 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004917 p4++;
4918
4919 /* here, we have the cookie name between p1 and p2,
4920 * and its value between p3 and p4.
4921 * we can process it.
4922 */
4923
4924 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004925 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004926 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004927 (p4 - p1 >= t->be->capture_namelen) &&
4928 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004929 int log_len = p4 - p1;
4930
Willy Tarreau086b3b42007-05-13 21:45:51 +02004931 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004932 Alert("HTTP logging : out of memory.\n");
4933 }
4934
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004935 if (log_len > t->be->capture_len)
4936 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004937 memcpy(txn->srv_cookie, p1, log_len);
4938 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004939 }
4940
4941 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004942 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4943 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004945 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004946
4947 /* If the cookie is in insert mode on a known server, we'll delete
4948 * this occurrence because we'll insert another one later.
4949 * We'll delete it too if the "indirect" option is set and we're in
4950 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004951 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4952 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004953 /* this header must be deleted */
4954 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4955 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4956 txn->hdr_idx.used--;
4957 cur_hdr->len = 0;
4958 cur_next += delta;
4959 txn->rsp.eoh += delta;
4960
Willy Tarreau3d300592007-03-18 18:34:41 +01004961 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004962 }
4963 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004964 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004965 /* replace bytes p3->p4 with the cookie name associated
4966 * with this server since we know it.
4967 */
4968 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4969 cur_hdr->len += delta;
4970 cur_next += delta;
4971 txn->rsp.eoh += delta;
4972
Willy Tarreau3d300592007-03-18 18:34:41 +01004973 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004974 }
4975 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004976 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 /* insert the cookie name associated with this server
4978 * before existing cookie, and insert a delimitor between them..
4979 */
4980 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4981 cur_hdr->len += delta;
4982 cur_next += delta;
4983 txn->rsp.eoh += delta;
4984
4985 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004986 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004987 }
4988 }
4989 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004990 else if ((t->be->appsession_name != NULL) &&
4991 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004992
4993 /* Cool... it's the right one */
4994
4995 size_t server_id_len = strlen(t->srv->id) + 1;
4996 asession_temp = &local_asession;
4997
Willy Tarreau63963c62007-05-13 21:29:55 +02004998 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004999 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5000 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5001 return;
5002 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005003 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5004 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005 asession_temp->serverid = NULL;
5006
5007 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005008 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5009 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005010 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005011 Alert("Not enough Memory process_srv():asession:calloc().\n");
5012 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5013 return;
5014 }
5015 asession_temp->sessid = local_asession.sessid;
5016 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005017 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005018 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5019 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005020 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005021 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005022 }
5023
Willy Tarreaua15645d2007-03-18 16:22:39 +01005024 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005025 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005026 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5027 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5028 return;
5029 }
5030 asession_temp->serverid[0] = '\0';
5031 }
5032
5033 if (asession_temp->serverid[0] == '\0')
5034 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5035
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005036 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005037 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005038#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005039 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005040 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005041#endif
5042 }/* end if ((t->proxy->appsession_name != NULL) ... */
5043 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5044 } /* we're now at the end of the cookie value */
5045
5046 /* keep the link from this header to next one */
5047 old_idx = cur_idx;
5048 } /* end of cookie processing on this header */
5049}
5050
5051
5052
5053/*
5054 * Check if response is cacheable or not. Updates t->flags.
5055 */
5056void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5057{
5058 struct http_txn *txn = &t->txn;
5059 char *p1, *p2;
5060
5061 char *cur_ptr, *cur_end, *cur_next;
5062 int cur_idx;
5063
Willy Tarreau5df51872007-11-25 16:20:08 +01005064 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005065 return;
5066
5067 /* Iterate through the headers.
5068 * we start with the start line.
5069 */
5070 cur_idx = 0;
5071 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5072
5073 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5074 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005075 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005076
5077 cur_hdr = &txn->hdr_idx.v[cur_idx];
5078 cur_ptr = cur_next;
5079 cur_end = cur_ptr + cur_hdr->len;
5080 cur_next = cur_end + cur_hdr->cr + 1;
5081
5082 /* We have one full header between cur_ptr and cur_end, and the
5083 * next header starts at cur_next. We're only interested in
5084 * "Cookie:" headers.
5085 */
5086
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005087 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5088 if (val) {
5089 if ((cur_end - (cur_ptr + val) >= 8) &&
5090 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5091 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5092 return;
5093 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005094 }
5095
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005096 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5097 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005098 continue;
5099
5100 /* OK, right now we know we have a cache-control header at cur_ptr */
5101
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005102 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005103
5104 if (p1 >= cur_end) /* no more info */
5105 continue;
5106
5107 /* p1 is at the beginning of the value */
5108 p2 = p1;
5109
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005110 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005111 p2++;
5112
5113 /* we have a complete value between p1 and p2 */
5114 if (p2 < cur_end && *p2 == '=') {
5115 /* we have something of the form no-cache="set-cookie" */
5116 if ((cur_end - p1 >= 21) &&
5117 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5118 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005119 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005120 continue;
5121 }
5122
5123 /* OK, so we know that either p2 points to the end of string or to a comma */
5124 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5125 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5126 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5127 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005129 return;
5130 }
5131
5132 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005133 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005134 continue;
5135 }
5136 }
5137}
5138
5139
Willy Tarreau58f10d72006-12-04 02:26:12 +01005140/*
5141 * Try to retrieve a known appsession in the URI, then the associated server.
5142 * If the server is found, it's assigned to the session.
5143 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005144void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005145{
Willy Tarreau3d300592007-03-18 18:34:41 +01005146 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147 appsess *asession_temp = NULL;
5148 appsess local_asession;
5149 char *request_line;
5150
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005151 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005152 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005153 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005154 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155 return;
5156
5157 /* skip ';' */
5158 request_line++;
5159
5160 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005161 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005162 return;
5163
5164 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005165 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005166
5167 /* First try if we already have an appsession */
5168 asession_temp = &local_asession;
5169
Willy Tarreau63963c62007-05-13 21:29:55 +02005170 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005171 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5172 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5173 return;
5174 }
5175
5176 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005177 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5178 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005179 asession_temp->serverid = NULL;
5180
5181 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005182 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5183 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005184 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005185 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005186 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005187 Alert("Not enough memory process_cli():asession:calloc().\n");
5188 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5189 return;
5190 }
5191 asession_temp->sessid = local_asession.sessid;
5192 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005193 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005194 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005195 }
5196 else {
5197 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005198 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005199 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005200
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005201 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005203
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005205 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005206 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005207#endif
5208 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005209 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005210 Alert("Found Application Session without matching server.\n");
5211 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005212 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005213 while (srv) {
5214 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005215 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005216 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005217 txn->flags &= ~TX_CK_MASK;
5218 txn->flags |= TX_CK_VALID;
5219 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005220 t->srv = srv;
5221 break;
5222 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005223 txn->flags &= ~TX_CK_MASK;
5224 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005225 }
5226 }
5227 srv = srv->next;
5228 }
5229 }
5230}
5231
5232
Willy Tarreaub2513902006-12-17 14:52:38 +01005233/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005234 * In a GET or HEAD request, check if the requested URI matches the stats uri
5235 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005236 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005237 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005238 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005239 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005240 *
5241 * Returns 1 if the session's state changes, otherwise 0.
5242 */
5243int stats_check_uri_auth(struct session *t, struct proxy *backend)
5244{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005245 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005246 struct uri_auth *uri_auth = backend->uri_auth;
5247 struct user_auth *user;
5248 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005249 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005250
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005251 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005253 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005254 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005255 return 0;
5256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005257 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005258
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005259 /* the URI is in h */
5260 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005261 return 0;
5262
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005263 h += uri_auth->uri_len;
5264 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5265 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005266 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005267 break;
5268 }
5269 h++;
5270 }
5271
5272 if (uri_auth->refresh) {
5273 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5274 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5275 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005276 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005277 break;
5278 }
5279 h++;
5280 }
5281 }
5282
Willy Tarreau55bb8452007-10-17 18:44:57 +02005283 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5284 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5285 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005286 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005287 break;
5288 }
5289 h++;
5290 }
5291
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005292 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5293
Willy Tarreaub2513902006-12-17 14:52:38 +01005294 /* we are in front of a interceptable URI. Let's check
5295 * if there's an authentication and if it's valid.
5296 */
5297 user = uri_auth->users;
5298 if (!user) {
5299 /* no user auth required, it's OK */
5300 authenticated = 1;
5301 } else {
5302 authenticated = 0;
5303
5304 /* a user list is defined, we have to check.
5305 * skip 21 chars for "Authorization: Basic ".
5306 */
5307
5308 /* FIXME: this should move to an earlier place */
5309 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005310 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5311 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5312 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005313 if (len > 14 &&
5314 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005315 txn->auth_hdr.str = h;
5316 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005317 break;
5318 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005319 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005320 }
5321
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005322 if (txn->auth_hdr.len < 21 ||
5323 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005324 user = NULL;
5325
5326 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005327 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5328 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005329 user->user_pwd, user->user_len)) {
5330 authenticated = 1;
5331 break;
5332 }
5333 user = user->next;
5334 }
5335 }
5336
5337 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005338 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005339
5340 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005341 msg.str = trash;
5342 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005343 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005344 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005345 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005346 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005347 if (!(t->flags & SN_ERR_MASK))
5348 t->flags |= SN_ERR_PRXCOND;
5349 if (!(t->flags & SN_FINST_MASK))
5350 t->flags |= SN_FINST_R;
5351 return 1;
5352 }
5353
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005354 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005355 * data.
5356 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01005357 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02005358 buffer_shutw_now(t->req);
5359 buffer_shutr_now(t->rep);
5360 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005361 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005362 t->data_source = DATA_SRC_STATS;
5363 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005364 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005365 produce_content(t);
5366 return 1;
5367}
5368
5369
Willy Tarreaubaaee002006-06-26 02:48:02 +02005370/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005371 * Print a debug line with a header
5372 */
5373void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5374{
5375 int len, max;
5376 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005377 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005378 max = end - start;
5379 UBOUND(max, sizeof(trash) - len - 1);
5380 len += strlcpy2(trash + len, start, max + 1);
5381 trash[len++] = '\n';
5382 write(1, trash, len);
5383}
5384
5385
Willy Tarreau8797c062007-05-07 00:55:35 +02005386/************************************************************************/
5387/* The code below is dedicated to ACL parsing and matching */
5388/************************************************************************/
5389
5390
5391
5392
5393/* 1. Check on METHOD
5394 * We use the pre-parsed method if it is known, and store its number as an
5395 * integer. If it is unknown, we use the pointer and the length.
5396 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005397static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005398{
5399 int len, meth;
5400
Willy Tarreauae8b7962007-06-09 23:10:04 +02005401 len = strlen(*text);
5402 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005403
5404 pattern->val.i = meth;
5405 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005406 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005407 if (!pattern->ptr.str)
5408 return 0;
5409 pattern->len = len;
5410 }
5411 return 1;
5412}
5413
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005414static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005415acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5416 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005417{
5418 int meth;
5419 struct http_txn *txn = l7;
5420
Willy Tarreaub6866442008-07-14 23:54:42 +02005421 if (!txn)
5422 return 0;
5423
Willy Tarreauc11416f2007-06-17 16:58:38 +02005424 if (txn->req.msg_state != HTTP_MSG_BODY)
5425 return 0;
5426
Willy Tarreau8797c062007-05-07 00:55:35 +02005427 meth = txn->meth;
5428 test->i = meth;
5429 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005430 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5431 /* ensure the indexes are not affected */
5432 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005433 test->len = txn->req.sl.rq.m_l;
5434 test->ptr = txn->req.sol;
5435 }
5436 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5437 return 1;
5438}
5439
5440static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5441{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005442 int icase;
5443
Willy Tarreau8797c062007-05-07 00:55:35 +02005444 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005445 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005446
5447 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005448 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005449
5450 /* Other method, we must compare the strings */
5451 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005452 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005453
5454 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5455 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5456 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005457 return ACL_PAT_FAIL;
5458 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005459}
5460
5461/* 2. Check on Request/Status Version
5462 * We simply compare strings here.
5463 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005464static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005465{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005466 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005467 if (!pattern->ptr.str)
5468 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005469 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005470 return 1;
5471}
5472
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005473static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005474acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5475 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005476{
5477 struct http_txn *txn = l7;
5478 char *ptr;
5479 int len;
5480
Willy Tarreaub6866442008-07-14 23:54:42 +02005481 if (!txn)
5482 return 0;
5483
Willy Tarreauc11416f2007-06-17 16:58:38 +02005484 if (txn->req.msg_state != HTTP_MSG_BODY)
5485 return 0;
5486
Willy Tarreau8797c062007-05-07 00:55:35 +02005487 len = txn->req.sl.rq.v_l;
5488 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5489
5490 while ((len-- > 0) && (*ptr++ != '/'));
5491 if (len <= 0)
5492 return 0;
5493
5494 test->ptr = ptr;
5495 test->len = len;
5496
5497 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5498 return 1;
5499}
5500
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005501static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005502acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5503 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005504{
5505 struct http_txn *txn = l7;
5506 char *ptr;
5507 int len;
5508
Willy Tarreaub6866442008-07-14 23:54:42 +02005509 if (!txn)
5510 return 0;
5511
Willy Tarreauc11416f2007-06-17 16:58:38 +02005512 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5513 return 0;
5514
Willy Tarreau8797c062007-05-07 00:55:35 +02005515 len = txn->rsp.sl.st.v_l;
5516 ptr = txn->rsp.sol;
5517
5518 while ((len-- > 0) && (*ptr++ != '/'));
5519 if (len <= 0)
5520 return 0;
5521
5522 test->ptr = ptr;
5523 test->len = len;
5524
5525 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5526 return 1;
5527}
5528
5529/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005530static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005531acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5532 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005533{
5534 struct http_txn *txn = l7;
5535 char *ptr;
5536 int len;
5537
Willy Tarreaub6866442008-07-14 23:54:42 +02005538 if (!txn)
5539 return 0;
5540
Willy Tarreauc11416f2007-06-17 16:58:38 +02005541 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5542 return 0;
5543
Willy Tarreau8797c062007-05-07 00:55:35 +02005544 len = txn->rsp.sl.st.c_l;
5545 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5546
5547 test->i = __strl2ui(ptr, len);
5548 test->flags = ACL_TEST_F_VOL_1ST;
5549 return 1;
5550}
5551
5552/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005553static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005554acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5555 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005556{
5557 struct http_txn *txn = l7;
5558
Willy Tarreaub6866442008-07-14 23:54:42 +02005559 if (!txn)
5560 return 0;
5561
Willy Tarreauc11416f2007-06-17 16:58:38 +02005562 if (txn->req.msg_state != HTTP_MSG_BODY)
5563 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005564
Willy Tarreauc11416f2007-06-17 16:58:38 +02005565 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5566 /* ensure the indexes are not affected */
5567 return 0;
5568
Willy Tarreau8797c062007-05-07 00:55:35 +02005569 test->len = txn->req.sl.rq.u_l;
5570 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5571
Willy Tarreauf3d25982007-05-08 22:45:09 +02005572 /* we do not need to set READ_ONLY because the data is in a buffer */
5573 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005574 return 1;
5575}
5576
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005577static int
5578acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5579 struct acl_expr *expr, struct acl_test *test)
5580{
5581 struct http_txn *txn = l7;
5582
Willy Tarreaub6866442008-07-14 23:54:42 +02005583 if (!txn)
5584 return 0;
5585
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005586 if (txn->req.msg_state != HTTP_MSG_BODY)
5587 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005588
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005589 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5590 /* ensure the indexes are not affected */
5591 return 0;
5592
5593 /* Parse HTTP request */
5594 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5595 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5596 test->i = AF_INET;
5597
5598 /*
5599 * If we are parsing url in frontend space, we prepare backend stage
5600 * to not parse again the same url ! optimization lazyness...
5601 */
5602 if (px->options & PR_O_HTTP_PROXY)
5603 l4->flags |= SN_ADDR_SET;
5604
5605 test->flags = ACL_TEST_F_READ_ONLY;
5606 return 1;
5607}
5608
5609static int
5610acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5611 struct acl_expr *expr, struct acl_test *test)
5612{
5613 struct http_txn *txn = l7;
5614
Willy Tarreaub6866442008-07-14 23:54:42 +02005615 if (!txn)
5616 return 0;
5617
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005618 if (txn->req.msg_state != HTTP_MSG_BODY)
5619 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005620
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005621 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5622 /* ensure the indexes are not affected */
5623 return 0;
5624
5625 /* Same optimization as url_ip */
5626 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5627 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5628
5629 if (px->options & PR_O_HTTP_PROXY)
5630 l4->flags |= SN_ADDR_SET;
5631
5632 test->flags = ACL_TEST_F_READ_ONLY;
5633 return 1;
5634}
5635
Willy Tarreauc11416f2007-06-17 16:58:38 +02005636/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5637 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5638 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005639static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005640acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005641 struct acl_expr *expr, struct acl_test *test)
5642{
5643 struct http_txn *txn = l7;
5644 struct hdr_idx *idx = &txn->hdr_idx;
5645 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005646
Willy Tarreaub6866442008-07-14 23:54:42 +02005647 if (!txn)
5648 return 0;
5649
Willy Tarreau33a7e692007-06-10 19:45:56 +02005650 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5651 /* search for header from the beginning */
5652 ctx->idx = 0;
5653
Willy Tarreau33a7e692007-06-10 19:45:56 +02005654 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5655 test->flags |= ACL_TEST_F_FETCH_MORE;
5656 test->flags |= ACL_TEST_F_VOL_HDR;
5657 test->len = ctx->vlen;
5658 test->ptr = (char *)ctx->line + ctx->val;
5659 return 1;
5660 }
5661
5662 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5663 test->flags |= ACL_TEST_F_VOL_HDR;
5664 return 0;
5665}
5666
Willy Tarreau33a7e692007-06-10 19:45:56 +02005667static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005668acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5669 struct acl_expr *expr, struct acl_test *test)
5670{
5671 struct http_txn *txn = l7;
5672
Willy Tarreaub6866442008-07-14 23:54:42 +02005673 if (!txn)
5674 return 0;
5675
Willy Tarreauc11416f2007-06-17 16:58:38 +02005676 if (txn->req.msg_state != HTTP_MSG_BODY)
5677 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005678
Willy Tarreauc11416f2007-06-17 16:58:38 +02005679 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5680 /* ensure the indexes are not affected */
5681 return 0;
5682
5683 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5684}
5685
5686static int
5687acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5688 struct acl_expr *expr, struct acl_test *test)
5689{
5690 struct http_txn *txn = l7;
5691
Willy Tarreaub6866442008-07-14 23:54:42 +02005692 if (!txn)
5693 return 0;
5694
Willy Tarreauc11416f2007-06-17 16:58:38 +02005695 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5696 return 0;
5697
5698 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5699}
5700
5701/* 6. Check on HTTP header count. The number of occurrences is returned.
5702 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5703 */
5704static int
5705acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005706 struct acl_expr *expr, struct acl_test *test)
5707{
5708 struct http_txn *txn = l7;
5709 struct hdr_idx *idx = &txn->hdr_idx;
5710 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005711 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005712
Willy Tarreaub6866442008-07-14 23:54:42 +02005713 if (!txn)
5714 return 0;
5715
Willy Tarreau33a7e692007-06-10 19:45:56 +02005716 ctx.idx = 0;
5717 cnt = 0;
5718 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5719 cnt++;
5720
5721 test->i = cnt;
5722 test->flags = ACL_TEST_F_VOL_HDR;
5723 return 1;
5724}
5725
Willy Tarreauc11416f2007-06-17 16:58:38 +02005726static int
5727acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5728 struct acl_expr *expr, struct acl_test *test)
5729{
5730 struct http_txn *txn = l7;
5731
Willy Tarreaub6866442008-07-14 23:54:42 +02005732 if (!txn)
5733 return 0;
5734
Willy Tarreauc11416f2007-06-17 16:58:38 +02005735 if (txn->req.msg_state != HTTP_MSG_BODY)
5736 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005737
Willy Tarreauc11416f2007-06-17 16:58:38 +02005738 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5739 /* ensure the indexes are not affected */
5740 return 0;
5741
5742 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5743}
5744
5745static int
5746acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5747 struct acl_expr *expr, struct acl_test *test)
5748{
5749 struct http_txn *txn = l7;
5750
Willy Tarreaub6866442008-07-14 23:54:42 +02005751 if (!txn)
5752 return 0;
5753
Willy Tarreauc11416f2007-06-17 16:58:38 +02005754 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5755 return 0;
5756
5757 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5758}
5759
Willy Tarreau33a7e692007-06-10 19:45:56 +02005760/* 7. Check on HTTP header's integer value. The integer value is returned.
5761 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005762 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005763 */
5764static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005765acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005766 struct acl_expr *expr, struct acl_test *test)
5767{
5768 struct http_txn *txn = l7;
5769 struct hdr_idx *idx = &txn->hdr_idx;
5770 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005771
Willy Tarreaub6866442008-07-14 23:54:42 +02005772 if (!txn)
5773 return 0;
5774
Willy Tarreau33a7e692007-06-10 19:45:56 +02005775 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5776 /* search for header from the beginning */
5777 ctx->idx = 0;
5778
Willy Tarreau33a7e692007-06-10 19:45:56 +02005779 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5780 test->flags |= ACL_TEST_F_FETCH_MORE;
5781 test->flags |= ACL_TEST_F_VOL_HDR;
5782 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5783 return 1;
5784 }
5785
5786 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5787 test->flags |= ACL_TEST_F_VOL_HDR;
5788 return 0;
5789}
5790
Willy Tarreauc11416f2007-06-17 16:58:38 +02005791static int
5792acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5793 struct acl_expr *expr, struct acl_test *test)
5794{
5795 struct http_txn *txn = l7;
5796
Willy Tarreaub6866442008-07-14 23:54:42 +02005797 if (!txn)
5798 return 0;
5799
Willy Tarreauc11416f2007-06-17 16:58:38 +02005800 if (txn->req.msg_state != HTTP_MSG_BODY)
5801 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005802
Willy Tarreauc11416f2007-06-17 16:58:38 +02005803 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5804 /* ensure the indexes are not affected */
5805 return 0;
5806
5807 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5808}
5809
5810static int
5811acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5812 struct acl_expr *expr, struct acl_test *test)
5813{
5814 struct http_txn *txn = l7;
5815
Willy Tarreaub6866442008-07-14 23:54:42 +02005816 if (!txn)
5817 return 0;
5818
Willy Tarreauc11416f2007-06-17 16:58:38 +02005819 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5820 return 0;
5821
5822 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5823}
5824
Willy Tarreau737b0c12007-06-10 21:28:46 +02005825/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5826 * the first '/' after the possible hostname, and ends before the possible '?'.
5827 */
5828static int
5829acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5830 struct acl_expr *expr, struct acl_test *test)
5831{
5832 struct http_txn *txn = l7;
5833 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005834
Willy Tarreaub6866442008-07-14 23:54:42 +02005835 if (!txn)
5836 return 0;
5837
Willy Tarreauc11416f2007-06-17 16:58:38 +02005838 if (txn->req.msg_state != HTTP_MSG_BODY)
5839 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005840
Willy Tarreauc11416f2007-06-17 16:58:38 +02005841 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5842 /* ensure the indexes are not affected */
5843 return 0;
5844
Willy Tarreau21d2af32008-02-14 20:25:24 +01005845 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5846 ptr = http_get_path(txn);
5847 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005848 return 0;
5849
5850 /* OK, we got the '/' ! */
5851 test->ptr = ptr;
5852
5853 while (ptr < end && *ptr != '?')
5854 ptr++;
5855
5856 test->len = ptr - test->ptr;
5857
5858 /* we do not need to set READ_ONLY because the data is in a buffer */
5859 test->flags = ACL_TEST_F_VOL_1ST;
5860 return 1;
5861}
5862
5863
Willy Tarreau8797c062007-05-07 00:55:35 +02005864
5865/************************************************************************/
5866/* All supported keywords must be declared here. */
5867/************************************************************************/
5868
5869/* Note: must not be declared <const> as its list will be overwritten */
5870static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005871 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5872 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5873 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5874 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005875
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005876 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5877 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5878 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5879 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5880 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5881 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5882 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5883 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5884 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005885
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005886 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5887 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5888 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5889 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5890 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5891 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5892 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5893 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5894 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5895 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005896
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005897 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5898 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5899 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5900 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5901 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5902 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5903 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5904 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5905 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005906
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005907 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5908 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5909 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5910 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5911 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5912 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5913 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005914
Willy Tarreauf3d25982007-05-08 22:45:09 +02005915 { NULL, NULL, NULL, NULL },
5916
5917#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005918 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5919 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5920 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5921 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5922 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5923 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5924 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5925
Willy Tarreau8797c062007-05-07 00:55:35 +02005926 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5927 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5928 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5929 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5930 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5931 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5932 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5933 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5934
5935 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5936 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5937 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5938 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5939 { NULL, NULL, NULL, NULL },
5940#endif
5941}};
5942
5943
5944__attribute__((constructor))
5945static void __http_protocol_init(void)
5946{
5947 acl_register_keywords(&acl_kws);
5948}
5949
5950
Willy Tarreau58f10d72006-12-04 02:26:12 +01005951/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005952 * Local variables:
5953 * c-indent-level: 8
5954 * c-basic-offset: 8
5955 * End:
5956 */