blob: 34850c9c845f8277fd95ce76e837b6be9b4ef0e9 [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>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreauadfb8562008-08-11 15:24:42 +0200369static char *srv_stnames[7] = { "IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200549 buffer_shutw_done(t->req);
550 buffer_shutr_done(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648/* Processes the client and server jobs of a session task, then
649 * puts it back to the wait queue in a clean state, or
650 * cleans up its resources if it must be deleted. Returns
651 * the time the task accepts to wait, or TIME_ETERNITY for
652 * infinity.
653 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200654void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655{
656 struct session *s = t->context;
657 int fsm_resync = 0;
658
659 do {
660 fsm_resync = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200661
662 //fprintf(stderr,"before_cli:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 fsm_resync |= process_cli(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200664
665 //fprintf(stderr,"before_req:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
666 if (s->analysis & AN_REQ_ANY)
667 fsm_resync |= process_request(s);
668
669 //fprintf(stderr,"before_srv:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 fsm_resync |= process_srv(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200671
672 //fprintf(stderr,"endof_loop:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673 } while (fsm_resync);
674
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200675 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100676
677 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
678 session_process_counters(s);
679
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200680 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
681 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreau7f875f62008-08-11 17:35:01 +0200683 /* Trick: if a request is being waiting for the server to respond,
684 * and if we know the server can timeout, we don't want the timeout
685 * to expire on the client side first, but we're still interested
686 * in passing data from the client to the server (eg: POST). Thus,
687 * we can cancel the client's request timeout if the server's
688 * request timeout is set and the server has not yet sent a response.
689 */
690
691 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR_STATUS)) == 0 &&
692 (tick_isset(s->req->cex) || tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
693 s->req->rex = TICK_ETERNITY;
694
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200695 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
696 tick_first(s->rep->rex, s->rep->wex));
697 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200698 if (s->analysis & AN_REQ_ANY) {
699 if (s->analysis & AN_REQ_INSPECT)
700 t->expire = tick_first(t->expire, s->inspect_exp);
701 else if (s->analysis & AN_REQ_HTTP_HDR)
702 t->expire = tick_first(t->expire, s->txn.exp);
703 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704
705 /* restore t to its place in the task list */
706 task_queue(t);
707
Willy Tarreaud825eef2007-05-12 22:35:00 +0200708 *next = t->expire;
709 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710 }
711
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100712 s->fe->feconn--;
713 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200714 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 actconn--;
716
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200717 if (unlikely((global.mode & MODE_DEBUG) &&
718 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100720 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200721 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100722 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 write(1, trash, len);
724 }
725
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200726 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100727 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728
729 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200730 if (s->logs.logwait &&
731 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200732 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
733 if (s->fe->to_log & LW_REQ)
734 http_sess_log(s);
735 else
736 tcp_sess_log(s);
737 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738
739 /* the task MUST not be in the run queue anymore */
740 task_delete(t);
741 session_free(s);
742 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200743 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744}
745
746
Willy Tarreau42250582007-04-01 01:30:43 +0200747extern const char sess_term_cond[8];
748extern const char sess_fin_state[8];
749extern const char *monthname[12];
750const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
751const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
752 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
753 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200754struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200755struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100756
Willy Tarreau42250582007-04-01 01:30:43 +0200757/*
758 * send a log for the session when we have enough info about it.
759 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760 */
Willy Tarreau42250582007-04-01 01:30:43 +0200761static void http_sess_log(struct session *s)
762{
763 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
764 struct proxy *fe = s->fe;
765 struct proxy *be = s->be;
766 struct proxy *prx_log;
767 struct http_txn *txn = &s->txn;
768 int tolog;
769 char *uri, *h;
770 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200771 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200772 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200773 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200774 int hdr;
775
776 if (fe->logfac1 < 0 && fe->logfac2 < 0)
777 return;
778 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100779
Willy Tarreau42250582007-04-01 01:30:43 +0200780 if (s->cli_addr.ss_family == AF_INET)
781 inet_ntop(AF_INET,
782 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
783 pn, sizeof(pn));
784 else
785 inet_ntop(AF_INET6,
786 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
787 pn, sizeof(pn));
788
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200789 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200790
791 /* FIXME: let's limit ourselves to frontend logging for now. */
792 tolog = fe->to_log;
793
794 h = tmpline;
795 if (fe->to_log & LW_REQHDR &&
796 txn->req.cap &&
797 (h < tmpline + sizeof(tmpline) - 10)) {
798 *(h++) = ' ';
799 *(h++) = '{';
800 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
801 if (hdr)
802 *(h++) = '|';
803 if (txn->req.cap[hdr] != NULL)
804 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
805 '#', hdr_encode_map, txn->req.cap[hdr]);
806 }
807 *(h++) = '}';
808 }
809
810 if (fe->to_log & LW_RSPHDR &&
811 txn->rsp.cap &&
812 (h < tmpline + sizeof(tmpline) - 7)) {
813 *(h++) = ' ';
814 *(h++) = '{';
815 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
816 if (hdr)
817 *(h++) = '|';
818 if (txn->rsp.cap[hdr] != NULL)
819 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
820 '#', hdr_encode_map, txn->rsp.cap[hdr]);
821 }
822 *(h++) = '}';
823 }
824
825 if (h < tmpline + sizeof(tmpline) - 4) {
826 *(h++) = ' ';
827 *(h++) = '"';
828 uri = txn->uri ? txn->uri : "<BADREQ>";
829 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
830 '#', url_encode_map, uri);
831 *(h++) = '"';
832 }
833 *h = '\0';
834
835 svid = (tolog & LW_SVID) ?
836 (s->data_source != DATA_SRC_STATS) ?
837 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
838
Willy Tarreau70089872008-06-13 21:12:51 +0200839 t_request = -1;
840 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
841 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
842
Willy Tarreau42250582007-04-01 01:30:43 +0200843 send_log(prx_log, LOG_INFO,
844 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
845 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100846 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200847 pn,
848 (s->cli_addr.ss_family == AF_INET) ?
849 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
850 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200851 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200852 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200853 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200854 t_request,
855 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200856 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
857 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
858 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
859 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100860 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200861 txn->cli_cookie ? txn->cli_cookie : "-",
862 txn->srv_cookie ? txn->srv_cookie : "-",
863 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
864 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
865 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
866 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
867 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100868 (s->flags & SN_REDISP)?"+":"",
869 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200870 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
871
872 s->logs.logwait = 0;
873}
874
Willy Tarreau117f59e2007-03-04 18:17:17 +0100875
876/*
877 * Capture headers from message starting at <som> according to header list
878 * <cap_hdr>, and fill the <idx> structure appropriately.
879 */
880void capture_headers(char *som, struct hdr_idx *idx,
881 char **cap, struct cap_hdr *cap_hdr)
882{
883 char *eol, *sol, *col, *sov;
884 int cur_idx;
885 struct cap_hdr *h;
886 int len;
887
888 sol = som + hdr_idx_first_pos(idx);
889 cur_idx = hdr_idx_first_idx(idx);
890
891 while (cur_idx) {
892 eol = sol + idx->v[cur_idx].len;
893
894 col = sol;
895 while (col < eol && *col != ':')
896 col++;
897
898 sov = col + 1;
899 while (sov < eol && http_is_lws[(unsigned char)*sov])
900 sov++;
901
902 for (h = cap_hdr; h; h = h->next) {
903 if ((h->namelen == col - sol) &&
904 (strncasecmp(sol, h->name, h->namelen) == 0)) {
905 if (cap[h->index] == NULL)
906 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200907 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100908
909 if (cap[h->index] == NULL) {
910 Alert("HTTP capture : out of memory.\n");
911 continue;
912 }
913
914 len = eol - sov;
915 if (len > h->len)
916 len = h->len;
917
918 memcpy(cap[h->index], sov, len);
919 cap[h->index][len]=0;
920 }
921 }
922 sol = eol + idx->v[cur_idx].cr + 1;
923 cur_idx = idx->v[cur_idx].next;
924 }
925}
926
927
Willy Tarreau42250582007-04-01 01:30:43 +0200928/* either we find an LF at <ptr> or we jump to <bad>.
929 */
930#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
931
932/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
933 * otherwise to <http_msg_ood> with <state> set to <st>.
934 */
935#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
936 ptr++; \
937 if (likely(ptr < end)) \
938 goto good; \
939 else { \
940 state = (st); \
941 goto http_msg_ood; \
942 } \
943 } while (0)
944
945
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100947 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100948 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
949 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
950 * will give undefined results.
951 * Note that it is upon the caller's responsibility to ensure that ptr < end,
952 * and that msg->sol points to the beginning of the response.
953 * If a complete line is found (which implies that at least one CR or LF is
954 * found before <end>, the updated <ptr> is returned, otherwise NULL is
955 * returned indicating an incomplete line (which does not mean that parts have
956 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
957 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
958 * upon next call.
959 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200960 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
962 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200963 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100965const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
966 unsigned int state, const char *ptr, const char *end,
967 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100968{
969 __label__
970 http_msg_rpver,
971 http_msg_rpver_sp,
972 http_msg_rpcode,
973 http_msg_rpcode_sp,
974 http_msg_rpreason,
975 http_msg_rpline_eol,
976 http_msg_ood, /* out of data */
977 http_msg_invalid;
978
979 switch (state) {
980 http_msg_rpver:
981 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100982 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
984
985 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100986 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
988 }
989 goto http_msg_invalid;
990
991 http_msg_rpver_sp:
992 case HTTP_MSG_RPVER_SP:
993 if (likely(!HTTP_IS_LWS(*ptr))) {
994 msg->sl.st.c = ptr - msg_buf;
995 goto http_msg_rpcode;
996 }
997 if (likely(HTTP_IS_SPHT(*ptr)))
998 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
999 /* so it's a CR/LF, this is invalid */
1000 goto http_msg_invalid;
1001
1002 http_msg_rpcode:
1003 case HTTP_MSG_RPCODE:
1004 if (likely(!HTTP_IS_LWS(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1006
1007 if (likely(HTTP_IS_SPHT(*ptr))) {
1008 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1010 }
1011
1012 /* so it's a CR/LF, so there is no reason phrase */
1013 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1014 http_msg_rsp_reason:
1015 /* FIXME: should we support HTTP responses without any reason phrase ? */
1016 msg->sl.st.r = ptr - msg_buf;
1017 msg->sl.st.r_l = 0;
1018 goto http_msg_rpline_eol;
1019
1020 http_msg_rpcode_sp:
1021 case HTTP_MSG_RPCODE_SP:
1022 if (likely(!HTTP_IS_LWS(*ptr))) {
1023 msg->sl.st.r = ptr - msg_buf;
1024 goto http_msg_rpreason;
1025 }
1026 if (likely(HTTP_IS_SPHT(*ptr)))
1027 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1028 /* so it's a CR/LF, so there is no reason phrase */
1029 goto http_msg_rsp_reason;
1030
1031 http_msg_rpreason:
1032 case HTTP_MSG_RPREASON:
1033 if (likely(!HTTP_IS_CRLF(*ptr)))
1034 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1035 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1036 http_msg_rpline_eol:
1037 /* We have seen the end of line. Note that we do not
1038 * necessarily have the \n yet, but at least we know that we
1039 * have EITHER \r OR \n, otherwise the response would not be
1040 * complete. We can then record the response length and return
1041 * to the caller which will be able to register it.
1042 */
1043 msg->sl.st.l = ptr - msg->sol;
1044 return ptr;
1045
1046#ifdef DEBUG_FULL
1047 default:
1048 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1049 exit(1);
1050#endif
1051 }
1052
1053 http_msg_ood:
1054 /* out of data */
1055 if (ret_state)
1056 *ret_state = state;
1057 if (ret_ptr)
1058 *ret_ptr = (char *)ptr;
1059 return NULL;
1060
1061 http_msg_invalid:
1062 /* invalid message */
1063 if (ret_state)
1064 *ret_state = HTTP_MSG_ERROR;
1065 return NULL;
1066}
1067
1068
1069/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 * This function parses a request line between <ptr> and <end>, starting with
1071 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1072 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1073 * will give undefined results.
1074 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1075 * and that msg->sol points to the beginning of the request.
1076 * If a complete line is found (which implies that at least one CR or LF is
1077 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1078 * returned indicating an incomplete line (which does not mean that parts have
1079 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1080 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1081 * upon next call.
1082 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001083 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1085 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001086 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001088const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1089 unsigned int state, const char *ptr, const char *end,
1090 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 __label__
1093 http_msg_rqmeth,
1094 http_msg_rqmeth_sp,
1095 http_msg_rquri,
1096 http_msg_rquri_sp,
1097 http_msg_rqver,
1098 http_msg_rqline_eol,
1099 http_msg_ood, /* out of data */
1100 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 switch (state) {
1103 http_msg_rqmeth:
1104 case HTTP_MSG_RQMETH:
1105 if (likely(HTTP_IS_TOKEN(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001109 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 if (likely(HTTP_IS_CRLF(*ptr))) {
1114 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001115 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001116 http_msg_req09_uri:
1117 msg->sl.rq.u = ptr - msg_buf;
1118 http_msg_req09_uri_e:
1119 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1120 http_msg_req09_ver:
1121 msg->sl.rq.v = ptr - msg_buf;
1122 msg->sl.rq.v_l = 0;
1123 goto http_msg_rqline_eol;
1124 }
1125 goto http_msg_invalid;
1126
1127 http_msg_rqmeth_sp:
1128 case HTTP_MSG_RQMETH_SP:
1129 if (likely(!HTTP_IS_LWS(*ptr))) {
1130 msg->sl.rq.u = ptr - msg_buf;
1131 goto http_msg_rquri;
1132 }
1133 if (likely(HTTP_IS_SPHT(*ptr)))
1134 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1135 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1136 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001138 http_msg_rquri:
1139 case HTTP_MSG_RQURI:
1140 if (likely(!HTTP_IS_LWS(*ptr)))
1141 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 if (likely(HTTP_IS_SPHT(*ptr))) {
1144 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1146 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1149 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 http_msg_rquri_sp:
1152 case HTTP_MSG_RQURI_SP:
1153 if (likely(!HTTP_IS_LWS(*ptr))) {
1154 msg->sl.rq.v = ptr - msg_buf;
1155 goto http_msg_rqver;
1156 }
1157 if (likely(HTTP_IS_SPHT(*ptr)))
1158 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1159 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1160 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 http_msg_rqver:
1163 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001164 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001166
1167 if (likely(HTTP_IS_CRLF(*ptr))) {
1168 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1169 http_msg_rqline_eol:
1170 /* We have seen the end of line. Note that we do not
1171 * necessarily have the \n yet, but at least we know that we
1172 * have EITHER \r OR \n, otherwise the request would not be
1173 * complete. We can then record the request length and return
1174 * to the caller which will be able to register it.
1175 */
1176 msg->sl.rq.l = ptr - msg->sol;
1177 return ptr;
1178 }
1179
1180 /* neither an HTTP_VER token nor a CRLF */
1181 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183#ifdef DEBUG_FULL
1184 default:
1185 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1186 exit(1);
1187#endif
1188 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 http_msg_ood:
1191 /* out of data */
1192 if (ret_state)
1193 *ret_state = state;
1194 if (ret_ptr)
1195 *ret_ptr = (char *)ptr;
1196 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198 http_msg_invalid:
1199 /* invalid message */
1200 if (ret_state)
1201 *ret_state = HTTP_MSG_ERROR;
1202 return NULL;
1203}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001204
1205
Willy Tarreau8973c702007-01-21 23:58:29 +01001206/*
1207 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 * when data are missing and recalled at the exact same location with no
1210 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001211 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1212 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001214void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1215{
1216 __label__
1217 http_msg_rqbefore,
1218 http_msg_rqbefore_cr,
1219 http_msg_rqmeth,
1220 http_msg_rqline_end,
1221 http_msg_hdr_first,
1222 http_msg_hdr_name,
1223 http_msg_hdr_l1_sp,
1224 http_msg_hdr_l1_lf,
1225 http_msg_hdr_l1_lws,
1226 http_msg_hdr_val,
1227 http_msg_hdr_l2_lf,
1228 http_msg_hdr_l2_lws,
1229 http_msg_complete_header,
1230 http_msg_last_lf,
1231 http_msg_ood, /* out of data */
1232 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001233
Willy Tarreaue69eada2008-01-27 00:34:10 +01001234 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001237 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 ptr = buf->lr;
1239 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 if (unlikely(ptr >= end))
1242 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 /*
1246 * First, states that are specific to the response only.
1247 * We check them first so that request and headers are
1248 * closer to each other (accessed more often).
1249 */
1250 http_msg_rpbefore:
1251 case HTTP_MSG_RPBEFORE:
1252 if (likely(HTTP_IS_TOKEN(*ptr))) {
1253 if (likely(ptr == buf->data)) {
1254 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001255 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 } else {
1257#if PARSE_PRESERVE_EMPTY_LINES
1258 /* only skip empty leading lines, don't remove them */
1259 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001260 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001261#else
1262 /* Remove empty leading lines, as recommended by
1263 * RFC2616. This takes a lot of time because we
1264 * must move all the buffer backwards, but this
1265 * is rarely needed. The method above will be
1266 * cleaner when we'll be able to start sending
1267 * the request from any place in the buffer.
1268 */
1269 buf->lr = ptr;
1270 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001271 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 msg->sol = buf->data;
1273 ptr = buf->data;
1274 end = buf->r;
1275#endif
1276 }
1277 hdr_idx_init(idx);
1278 state = HTTP_MSG_RPVER;
1279 goto http_msg_rpver;
1280 }
1281
1282 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1283 goto http_msg_invalid;
1284
1285 if (unlikely(*ptr == '\n'))
1286 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1287 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1288 /* stop here */
1289
1290 http_msg_rpbefore_cr:
1291 case HTTP_MSG_RPBEFORE_CR:
1292 EXPECT_LF_HERE(ptr, http_msg_invalid);
1293 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1294 /* stop here */
1295
1296 http_msg_rpver:
1297 case HTTP_MSG_RPVER:
1298 case HTTP_MSG_RPVER_SP:
1299 case HTTP_MSG_RPCODE:
1300 case HTTP_MSG_RPCODE_SP:
1301 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001302 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001303 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 if (unlikely(!ptr))
1305 return;
1306
1307 /* we have a full response and we know that we have either a CR
1308 * or an LF at <ptr>.
1309 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001310 //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 +01001311 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1312
1313 msg->sol = ptr;
1314 if (likely(*ptr == '\r'))
1315 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1316 goto http_msg_rpline_end;
1317
1318 http_msg_rpline_end:
1319 case HTTP_MSG_RPLINE_END:
1320 /* msg->sol must point to the first of CR or LF. */
1321 EXPECT_LF_HERE(ptr, http_msg_invalid);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1323 /* stop here */
1324
1325 /*
1326 * Second, states that are specific to the request only
1327 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 http_msg_rqbefore:
1329 case HTTP_MSG_RQBEFORE:
1330 if (likely(HTTP_IS_TOKEN(*ptr))) {
1331 if (likely(ptr == buf->data)) {
1332 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001333 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 } else {
1335#if PARSE_PRESERVE_EMPTY_LINES
1336 /* only skip empty leading lines, don't remove them */
1337 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001338 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001339#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 /* Remove empty leading lines, as recommended by
1341 * RFC2616. This takes a lot of time because we
1342 * must move all the buffer backwards, but this
1343 * is rarely needed. The method above will be
1344 * cleaner when we'll be able to start sending
1345 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 buf->lr = ptr;
1348 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001349 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 msg->sol = buf->data;
1351 ptr = buf->data;
1352 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001353#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001355 /* we will need this when keep-alive will be supported
1356 hdr_idx_init(idx);
1357 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 state = HTTP_MSG_RQMETH;
1359 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1363 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (unlikely(*ptr == '\n'))
1366 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1367 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 http_msg_rqbefore_cr:
1371 case HTTP_MSG_RQBEFORE_CR:
1372 EXPECT_LF_HERE(ptr, http_msg_invalid);
1373 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 http_msg_rqmeth:
1377 case HTTP_MSG_RQMETH:
1378 case HTTP_MSG_RQMETH_SP:
1379 case HTTP_MSG_RQURI:
1380 case HTTP_MSG_RQURI_SP:
1381 case HTTP_MSG_RQVER:
1382 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001383 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (unlikely(!ptr))
1385 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 /* we have a full request and we know that we have either a CR
1388 * or an LF at <ptr>.
1389 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001390 //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 +01001391 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 msg->sol = ptr;
1394 if (likely(*ptr == '\r'))
1395 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 http_msg_rqline_end:
1399 case HTTP_MSG_RQLINE_END:
1400 /* check for HTTP/0.9 request : no version information available.
1401 * msg->sol must point to the first of CR or LF.
1402 */
1403 if (unlikely(msg->sl.rq.v_l == 0))
1404 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 EXPECT_LF_HERE(ptr, http_msg_invalid);
1407 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001409
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 /*
1411 * Common states below
1412 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_hdr_first:
1414 case HTTP_MSG_HDR_FIRST:
1415 msg->sol = ptr;
1416 if (likely(!HTTP_IS_CRLF(*ptr))) {
1417 goto http_msg_hdr_name;
1418 }
1419
1420 if (likely(*ptr == '\r'))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1422 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_hdr_name:
1425 case HTTP_MSG_HDR_NAME:
1426 /* assumes msg->sol points to the first char */
1427 if (likely(HTTP_IS_TOKEN(*ptr)))
1428 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (likely(*ptr == ':')) {
1431 msg->col = ptr - buf->data;
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 http_msg_hdr_l1_sp:
1438 case HTTP_MSG_HDR_L1_SP:
1439 /* assumes msg->sol points to the first char and msg->col to the colon */
1440 if (likely(HTTP_IS_SPHT(*ptr)))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 /* header value can be basically anything except CR/LF */
1444 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 if (likely(!HTTP_IS_CRLF(*ptr))) {
1447 goto http_msg_hdr_val;
1448 }
1449
1450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1452 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 http_msg_hdr_l1_lf:
1455 case HTTP_MSG_HDR_L1_LF:
1456 EXPECT_LF_HERE(ptr, http_msg_invalid);
1457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 http_msg_hdr_l1_lws:
1460 case HTTP_MSG_HDR_L1_LWS:
1461 if (likely(HTTP_IS_SPHT(*ptr))) {
1462 /* replace HT,CR,LF with spaces */
1463 for (; buf->data+msg->sov < ptr; msg->sov++)
1464 buf->data[msg->sov] = ' ';
1465 goto http_msg_hdr_l1_sp;
1466 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001467 /* we had a header consisting only in spaces ! */
1468 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 goto http_msg_complete_header;
1470
1471 http_msg_hdr_val:
1472 case HTTP_MSG_HDR_VAL:
1473 /* assumes msg->sol points to the first char, msg->col to the
1474 * colon, and msg->sov points to the first character of the
1475 * value.
1476 */
1477 if (likely(!HTTP_IS_CRLF(*ptr)))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 msg->eol = ptr;
1481 /* Note: we could also copy eol into ->eoh so that we have the
1482 * real header end in case it ends with lots of LWS, but is this
1483 * really needed ?
1484 */
1485 if (likely(*ptr == '\r'))
1486 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1487 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 http_msg_hdr_l2_lf:
1490 case HTTP_MSG_HDR_L2_LF:
1491 EXPECT_LF_HERE(ptr, http_msg_invalid);
1492 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_hdr_l2_lws:
1495 case HTTP_MSG_HDR_L2_LWS:
1496 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1497 /* LWS: replace HT,CR,LF with spaces */
1498 for (; msg->eol < ptr; msg->eol++)
1499 *msg->eol = ' ';
1500 goto http_msg_hdr_val;
1501 }
1502 http_msg_complete_header:
1503 /*
1504 * It was a new header, so the last one is finished.
1505 * Assumes msg->sol points to the first char, msg->col to the
1506 * colon, msg->sov points to the first character of the value
1507 * and msg->eol to the first CR or LF so we know how the line
1508 * ends. We insert last header into the index.
1509 */
1510 /*
1511 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1512 write(2, msg->sol, msg->eol-msg->sol);
1513 fprintf(stderr,"\n");
1514 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1517 idx, idx->tail) < 0))
1518 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 msg->sol = ptr;
1521 if (likely(!HTTP_IS_CRLF(*ptr))) {
1522 goto http_msg_hdr_name;
1523 }
1524
1525 if (likely(*ptr == '\r'))
1526 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1527 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 http_msg_last_lf:
1530 case HTTP_MSG_LAST_LF:
1531 /* Assumes msg->sol points to the first of either CR or LF */
1532 EXPECT_LF_HERE(ptr, http_msg_invalid);
1533 ptr++;
1534 buf->lr = ptr;
1535 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001536 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 return;
1538#ifdef DEBUG_FULL
1539 default:
1540 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1541 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001542#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 }
1544 http_msg_ood:
1545 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001546 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 buf->lr = ptr;
1548 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 http_msg_invalid:
1551 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001552 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 return;
1554}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001555
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001556/* This function performs all the processing enabled for the current request.
1557 * It returns 1 if it changes its state and it believes that another function
1558 * must be updated, otherwise zero. It might make sense to explode it into
1559 * several other functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001561int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 struct buffer *req = t->req;
1564 struct buffer *rep = t->rep;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001565 int fsm_resync = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001566
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001567 DPRINTF(stderr,"process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
1568 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreauf161a342007-04-08 16:59:42 +02001569 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001570 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001572 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001573 struct tcp_rule *rule;
1574 int partial;
1575
1576 /* We will abort if we encounter a read error. In theory,
1577 * we should not abort if we get a close, it might be
1578 * valid, also very unlikely. FIXME: we'll abort for now,
1579 * this will be easier to change later.
1580 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001581 if (req->flags & BF_READ_ERROR) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001582 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001583 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001584 t->fe->failed_req++;
1585 if (!(t->flags & SN_ERR_MASK))
1586 t->flags |= SN_ERR_CLICL;
1587 if (!(t->flags & SN_FINST_MASK))
1588 t->flags |= SN_FINST_R;
1589 return 1;
1590 }
1591
1592 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001593 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001594 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001595 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001596 t->fe->failed_req++;
1597 if (!(t->flags & SN_ERR_MASK))
1598 t->flags |= SN_ERR_CLITO;
1599 if (!(t->flags & SN_FINST_MASK))
1600 t->flags |= SN_FINST_R;
1601 return 1;
1602 }
1603
1604 /* We don't know whether we have enough data, so must proceed
1605 * this way :
1606 * - iterate through all rules in their declaration order
1607 * - if one rule returns MISS, it means the inspect delay is
1608 * not over yet, then return immediately, otherwise consider
1609 * it as a non-match.
1610 * - if one rule returns OK, then return OK
1611 * - if one rule returns KO, then return KO
1612 */
1613
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001614 if (req->flags & (BF_READ_NULL | BF_SHUTR_STATUS) ||
1615 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001616 partial = 0;
1617 else
1618 partial = ACL_PARTIAL;
1619
1620 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1621 int ret = ACL_PAT_PASS;
1622
1623 if (rule->cond) {
1624 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1625 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001626 /* just set the request timeout once at the beginning of the request */
1627 if (!tick_isset(t->inspect_exp))
1628 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
1629
1630 return fsm_resync;
Willy Tarreaub6866442008-07-14 23:54:42 +02001631 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001632
Willy Tarreaub6866442008-07-14 23:54:42 +02001633 ret = acl_pass(ret);
1634 if (rule->cond->pol == ACL_COND_UNLESS)
1635 ret = !ret;
1636 }
1637
1638 if (ret) {
1639 /* we have a matching rule. */
1640 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001641 buffer_shutr_done(req);
1642 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001643 fd_delete(t->cli_fd);
1644 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001645 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001646 t->fe->failed_req++;
1647 if (!(t->flags & SN_ERR_MASK))
1648 t->flags |= SN_ERR_PRXCOND;
1649 if (!(t->flags & SN_FINST_MASK))
1650 t->flags |= SN_FINST_R;
1651 t->inspect_exp = TICK_ETERNITY;
1652 return 1;
1653 }
1654 /* otherwise accept */
1655 break;
1656 }
1657 }
1658
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001659 /* if we get there, it means we have no rule which matches, or
1660 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001661 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001662 t->analysis &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001663 t->inspect_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001664 fsm_resync = 1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001665 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001666
1667 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 /*
1669 * Now parse the partial (or complete) lines.
1670 * We will check the request syntax, and also join multi-line
1671 * headers. An index of all the lines will be elaborated while
1672 * parsing.
1673 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001677 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 * req->data + req->eoh = end of processed headers / start of current one
1679 * req->data + req->eol = end of current header or line (LF or CRLF)
1680 * req->lr = first non-visited byte
1681 * req->r = end of data
1682 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001685 struct http_txn *txn = &t->txn;
1686 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001690 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 /* 1: we might have to print this header in debug mode */
1693 if (unlikely((global.mode & MODE_DEBUG) &&
1694 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001695 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001697
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001698 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 eol = sol + msg->sl.rq.l;
1700 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001701
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001702 sol += hdr_idx_first_pos(&txn->hdr_idx);
1703 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001706 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001708 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1709 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 }
1711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001712
Willy Tarreau58f10d72006-12-04 02:26:12 +01001713
1714 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001716 * If not so, we check the FD and buffer states before leaving.
1717 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1719 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001720 *
1721 */
1722
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001723 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 /*
1725 * First, let's catch bad requests.
1726 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001729
1730 /* 1: Since we are in header mode, if there's no space
1731 * left for headers, we won't be able to free more
1732 * later, so the session will never terminate. We
1733 * must terminate it now.
1734 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 if (unlikely(req->l >= req->rlim - req->data)) {
1736 /* FIXME: check if URI is set and return Status
1737 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738 */
Willy Tarreau06619262006-12-17 08:37:22 +01001739 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001740 }
1741
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001742 /* 2: have we encountered a close ? */
1743 else if (req->flags & BF_READ_NULL) {
1744 txn->status = 400;
1745 client_retnclose(t, error_message(t, HTTP_ERR_400));
1746 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001747 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001748 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001749
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750 if (!(t->flags & SN_ERR_MASK))
1751 t->flags |= SN_ERR_CLICL;
1752 if (!(t->flags & SN_FINST_MASK))
1753 t->flags |= SN_FINST_R;
1754 return 1;
1755 }
1756
1757 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001758 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001759 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001760 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001761 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001762 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001763 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001764 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001765 if (!(t->flags & SN_ERR_MASK))
1766 t->flags |= SN_ERR_CLITO;
1767 if (!(t->flags & SN_FINST_MASK))
1768 t->flags |= SN_FINST_R;
1769 return 1;
1770 }
1771
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001772 /* 4: have we encountered a read error or did we have to shutdown ? */
1773 else if (req->flags & (BF_READ_ERROR | BF_SHUTR_STATUS)) {
1774 /* we cannot return any message on error */
1775 msg->msg_state = HTTP_MSG_ERROR;
1776 t->analysis &= ~AN_REQ_ANY;
1777 t->fe->failed_req++;
1778 if (!(t->flags & SN_ERR_MASK))
1779 t->flags |= SN_ERR_CLICL;
1780 if (!(t->flags & SN_FINST_MASK))
1781 t->flags |= SN_FINST_R;
1782 return 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001783 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001784
1785 /* just set the request timeout once at the beginning of the request */
1786 if (!tick_isset(t->txn.exp))
1787 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1788
1789 /* we're not ready yet */
1790 return fsm_resync;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791 }
1792
1793
1794 /****************************************************************
1795 * More interesting part now : we know that we have a complete *
1796 * request which at least looks like HTTP. We have an indicator *
1797 * of each header's length, so we can parse them quickly. *
1798 ****************************************************************/
1799
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001800 t->analysis &= ~AN_REQ_HTTP_HDR;
1801
Willy Tarreau9cdde232007-05-02 20:58:19 +02001802 /* ensure we keep this pointer to the beginning of the message */
1803 msg->sol = req->data + msg->som;
1804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 /*
1806 * 1: identify the method
1807 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001808 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001809
1810 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001812 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001814 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 if (unlikely((t->fe->monitor_uri_len != 0) &&
1816 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1817 !memcmp(&req->data[msg->sl.rq.u],
1818 t->fe->monitor_uri,
1819 t->fe->monitor_uri_len))) {
1820 /*
1821 * We have found the monitor URI
1822 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001823 struct acl_cond *cond;
1824 cur_proxy = t->fe;
1825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001827
1828 /* Check if we want to fail this monitor request or not */
1829 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1830 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001831
1832 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001833 if (cond->pol == ACL_COND_UNLESS)
1834 ret = !ret;
1835
1836 if (ret) {
1837 /* we fail this request, let's return 503 service unavail */
1838 txn->status = 503;
1839 client_retnclose(t, error_message(t, HTTP_ERR_503));
1840 goto return_prx_cond;
1841 }
1842 }
1843
1844 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001845 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 client_retnclose(t, &http_200_chunk);
1847 goto return_prx_cond;
1848 }
1849
1850 /*
1851 * 3: Maybe we have to copy the original REQURI for the logs ?
1852 * Note: we cannot log anymore if the request has been
1853 * classified as invalid.
1854 */
1855 if (unlikely(t->logs.logwait & LW_REQ)) {
1856 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001857 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 int urilen = msg->sl.rq.l;
1859
1860 if (urilen >= REQURI_LEN)
1861 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001862 memcpy(txn->uri, &req->data[msg->som], urilen);
1863 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864
1865 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001866 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 } else {
1868 Alert("HTTP logging : out of memory.\n");
1869 }
1870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001871
Willy Tarreau06619262006-12-17 08:37:22 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1874 if (unlikely(msg->sl.rq.v_l == 0)) {
1875 int delta;
1876 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001877 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 cur_end = msg->sol + msg->sl.rq.l;
1879 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 if (msg->sl.rq.u_l == 0) {
1882 /* if no URI was set, add "/" */
1883 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1884 cur_end += delta;
1885 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001886 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 /* add HTTP version */
1888 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1889 msg->eoh += delta;
1890 cur_end += delta;
1891 cur_end = (char *)http_parse_reqline(msg, req->data,
1892 HTTP_MSG_RQMETH,
1893 msg->sol, cur_end + 1,
1894 NULL, NULL);
1895 if (unlikely(!cur_end))
1896 goto return_bad_req;
1897
1898 /* we have a full HTTP/1.0 request now and we know that
1899 * we have either a CR or an LF at <ptr>.
1900 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001901 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902 }
1903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001904
1905 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001906 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001907 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001908 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001909
1910 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912 * As opposed to version 1.2, now they will be evaluated in the
1913 * filters order and not in the header order. This means that
1914 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001915 *
1916 * We can now check whether we want to switch to another
1917 * backend, in which case we will re-check the backend's
1918 * filters and various options. In order to support 3-level
1919 * switching, here's how we should proceed :
1920 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001921 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001922 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001923 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001924 * There cannot be any switch from there, so ->be cannot be
1925 * changed anymore.
1926 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001927 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001928 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001929 * The response path will be able to apply either ->be, or
1930 * ->be then ->fe filters in order to match the reverse of
1931 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001932 */
1933
Willy Tarreau06619262006-12-17 08:37:22 +01001934 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001935 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001936 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001937 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001938 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001939
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001940 /* first check whether we have some ACLs set to redirect this request */
1941 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1942 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001943
1944 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001945 if (rule->cond->pol == ACL_COND_UNLESS)
1946 ret = !ret;
1947
1948 if (ret) {
1949 struct chunk rdr = { trash, 0 };
1950 const char *msg_fmt;
1951
1952 /* build redirect message */
1953 switch(rule->code) {
1954 case 303:
1955 rdr.len = strlen(HTTP_303);
1956 msg_fmt = HTTP_303;
1957 break;
1958 case 301:
1959 rdr.len = strlen(HTTP_301);
1960 msg_fmt = HTTP_301;
1961 break;
1962 case 302:
1963 default:
1964 rdr.len = strlen(HTTP_302);
1965 msg_fmt = HTTP_302;
1966 break;
1967 }
1968
1969 if (unlikely(rdr.len > sizeof(trash)))
1970 goto return_bad_req;
1971 memcpy(rdr.str, msg_fmt, rdr.len);
1972
1973 switch(rule->type) {
1974 case REDIRECT_TYPE_PREFIX: {
1975 const char *path;
1976 int pathlen;
1977
1978 path = http_get_path(txn);
1979 /* build message using path */
1980 if (path) {
1981 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1982 } else {
1983 path = "/";
1984 pathlen = 1;
1985 }
1986
1987 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1988 goto return_bad_req;
1989
1990 /* add prefix */
1991 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1992 rdr.len += rule->rdr_len;
1993
1994 /* add path */
1995 memcpy(rdr.str + rdr.len, path, pathlen);
1996 rdr.len += pathlen;
1997 break;
1998 }
1999 case REDIRECT_TYPE_LOCATION:
2000 default:
2001 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2002 goto return_bad_req;
2003
2004 /* add location */
2005 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2006 rdr.len += rule->rdr_len;
2007 break;
2008 }
2009
2010 /* add end of headers */
2011 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2012 rdr.len += 4;
2013
2014 txn->status = rule->code;
2015 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002016 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002017 client_retnclose(t, &rdr);
2018 goto return_prx_cond;
2019 }
2020 }
2021
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002022 /* first check whether we have some ACLs set to block this request */
2023 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002024 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002025
2026 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002027 if (cond->pol == ACL_COND_UNLESS)
2028 ret = !ret;
2029
2030 if (ret) {
2031 txn->status = 403;
2032 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002033 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002034 client_retnclose(t, error_message(t, HTTP_ERR_403));
2035 goto return_prx_cond;
2036 }
2037 }
2038
Willy Tarreau06619262006-12-17 08:37:22 +01002039 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002040 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002041 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2042 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002043 }
2044
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002045 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2046 /* to ensure correct connection accounting on
2047 * the backend, we count the connection for the
2048 * one managing the queue.
2049 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002050 t->be->beconn++;
2051 if (t->be->beconn > t->be->beconn_max)
2052 t->be->beconn_max = t->be->beconn;
2053 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002054 t->flags |= SN_BE_ASSIGNED;
2055 }
2056
Willy Tarreau06619262006-12-17 08:37:22 +01002057 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002058 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002059 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002060 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002061 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002062 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002063 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002064 goto return_prx_cond;
2065 }
2066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002067 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002068 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 !(t->flags & SN_CONN_CLOSED)) {
2070 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002071 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002072 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002073
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002074 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002075 old_idx = 0;
2076
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002077 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2078 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 cur_ptr = cur_next;
2080 cur_end = cur_ptr + cur_hdr->len;
2081 cur_next = cur_end + cur_hdr->cr + 1;
2082
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002083 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2084 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002085 /* 3 possibilities :
2086 * - we have already set Connection: close,
2087 * so we remove this line.
2088 * - we have not yet set Connection: close,
2089 * but this line indicates close. We leave
2090 * it untouched and set the flag.
2091 * - we have not yet set Connection: close,
2092 * and this line indicates non-close. We
2093 * replace it.
2094 */
2095 if (t->flags & SN_CONN_CLOSED) {
2096 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002097 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002098 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002099 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2100 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 cur_hdr->len = 0;
2102 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002103 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2104 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2105 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002106 cur_next += delta;
2107 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002108 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002109 }
2110 t->flags |= SN_CONN_CLOSED;
2111 }
2112 }
2113 old_idx = cur_idx;
2114 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002115 }
2116 /* add request headers from the rule sets in the same order */
2117 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2118 if (unlikely(http_header_add_tail(req,
2119 &txn->req,
2120 &txn->hdr_idx,
2121 rule_set->req_add[cur_idx])) < 0)
2122 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002123 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002125 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002126 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002127 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002128 /* we have to check the URI and auth for this request.
2129 * FIXME!!! that one is rather dangerous, we want to
2130 * make it follow standard rules (eg: clear t->analysis).
2131 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002132 if (stats_check_uri_auth(t, rule_set))
2133 return 1;
2134 }
2135
Willy Tarreau55ea7572007-06-17 19:56:27 +02002136 /* now check whether we have some switching rules for this request */
2137 if (!(t->flags & SN_BE_ASSIGNED)) {
2138 struct switching_rule *rule;
2139
2140 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2141 int ret;
2142
2143 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002144
2145 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002146 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002147 ret = !ret;
2148
2149 if (ret) {
2150 t->be = rule->be.backend;
2151 t->be->beconn++;
2152 if (t->be->beconn > t->be->beconn_max)
2153 t->be->beconn_max = t->be->beconn;
2154 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002155
2156 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002157 t->rep->rto = t->req->wto = t->be->timeout.server;
2158 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002159 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002160 t->flags |= SN_BE_ASSIGNED;
2161 break;
2162 }
2163 }
2164 }
2165
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002166 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2167 /* No backend was set, but there was a default
2168 * backend set in the frontend, so we use it and
2169 * loop again.
2170 */
2171 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002172 t->be->beconn++;
2173 if (t->be->beconn > t->be->beconn_max)
2174 t->be->beconn_max = t->be->beconn;
2175 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002176
2177 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002178 t->rep->rto = t->req->wto = t->be->timeout.server;
2179 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002180 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002181 t->flags |= SN_BE_ASSIGNED;
2182 }
2183 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002184
Willy Tarreau58f10d72006-12-04 02:26:12 +01002185
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002186 if (!(t->flags & SN_BE_ASSIGNED)) {
2187 /* To ensure correct connection accounting on
2188 * the backend, we count the connection for the
2189 * one managing the queue.
2190 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002191 t->be->beconn++;
2192 if (t->be->beconn > t->be->beconn_max)
2193 t->be->beconn_max = t->be->beconn;
2194 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002195 t->flags |= SN_BE_ASSIGNED;
2196 }
2197
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002198 /*
2199 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002200 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002201 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002202 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002203 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002204
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002205 /*
2206 * If HTTP PROXY is set we simply get remote server address
2207 * parsing incoming request.
2208 */
2209 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2210 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2211 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002212
Willy Tarreau2a324282006-12-05 00:05:46 +01002213 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002214 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002215 * so let's do the same now.
2216 */
2217
2218 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002219 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002220 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002221 }
2222
2223
2224 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002225 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002226 * Note that doing so might move headers in the request, but
2227 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002228 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002229 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002230 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2231 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002232 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002233
Willy Tarreau58f10d72006-12-04 02:26:12 +01002234
Willy Tarreau2a324282006-12-05 00:05:46 +01002235 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002236 * 9: add X-Forwarded-For if either the frontend or the backend
2237 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002238 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002239 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002240 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002241 /* Add an X-Forwarded-For header unless the source IP is
2242 * in the 'except' network range.
2243 */
2244 if ((!t->fe->except_mask.s_addr ||
2245 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2246 != t->fe->except_net.s_addr) &&
2247 (!t->be->except_mask.s_addr ||
2248 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2249 != t->be->except_net.s_addr)) {
2250 int len;
2251 unsigned char *pn;
2252 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002253
Ross Westaf72a1d2008-08-03 10:51:45 +02002254 /* Note: we rely on the backend to get the header name to be used for
2255 * x-forwarded-for, because the header is really meant for the backends.
2256 * However, if the backend did not specify any option, we have to rely
2257 * on the frontend's header name.
2258 */
2259 if (t->be->fwdfor_hdr_len) {
2260 len = t->be->fwdfor_hdr_len;
2261 memcpy(trash, t->be->fwdfor_hdr_name, len);
2262 } else {
2263 len = t->fe->fwdfor_hdr_len;
2264 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2265 }
2266 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002267
Ross Westaf72a1d2008-08-03 10:51:45 +02002268 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002269 &txn->hdr_idx, trash, len)) < 0)
2270 goto return_bad_req;
2271 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002272 }
2273 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002274 /* FIXME: for the sake of completeness, we should also support
2275 * 'except' here, although it is mostly useless in this case.
2276 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002277 int len;
2278 char pn[INET6_ADDRSTRLEN];
2279 inet_ntop(AF_INET6,
2280 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2281 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002282
2283 /* Note: we rely on the backend to get the header name to be used for
2284 * x-forwarded-for, because the header is really meant for the backends.
2285 * However, if the backend did not specify any option, we have to rely
2286 * on the frontend's header name.
2287 */
2288 if (t->be->fwdfor_hdr_len) {
2289 len = t->be->fwdfor_hdr_len;
2290 memcpy(trash, t->be->fwdfor_hdr_name, len);
2291 } else {
2292 len = t->fe->fwdfor_hdr_len;
2293 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2294 }
2295 len += sprintf(trash + len, ": %s", pn);
2296
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002297 if (unlikely(http_header_add_tail2(req, &txn->req,
2298 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002299 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002300 }
2301 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002302
Willy Tarreau2a324282006-12-05 00:05:46 +01002303 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002304 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002305 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002306 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002307 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002308 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002309 if ((unlikely(msg->sl.rq.v_l != 8) ||
2310 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2311 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002312 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002313 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002314 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002315 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002316 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2317 * If not assigned, perhaps we are balancing on url_param, but this is a
2318 * POST; and the parameters are in the body, maybe scan there to find our server.
2319 * (unless headers overflowed the buffer?)
2320 */
2321 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2322 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002323 t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002324 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2325 /* are there enough bytes here? total == l || r || rlim ?
2326 * len is unsigned, but eoh is int,
2327 * how many bytes of body have we received?
2328 * eoh is the first empty line of the header
2329 */
2330 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002331 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 +02002332
2333 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2334 * We can't assume responsibility for the server's decision,
2335 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2336 * We also can't change our mind later, about which server to choose, so round robin.
2337 */
2338 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2339 struct hdr_ctx ctx;
2340 ctx.idx = 0;
2341 /* Expect is allowed in 1.1, look for it */
2342 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2343 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002344 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002345 /* We can't reliablly stall and wait for data, because of
2346 * .NET clients that don't conform to rfc2616; so, no need for
2347 * the next block to check length expectations.
2348 * We could send 100 status back to the client, but then we need to
2349 * re-write headers, and send the message. And this isn't the right
2350 * place for that action.
2351 * TODO: support Expect elsewhere and delete this block.
2352 */
2353 goto end_check_maybe_wait_for_body;
2354 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002355
2356 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002357 /* nothing to do, we got enough */
2358 } else {
2359 /* limit implies we are supposed to need this many bytes
2360 * to find the parameter. Let's see how many bytes we can wait for.
2361 */
2362 long long hint = len;
2363 struct hdr_ctx ctx;
2364 ctx.idx = 0;
2365 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002366 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002367 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002368 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002369 ctx.idx = 0;
2370 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2371 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002372 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002373 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002374 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002375 hint = 0; /* parse failure, untrusted client */
2376 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002377 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002378 msg->hdr_content_len = hint;
2379 else
2380 hint = 0; /* bad client, sent negative length */
2381 }
2382 }
2383 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002384 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002385 hint = t->be->url_param_post_limit;
2386 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002387 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002388 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002389 /* else... There are no body bytes to wait for */
2390 }
2391 }
2392 }
2393 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002394
Willy Tarreau2a324282006-12-05 00:05:46 +01002395 /*************************************************************
2396 * OK, that's finished for the headers. We have done what we *
2397 * could. Let's switch to the DATA state. *
2398 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002399
Willy Tarreauadfb8562008-08-11 15:24:42 +02002400 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002401 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402
Willy Tarreau1fa31262007-12-03 00:36:16 +01002403 /* When a connection is tarpitted, we use the tarpit timeout,
2404 * which may be the same as the connect timeout if unspecified.
2405 * If unset, then set it to zero because we really want it to
2406 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002407 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002408 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002409 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002410 t->req->l = 0;
2411 /* flush the request so that we can drop the connection early
2412 * if the client closes first.
2413 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002414 req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2415 if (!req->cex)
2416 req->cex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002419 /* OK let's go on with the BODY now */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002420 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002421 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002422
2423 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002424 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002425 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002426 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002427 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002428 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002429 return_prx_cond:
2430 if (!(t->flags & SN_ERR_MASK))
2431 t->flags |= SN_ERR_PRXCOND;
2432 if (!(t->flags & SN_FINST_MASK))
2433 t->flags |= SN_FINST_R;
2434 return 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002435 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002436 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002437 }
2438
2439 if (t->analysis & AN_REQ_HTTP_BODY) {
2440 /* We have to parse the HTTP request body to find any required data.
2441 * "balance url_param check_post" should have been the only way to get
2442 * into this. We were brought here after HTTP header analysis, so all
2443 * related structures are ready.
2444 */
2445 struct http_msg *msg = &t->txn.req;
2446 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2447 long long limit = t->be->url_param_post_limit;
2448 struct hdr_ctx ctx;
2449
2450 ctx.idx = 0;
2451
2452 /* now if we have a length, we'll take the hint */
2453 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2454 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2455 unsigned int chunk = 0;
2456 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2457 char c = msg->sol[body];
2458 if (ishex(c)) {
2459 unsigned int hex = toupper(c) - '0';
2460 if (hex > 9)
2461 hex -= 'A' - '9' - 1;
2462 chunk = (chunk << 4) | hex;
2463 } else
2464 break;
2465 body++;
2466 }
2467 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002468 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002469
2470 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002471 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002472
2473 body += 2; // skip CRLF
2474
2475 /* if we support more then one chunk here, we have to do it again when assigning server
2476 * 1. how much entity data do we have? new var
2477 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2478 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2479 */
2480
2481 if (chunk < limit)
2482 limit = chunk; /* only reading one chunk */
2483 } else {
2484 if (msg->hdr_content_len < limit)
2485 limit = msg->hdr_content_len;
2486 }
2487
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002488 http_body_end:
2489 /* we leave once we know we have nothing left to do. This means that we have
2490 * enough bytes, or that we know we'll not get any more (buffer full, read
2491 * buffer closed).
2492 */
2493 if (req->l - body >= limit || /* enough bytes! */
2494 req->l >= req->rlim - req->data || /* full */
2495 req->flags & (BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
2496 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002497 t->logs.tv_request = now; /* update the request timer to reflect full request */
2498 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002499 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002500 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002501 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002502
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002503 return fsm_resync;
2504}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002505
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002506/*
2507 * manages the client FSM and its socket. BTW, it also tries to handle the
2508 * cookie. It returns 1 if a state has changed (and a resync may be needed),
2509 * 0 else.
2510 */
2511int process_cli(struct session *t)
2512{
2513 int s = t->srv_state;
2514 int c = t->cli_state;
2515 struct buffer *req = t->req;
2516 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002517
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002518 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x\n",
2519 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
2520 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2521 req->rex, rep->wex,
2522 req->flags, rep->flags);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002523
Willy Tarreau7f875f62008-08-11 17:35:01 +02002524 /* if no analysis remains, it's time to forward the connection */
2525 if (!(t->analysis & AN_REQ_ANY) && !(req->flags & (BF_MAY_CONNECT|BF_MAY_FORWARD)))
2526 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
2527
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002528 if (c == CL_STDATA || c == CL_STSHUTR || c == CL_STSHUTW) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002530 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002531 buffer_shutr_done(req);
2532 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002533 fd_delete(t->cli_fd);
2534 t->cli_state = CL_STCLOSE;
2535 if (!(t->flags & SN_ERR_MASK))
2536 t->flags |= SN_ERR_CLICL;
2537 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002538 if (t->analysis & AN_REQ_ANY)
2539 t->flags |= SN_FINST_R;
2540 else if (t->pend_pos)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002541 t->flags |= SN_FINST_Q;
2542 else if (s == SV_STCONN)
2543 t->flags |= SN_FINST_C;
2544 else
2545 t->flags |= SN_FINST_D;
2546 }
2547 return 1;
2548 }
2549 /* last read, or end of server write */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002550 else if (!(req->flags & BF_SHUTR_STATUS) && /* already done */
2551 req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
2552 buffer_shutr_done(req);
2553 if (!(rep->flags & BF_SHUTW_STATUS)) {
2554 EV_FD_CLR(t->cli_fd, DIR_RD);
2555 t->cli_state = CL_STSHUTR;
2556 } else {
2557 /* output was already closed */
2558 fd_delete(t->cli_fd);
2559 t->cli_state = CL_STCLOSE;
2560 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561 return 1;
2562 }
2563 /* last server read and buffer empty */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002564 else if (!(rep->flags & BF_SHUTW_STATUS) && /* already done */
2565 rep->l == 0 && rep->flags & BF_SHUTR_STATUS && !(t->flags & SN_SELF_GEN)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002566 buffer_shutw_done(rep);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002567 if (!(req->flags & BF_SHUTR_STATUS)) {
2568 EV_FD_CLR(t->cli_fd, DIR_WR);
2569 shutdown(t->cli_fd, SHUT_WR);
2570 /* We must ensure that the read part is still alive when switching to shutw */
2571 /* FIXME: is this still true ? */
2572 EV_FD_SET(t->cli_fd, DIR_RD);
2573 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2574 t->cli_state = CL_STSHUTW;
2575 } else {
2576 fd_delete(t->cli_fd);
2577 t->cli_state = CL_STCLOSE;
2578 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002579 return 1;
2580 }
2581 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002582 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002583 buffer_shutr_done(req);
2584 req->flags |= BF_READ_TIMEOUT;
2585 if (!(rep->flags & BF_SHUTW_STATUS)) {
2586 EV_FD_CLR(t->cli_fd, DIR_RD);
2587 t->cli_state = CL_STSHUTR;
2588 } else {
2589 /* output was already closed */
2590 fd_delete(t->cli_fd);
2591 t->cli_state = CL_STCLOSE;
2592 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002593 if (!(t->flags & SN_ERR_MASK))
2594 t->flags |= SN_ERR_CLITO;
2595 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002596 if (t->analysis & AN_REQ_ANY)
2597 t->flags |= SN_FINST_R;
2598 else if (t->pend_pos)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599 t->flags |= SN_FINST_Q;
2600 else if (s == SV_STCONN)
2601 t->flags |= SN_FINST_C;
2602 else
2603 t->flags |= SN_FINST_D;
2604 }
2605 return 1;
2606 }
2607 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002608 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002609 buffer_shutw_done(rep);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002610 rep->flags |= BF_WRITE_TIMEOUT;
2611 if (!(req->flags & BF_SHUTR_STATUS)) {
2612 EV_FD_CLR(t->cli_fd, DIR_WR);
2613 shutdown(t->cli_fd, SHUT_WR);
2614 /* We must ensure that the read part is still alive when switching to shutw */
2615 /* FIXME: is this still true ? */
2616 EV_FD_SET(t->cli_fd, DIR_RD);
2617 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2618 t->cli_state = CL_STSHUTW;
2619 } else {
2620 fd_delete(t->cli_fd);
2621 t->cli_state = CL_STCLOSE;
2622 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624 if (!(t->flags & SN_ERR_MASK))
2625 t->flags |= SN_ERR_CLITO;
2626 if (!(t->flags & SN_FINST_MASK)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002627 if (t->analysis & AN_REQ_ANY)
2628 t->flags |= SN_FINST_R;
2629 else if (t->pend_pos)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630 t->flags |= SN_FINST_Q;
2631 else if (s == SV_STCONN)
2632 t->flags |= SN_FINST_C;
2633 else
2634 t->flags |= SN_FINST_D;
2635 }
2636 return 1;
2637 }
2638
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002639 /* manage read timeout */
2640 if (!(req->flags & BF_SHUTR_STATUS)) {
2641 if (req->l >= req->rlim - req->data) {
2642 /* no room to read more data */
2643 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
2644 /* stop reading until we get some space */
2645 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002646 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002647 } else {
2648 EV_FD_COND_S(t->cli_fd, DIR_RD);
2649 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002650 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002651 }
2652
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002653 /* manage write timeout */
2654 if (!(rep->flags & BF_SHUTW_STATUS)) {
2655 /* first, we may have to produce data (eg: stats).
2656 * right now, this is limited to the SHUTR state.
2657 */
2658 if (req->flags & BF_SHUTR_STATUS && t->flags & SN_SELF_GEN) {
2659 produce_content(t);
2660 if (rep->l == 0) {
2661 buffer_shutw_done(rep);
2662 fd_delete(t->cli_fd);
2663 t->cli_state = CL_STCLOSE;
2664 return 1;
2665 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002666 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002667
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002668 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
2669 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
2670 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2671 /* stop writing */
2672 rep->wex = TICK_ETERNITY;
2673 }
2674 } else {
2675 /* buffer not empty */
2676 if (!tick_isset(rep->wex)) {
2677 EV_FD_COND_S(t->cli_fd, DIR_WR);
2678 /* restart writing */
2679 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
2680 if (!(req->flags & BF_SHUTR_STATUS) && tick_isset(rep->wex) && tick_isset(req->rex)) {
2681 /* FIXME: to prevent the client from expiring read timeouts during writes,
2682 * we refresh it, except if it was already infinite. */
2683 req->rex = rep->wex;
2684 }
2685 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002687 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002688 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689 }
2690 else { /* CL_STCLOSE: nothing to do */
2691 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2692 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002693 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002694 write(1, trash, len);
2695 }
2696 return 0;
2697 }
2698 return 0;
2699}
2700
2701
2702/*
2703 * manages the server FSM and its socket. It returns 1 if a state has changed
2704 * (and a resync may be needed), 0 else.
2705 */
2706int process_srv(struct session *t)
2707{
2708 int s = t->srv_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002709 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002710 struct buffer *req = t->req;
2711 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002712 int conn_err;
2713
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002714 DPRINTF(stderr,"process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x\n",
Willy Tarreau9f1f24b2008-08-11 11:20:03 +02002715 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreau6468d922008-08-03 19:15:35 +02002716 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002717 rep->rex, req->wex,
2718 req->flags, rep->flags);
Willy Tarreauee991362007-05-14 14:37:50 +02002719
Willy Tarreaubaaee002006-06-26 02:48:02 +02002720 if (s == SV_STIDLE) {
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002721 if ((rep->flags & BF_SHUTW_STATUS) ||
Willy Tarreau6468d922008-08-03 19:15:35 +02002722 ((req->flags & BF_SHUTR_STATUS) &&
2723 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002724 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002725 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002726 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 /* note that this must not return any error because it would be able to
2728 * overwrite the client_retnclose() output.
2729 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002730 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002731 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002732 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002733 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734
2735 return 1;
2736 }
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002737 else if (req->flags & BF_MAY_CONNECT) {
2738 /* the client allows the server to connect */
Willy Tarreau3d300592007-03-18 18:34:41 +01002739 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002740 /* This connection is being tarpitted. The CLIENT side has
2741 * already set the connect expiration date to the right
2742 * timeout. We just have to check that it has not expired.
2743 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002744 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002745 return 0;
2746
2747 /* We will set the queue timer to the time spent, just for
2748 * logging purposes. We fake a 500 server error, so that the
2749 * attacker will not suspect his connection has been tarpitted.
2750 * It will not cause trouble to the logs because we can exclude
2751 * the tarpitted connections by filtering on the 'PT' status flags.
2752 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002753 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002754 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002755 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002756 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002757 return 1;
2758 }
2759
Willy Tarreaubaaee002006-06-26 02:48:02 +02002760 /* Right now, we will need to create a connection to the server.
2761 * We might already have tried, and got a connection pending, in
2762 * which case we will not do anything till it's pending. It's up
2763 * to any other session to release it and wake us up again.
2764 */
2765 if (t->pend_pos) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002766 if (!tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 return 0;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002768 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002769 /* we've been waiting too long here */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002770 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002771 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002772 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002773 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 if (t->srv)
2775 t->srv->failed_conns++;
Willy Tarreau50fd1e12007-12-10 15:25:35 +01002776 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002777 return 1;
2778 }
2779 }
2780
2781 do {
2782 /* first, get a connection */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002783 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2784 t->flags |= SN_REDIRECTABLE;
2785
Willy Tarreaubaaee002006-06-26 02:48:02 +02002786 if (srv_redispatch_connect(t))
2787 return t->srv_state != SV_STIDLE;
2788
Willy Tarreau21d2af32008-02-14 20:25:24 +01002789 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
2790 /* Server supporting redirection and it is possible.
2791 * Invalid requests are reported as such. It concerns all
2792 * the largest ones.
2793 */
2794 struct chunk rdr;
2795 char *path;
2796 int len;
2797
2798 /* 1: create the response header */
2799 rdr.len = strlen(HTTP_302);
2800 rdr.str = trash;
2801 memcpy(rdr.str, HTTP_302, rdr.len);
2802
2803 /* 2: add the server's prefix */
2804 if (rdr.len + t->srv->rdr_len > sizeof(trash))
2805 goto cancel_redir;
2806
2807 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
2808 rdr.len += t->srv->rdr_len;
2809
2810 /* 3: add the request URI */
2811 path = http_get_path(txn);
2812 if (!path)
2813 goto cancel_redir;
2814 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2815 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
2816 goto cancel_redir;
2817
2818 memcpy(rdr.str + rdr.len, path, len);
2819 rdr.len += len;
2820 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2821 rdr.len += 4;
2822
2823 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
2824 /* FIXME: we should increase a counter of redirects per server and per backend. */
2825 if (t->srv)
2826 t->srv->cum_sess++;
2827 return 1;
2828 cancel_redir:
2829 txn->status = 400;
2830 t->fe->failed_req++;
2831 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
2832 400, error_message(t, HTTP_ERR_400));
2833 return 1;
2834 }
2835
Willy Tarreaubaaee002006-06-26 02:48:02 +02002836 /* try to (re-)connect to the server, and fail if we expire the
2837 * number of retries.
2838 */
2839 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002840 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002841 return t->srv_state != SV_STIDLE;
2842 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002843 } while (1);
2844 }
2845 }
2846 else if (s == SV_STCONN) { /* connection in progress */
Willy Tarreau6468d922008-08-03 19:15:35 +02002847 if ((rep->flags & BF_SHUTW_STATUS) ||
2848 ((req->flags & BF_SHUTR_STATUS) &&
2849 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002850 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002851 req->cex = TICK_ETERNITY;
Willy Tarreauf899b942008-03-28 18:09:38 +01002852 if (!(t->flags & SN_CONN_TAR)) {
2853 /* if we are in turn-around, we have already closed the FD */
2854 fd_delete(t->srv_fd);
2855 if (t->srv) {
2856 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002857 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002858 }
Willy Tarreau51406232008-03-10 22:04:20 +01002859 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860
2861 /* note that this must not return any error because it would be able to
2862 * overwrite the client_retnclose() output.
2863 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002864 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 return 1;
2866 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002867 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002868 return 0; /* nothing changed */
2869 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002870 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002871 /* timeout, asynchronous connect error or first write error */
2872 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2873
Willy Tarreau541b5c22008-01-06 23:34:21 +01002874 if (t->flags & SN_CONN_TAR) {
2875 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002876 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreau541b5c22008-01-06 23:34:21 +01002877 return 0;
2878 t->flags &= ~SN_CONN_TAR;
2879 }
2880 else {
2881 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01002882 if (t->srv) {
Willy Tarreau541b5c22008-01-06 23:34:21 +01002883 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002884 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01002885 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886
Willy Tarreau541b5c22008-01-06 23:34:21 +01002887 if (!(req->flags & BF_WRITE_STATUS))
2888 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2889 else
2890 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2891
2892 /* ensure that we have enough retries left */
2893 if (srv_count_retry_down(t, conn_err))
2894 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895
Willy Tarreau541b5c22008-01-06 23:34:21 +01002896 if (req->flags & BF_WRITE_ERROR) {
2897 /* we encountered an immediate connection error, and we
2898 * will have to retry connecting to the same server, most
2899 * likely leading to the same result. To avoid this, we
2900 * fake a connection timeout to retry after a turn-around
2901 * time of 1 second. We will wait in the previous if block.
2902 */
2903 t->flags |= SN_CONN_TAR;
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002904 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreau541b5c22008-01-06 23:34:21 +01002905 return 0;
2906 }
2907 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002908
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002909 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002910 /* We're on our last chance, and the REDISP option was specified.
2911 * We will ignore cookie and force to balance or use the dispatcher.
2912 */
2913 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002914 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02002915 process_srv_queue(t->srv);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002916
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01002917 /* it's left to the dispatcher to choose a server */
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002918 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002919 t->prev_srv = t->srv;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002920
2921 /* first, get a connection */
2922 if (srv_redispatch_connect(t))
Willy Tarreau00559e72008-01-06 23:46:19 +01002923 return t->srv_state != SV_STCONN;
Krzysztof Piotr Oledzki626a19b2008-02-04 02:10:09 +01002924 } else {
2925 if (t->srv)
2926 t->srv->retries++;
2927 t->be->retries++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002928 }
2929
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930 do {
2931 /* Now we will try to either reconnect to the same server or
2932 * connect to another server. If the connection gets queued
2933 * because all servers are saturated, then we will go back to
2934 * the SV_STIDLE state.
2935 */
2936 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002937 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002938 return t->srv_state != SV_STCONN;
2939 }
2940
2941 /* we need to redispatch the connection to another server */
2942 if (srv_redispatch_connect(t))
2943 return t->srv_state != SV_STCONN;
2944 } while (1);
2945 }
2946 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002947 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948
2949 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2950 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002951 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002952 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002953 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002954 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002955 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002956 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002957 /* FIXME: to prevent the server from expiring read timeouts during writes,
2958 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002959 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002960 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961 }
2962
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002963 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002964 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002965 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002966 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002967 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002968 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2969
2970 /* if the user wants to log as soon as possible, without counting
2971 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002972 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002974 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002976#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002977 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002978 /* TCP splicing supported by both FE and BE */
2979 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2980 }
2981#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982 }
2983 else {
2984 t->srv_state = SV_STHEADERS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002986 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2987 /* reset hdr_idx which was already initialized by the request.
2988 * right now, the http parser does it.
2989 * hdr_idx_init(&t->txn.hdr_idx);
2990 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002992 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002993 return 1;
2994 }
2995 }
2996 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002997 /*
2998 * Now parse the partial (or complete) lines.
2999 * We will check the response syntax, and also join multi-line
3000 * headers. An index of all the lines will be elaborated while
3001 * parsing.
3002 *
3003 * For the parsing, we use a 28 states FSM.
3004 *
3005 * Here is the information we currently have :
3006 * rep->data + req->som = beginning of response
3007 * rep->data + req->eoh = end of processed headers / start of current one
3008 * rep->data + req->eol = end of current header or line (LF or CRLF)
3009 * rep->lr = first non-visited byte
3010 * rep->r = end of data
3011 */
3012
3013 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003014 struct http_msg *msg = &txn->rsp;
3015 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003016
Willy Tarreaua15645d2007-03-18 16:22:39 +01003017 if (likely(rep->lr < rep->r))
3018 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019
Willy Tarreaua15645d2007-03-18 16:22:39 +01003020 /* 1: we might have to print this header in debug mode */
3021 if (unlikely((global.mode & MODE_DEBUG) &&
3022 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3023 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
3024 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003025
Willy Tarreaua15645d2007-03-18 16:22:39 +01003026 sol = rep->data + msg->som;
3027 eol = sol + msg->sl.rq.l;
3028 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003029
Willy Tarreaua15645d2007-03-18 16:22:39 +01003030 sol += hdr_idx_first_pos(&txn->hdr_idx);
3031 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033 while (cur_idx) {
3034 eol = sol + txn->hdr_idx.v[cur_idx].len;
3035 debug_hdr("srvhdr", t, sol, eol);
3036 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3037 cur_idx = txn->hdr_idx.v[cur_idx].next;
3038 }
3039 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003040
Willy Tarreaubaaee002006-06-26 02:48:02 +02003041
Willy Tarreauadfb8562008-08-11 15:24:42 +02003042 if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) {
3043 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreauf161a342007-04-08 16:59:42 +02003044 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01003045 * full. We cannot loop here since stream_sock_read will disable it only if
3046 * rep->l == rlim-data
3047 */
Willy Tarreauce09c522008-08-11 10:35:07 +02003048 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003049 }
3050
3051
3052 /*
3053 * Now we quickly check if we have found a full valid response.
3054 * If not so, we check the FD and buffer states before leaving.
3055 * A full response is indicated by the fact that we have seen
3056 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
3057 * responses are checked first.
3058 *
3059 * Depending on whether the client is still there or not, we
3060 * may send an error response back or not. Note that normally
3061 * we should only check for HTTP status there, and check I/O
3062 * errors somewhere else.
3063 */
3064
3065 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
3066
3067 /* Invalid response, or read error or write error */
3068 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
3069 (req->flags & BF_WRITE_ERROR) ||
3070 (rep->flags & BF_READ_ERROR))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003071 buffer_shutr_done(rep);
3072 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003073 fd_delete(t->srv_fd);
3074 if (t->srv) {
3075 t->srv->cur_sess--;
3076 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003077 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003078 }
3079 t->be->failed_resp++;
3080 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003081 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003082 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003083 client_return(t, error_message(t, HTTP_ERR_502));
3084 if (!(t->flags & SN_ERR_MASK))
3085 t->flags |= SN_ERR_SRVCL;
3086 if (!(t->flags & SN_FINST_MASK))
3087 t->flags |= SN_FINST_H;
3088 /* We used to have a free connection slot. Since we'll never use it,
3089 * we have to inform the server that it may be used by another session.
3090 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003091 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003092 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093
Willy Tarreaua15645d2007-03-18 16:22:39 +01003094 return 1;
3095 }
3096
3097 /* end of client write or end of server read.
3098 * since we are in header mode, if there's no space left for headers, we
3099 * won't be able to free more later, so the session will never terminate.
3100 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003101 else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01003102 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003103 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003104 buffer_shutr_done(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003105 t->srv_state = SV_STSHUTR;
3106 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3107 return 1;
3108 }
3109
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003110 /* read timeout : return a 504 to the client. */
Willy Tarreauf161a342007-04-08 16:59:42 +02003111 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003112 tick_is_expired(rep->rex, now_ms))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003113 buffer_shutr_done(rep);
3114 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003115 fd_delete(t->srv_fd);
3116 if (t->srv) {
3117 t->srv->cur_sess--;
3118 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003119 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003120 }
3121 t->be->failed_resp++;
3122 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003123 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003124 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003125 client_return(t, error_message(t, HTTP_ERR_504));
3126 if (!(t->flags & SN_ERR_MASK))
3127 t->flags |= SN_ERR_SRVTO;
3128 if (!(t->flags & SN_FINST_MASK))
3129 t->flags |= SN_FINST_H;
3130 /* We used to have a free connection slot. Since we'll never use it,
3131 * we have to inform the server that it may be used by another session.
3132 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003133 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003134 process_srv_queue(t->srv);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003135 return 1;
3136 }
3137
3138 /* last client read and buffer empty */
3139 /* FIXME!!! here, we don't want to switch to SHUTW if the
3140 * client shuts read too early, because we may still have
3141 * some work to do on the headers.
3142 * The side-effect is that if the client completely closes its
3143 * connection during SV_STHEADER, the connection to the server
3144 * is kept until a response comes back or the timeout is reached.
3145 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003146 else if (0 && /* we don't want to switch to shutw for now */
3147 unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) {
3148
Willy Tarreauf161a342007-04-08 16:59:42 +02003149 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003150 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003151
3152 /* We must ensure that the read part is still
3153 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003154 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003155 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003156
3157 shutdown(t->srv_fd, SHUT_WR);
3158 t->srv_state = SV_STSHUTW;
3159 return 1;
3160 }
3161
3162 /* write timeout */
3163 /* FIXME!!! here, we don't want to switch to SHUTW if the
3164 * client shuts read too early, because we may still have
3165 * some work to do on the headers.
3166 */
Willy Tarreauf161a342007-04-08 16:59:42 +02003167 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003168 tick_is_expired(req->wex, now_ms))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003169 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003170 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003171 shutdown(t->srv_fd, SHUT_WR);
3172 /* We must ensure that the read part is still alive
3173 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003174 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003175 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003176
3177 t->srv_state = SV_STSHUTW;
3178 if (!(t->flags & SN_ERR_MASK))
3179 t->flags |= SN_ERR_SRVTO;
3180 if (!(t->flags & SN_FINST_MASK))
3181 t->flags |= SN_FINST_H;
3182 return 1;
3183 }
3184
3185 /*
3186 * And now the non-error cases.
3187 */
3188
3189 /* Data remaining in the request buffer.
3190 * This happens during the first pass here, and during
3191 * long posts.
3192 */
3193 else if (likely(req->l)) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003194 if (!tick_isset(req->wex)) {
3195 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003196 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003197 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003198 if (tick_isset(req->wex)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003199 /* FIXME: to prevent the server from expiring read timeouts during writes,
3200 * we refresh it. */
3201 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202 }
3203 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003204 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003205
Willy Tarreaua15645d2007-03-18 16:22:39 +01003206 /* nothing left in the request buffer */
3207 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003208 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3209 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003210 req->wex = TICK_ETERNITY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003211 }
3212 }
3213
3214 return t->srv_state != SV_STHEADERS;
3215 }
3216
3217
3218 /*****************************************************************
3219 * More interesting part now : we know that we have a complete *
3220 * response which at least looks like HTTP. We have an indicator *
3221 * of each header's length, so we can parse them quickly. *
3222 ****************************************************************/
3223
Willy Tarreau9cdde232007-05-02 20:58:19 +02003224 /* ensure we keep this pointer to the beginning of the message */
3225 msg->sol = rep->data + msg->som;
3226
Willy Tarreaua15645d2007-03-18 16:22:39 +01003227 /*
3228 * 1: get the status code and check for cacheability.
3229 */
3230
3231 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003232 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003233
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003234 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003235 case 200:
3236 case 203:
3237 case 206:
3238 case 300:
3239 case 301:
3240 case 410:
3241 /* RFC2616 @13.4:
3242 * "A response received with a status code of
3243 * 200, 203, 206, 300, 301 or 410 MAY be stored
3244 * by a cache (...) unless a cache-control
3245 * directive prohibits caching."
3246 *
3247 * RFC2616 @9.5: POST method :
3248 * "Responses to this method are not cacheable,
3249 * unless the response includes appropriate
3250 * Cache-Control or Expires header fields."
3251 */
3252 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003253 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01003254 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003255 break;
3256 default:
3257 break;
3258 }
3259
3260 /*
3261 * 2: we may need to capture headers
3262 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003263 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003264 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003265 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003266
3267 /*
3268 * 3: we will have to evaluate the filters.
3269 * As opposed to version 1.2, now they will be evaluated in the
3270 * filters order and not in the header order. This means that
3271 * each filter has to be validated among all headers.
3272 *
3273 * Filters are tried with ->be first, then with ->fe if it is
3274 * different from ->be.
3275 */
3276
3277 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3278
3279 cur_proxy = t->be;
3280 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003281 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003282
3283 /* try headers filters */
3284 if (rule_set->rsp_exp != NULL) {
3285 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3286 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287 if (t->srv) {
3288 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003289 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003290 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003292 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003293 return_srv_prx_502:
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003294 buffer_shutr_done(rep);
3295 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003296 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003297 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003298 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003299 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01003300 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301 if (!(t->flags & SN_ERR_MASK))
3302 t->flags |= SN_ERR_PRXCOND;
3303 if (!(t->flags & SN_FINST_MASK))
3304 t->flags |= SN_FINST_H;
3305 /* We used to have a free connection slot. Since we'll never use it,
3306 * we have to inform the server that it may be used by another session.
3307 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003308 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003309 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003310 return 1;
3311 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003312 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreaua15645d2007-03-18 16:22:39 +01003314 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01003315 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003316 if (t->srv) {
3317 t->srv->cur_sess--;
3318 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003319 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003320 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003321 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003322 goto return_srv_prx_502;
3323 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324
Willy Tarreaua15645d2007-03-18 16:22:39 +01003325 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003326 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003327 !(t->flags & SN_CONN_CLOSED)) {
3328 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003329 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003330 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreaua15645d2007-03-18 16:22:39 +01003332 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3333 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003334
Willy Tarreaua15645d2007-03-18 16:22:39 +01003335 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3336 cur_hdr = &txn->hdr_idx.v[cur_idx];
3337 cur_ptr = cur_next;
3338 cur_end = cur_ptr + cur_hdr->len;
3339 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003340
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003341 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3342 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003343 /* 3 possibilities :
3344 * - we have already set Connection: close,
3345 * so we remove this line.
3346 * - we have not yet set Connection: close,
3347 * but this line indicates close. We leave
3348 * it untouched and set the flag.
3349 * - we have not yet set Connection: close,
3350 * and this line indicates non-close. We
3351 * replace it.
3352 */
3353 if (t->flags & SN_CONN_CLOSED) {
3354 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3355 txn->rsp.eoh += delta;
3356 cur_next += delta;
3357 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3358 txn->hdr_idx.used--;
3359 cur_hdr->len = 0;
3360 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003361 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3362 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3363 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003364 cur_next += delta;
3365 cur_hdr->len += delta;
3366 txn->rsp.eoh += delta;
3367 }
3368 t->flags |= SN_CONN_CLOSED;
3369 }
3370 }
3371 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003372 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003373 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003374
Willy Tarreaua15645d2007-03-18 16:22:39 +01003375 /* add response headers from the rule sets in the same order */
3376 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003377 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3378 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003379 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380 }
3381
Willy Tarreaua15645d2007-03-18 16:22:39 +01003382 /* check whether we're already working on the frontend */
3383 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003384 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003385 cur_proxy = t->fe;
3386 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003387
Willy Tarreaua15645d2007-03-18 16:22:39 +01003388 /*
3389 * 4: check for server cookie.
3390 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003391 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3392 || (t->be->options & PR_O_CHK_CACHE))
3393 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003394
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003395
Willy Tarreaua15645d2007-03-18 16:22:39 +01003396 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003397 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003398 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003399 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3400 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003401
3402 /*
3403 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01003404 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003405 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3406 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003407 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003408
Willy Tarreaua15645d2007-03-18 16:22:39 +01003409 /* the server is known, it's not the one the client requested, we have to
3410 * insert a set-cookie here, except if we want to insert only on POST
3411 * requests and this one isn't. Note that servers which don't have cookies
3412 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003414 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003415 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003416 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003417
Krzysztof Piotr Oledzki1acf2172008-05-29 23:03:34 +02003418 if (t->be->cookie_domain)
3419 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003420
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003421 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3422 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003423 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003424 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003425
Willy Tarreaua15645d2007-03-18 16:22:39 +01003426 /* Here, we will tell an eventual cache on the client side that we don't
3427 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3428 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3429 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3430 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003431 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3432
3433 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3434
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003435 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3436 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003437 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003438 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003439 }
3440
3441
3442 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003443 * 7: check if result will be cacheable with a cookie.
3444 * We'll block the response if security checks have caught
3445 * nasty things such as a cacheable cookie.
3446 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003447 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3448 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003449 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003450
Willy Tarreaua15645d2007-03-18 16:22:39 +01003451 /* we're in presence of a cacheable response containing
3452 * a set-cookie header. We'll block it as requested by
3453 * the 'checkcache' option, and send an alert.
3454 */
3455 if (t->srv) {
3456 t->srv->cur_sess--;
3457 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003458 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003460 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003461
Willy Tarreaua15645d2007-03-18 16:22:39 +01003462 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003463 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003464 send_log(t->be, LOG_ALERT,
3465 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003466 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003467 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003468 }
3469
Willy Tarreaua15645d2007-03-18 16:22:39 +01003470 /*
3471 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003472 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003473 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003474 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003475 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003476 if ((unlikely(msg->sl.st.v_l != 8) ||
3477 unlikely(req->data[msg->som + 7] != '0')) &&
3478 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003479 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003480 goto return_bad_resp;
3481 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003482 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003483
Willy Tarreaubaaee002006-06-26 02:48:02 +02003484
Willy Tarreaua15645d2007-03-18 16:22:39 +01003485 /*************************************************************
3486 * OK, that's finished for the headers. We have done what we *
3487 * could. Let's switch to the DATA state. *
3488 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489
Willy Tarreaua15645d2007-03-18 16:22:39 +01003490 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02003491 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003492 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003493 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003494
3495 /* client connection already closed or option 'forceclose' required :
3496 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003497 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003498 if ((req->l == 0) &&
Willy Tarreau6468d922008-08-03 19:15:35 +02003499 (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003500 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003501 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003502
3503 /* We must ensure that the read part is still alive when switching
3504 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003505 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003506 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003507
Willy Tarreaua15645d2007-03-18 16:22:39 +01003508 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003510 }
3511
Willy Tarreaua15645d2007-03-18 16:22:39 +01003512#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003513 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003514 /* TCP splicing supported by both FE and BE */
3515 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003516 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003517#endif
3518 /* if the user wants to log as soon as possible, without counting
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003519 * bytes from the server, then this is the right moment. We have
3520 * to temporarily assign bytes_out to log what we currently have.
3521 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003522 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3523 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreau8b3977f2008-01-18 11:16:32 +01003524 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003525 if (t->fe->to_log & LW_REQ)
3526 http_sess_log(t);
3527 else
3528 tcp_sess_log(t);
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003529 t->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003530 }
3531
Willy Tarreaua15645d2007-03-18 16:22:39 +01003532 /* Note: we must not try to cheat by jumping directly to DATA,
3533 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003534 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003535
3536 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003537 }
3538 else if (s == SV_STDATA) {
3539 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003540 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003541 buffer_shutr_done(rep);
3542 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003543 fd_delete(t->srv_fd);
3544 if (t->srv) {
3545 t->srv->cur_sess--;
3546 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003547 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003548 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003549 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003551 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003552 if (!(t->flags & SN_ERR_MASK))
3553 t->flags |= SN_ERR_SRVCL;
3554 if (!(t->flags & SN_FINST_MASK))
3555 t->flags |= SN_FINST_D;
3556 /* We used to have a free connection slot. Since we'll never use it,
3557 * we have to inform the server that it may be used by another session.
3558 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003559 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003560 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003561
3562 return 1;
3563 }
3564 /* last read, or end of client write */
Willy Tarreau6468d922008-08-03 19:15:35 +02003565 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003566 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003567 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003568 t->srv_state = SV_STSHUTR;
3569 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3570 return 1;
3571 }
3572 /* end of client read and no more data to send */
Willy Tarreau6468d922008-08-03 19:15:35 +02003573 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003574 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003575 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003576 shutdown(t->srv_fd, SHUT_WR);
3577 /* We must ensure that the read part is still alive when switching
3578 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003579 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003580 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003581
3582 t->srv_state = SV_STSHUTW;
3583 return 1;
3584 }
3585 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003586 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003587 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003588 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003589 t->srv_state = SV_STSHUTR;
3590 if (!(t->flags & SN_ERR_MASK))
3591 t->flags |= SN_ERR_SRVTO;
3592 if (!(t->flags & SN_FINST_MASK))
3593 t->flags |= SN_FINST_D;
3594 return 1;
3595 }
3596 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003597 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003598 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003599 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003600 shutdown(t->srv_fd, SHUT_WR);
3601 /* We must ensure that the read part is still alive when switching
3602 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003603 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003604 rep->cex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003605 t->srv_state = SV_STSHUTW;
3606 if (!(t->flags & SN_ERR_MASK))
3607 t->flags |= SN_ERR_SRVTO;
3608 if (!(t->flags & SN_FINST_MASK))
3609 t->flags |= SN_FINST_D;
3610 return 1;
3611 }
3612
3613 /* recompute request time-outs */
3614 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003615 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3616 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003617 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003618 }
3619 }
3620 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003621 if (!tick_isset(req->wex)) {
3622 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003623 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003624 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003625 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003626 /* FIXME: to prevent the server from expiring read timeouts during writes,
3627 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003628 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003630 }
3631 }
3632
3633 /* recompute response time-outs */
3634 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003635 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003636 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003637 }
3638 }
3639 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003640 if (!tick_isset(rep->rex)) {
3641 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003642 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003643 }
3644 }
3645
3646 return 0; /* other cases change nothing */
3647 }
3648 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003649 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003650 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003651 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003652 fd_delete(t->srv_fd);
3653 if (t->srv) {
3654 t->srv->cur_sess--;
3655 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003656 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003657 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003658 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003659 //close(t->srv_fd);
3660 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003661 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003662 if (!(t->flags & SN_ERR_MASK))
3663 t->flags |= SN_ERR_SRVCL;
3664 if (!(t->flags & SN_FINST_MASK))
3665 t->flags |= SN_FINST_D;
3666 /* We used to have a free connection slot. Since we'll never use it,
3667 * we have to inform the server that it may be used by another session.
3668 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003669 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003670 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003671
3672 return 1;
3673 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003674 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003675 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003676 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003677 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003678 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003679 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003680 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003681 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682 //close(t->srv_fd);
3683 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003684 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003685 /* We used to have a free connection slot. Since we'll never use it,
3686 * we have to inform the server that it may be used by another session.
3687 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003688 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003689 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003690
3691 return 1;
3692 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003693 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003694 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003695 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003696 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003697 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003698 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003699 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003700 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003701 //close(t->srv_fd);
3702 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003703 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003704 if (!(t->flags & SN_ERR_MASK))
3705 t->flags |= SN_ERR_SRVTO;
3706 if (!(t->flags & SN_FINST_MASK))
3707 t->flags |= SN_FINST_D;
3708 /* We used to have a free connection slot. Since we'll never use it,
3709 * we have to inform the server that it may be used by another session.
3710 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003711 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003712 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003713
3714 return 1;
3715 }
3716 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003717 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3718 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003719 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003720 }
3721 }
3722 else { /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003723 if (!tick_isset(req->wex)) {
3724 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003725 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003726 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727 }
3728 }
3729 return 0;
3730 }
3731 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003732 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003733 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003734 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003735 fd_delete(t->srv_fd);
3736 if (t->srv) {
3737 t->srv->cur_sess--;
3738 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003739 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003740 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003741 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003742 //close(t->srv_fd);
3743 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003744 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003745 if (!(t->flags & SN_ERR_MASK))
3746 t->flags |= SN_ERR_SRVCL;
3747 if (!(t->flags & SN_FINST_MASK))
3748 t->flags |= SN_FINST_D;
3749 /* We used to have a free connection slot. Since we'll never use it,
3750 * we have to inform the server that it may be used by another session.
3751 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003752 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003753 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003754
3755 return 1;
3756 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003757 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003758 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003759 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003760 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003761 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003762 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003763 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003764 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003765 //close(t->srv_fd);
3766 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003767 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003768 /* We used to have a free connection slot. Since we'll never use it,
3769 * we have to inform the server that it may be used by another session.
3770 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003771 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003772 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003773
3774 return 1;
3775 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003776 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003777 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003778 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003779 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003780 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003782 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003783 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003784 //close(t->srv_fd);
3785 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003786 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003787 if (!(t->flags & SN_ERR_MASK))
3788 t->flags |= SN_ERR_SRVTO;
3789 if (!(t->flags & SN_FINST_MASK))
3790 t->flags |= SN_FINST_D;
3791 /* We used to have a free connection slot. Since we'll never use it,
3792 * we have to inform the server that it may be used by another session.
3793 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003794 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003795 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003796
3797 return 1;
3798 }
3799 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003800 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003801 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003802 }
3803 }
3804 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003805 if (!tick_isset(rep->rex)) {
3806 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003807 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003808 }
3809 }
3810 return 0;
3811 }
3812 else { /* SV_STCLOSE : nothing to do */
3813 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3814 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003815 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003816 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003817 write(1, trash, len);
3818 }
3819 return 0;
3820 }
3821 return 0;
3822}
3823
3824
3825/*
3826 * Produces data for the session <s> depending on its source. Expects to be
3827 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3828 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3829 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003830 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreaubaaee002006-06-26 02:48:02 +02003831 * if it changes the session state from CL_STSHUTR, otherwise 0.
3832 */
3833int produce_content(struct session *s)
3834{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003835 if (s->data_source == DATA_SRC_NONE) {
3836 s->flags &= ~SN_SELF_GEN;
3837 return 1;
3838 }
3839 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003840 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003841 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003842 if (ret >= 0)
3843 return ret;
3844 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003846
Willy Tarreau91861262007-10-17 17:06:05 +02003847 /* unknown data source or internal error */
3848 s->txn.status = 500;
3849 client_retnclose(s, error_message(s, HTTP_ERR_500));
3850 if (!(s->flags & SN_ERR_MASK))
3851 s->flags |= SN_ERR_PRXCOND;
3852 if (!(s->flags & SN_FINST_MASK))
3853 s->flags |= SN_FINST_R;
3854 s->flags &= ~SN_SELF_GEN;
3855 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003856}
3857
3858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003859/* Iterate the same filter through all request headers.
3860 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003861 * Since it can manage the switch to another backend, it updates the per-proxy
3862 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003863 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003864int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003865{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003866 char term;
3867 char *cur_ptr, *cur_end, *cur_next;
3868 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003869 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003870 struct hdr_idx_elem *cur_hdr;
3871 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003873 last_hdr = 0;
3874
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003875 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003876 old_idx = 0;
3877
3878 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003879 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003880 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003881 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003882 (exp->action == ACT_ALLOW ||
3883 exp->action == ACT_DENY ||
3884 exp->action == ACT_TARPIT))
3885 return 0;
3886
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003887 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003888 if (!cur_idx)
3889 break;
3890
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003891 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003892 cur_ptr = cur_next;
3893 cur_end = cur_ptr + cur_hdr->len;
3894 cur_next = cur_end + cur_hdr->cr + 1;
3895
3896 /* Now we have one header between cur_ptr and cur_end,
3897 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003898 */
3899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003900 /* The annoying part is that pattern matching needs
3901 * that we modify the contents to null-terminate all
3902 * strings before testing them.
3903 */
3904
3905 term = *cur_end;
3906 *cur_end = '\0';
3907
3908 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3909 switch (exp->action) {
3910 case ACT_SETBE:
3911 /* It is not possible to jump a second time.
3912 * FIXME: should we return an HTTP/500 here so that
3913 * the admin knows there's a problem ?
3914 */
3915 if (t->be != t->fe)
3916 break;
3917
3918 /* Swithing Proxy */
3919 t->be = (struct proxy *) exp->replace;
3920
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003921 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003922 * because we have associated req_cap and rsp_cap to the
3923 * frontend, and the beconn will be updated later.
3924 */
3925
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003926 t->rep->rto = t->req->wto = t->be->timeout.server;
3927 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003928 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003929 last_hdr = 1;
3930 break;
3931
3932 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003933 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003934 last_hdr = 1;
3935 break;
3936
3937 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003938 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003939 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003940 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003941 break;
3942
3943 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003944 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003945 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003946 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003947 break;
3948
3949 case ACT_REPLACE:
3950 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3951 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3952 /* FIXME: if the user adds a newline in the replacement, the
3953 * index will not be recalculated for now, and the new line
3954 * will not be counted as a new header.
3955 */
3956
3957 cur_end += delta;
3958 cur_next += delta;
3959 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003960 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 break;
3962
3963 case ACT_REMOVE:
3964 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3965 cur_next += delta;
3966
3967 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003968 txn->req.eoh += delta;
3969 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3970 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003971 cur_hdr->len = 0;
3972 cur_end = NULL; /* null-term has been rewritten */
3973 break;
3974
3975 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003977 if (cur_end)
3978 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 /* keep the link from this header to next one in case of later
3981 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 old_idx = cur_idx;
3984 }
3985 return 0;
3986}
3987
3988
3989/* Apply the filter to the request line.
3990 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3991 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003992 * Since it can manage the switch to another backend, it updates the per-proxy
3993 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003994 */
3995int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3996{
3997 char term;
3998 char *cur_ptr, *cur_end;
3999 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004000 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004001 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004002
Willy Tarreau58f10d72006-12-04 02:26:12 +01004003
Willy Tarreau3d300592007-03-18 18:34:41 +01004004 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004005 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004006 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004007 (exp->action == ACT_ALLOW ||
4008 exp->action == ACT_DENY ||
4009 exp->action == ACT_TARPIT))
4010 return 0;
4011 else if (exp->action == ACT_REMOVE)
4012 return 0;
4013
4014 done = 0;
4015
Willy Tarreau9cdde232007-05-02 20:58:19 +02004016 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004017 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004018
4019 /* Now we have the request line between cur_ptr and cur_end */
4020
4021 /* The annoying part is that pattern matching needs
4022 * that we modify the contents to null-terminate all
4023 * strings before testing them.
4024 */
4025
4026 term = *cur_end;
4027 *cur_end = '\0';
4028
4029 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4030 switch (exp->action) {
4031 case ACT_SETBE:
4032 /* It is not possible to jump a second time.
4033 * FIXME: should we return an HTTP/500 here so that
4034 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004035 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004036 if (t->be != t->fe)
4037 break;
4038
4039 /* Swithing Proxy */
4040 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004042 /* right now, the backend switch is not too much complicated
4043 * because we have associated req_cap and rsp_cap to the
4044 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004045 */
4046
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004047 t->rep->rto = t->req->wto = t->be->timeout.server;
4048 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004049 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004050 done = 1;
4051 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004053 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004054 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004055 done = 1;
4056 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004059 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004060 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 done = 1;
4062 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004064 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004065 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004066 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 done = 1;
4068 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004070 case ACT_REPLACE:
4071 *cur_end = term; /* restore the string terminator */
4072 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4073 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4074 /* FIXME: if the user adds a newline in the replacement, the
4075 * index will not be recalculated for now, and the new line
4076 * will not be counted as a new header.
4077 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004078
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004079 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004080 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004081
Willy Tarreau9cdde232007-05-02 20:58:19 +02004082 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004083 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004084 HTTP_MSG_RQMETH,
4085 cur_ptr, cur_end + 1,
4086 NULL, NULL);
4087 if (unlikely(!cur_end))
4088 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004089
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004090 /* we have a full request and we know that we have either a CR
4091 * or an LF at <ptr>.
4092 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004093 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4094 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004095 /* there is no point trying this regex on headers */
4096 return 1;
4097 }
4098 }
4099 *cur_end = term; /* restore the string terminator */
4100 return done;
4101}
Willy Tarreau97de6242006-12-27 17:18:38 +01004102
Willy Tarreau58f10d72006-12-04 02:26:12 +01004103
Willy Tarreau58f10d72006-12-04 02:26:12 +01004104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004105/*
4106 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4107 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108 * unparsable request. Since it can manage the switch to another backend, it
4109 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004110 */
4111int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4112{
Willy Tarreau3d300592007-03-18 18:34:41 +01004113 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004114 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004115 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004116 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004118 /*
4119 * The interleaving of transformations and verdicts
4120 * makes it difficult to decide to continue or stop
4121 * the evaluation.
4122 */
4123
Willy Tarreau3d300592007-03-18 18:34:41 +01004124 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004125 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4126 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4127 exp = exp->next;
4128 continue;
4129 }
4130
4131 /* Apply the filter to the request line. */
4132 ret = apply_filter_to_req_line(t, req, exp);
4133 if (unlikely(ret < 0))
4134 return -1;
4135
4136 if (likely(ret == 0)) {
4137 /* The filter did not match the request, it can be
4138 * iterated through all headers.
4139 */
4140 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 }
4142 exp = exp->next;
4143 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004144 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004145}
4146
4147
Willy Tarreaua15645d2007-03-18 16:22:39 +01004148
Willy Tarreau58f10d72006-12-04 02:26:12 +01004149/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004150 * Manage client-side cookie. It can impact performance by about 2% so it is
4151 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004152 */
4153void manage_client_side_cookies(struct session *t, struct buffer *req)
4154{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004155 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156 char *p1, *p2, *p3, *p4;
4157 char *del_colon, *del_cookie, *colon;
4158 int app_cookies;
4159
4160 appsess *asession_temp = NULL;
4161 appsess local_asession;
4162
4163 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004165
Willy Tarreau2a324282006-12-05 00:05:46 +01004166 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 * we start with the start line.
4168 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004169 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004170 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004172 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004173 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004174 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004175
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004176 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004177 cur_ptr = cur_next;
4178 cur_end = cur_ptr + cur_hdr->len;
4179 cur_next = cur_end + cur_hdr->cr + 1;
4180
4181 /* We have one full header between cur_ptr and cur_end, and the
4182 * next header starts at cur_next. We're only interested in
4183 * "Cookie:" headers.
4184 */
4185
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004186 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4187 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188 old_idx = cur_idx;
4189 continue;
4190 }
4191
4192 /* Now look for cookies. Conforming to RFC2109, we have to support
4193 * attributes whose name begin with a '$', and associate them with
4194 * the right cookie, if we want to delete this cookie.
4195 * So there are 3 cases for each cookie read :
4196 * 1) it's a special attribute, beginning with a '$' : ignore it.
4197 * 2) it's a server id cookie that we *MAY* want to delete : save
4198 * some pointers on it (last semi-colon, beginning of cookie...)
4199 * 3) it's an application cookie : we *MAY* have to delete a previous
4200 * "special" cookie.
4201 * At the end of loop, if a "special" cookie remains, we may have to
4202 * remove it. If no application cookie persists in the header, we
4203 * *MUST* delete it
4204 */
4205
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004206 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004207
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 /* del_cookie == NULL => nothing to be deleted */
4209 del_colon = del_cookie = NULL;
4210 app_cookies = 0;
4211
4212 while (p1 < cur_end) {
4213 /* skip spaces and colons, but keep an eye on these ones */
4214 while (p1 < cur_end) {
4215 if (*p1 == ';' || *p1 == ',')
4216 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004217 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218 break;
4219 p1++;
4220 }
4221
4222 if (p1 == cur_end)
4223 break;
4224
4225 /* p1 is at the beginning of the cookie name */
4226 p2 = p1;
4227 while (p2 < cur_end && *p2 != '=')
4228 p2++;
4229
4230 if (p2 == cur_end)
4231 break;
4232
4233 p3 = p2 + 1; /* skips the '=' sign */
4234 if (p3 == cur_end)
4235 break;
4236
4237 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004238 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239 p4++;
4240
4241 /* here, we have the cookie name between p1 and p2,
4242 * and its value between p3 and p4.
4243 * we can process it :
4244 *
4245 * Cookie: NAME=VALUE;
4246 * | || || |
4247 * | || || +--> p4
4248 * | || |+-------> p3
4249 * | || +--------> p2
4250 * | |+------------> p1
4251 * | +-------------> colon
4252 * +--------------------> cur_ptr
4253 */
4254
4255 if (*p1 == '$') {
4256 /* skip this one */
4257 }
4258 else {
4259 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004260 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004261 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004262 (p4 - p1 >= t->fe->capture_namelen) &&
4263 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004264 int log_len = p4 - p1;
4265
Willy Tarreau086b3b42007-05-13 21:45:51 +02004266 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267 Alert("HTTP logging : out of memory.\n");
4268 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004269 if (log_len > t->fe->capture_len)
4270 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004271 memcpy(txn->cli_cookie, p1, log_len);
4272 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 }
4274 }
4275
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004276 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4277 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004279 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004280 char *delim;
4281
4282 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4283 * have the server ID betweek p3 and delim, and the original cookie between
4284 * delim+1 and p4. Otherwise, delim==p4 :
4285 *
4286 * Cookie: NAME=SRV~VALUE;
4287 * | || || | |
4288 * | || || | +--> p4
4289 * | || || +--------> delim
4290 * | || |+-----------> p3
4291 * | || +------------> p2
4292 * | |+----------------> p1
4293 * | +-----------------> colon
4294 * +------------------------> cur_ptr
4295 */
4296
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004297 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004298 for (delim = p3; delim < p4; delim++)
4299 if (*delim == COOKIE_DELIM)
4300 break;
4301 }
4302 else
4303 delim = p4;
4304
4305
4306 /* Here, we'll look for the first running server which supports the cookie.
4307 * This allows to share a same cookie between several servers, for example
4308 * to dedicate backup servers to specific servers only.
4309 * However, to prevent clients from sticking to cookie-less backup server
4310 * when they have incidentely learned an empty cookie, we simply ignore
4311 * empty cookies and mark them as invalid.
4312 */
4313 if (delim == p3)
4314 srv = NULL;
4315
4316 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004317 if (srv->cookie && (srv->cklen == delim - p3) &&
4318 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004319 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004320 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004321 txn->flags &= ~TX_CK_MASK;
4322 txn->flags |= TX_CK_VALID;
4323 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004324 t->srv = srv;
4325 break;
4326 } else {
4327 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004328 txn->flags &= ~TX_CK_MASK;
4329 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004330 }
4331 }
4332 srv = srv->next;
4333 }
4334
Willy Tarreau3d300592007-03-18 18:34:41 +01004335 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004337 txn->flags &= ~TX_CK_MASK;
4338 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004339 }
4340
4341 /* depending on the cookie mode, we may have to either :
4342 * - delete the complete cookie if we're in insert+indirect mode, so that
4343 * the server never sees it ;
4344 * - remove the server id from the cookie value, and tag the cookie as an
4345 * application cookie so that it does not get accidentely removed later,
4346 * if we're in cookie prefix mode
4347 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004348 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004349 int delta; /* negative */
4350
4351 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4352 p4 += delta;
4353 cur_end += delta;
4354 cur_next += delta;
4355 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004356 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004357
4358 del_cookie = del_colon = NULL;
4359 app_cookies++; /* protect the header from deletion */
4360 }
4361 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004362 (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 +01004363 del_cookie = p1;
4364 del_colon = colon;
4365 }
4366 } else {
4367 /* now we know that we must keep this cookie since it's
4368 * not ours. But if we wanted to delete our cookie
4369 * earlier, we cannot remove the complete header, but we
4370 * can remove the previous block itself.
4371 */
4372 app_cookies++;
4373
4374 if (del_cookie != NULL) {
4375 int delta; /* negative */
4376
4377 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4378 p4 += delta;
4379 cur_end += delta;
4380 cur_next += delta;
4381 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004382 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004383 del_cookie = del_colon = NULL;
4384 }
4385 }
4386
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004387 if ((t->be->appsession_name != NULL) &&
4388 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004389 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004390
Willy Tarreau58f10d72006-12-04 02:26:12 +01004391 /* Cool... it's the right one */
4392
4393 asession_temp = &local_asession;
4394
Willy Tarreau63963c62007-05-13 21:29:55 +02004395 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004396 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4397 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4398 return;
4399 }
4400
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004401 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4402 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004404
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004406 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4407 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004408 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004409 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004410 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411 Alert("Not enough memory process_cli():asession:calloc().\n");
4412 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4413 return;
4414 }
4415
4416 asession_temp->sessid = local_asession.sessid;
4417 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004418 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004419 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 } else {
4421 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004422 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004424 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004425 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 Alert("Found Application Session without matching server.\n");
4427 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004428 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 while (srv) {
4430 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004431 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004432 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004433 txn->flags &= ~TX_CK_MASK;
4434 txn->flags |= TX_CK_VALID;
4435 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004436 t->srv = srv;
4437 break;
4438 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004439 txn->flags &= ~TX_CK_MASK;
4440 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004441 }
4442 }
4443 srv = srv->next;
4444 }/* end while(srv) */
4445 }/* end else if server == NULL */
4446
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004447 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004448 asession_temp->request_count++;
4449#if defined(DEBUG_HASH)
4450 Alert("manage_client_side_cookies\n");
4451 appsession_hash_dump(&(t->be->htbl_proxy));
4452#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004453 }/* end if ((t->proxy->appsession_name != NULL) ... */
4454 }
4455
4456 /* we'll have to look for another cookie ... */
4457 p1 = p4;
4458 } /* while (p1 < cur_end) */
4459
4460 /* There's no more cookie on this line.
4461 * We may have marked the last one(s) for deletion.
4462 * We must do this now in two ways :
4463 * - if there is no app cookie, we simply delete the header ;
4464 * - if there are app cookies, we must delete the end of the
4465 * string properly, including the colon/semi-colon before
4466 * the cookie name.
4467 */
4468 if (del_cookie != NULL) {
4469 int delta;
4470 if (app_cookies) {
4471 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4472 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004473 cur_hdr->len += delta;
4474 } else {
4475 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004476
4477 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004478 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4479 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004480 cur_hdr->len = 0;
4481 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004482 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004483 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004484 }
4485
4486 /* keep the link from this header to next one */
4487 old_idx = cur_idx;
4488 } /* end of cookie processing on this header */
4489}
4490
4491
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492/* Iterate the same filter through all response headers contained in <rtr>.
4493 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4494 */
4495int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4496{
4497 char term;
4498 char *cur_ptr, *cur_end, *cur_next;
4499 int cur_idx, old_idx, last_hdr;
4500 struct http_txn *txn = &t->txn;
4501 struct hdr_idx_elem *cur_hdr;
4502 int len, delta;
4503
4504 last_hdr = 0;
4505
4506 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4507 old_idx = 0;
4508
4509 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004511 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004512 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004513 (exp->action == ACT_ALLOW ||
4514 exp->action == ACT_DENY))
4515 return 0;
4516
4517 cur_idx = txn->hdr_idx.v[old_idx].next;
4518 if (!cur_idx)
4519 break;
4520
4521 cur_hdr = &txn->hdr_idx.v[cur_idx];
4522 cur_ptr = cur_next;
4523 cur_end = cur_ptr + cur_hdr->len;
4524 cur_next = cur_end + cur_hdr->cr + 1;
4525
4526 /* Now we have one header between cur_ptr and cur_end,
4527 * and the next header starts at cur_next.
4528 */
4529
4530 /* The annoying part is that pattern matching needs
4531 * that we modify the contents to null-terminate all
4532 * strings before testing them.
4533 */
4534
4535 term = *cur_end;
4536 *cur_end = '\0';
4537
4538 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4539 switch (exp->action) {
4540 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004541 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004542 last_hdr = 1;
4543 break;
4544
4545 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004546 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004547 last_hdr = 1;
4548 break;
4549
4550 case ACT_REPLACE:
4551 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4552 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4553 /* FIXME: if the user adds a newline in the replacement, the
4554 * index will not be recalculated for now, and the new line
4555 * will not be counted as a new header.
4556 */
4557
4558 cur_end += delta;
4559 cur_next += delta;
4560 cur_hdr->len += delta;
4561 txn->rsp.eoh += delta;
4562 break;
4563
4564 case ACT_REMOVE:
4565 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4566 cur_next += delta;
4567
4568 /* FIXME: this should be a separate function */
4569 txn->rsp.eoh += delta;
4570 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4571 txn->hdr_idx.used--;
4572 cur_hdr->len = 0;
4573 cur_end = NULL; /* null-term has been rewritten */
4574 break;
4575
4576 }
4577 }
4578 if (cur_end)
4579 *cur_end = term; /* restore the string terminator */
4580
4581 /* keep the link from this header to next one in case of later
4582 * removal of next header.
4583 */
4584 old_idx = cur_idx;
4585 }
4586 return 0;
4587}
4588
4589
4590/* Apply the filter to the status line in the response buffer <rtr>.
4591 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4592 * or -1 if a replacement resulted in an invalid status line.
4593 */
4594int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4595{
4596 char term;
4597 char *cur_ptr, *cur_end;
4598 int done;
4599 struct http_txn *txn = &t->txn;
4600 int len, delta;
4601
4602
Willy Tarreau3d300592007-03-18 18:34:41 +01004603 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004604 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004605 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004606 (exp->action == ACT_ALLOW ||
4607 exp->action == ACT_DENY))
4608 return 0;
4609 else if (exp->action == ACT_REMOVE)
4610 return 0;
4611
4612 done = 0;
4613
Willy Tarreau9cdde232007-05-02 20:58:19 +02004614 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004615 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4616
4617 /* Now we have the status line between cur_ptr and cur_end */
4618
4619 /* The annoying part is that pattern matching needs
4620 * that we modify the contents to null-terminate all
4621 * strings before testing them.
4622 */
4623
4624 term = *cur_end;
4625 *cur_end = '\0';
4626
4627 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4628 switch (exp->action) {
4629 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004630 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004631 done = 1;
4632 break;
4633
4634 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004635 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004636 done = 1;
4637 break;
4638
4639 case ACT_REPLACE:
4640 *cur_end = term; /* restore the string terminator */
4641 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4642 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4643 /* FIXME: if the user adds a newline in the replacement, the
4644 * index will not be recalculated for now, and the new line
4645 * will not be counted as a new header.
4646 */
4647
4648 txn->rsp.eoh += delta;
4649 cur_end += delta;
4650
Willy Tarreau9cdde232007-05-02 20:58:19 +02004651 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004652 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004653 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004654 cur_ptr, cur_end + 1,
4655 NULL, NULL);
4656 if (unlikely(!cur_end))
4657 return -1;
4658
4659 /* we have a full respnse and we know that we have either a CR
4660 * or an LF at <ptr>.
4661 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004662 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4664 /* there is no point trying this regex on headers */
4665 return 1;
4666 }
4667 }
4668 *cur_end = term; /* restore the string terminator */
4669 return done;
4670}
4671
4672
4673
4674/*
4675 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4676 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4677 * unparsable response.
4678 */
4679int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4680{
Willy Tarreau3d300592007-03-18 18:34:41 +01004681 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004682 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004683 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004684 int ret;
4685
4686 /*
4687 * The interleaving of transformations and verdicts
4688 * makes it difficult to decide to continue or stop
4689 * the evaluation.
4690 */
4691
Willy Tarreau3d300592007-03-18 18:34:41 +01004692 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004693 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4694 exp->action == ACT_PASS)) {
4695 exp = exp->next;
4696 continue;
4697 }
4698
4699 /* Apply the filter to the status line. */
4700 ret = apply_filter_to_sts_line(t, rtr, exp);
4701 if (unlikely(ret < 0))
4702 return -1;
4703
4704 if (likely(ret == 0)) {
4705 /* The filter did not match the response, it can be
4706 * iterated through all headers.
4707 */
4708 apply_filter_to_resp_headers(t, rtr, exp);
4709 }
4710 exp = exp->next;
4711 }
4712 return 0;
4713}
4714
4715
4716
4717/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004718 * Manage server-side cookies. It can impact performance by about 2% so it is
4719 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720 */
4721void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4722{
4723 struct http_txn *txn = &t->txn;
4724 char *p1, *p2, *p3, *p4;
4725
4726 appsess *asession_temp = NULL;
4727 appsess local_asession;
4728
4729 char *cur_ptr, *cur_end, *cur_next;
4730 int cur_idx, old_idx, delta;
4731
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 /* Iterate through the headers.
4733 * we start with the start line.
4734 */
4735 old_idx = 0;
4736 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4737
4738 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4739 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004740 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004741
4742 cur_hdr = &txn->hdr_idx.v[cur_idx];
4743 cur_ptr = cur_next;
4744 cur_end = cur_ptr + cur_hdr->len;
4745 cur_next = cur_end + cur_hdr->cr + 1;
4746
4747 /* We have one full header between cur_ptr and cur_end, and the
4748 * next header starts at cur_next. We're only interested in
4749 * "Cookie:" headers.
4750 */
4751
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004752 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4753 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004754 old_idx = cur_idx;
4755 continue;
4756 }
4757
4758 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004759 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004760
4761
4762 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004763 if (t->be->cookie_name == NULL &&
4764 t->be->appsession_name == NULL &&
4765 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004766 return;
4767
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004768 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769
4770 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004771 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4772 break;
4773
4774 /* p1 is at the beginning of the cookie name */
4775 p2 = p1;
4776
4777 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4778 p2++;
4779
4780 if (p2 == cur_end || *p2 == ';') /* next cookie */
4781 break;
4782
4783 p3 = p2 + 1; /* skip the '=' sign */
4784 if (p3 == cur_end)
4785 break;
4786
4787 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004788 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004789 p4++;
4790
4791 /* here, we have the cookie name between p1 and p2,
4792 * and its value between p3 and p4.
4793 * we can process it.
4794 */
4795
4796 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004797 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004798 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004799 (p4 - p1 >= t->be->capture_namelen) &&
4800 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004801 int log_len = p4 - p1;
4802
Willy Tarreau086b3b42007-05-13 21:45:51 +02004803 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004804 Alert("HTTP logging : out of memory.\n");
4805 }
4806
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004807 if (log_len > t->be->capture_len)
4808 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004809 memcpy(txn->srv_cookie, p1, log_len);
4810 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004811 }
4812
4813 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004814 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4815 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004817 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004818
4819 /* If the cookie is in insert mode on a known server, we'll delete
4820 * this occurrence because we'll insert another one later.
4821 * We'll delete it too if the "indirect" option is set and we're in
4822 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004823 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4824 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004825 /* this header must be deleted */
4826 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4827 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4828 txn->hdr_idx.used--;
4829 cur_hdr->len = 0;
4830 cur_next += delta;
4831 txn->rsp.eoh += delta;
4832
Willy Tarreau3d300592007-03-18 18:34:41 +01004833 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004834 }
4835 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004836 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004837 /* replace bytes p3->p4 with the cookie name associated
4838 * with this server since we know it.
4839 */
4840 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4841 cur_hdr->len += delta;
4842 cur_next += delta;
4843 txn->rsp.eoh += delta;
4844
Willy Tarreau3d300592007-03-18 18:34:41 +01004845 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004846 }
4847 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004848 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004849 /* insert the cookie name associated with this server
4850 * before existing cookie, and insert a delimitor between them..
4851 */
4852 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4853 cur_hdr->len += delta;
4854 cur_next += delta;
4855 txn->rsp.eoh += delta;
4856
4857 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004858 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004859 }
4860 }
4861 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004862 else if ((t->be->appsession_name != NULL) &&
4863 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004864
4865 /* Cool... it's the right one */
4866
4867 size_t server_id_len = strlen(t->srv->id) + 1;
4868 asession_temp = &local_asession;
4869
Willy Tarreau63963c62007-05-13 21:29:55 +02004870 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004871 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4872 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4873 return;
4874 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004875 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4876 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004877 asession_temp->serverid = NULL;
4878
4879 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004880 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4881 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004882 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004883 Alert("Not enough Memory process_srv():asession:calloc().\n");
4884 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4885 return;
4886 }
4887 asession_temp->sessid = local_asession.sessid;
4888 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004889 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004890 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4891 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004892 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004893 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004894 }
4895
Willy Tarreaua15645d2007-03-18 16:22:39 +01004896 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004897 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004898 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4899 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4900 return;
4901 }
4902 asession_temp->serverid[0] = '\0';
4903 }
4904
4905 if (asession_temp->serverid[0] == '\0')
4906 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4907
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004908 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004909 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004911 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004912 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004913#endif
4914 }/* end if ((t->proxy->appsession_name != NULL) ... */
4915 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4916 } /* we're now at the end of the cookie value */
4917
4918 /* keep the link from this header to next one */
4919 old_idx = cur_idx;
4920 } /* end of cookie processing on this header */
4921}
4922
4923
4924
4925/*
4926 * Check if response is cacheable or not. Updates t->flags.
4927 */
4928void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4929{
4930 struct http_txn *txn = &t->txn;
4931 char *p1, *p2;
4932
4933 char *cur_ptr, *cur_end, *cur_next;
4934 int cur_idx;
4935
Willy Tarreau5df51872007-11-25 16:20:08 +01004936 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004937 return;
4938
4939 /* Iterate through the headers.
4940 * we start with the start line.
4941 */
4942 cur_idx = 0;
4943 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4944
4945 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4946 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004947 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948
4949 cur_hdr = &txn->hdr_idx.v[cur_idx];
4950 cur_ptr = cur_next;
4951 cur_end = cur_ptr + cur_hdr->len;
4952 cur_next = cur_end + cur_hdr->cr + 1;
4953
4954 /* We have one full header between cur_ptr and cur_end, and the
4955 * next header starts at cur_next. We're only interested in
4956 * "Cookie:" headers.
4957 */
4958
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004959 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4960 if (val) {
4961 if ((cur_end - (cur_ptr + val) >= 8) &&
4962 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4963 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4964 return;
4965 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004966 }
4967
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004968 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4969 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004970 continue;
4971
4972 /* OK, right now we know we have a cache-control header at cur_ptr */
4973
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004974 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004975
4976 if (p1 >= cur_end) /* no more info */
4977 continue;
4978
4979 /* p1 is at the beginning of the value */
4980 p2 = p1;
4981
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004982 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004983 p2++;
4984
4985 /* we have a complete value between p1 and p2 */
4986 if (p2 < cur_end && *p2 == '=') {
4987 /* we have something of the form no-cache="set-cookie" */
4988 if ((cur_end - p1 >= 21) &&
4989 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4990 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004991 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004992 continue;
4993 }
4994
4995 /* OK, so we know that either p2 points to the end of string or to a comma */
4996 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4997 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4998 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4999 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005000 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005001 return;
5002 }
5003
5004 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005005 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005006 continue;
5007 }
5008 }
5009}
5010
5011
Willy Tarreau58f10d72006-12-04 02:26:12 +01005012/*
5013 * Try to retrieve a known appsession in the URI, then the associated server.
5014 * If the server is found, it's assigned to the session.
5015 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005016void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005017{
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005019 appsess *asession_temp = NULL;
5020 appsess local_asession;
5021 char *request_line;
5022
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005023 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005024 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005025 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005026 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005027 return;
5028
5029 /* skip ';' */
5030 request_line++;
5031
5032 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005033 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005034 return;
5035
5036 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005037 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005038
5039 /* First try if we already have an appsession */
5040 asession_temp = &local_asession;
5041
Willy Tarreau63963c62007-05-13 21:29:55 +02005042 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005043 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5044 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5045 return;
5046 }
5047
5048 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005049 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5050 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005051 asession_temp->serverid = NULL;
5052
5053 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005054 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5055 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005056 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005057 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005058 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005059 Alert("Not enough memory process_cli():asession:calloc().\n");
5060 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5061 return;
5062 }
5063 asession_temp->sessid = local_asession.sessid;
5064 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005065 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005066 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005067 }
5068 else {
5069 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005070 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005071 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005072
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005073 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005074 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005075
Willy Tarreau58f10d72006-12-04 02:26:12 +01005076#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005077 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005078 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005079#endif
5080 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005081 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005082 Alert("Found Application Session without matching server.\n");
5083 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005084 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005085 while (srv) {
5086 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005087 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005089 txn->flags &= ~TX_CK_MASK;
5090 txn->flags |= TX_CK_VALID;
5091 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005092 t->srv = srv;
5093 break;
5094 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005095 txn->flags &= ~TX_CK_MASK;
5096 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005097 }
5098 }
5099 srv = srv->next;
5100 }
5101 }
5102}
5103
5104
Willy Tarreaub2513902006-12-17 14:52:38 +01005105/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005106 * In a GET or HEAD request, check if the requested URI matches the stats uri
5107 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005108 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005109 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005110 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005111 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005112 *
5113 * Returns 1 if the session's state changes, otherwise 0.
5114 */
5115int stats_check_uri_auth(struct session *t, struct proxy *backend)
5116{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005117 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005118 struct uri_auth *uri_auth = backend->uri_auth;
5119 struct user_auth *user;
5120 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005121 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005122
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005123 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005125 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005126 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005127 return 0;
5128
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005129 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005130
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005131 /* the URI is in h */
5132 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005133 return 0;
5134
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005135 h += uri_auth->uri_len;
5136 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5137 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005138 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005139 break;
5140 }
5141 h++;
5142 }
5143
5144 if (uri_auth->refresh) {
5145 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5146 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5147 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005148 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005149 break;
5150 }
5151 h++;
5152 }
5153 }
5154
Willy Tarreau55bb8452007-10-17 18:44:57 +02005155 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5156 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5157 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005158 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005159 break;
5160 }
5161 h++;
5162 }
5163
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005164 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5165
Willy Tarreaub2513902006-12-17 14:52:38 +01005166 /* we are in front of a interceptable URI. Let's check
5167 * if there's an authentication and if it's valid.
5168 */
5169 user = uri_auth->users;
5170 if (!user) {
5171 /* no user auth required, it's OK */
5172 authenticated = 1;
5173 } else {
5174 authenticated = 0;
5175
5176 /* a user list is defined, we have to check.
5177 * skip 21 chars for "Authorization: Basic ".
5178 */
5179
5180 /* FIXME: this should move to an earlier place */
5181 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005182 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5183 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5184 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005185 if (len > 14 &&
5186 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005187 txn->auth_hdr.str = h;
5188 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005189 break;
5190 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005191 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005192 }
5193
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005194 if (txn->auth_hdr.len < 21 ||
5195 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005196 user = NULL;
5197
5198 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005199 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5200 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005201 user->user_pwd, user->user_len)) {
5202 authenticated = 1;
5203 break;
5204 }
5205 user = user->next;
5206 }
5207 }
5208
5209 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005210 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005211
5212 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005213 msg.str = trash;
5214 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005215 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005216 client_retnclose(t, &msg);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005217 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005218 if (!(t->flags & SN_ERR_MASK))
5219 t->flags |= SN_ERR_PRXCOND;
5220 if (!(t->flags & SN_FINST_MASK))
5221 t->flags |= SN_FINST_R;
5222 return 1;
5223 }
5224
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005225 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005226 * data.
5227 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005228 EV_FD_CLR(t->cli_fd, DIR_RD);
5229 buffer_shutr(t->req);
5230 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005231 t->cli_state = CL_STSHUTR;
5232 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005233 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005234 t->data_source = DATA_SRC_STATS;
5235 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005236 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005237 produce_content(t);
5238 return 1;
5239}
5240
5241
Willy Tarreaubaaee002006-06-26 02:48:02 +02005242/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005243 * Print a debug line with a header
5244 */
5245void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5246{
5247 int len, max;
5248 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5249 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5250 max = end - start;
5251 UBOUND(max, sizeof(trash) - len - 1);
5252 len += strlcpy2(trash + len, start, max + 1);
5253 trash[len++] = '\n';
5254 write(1, trash, len);
5255}
5256
5257
Willy Tarreau8797c062007-05-07 00:55:35 +02005258/************************************************************************/
5259/* The code below is dedicated to ACL parsing and matching */
5260/************************************************************************/
5261
5262
5263
5264
5265/* 1. Check on METHOD
5266 * We use the pre-parsed method if it is known, and store its number as an
5267 * integer. If it is unknown, we use the pointer and the length.
5268 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005269static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005270{
5271 int len, meth;
5272
Willy Tarreauae8b7962007-06-09 23:10:04 +02005273 len = strlen(*text);
5274 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005275
5276 pattern->val.i = meth;
5277 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005278 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005279 if (!pattern->ptr.str)
5280 return 0;
5281 pattern->len = len;
5282 }
5283 return 1;
5284}
5285
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005286static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005287acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5288 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005289{
5290 int meth;
5291 struct http_txn *txn = l7;
5292
Willy Tarreaub6866442008-07-14 23:54:42 +02005293 if (!txn)
5294 return 0;
5295
Willy Tarreauc11416f2007-06-17 16:58:38 +02005296 if (txn->req.msg_state != HTTP_MSG_BODY)
5297 return 0;
5298
Willy Tarreau8797c062007-05-07 00:55:35 +02005299 meth = txn->meth;
5300 test->i = meth;
5301 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005302 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5303 /* ensure the indexes are not affected */
5304 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005305 test->len = txn->req.sl.rq.m_l;
5306 test->ptr = txn->req.sol;
5307 }
5308 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5309 return 1;
5310}
5311
5312static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5313{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005314 int icase;
5315
Willy Tarreau8797c062007-05-07 00:55:35 +02005316 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005317 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005318
5319 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005320 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005321
5322 /* Other method, we must compare the strings */
5323 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005324 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005325
5326 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5327 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5328 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005329 return ACL_PAT_FAIL;
5330 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005331}
5332
5333/* 2. Check on Request/Status Version
5334 * We simply compare strings here.
5335 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005336static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005337{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005338 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005339 if (!pattern->ptr.str)
5340 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005341 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005342 return 1;
5343}
5344
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005345static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005346acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5347 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005348{
5349 struct http_txn *txn = l7;
5350 char *ptr;
5351 int len;
5352
Willy Tarreaub6866442008-07-14 23:54:42 +02005353 if (!txn)
5354 return 0;
5355
Willy Tarreauc11416f2007-06-17 16:58:38 +02005356 if (txn->req.msg_state != HTTP_MSG_BODY)
5357 return 0;
5358
Willy Tarreau8797c062007-05-07 00:55:35 +02005359 len = txn->req.sl.rq.v_l;
5360 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5361
5362 while ((len-- > 0) && (*ptr++ != '/'));
5363 if (len <= 0)
5364 return 0;
5365
5366 test->ptr = ptr;
5367 test->len = len;
5368
5369 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5370 return 1;
5371}
5372
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005373static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005374acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5375 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005376{
5377 struct http_txn *txn = l7;
5378 char *ptr;
5379 int len;
5380
Willy Tarreaub6866442008-07-14 23:54:42 +02005381 if (!txn)
5382 return 0;
5383
Willy Tarreauc11416f2007-06-17 16:58:38 +02005384 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5385 return 0;
5386
Willy Tarreau8797c062007-05-07 00:55:35 +02005387 len = txn->rsp.sl.st.v_l;
5388 ptr = txn->rsp.sol;
5389
5390 while ((len-- > 0) && (*ptr++ != '/'));
5391 if (len <= 0)
5392 return 0;
5393
5394 test->ptr = ptr;
5395 test->len = len;
5396
5397 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5398 return 1;
5399}
5400
5401/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005402static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005403acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5404 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005405{
5406 struct http_txn *txn = l7;
5407 char *ptr;
5408 int len;
5409
Willy Tarreaub6866442008-07-14 23:54:42 +02005410 if (!txn)
5411 return 0;
5412
Willy Tarreauc11416f2007-06-17 16:58:38 +02005413 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5414 return 0;
5415
Willy Tarreau8797c062007-05-07 00:55:35 +02005416 len = txn->rsp.sl.st.c_l;
5417 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5418
5419 test->i = __strl2ui(ptr, len);
5420 test->flags = ACL_TEST_F_VOL_1ST;
5421 return 1;
5422}
5423
5424/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005425static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005426acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5427 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005428{
5429 struct http_txn *txn = l7;
5430
Willy Tarreaub6866442008-07-14 23:54:42 +02005431 if (!txn)
5432 return 0;
5433
Willy Tarreauc11416f2007-06-17 16:58:38 +02005434 if (txn->req.msg_state != HTTP_MSG_BODY)
5435 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005436
Willy Tarreauc11416f2007-06-17 16:58:38 +02005437 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5438 /* ensure the indexes are not affected */
5439 return 0;
5440
Willy Tarreau8797c062007-05-07 00:55:35 +02005441 test->len = txn->req.sl.rq.u_l;
5442 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5443
Willy Tarreauf3d25982007-05-08 22:45:09 +02005444 /* we do not need to set READ_ONLY because the data is in a buffer */
5445 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005446 return 1;
5447}
5448
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005449static int
5450acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5451 struct acl_expr *expr, struct acl_test *test)
5452{
5453 struct http_txn *txn = l7;
5454
Willy Tarreaub6866442008-07-14 23:54:42 +02005455 if (!txn)
5456 return 0;
5457
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005458 if (txn->req.msg_state != HTTP_MSG_BODY)
5459 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005460
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005461 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5462 /* ensure the indexes are not affected */
5463 return 0;
5464
5465 /* Parse HTTP request */
5466 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5467 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5468 test->i = AF_INET;
5469
5470 /*
5471 * If we are parsing url in frontend space, we prepare backend stage
5472 * to not parse again the same url ! optimization lazyness...
5473 */
5474 if (px->options & PR_O_HTTP_PROXY)
5475 l4->flags |= SN_ADDR_SET;
5476
5477 test->flags = ACL_TEST_F_READ_ONLY;
5478 return 1;
5479}
5480
5481static int
5482acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5483 struct acl_expr *expr, struct acl_test *test)
5484{
5485 struct http_txn *txn = l7;
5486
Willy Tarreaub6866442008-07-14 23:54:42 +02005487 if (!txn)
5488 return 0;
5489
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005490 if (txn->req.msg_state != HTTP_MSG_BODY)
5491 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005492
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005493 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5494 /* ensure the indexes are not affected */
5495 return 0;
5496
5497 /* Same optimization as url_ip */
5498 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5499 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5500
5501 if (px->options & PR_O_HTTP_PROXY)
5502 l4->flags |= SN_ADDR_SET;
5503
5504 test->flags = ACL_TEST_F_READ_ONLY;
5505 return 1;
5506}
5507
Willy Tarreauc11416f2007-06-17 16:58:38 +02005508/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5509 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5510 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005511static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005512acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005513 struct acl_expr *expr, struct acl_test *test)
5514{
5515 struct http_txn *txn = l7;
5516 struct hdr_idx *idx = &txn->hdr_idx;
5517 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005518
Willy Tarreaub6866442008-07-14 23:54:42 +02005519 if (!txn)
5520 return 0;
5521
Willy Tarreau33a7e692007-06-10 19:45:56 +02005522 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5523 /* search for header from the beginning */
5524 ctx->idx = 0;
5525
Willy Tarreau33a7e692007-06-10 19:45:56 +02005526 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5527 test->flags |= ACL_TEST_F_FETCH_MORE;
5528 test->flags |= ACL_TEST_F_VOL_HDR;
5529 test->len = ctx->vlen;
5530 test->ptr = (char *)ctx->line + ctx->val;
5531 return 1;
5532 }
5533
5534 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5535 test->flags |= ACL_TEST_F_VOL_HDR;
5536 return 0;
5537}
5538
Willy Tarreau33a7e692007-06-10 19:45:56 +02005539static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005540acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5541 struct acl_expr *expr, struct acl_test *test)
5542{
5543 struct http_txn *txn = l7;
5544
Willy Tarreaub6866442008-07-14 23:54:42 +02005545 if (!txn)
5546 return 0;
5547
Willy Tarreauc11416f2007-06-17 16:58:38 +02005548 if (txn->req.msg_state != HTTP_MSG_BODY)
5549 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005550
Willy Tarreauc11416f2007-06-17 16:58:38 +02005551 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5552 /* ensure the indexes are not affected */
5553 return 0;
5554
5555 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5556}
5557
5558static int
5559acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5560 struct acl_expr *expr, struct acl_test *test)
5561{
5562 struct http_txn *txn = l7;
5563
Willy Tarreaub6866442008-07-14 23:54:42 +02005564 if (!txn)
5565 return 0;
5566
Willy Tarreauc11416f2007-06-17 16:58:38 +02005567 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5568 return 0;
5569
5570 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5571}
5572
5573/* 6. Check on HTTP header count. The number of occurrences is returned.
5574 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5575 */
5576static int
5577acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005578 struct acl_expr *expr, struct acl_test *test)
5579{
5580 struct http_txn *txn = l7;
5581 struct hdr_idx *idx = &txn->hdr_idx;
5582 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005583 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005584
Willy Tarreaub6866442008-07-14 23:54:42 +02005585 if (!txn)
5586 return 0;
5587
Willy Tarreau33a7e692007-06-10 19:45:56 +02005588 ctx.idx = 0;
5589 cnt = 0;
5590 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5591 cnt++;
5592
5593 test->i = cnt;
5594 test->flags = ACL_TEST_F_VOL_HDR;
5595 return 1;
5596}
5597
Willy Tarreauc11416f2007-06-17 16:58:38 +02005598static int
5599acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5600 struct acl_expr *expr, struct acl_test *test)
5601{
5602 struct http_txn *txn = l7;
5603
Willy Tarreaub6866442008-07-14 23:54:42 +02005604 if (!txn)
5605 return 0;
5606
Willy Tarreauc11416f2007-06-17 16:58:38 +02005607 if (txn->req.msg_state != HTTP_MSG_BODY)
5608 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005609
Willy Tarreauc11416f2007-06-17 16:58:38 +02005610 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5611 /* ensure the indexes are not affected */
5612 return 0;
5613
5614 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5615}
5616
5617static int
5618acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5619 struct acl_expr *expr, struct acl_test *test)
5620{
5621 struct http_txn *txn = l7;
5622
Willy Tarreaub6866442008-07-14 23:54:42 +02005623 if (!txn)
5624 return 0;
5625
Willy Tarreauc11416f2007-06-17 16:58:38 +02005626 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5627 return 0;
5628
5629 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5630}
5631
Willy Tarreau33a7e692007-06-10 19:45:56 +02005632/* 7. Check on HTTP header's integer value. The integer value is returned.
5633 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005634 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005635 */
5636static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005637acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005638 struct acl_expr *expr, struct acl_test *test)
5639{
5640 struct http_txn *txn = l7;
5641 struct hdr_idx *idx = &txn->hdr_idx;
5642 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005643
Willy Tarreaub6866442008-07-14 23:54:42 +02005644 if (!txn)
5645 return 0;
5646
Willy Tarreau33a7e692007-06-10 19:45:56 +02005647 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5648 /* search for header from the beginning */
5649 ctx->idx = 0;
5650
Willy Tarreau33a7e692007-06-10 19:45:56 +02005651 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5652 test->flags |= ACL_TEST_F_FETCH_MORE;
5653 test->flags |= ACL_TEST_F_VOL_HDR;
5654 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5655 return 1;
5656 }
5657
5658 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5659 test->flags |= ACL_TEST_F_VOL_HDR;
5660 return 0;
5661}
5662
Willy Tarreauc11416f2007-06-17 16:58:38 +02005663static int
5664acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5665 struct acl_expr *expr, struct acl_test *test)
5666{
5667 struct http_txn *txn = l7;
5668
Willy Tarreaub6866442008-07-14 23:54:42 +02005669 if (!txn)
5670 return 0;
5671
Willy Tarreauc11416f2007-06-17 16:58:38 +02005672 if (txn->req.msg_state != HTTP_MSG_BODY)
5673 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005674
Willy Tarreauc11416f2007-06-17 16:58:38 +02005675 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5676 /* ensure the indexes are not affected */
5677 return 0;
5678
5679 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5680}
5681
5682static int
5683acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5684 struct acl_expr *expr, struct acl_test *test)
5685{
5686 struct http_txn *txn = l7;
5687
Willy Tarreaub6866442008-07-14 23:54:42 +02005688 if (!txn)
5689 return 0;
5690
Willy Tarreauc11416f2007-06-17 16:58:38 +02005691 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5692 return 0;
5693
5694 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5695}
5696
Willy Tarreau737b0c12007-06-10 21:28:46 +02005697/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5698 * the first '/' after the possible hostname, and ends before the possible '?'.
5699 */
5700static int
5701acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5702 struct acl_expr *expr, struct acl_test *test)
5703{
5704 struct http_txn *txn = l7;
5705 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005706
Willy Tarreaub6866442008-07-14 23:54:42 +02005707 if (!txn)
5708 return 0;
5709
Willy Tarreauc11416f2007-06-17 16:58:38 +02005710 if (txn->req.msg_state != HTTP_MSG_BODY)
5711 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005712
Willy Tarreauc11416f2007-06-17 16:58:38 +02005713 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5714 /* ensure the indexes are not affected */
5715 return 0;
5716
Willy Tarreau21d2af32008-02-14 20:25:24 +01005717 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5718 ptr = http_get_path(txn);
5719 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005720 return 0;
5721
5722 /* OK, we got the '/' ! */
5723 test->ptr = ptr;
5724
5725 while (ptr < end && *ptr != '?')
5726 ptr++;
5727
5728 test->len = ptr - test->ptr;
5729
5730 /* we do not need to set READ_ONLY because the data is in a buffer */
5731 test->flags = ACL_TEST_F_VOL_1ST;
5732 return 1;
5733}
5734
5735
Willy Tarreau8797c062007-05-07 00:55:35 +02005736
5737/************************************************************************/
5738/* All supported keywords must be declared here. */
5739/************************************************************************/
5740
5741/* Note: must not be declared <const> as its list will be overwritten */
5742static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005743 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5744 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5745 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5746 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005747
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005748 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5749 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5750 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5751 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5752 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5753 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5754 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5755 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5756 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005757
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005758 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5759 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5760 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5761 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5762 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5763 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5764 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5765 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5766 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5767 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005768
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005769 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5770 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5771 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5772 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5773 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5774 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5775 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5776 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5777 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005778
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005779 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5780 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5781 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5782 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5783 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5784 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5785 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005786
Willy Tarreauf3d25982007-05-08 22:45:09 +02005787 { NULL, NULL, NULL, NULL },
5788
5789#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005790 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5791 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5792 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5793 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5794 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5795 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5796 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5797
Willy Tarreau8797c062007-05-07 00:55:35 +02005798 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5799 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5800 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5801 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5802 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5803 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5804 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5805 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5806
5807 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5808 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5809 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5810 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5811 { NULL, NULL, NULL, NULL },
5812#endif
5813}};
5814
5815
5816__attribute__((constructor))
5817static void __http_protocol_init(void)
5818{
5819 acl_register_keywords(&acl_kws);
5820}
5821
5822
Willy Tarreau58f10d72006-12-04 02:26:12 +01005823/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005824 * Local variables:
5825 * c-indent-level: 8
5826 * c-basic-offset: 8
5827 * End:
5828 */