blob: ac5895047dff9067b7433c487eeec85d58b981ce [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 Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "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 Tarreauba392ce2008-08-16 21:13:23 +0200549 buffer_shutw(t->req);
550 buffer_shutr(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 Tarreaudafde432008-08-17 01:00:46 +0200648/* Processes the client, server, request and response jobs of a session task,
649 * then puts it back to the wait queue in a clean state, or cleans up its
650 * resources if it must be deleted. Returns in <next> the date the task wants
651 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
652 * nothing too many times, the request and response buffers flags are monitored
653 * and each function is called only if at least another function has changed at
654 * least one flag. If one of the functions called returns non-zero, then it
655 * will be called once again after all other functions. This permits explicit
656 * external loops which may be useful for complex state machines.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 */
Willy Tarreaudafde432008-08-17 01:00:46 +0200658#define PROCESS_CLI 0x1
659#define PROCESS_SRV 0x2
660#define PROCESS_REQ 0x4
661#define PROCESS_RTR 0x8
662#define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR)
663
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200664void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
666 struct session *s = t->context;
Willy Tarreaudafde432008-08-17 01:00:46 +0200667 unsigned resync = PROCESS_ALL;
668 unsigned int rqf;
669 unsigned int rpf;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
Willy Tarreau507385d2008-08-17 13:04:25 +0200671 /* check timeout expiration only once and adjust buffer flags
672 * accordingly.
673 */
674 if (unlikely(tick_is_expired(t->expire, now_ms))) {
675 if (tick_is_expired(s->req->rex, now_ms))
676 s->req->flags |= BF_READ_TIMEOUT;
677
678 if (tick_is_expired(s->req->wex, now_ms))
679 s->req->flags |= BF_WRITE_TIMEOUT;
680
681 if (tick_is_expired(s->rep->rex, now_ms))
682 s->rep->flags |= BF_READ_TIMEOUT;
683
684 if (tick_is_expired(s->rep->wex, now_ms))
685 s->rep->flags |= BF_WRITE_TIMEOUT;
686 }
687
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 do {
Willy Tarreaudafde432008-08-17 01:00:46 +0200689 if (resync & PROCESS_REQ) {
690 resync &= ~PROCESS_REQ;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200691 if (s->req->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200692 rqf = s->req->flags;
693 rpf = s->rep->flags;
694
695 if (process_request(s))
696 resync |= PROCESS_REQ;
697
Willy Tarreau2df28e82008-08-17 15:20:19 +0200698 if (!s->req->analysers && !(s->req->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200699 s->req->flags |= BF_MAY_FORWARD;
700
701 if (rqf != s->req->flags || rpf != s->rep->flags)
702 resync |= PROCESS_ALL & ~PROCESS_REQ;
703 }
704 }
705
706 if (resync & PROCESS_RTR) {
707 resync &= ~PROCESS_RTR;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200708 if (s->rep->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200709 rqf = s->req->flags;
710 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200711
Willy Tarreaudafde432008-08-17 01:00:46 +0200712 if (process_response(s))
713 resync |= PROCESS_RTR;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200714
Willy Tarreau2df28e82008-08-17 15:20:19 +0200715 if (!s->rep->analysers && !(s->rep->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200716 s->rep->flags |= BF_MAY_FORWARD;
717
718 if (rqf != s->req->flags || rpf != s->rep->flags)
719 resync |= PROCESS_ALL & ~PROCESS_RTR;
720 }
721 }
722
723 if (resync & PROCESS_CLI) {
724 rqf = s->req->flags;
725 rpf = s->rep->flags;
726
727 resync &= ~PROCESS_CLI;
728 if (process_cli(s))
729 resync |= PROCESS_CLI;
730
731 if (rqf != s->req->flags || rpf != s->rep->flags)
732 resync |= PROCESS_ALL & ~PROCESS_CLI;
733 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200734
Willy Tarreaudafde432008-08-17 01:00:46 +0200735 if (resync & PROCESS_SRV) {
736 rqf = s->req->flags;
737 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200738
Willy Tarreaudafde432008-08-17 01:00:46 +0200739 resync &= ~PROCESS_SRV;
740 if (process_srv(s))
741 resync |= PROCESS_SRV;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200742
Willy Tarreaudafde432008-08-17 01:00:46 +0200743 if (rqf != s->req->flags || rpf != s->rep->flags)
744 resync |= PROCESS_ALL & ~PROCESS_SRV;
745 }
746 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200748 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100749
750 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
751 session_process_counters(s);
752
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200753 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
754 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreau7f875f62008-08-11 17:35:01 +0200756 /* Trick: if a request is being waiting for the server to respond,
757 * and if we know the server can timeout, we don't want the timeout
758 * to expire on the client side first, but we're still interested
759 * in passing data from the client to the server (eg: POST). Thus,
760 * we can cancel the client's request timeout if the server's
761 * request timeout is set and the server has not yet sent a response.
762 */
763
Willy Tarreauba392ce2008-08-16 21:13:23 +0200764 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200765 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200766 s->req->rex = TICK_ETERNITY;
767
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200768 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
769 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreau2df28e82008-08-17 15:20:19 +0200770 if (s->req->analysers) {
771 if (s->req->analysers & AN_REQ_INSPECT)
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200772 t->expire = tick_first(t->expire, s->inspect_exp);
Willy Tarreau2df28e82008-08-17 15:20:19 +0200773 else if (s->req->analysers & AN_REQ_HTTP_HDR)
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200774 t->expire = tick_first(t->expire, s->txn.exp);
775 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776
777 /* restore t to its place in the task list */
778 task_queue(t);
779
Willy Tarreaua7c52762008-08-16 18:40:18 +0200780#ifdef DEBUG_DEV
781 /* this may only happen when no timeout is set or in case of an FSM bug */
782 if (!t->expire)
783 ABORT_NOW();
784#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200785 *next = t->expire;
786 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
788
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100789 s->fe->feconn--;
790 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200791 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 actconn--;
793
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200794 if (unlikely((global.mode & MODE_DEBUG) &&
795 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200797 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 s->uniq_id, s->be->id,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200799 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd,
800 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 write(1, trash, len);
802 }
803
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200804 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100805 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806
807 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200808 if (s->logs.logwait &&
809 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200810 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
811 if (s->fe->to_log & LW_REQ)
812 http_sess_log(s);
813 else
814 tcp_sess_log(s);
815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816
817 /* the task MUST not be in the run queue anymore */
818 task_delete(t);
819 session_free(s);
820 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200821 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822}
823
824
Willy Tarreau42250582007-04-01 01:30:43 +0200825extern const char sess_term_cond[8];
826extern const char sess_fin_state[8];
827extern const char *monthname[12];
828const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
829const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
830 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
831 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200832struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200833struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834
Willy Tarreau42250582007-04-01 01:30:43 +0200835/*
836 * send a log for the session when we have enough info about it.
837 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100838 */
Willy Tarreau42250582007-04-01 01:30:43 +0200839static void http_sess_log(struct session *s)
840{
841 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
842 struct proxy *fe = s->fe;
843 struct proxy *be = s->be;
844 struct proxy *prx_log;
845 struct http_txn *txn = &s->txn;
846 int tolog;
847 char *uri, *h;
848 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200849 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200850 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200851 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200852 int hdr;
853
854 if (fe->logfac1 < 0 && fe->logfac2 < 0)
855 return;
856 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100857
Willy Tarreau42250582007-04-01 01:30:43 +0200858 if (s->cli_addr.ss_family == AF_INET)
859 inet_ntop(AF_INET,
860 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
861 pn, sizeof(pn));
862 else
863 inet_ntop(AF_INET6,
864 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
865 pn, sizeof(pn));
866
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200867 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200868
869 /* FIXME: let's limit ourselves to frontend logging for now. */
870 tolog = fe->to_log;
871
872 h = tmpline;
873 if (fe->to_log & LW_REQHDR &&
874 txn->req.cap &&
875 (h < tmpline + sizeof(tmpline) - 10)) {
876 *(h++) = ' ';
877 *(h++) = '{';
878 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
879 if (hdr)
880 *(h++) = '|';
881 if (txn->req.cap[hdr] != NULL)
882 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
883 '#', hdr_encode_map, txn->req.cap[hdr]);
884 }
885 *(h++) = '}';
886 }
887
888 if (fe->to_log & LW_RSPHDR &&
889 txn->rsp.cap &&
890 (h < tmpline + sizeof(tmpline) - 7)) {
891 *(h++) = ' ';
892 *(h++) = '{';
893 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
894 if (hdr)
895 *(h++) = '|';
896 if (txn->rsp.cap[hdr] != NULL)
897 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
898 '#', hdr_encode_map, txn->rsp.cap[hdr]);
899 }
900 *(h++) = '}';
901 }
902
903 if (h < tmpline + sizeof(tmpline) - 4) {
904 *(h++) = ' ';
905 *(h++) = '"';
906 uri = txn->uri ? txn->uri : "<BADREQ>";
907 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
908 '#', url_encode_map, uri);
909 *(h++) = '"';
910 }
911 *h = '\0';
912
913 svid = (tolog & LW_SVID) ?
914 (s->data_source != DATA_SRC_STATS) ?
915 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
916
Willy Tarreau70089872008-06-13 21:12:51 +0200917 t_request = -1;
918 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
919 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
920
Willy Tarreau42250582007-04-01 01:30:43 +0200921 send_log(prx_log, LOG_INFO,
922 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
923 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100924 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200925 pn,
926 (s->cli_addr.ss_family == AF_INET) ?
927 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
928 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200929 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200930 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200931 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200932 t_request,
933 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200934 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
935 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
936 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
937 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100938 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200939 txn->cli_cookie ? txn->cli_cookie : "-",
940 txn->srv_cookie ? txn->srv_cookie : "-",
941 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
942 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
943 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
944 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
945 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100946 (s->flags & SN_REDISP)?"+":"",
947 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200948 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
949
950 s->logs.logwait = 0;
951}
952
Willy Tarreau117f59e2007-03-04 18:17:17 +0100953
954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001043const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1044 unsigned int state, const char *ptr, const char *end,
1045 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
1047 __label__
1048 http_msg_rpver,
1049 http_msg_rpver_sp,
1050 http_msg_rpcode,
1051 http_msg_rpcode_sp,
1052 http_msg_rpreason,
1053 http_msg_rpline_eol,
1054 http_msg_ood, /* out of data */
1055 http_msg_invalid;
1056
1057 switch (state) {
1058 http_msg_rpver:
1059 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001060 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1062
1063 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001064 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001065 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1066 }
1067 goto http_msg_invalid;
1068
1069 http_msg_rpver_sp:
1070 case HTTP_MSG_RPVER_SP:
1071 if (likely(!HTTP_IS_LWS(*ptr))) {
1072 msg->sl.st.c = ptr - msg_buf;
1073 goto http_msg_rpcode;
1074 }
1075 if (likely(HTTP_IS_SPHT(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1077 /* so it's a CR/LF, this is invalid */
1078 goto http_msg_invalid;
1079
1080 http_msg_rpcode:
1081 case HTTP_MSG_RPCODE:
1082 if (likely(!HTTP_IS_LWS(*ptr)))
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1084
1085 if (likely(HTTP_IS_SPHT(*ptr))) {
1086 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1087 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1088 }
1089
1090 /* so it's a CR/LF, so there is no reason phrase */
1091 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1092 http_msg_rsp_reason:
1093 /* FIXME: should we support HTTP responses without any reason phrase ? */
1094 msg->sl.st.r = ptr - msg_buf;
1095 msg->sl.st.r_l = 0;
1096 goto http_msg_rpline_eol;
1097
1098 http_msg_rpcode_sp:
1099 case HTTP_MSG_RPCODE_SP:
1100 if (likely(!HTTP_IS_LWS(*ptr))) {
1101 msg->sl.st.r = ptr - msg_buf;
1102 goto http_msg_rpreason;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1106 /* so it's a CR/LF, so there is no reason phrase */
1107 goto http_msg_rsp_reason;
1108
1109 http_msg_rpreason:
1110 case HTTP_MSG_RPREASON:
1111 if (likely(!HTTP_IS_CRLF(*ptr)))
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1113 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1114 http_msg_rpline_eol:
1115 /* We have seen the end of line. Note that we do not
1116 * necessarily have the \n yet, but at least we know that we
1117 * have EITHER \r OR \n, otherwise the response would not be
1118 * complete. We can then record the response length and return
1119 * to the caller which will be able to register it.
1120 */
1121 msg->sl.st.l = ptr - msg->sol;
1122 return ptr;
1123
1124#ifdef DEBUG_FULL
1125 default:
1126 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1127 exit(1);
1128#endif
1129 }
1130
1131 http_msg_ood:
1132 /* out of data */
1133 if (ret_state)
1134 *ret_state = state;
1135 if (ret_ptr)
1136 *ret_ptr = (char *)ptr;
1137 return NULL;
1138
1139 http_msg_invalid:
1140 /* invalid message */
1141 if (ret_state)
1142 *ret_state = HTTP_MSG_ERROR;
1143 return NULL;
1144}
1145
1146
1147/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 * This function parses a request line between <ptr> and <end>, starting with
1149 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1150 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1151 * will give undefined results.
1152 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1153 * and that msg->sol points to the beginning of the request.
1154 * If a complete line is found (which implies that at least one CR or LF is
1155 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1156 * returned indicating an incomplete line (which does not mean that parts have
1157 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1158 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1159 * upon next call.
1160 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001161 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1163 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001164 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001166const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1167 unsigned int state, const char *ptr, const char *end,
1168 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 __label__
1171 http_msg_rqmeth,
1172 http_msg_rqmeth_sp,
1173 http_msg_rquri,
1174 http_msg_rquri_sp,
1175 http_msg_rqver,
1176 http_msg_rqline_eol,
1177 http_msg_ood, /* out of data */
1178 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 switch (state) {
1181 http_msg_rqmeth:
1182 case HTTP_MSG_RQMETH:
1183 if (likely(HTTP_IS_TOKEN(*ptr)))
1184 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001187 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1189 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001191 if (likely(HTTP_IS_CRLF(*ptr))) {
1192 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001193 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_req09_uri:
1195 msg->sl.rq.u = ptr - msg_buf;
1196 http_msg_req09_uri_e:
1197 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1198 http_msg_req09_ver:
1199 msg->sl.rq.v = ptr - msg_buf;
1200 msg->sl.rq.v_l = 0;
1201 goto http_msg_rqline_eol;
1202 }
1203 goto http_msg_invalid;
1204
1205 http_msg_rqmeth_sp:
1206 case HTTP_MSG_RQMETH_SP:
1207 if (likely(!HTTP_IS_LWS(*ptr))) {
1208 msg->sl.rq.u = ptr - msg_buf;
1209 goto http_msg_rquri;
1210 }
1211 if (likely(HTTP_IS_SPHT(*ptr)))
1212 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1213 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1214 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001215
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 http_msg_rquri:
1217 case HTTP_MSG_RQURI:
1218 if (likely(!HTTP_IS_LWS(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001220
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221 if (likely(HTTP_IS_SPHT(*ptr))) {
1222 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1227 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 http_msg_rquri_sp:
1230 case HTTP_MSG_RQURI_SP:
1231 if (likely(!HTTP_IS_LWS(*ptr))) {
1232 msg->sl.rq.v = ptr - msg_buf;
1233 goto http_msg_rqver;
1234 }
1235 if (likely(HTTP_IS_SPHT(*ptr)))
1236 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1237 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1238 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 http_msg_rqver:
1241 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001242 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244
1245 if (likely(HTTP_IS_CRLF(*ptr))) {
1246 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1247 http_msg_rqline_eol:
1248 /* We have seen the end of line. Note that we do not
1249 * necessarily have the \n yet, but at least we know that we
1250 * have EITHER \r OR \n, otherwise the request would not be
1251 * complete. We can then record the request length and return
1252 * to the caller which will be able to register it.
1253 */
1254 msg->sl.rq.l = ptr - msg->sol;
1255 return ptr;
1256 }
1257
1258 /* neither an HTTP_VER token nor a CRLF */
1259 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261#ifdef DEBUG_FULL
1262 default:
1263 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1264 exit(1);
1265#endif
1266 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 http_msg_ood:
1269 /* out of data */
1270 if (ret_state)
1271 *ret_state = state;
1272 if (ret_ptr)
1273 *ret_ptr = (char *)ptr;
1274 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 http_msg_invalid:
1277 /* invalid message */
1278 if (ret_state)
1279 *ret_state = HTTP_MSG_ERROR;
1280 return NULL;
1281}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
1283
Willy Tarreau8973c702007-01-21 23:58:29 +01001284/*
1285 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 * when data are missing and recalled at the exact same location with no
1288 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1290 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1293{
1294 __label__
1295 http_msg_rqbefore,
1296 http_msg_rqbefore_cr,
1297 http_msg_rqmeth,
1298 http_msg_rqline_end,
1299 http_msg_hdr_first,
1300 http_msg_hdr_name,
1301 http_msg_hdr_l1_sp,
1302 http_msg_hdr_l1_lf,
1303 http_msg_hdr_l1_lws,
1304 http_msg_hdr_val,
1305 http_msg_hdr_l2_lf,
1306 http_msg_hdr_l2_lws,
1307 http_msg_complete_header,
1308 http_msg_last_lf,
1309 http_msg_ood, /* out of data */
1310 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001311
Willy Tarreaue69eada2008-01-27 00:34:10 +01001312 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001315 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 ptr = buf->lr;
1317 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (unlikely(ptr >= end))
1320 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 /*
1324 * First, states that are specific to the response only.
1325 * We check them first so that request and headers are
1326 * closer to each other (accessed more often).
1327 */
1328 http_msg_rpbefore:
1329 case HTTP_MSG_RPBEFORE:
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 Tarreau8973c702007-01-21 23:58:29 +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 Tarreau8973c702007-01-21 23:58:29 +01001339#else
1340 /* 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.
1346 */
1347 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 Tarreau8973c702007-01-21 23:58:29 +01001350 msg->sol = buf->data;
1351 ptr = buf->data;
1352 end = buf->r;
1353#endif
1354 }
1355 hdr_idx_init(idx);
1356 state = HTTP_MSG_RPVER;
1357 goto http_msg_rpver;
1358 }
1359
1360 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1361 goto http_msg_invalid;
1362
1363 if (unlikely(*ptr == '\n'))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1365 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1366 /* stop here */
1367
1368 http_msg_rpbefore_cr:
1369 case HTTP_MSG_RPBEFORE_CR:
1370 EXPECT_LF_HERE(ptr, http_msg_invalid);
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1372 /* stop here */
1373
1374 http_msg_rpver:
1375 case HTTP_MSG_RPVER:
1376 case HTTP_MSG_RPVER_SP:
1377 case HTTP_MSG_RPCODE:
1378 case HTTP_MSG_RPCODE_SP:
1379 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001380 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001381 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 if (unlikely(!ptr))
1383 return;
1384
1385 /* we have a full response and we know that we have either a CR
1386 * or an LF at <ptr>.
1387 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001388 //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 +01001389 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1390
1391 msg->sol = ptr;
1392 if (likely(*ptr == '\r'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1394 goto http_msg_rpline_end;
1395
1396 http_msg_rpline_end:
1397 case HTTP_MSG_RPLINE_END:
1398 /* msg->sol must point to the first of CR or LF. */
1399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1401 /* stop here */
1402
1403 /*
1404 * Second, states that are specific to the request only
1405 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 http_msg_rqbefore:
1407 case HTTP_MSG_RQBEFORE:
1408 if (likely(HTTP_IS_TOKEN(*ptr))) {
1409 if (likely(ptr == buf->data)) {
1410 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 } else {
1413#if PARSE_PRESERVE_EMPTY_LINES
1414 /* only skip empty leading lines, don't remove them */
1415 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001416 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 /* Remove empty leading lines, as recommended by
1419 * RFC2616. This takes a lot of time because we
1420 * must move all the buffer backwards, but this
1421 * is rarely needed. The method above will be
1422 * cleaner when we'll be able to start sending
1423 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 buf->lr = ptr;
1426 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001427 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 msg->sol = buf->data;
1429 ptr = buf->data;
1430 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001433 /* we will need this when keep-alive will be supported
1434 hdr_idx_init(idx);
1435 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 state = HTTP_MSG_RQMETH;
1437 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1441 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (unlikely(*ptr == '\n'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1445 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 http_msg_rqbefore_cr:
1449 case HTTP_MSG_RQBEFORE_CR:
1450 EXPECT_LF_HERE(ptr, http_msg_invalid);
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 http_msg_rqmeth:
1455 case HTTP_MSG_RQMETH:
1456 case HTTP_MSG_RQMETH_SP:
1457 case HTTP_MSG_RQURI:
1458 case HTTP_MSG_RQURI_SP:
1459 case HTTP_MSG_RQVER:
1460 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001461 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(!ptr))
1463 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* we have a full request and we know that we have either a CR
1466 * or an LF at <ptr>.
1467 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 //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 +01001469 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 msg->sol = ptr;
1472 if (likely(*ptr == '\r'))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 http_msg_rqline_end:
1477 case HTTP_MSG_RQLINE_END:
1478 /* check for HTTP/0.9 request : no version information available.
1479 * msg->sol must point to the first of CR or LF.
1480 */
1481 if (unlikely(msg->sl.rq.v_l == 0))
1482 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 /*
1489 * Common states below
1490 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_hdr_first:
1492 case HTTP_MSG_HDR_FIRST:
1493 msg->sol = ptr;
1494 if (likely(!HTTP_IS_CRLF(*ptr))) {
1495 goto http_msg_hdr_name;
1496 }
1497
1498 if (likely(*ptr == '\r'))
1499 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1500 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 http_msg_hdr_name:
1503 case HTTP_MSG_HDR_NAME:
1504 /* assumes msg->sol points to the first char */
1505 if (likely(HTTP_IS_TOKEN(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(*ptr == ':')) {
1509 msg->col = ptr - buf->data;
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 http_msg_hdr_l1_sp:
1516 case HTTP_MSG_HDR_L1_SP:
1517 /* assumes msg->sol points to the first char and msg->col to the colon */
1518 if (likely(HTTP_IS_SPHT(*ptr)))
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 /* header value can be basically anything except CR/LF */
1522 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (likely(!HTTP_IS_CRLF(*ptr))) {
1525 goto http_msg_hdr_val;
1526 }
1527
1528 if (likely(*ptr == '\r'))
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1530 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 http_msg_hdr_l1_lf:
1533 case HTTP_MSG_HDR_L1_LF:
1534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_hdr_l1_lws:
1538 case HTTP_MSG_HDR_L1_LWS:
1539 if (likely(HTTP_IS_SPHT(*ptr))) {
1540 /* replace HT,CR,LF with spaces */
1541 for (; buf->data+msg->sov < ptr; msg->sov++)
1542 buf->data[msg->sov] = ' ';
1543 goto http_msg_hdr_l1_sp;
1544 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001545 /* we had a header consisting only in spaces ! */
1546 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 goto http_msg_complete_header;
1548
1549 http_msg_hdr_val:
1550 case HTTP_MSG_HDR_VAL:
1551 /* assumes msg->sol points to the first char, msg->col to the
1552 * colon, and msg->sov points to the first character of the
1553 * value.
1554 */
1555 if (likely(!HTTP_IS_CRLF(*ptr)))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 msg->eol = ptr;
1559 /* Note: we could also copy eol into ->eoh so that we have the
1560 * real header end in case it ends with lots of LWS, but is this
1561 * really needed ?
1562 */
1563 if (likely(*ptr == '\r'))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1565 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 http_msg_hdr_l2_lf:
1568 case HTTP_MSG_HDR_L2_LF:
1569 EXPECT_LF_HERE(ptr, http_msg_invalid);
1570 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 http_msg_hdr_l2_lws:
1573 case HTTP_MSG_HDR_L2_LWS:
1574 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1575 /* LWS: replace HT,CR,LF with spaces */
1576 for (; msg->eol < ptr; msg->eol++)
1577 *msg->eol = ' ';
1578 goto http_msg_hdr_val;
1579 }
1580 http_msg_complete_header:
1581 /*
1582 * It was a new header, so the last one is finished.
1583 * Assumes msg->sol points to the first char, msg->col to the
1584 * colon, msg->sov points to the first character of the value
1585 * and msg->eol to the first CR or LF so we know how the line
1586 * ends. We insert last header into the index.
1587 */
1588 /*
1589 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1590 write(2, msg->sol, msg->eol-msg->sol);
1591 fprintf(stderr,"\n");
1592 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1595 idx, idx->tail) < 0))
1596 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 msg->sol = ptr;
1599 if (likely(!HTTP_IS_CRLF(*ptr))) {
1600 goto http_msg_hdr_name;
1601 }
1602
1603 if (likely(*ptr == '\r'))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1605 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 http_msg_last_lf:
1608 case HTTP_MSG_LAST_LF:
1609 /* Assumes msg->sol points to the first of either CR or LF */
1610 EXPECT_LF_HERE(ptr, http_msg_invalid);
1611 ptr++;
1612 buf->lr = ptr;
1613 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001614 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 return;
1616#ifdef DEBUG_FULL
1617 default:
1618 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1619 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001620#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 }
1622 http_msg_ood:
1623 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 buf->lr = ptr;
1626 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 http_msg_invalid:
1629 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001630 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 return;
1632}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001633
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001634/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001635 * It normally returns zero, but may return 1 if it absolutely needs to be
1636 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001637 * t->req->analysers. It might make sense to explode it into several other
1638 * functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001640int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 struct buffer *req = t->req;
1643 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001644
Willy Tarreau2df28e82008-08-17 15:20:19 +02001645 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02001646 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001647 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001648 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1649 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02001650 req->rex, rep->wex, req->flags, rep->flags, req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreaudafde432008-08-17 01:00:46 +02001652 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02001653 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001654 struct tcp_rule *rule;
1655 int partial;
1656
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001657 /* We will abort if we encounter a read error. In theory, we
1658 * should not abort if we get a close, it might be valid,
1659 * although very unlikely. FIXME: we'll abort for now, this
1660 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001661 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001662 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001663 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001664 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001665 t->fe->failed_req++;
1666 if (!(t->flags & SN_ERR_MASK))
1667 t->flags |= SN_ERR_CLICL;
1668 if (!(t->flags & SN_FINST_MASK))
1669 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001670 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001671 }
1672
1673 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001674 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001675 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001676 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001677 t->fe->failed_req++;
1678 if (!(t->flags & SN_ERR_MASK))
1679 t->flags |= SN_ERR_CLITO;
1680 if (!(t->flags & SN_FINST_MASK))
1681 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001682 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001683 }
1684
1685 /* We don't know whether we have enough data, so must proceed
1686 * this way :
1687 * - iterate through all rules in their declaration order
1688 * - if one rule returns MISS, it means the inspect delay is
1689 * not over yet, then return immediately, otherwise consider
1690 * it as a non-match.
1691 * - if one rule returns OK, then return OK
1692 * - if one rule returns KO, then return KO
1693 */
1694
Willy Tarreauba392ce2008-08-16 21:13:23 +02001695 if (req->flags & (BF_READ_NULL | BF_SHUTR) ||
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001696 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001697 partial = 0;
1698 else
1699 partial = ACL_PARTIAL;
1700
1701 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1702 int ret = ACL_PAT_PASS;
1703
1704 if (rule->cond) {
1705 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1706 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001707 /* just set the request timeout once at the beginning of the request */
1708 if (!tick_isset(t->inspect_exp))
1709 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001710 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001711 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001712
Willy Tarreaub6866442008-07-14 23:54:42 +02001713 ret = acl_pass(ret);
1714 if (rule->cond->pol == ACL_COND_UNLESS)
1715 ret = !ret;
1716 }
1717
1718 if (ret) {
1719 /* we have a matching rule. */
1720 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02001721 buffer_shutr(req);
1722 buffer_shutw(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001723 fd_delete(t->cli_fd);
1724 t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001725 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001726 t->fe->failed_req++;
1727 if (!(t->flags & SN_ERR_MASK))
1728 t->flags |= SN_ERR_PRXCOND;
1729 if (!(t->flags & SN_FINST_MASK))
1730 t->flags |= SN_FINST_R;
1731 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02001732 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001733 }
1734 /* otherwise accept */
1735 break;
1736 }
1737 }
1738
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001739 /* if we get there, it means we have no rule which matches, or
1740 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001741 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001742 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001743 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001744 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001745
Willy Tarreau2df28e82008-08-17 15:20:19 +02001746 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 /*
1748 * Now parse the partial (or complete) lines.
1749 * We will check the request syntax, and also join multi-line
1750 * headers. An index of all the lines will be elaborated while
1751 * parsing.
1752 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001753 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 * req->data + req->eoh = end of processed headers / start of current one
1758 * req->data + req->eol = end of current header or line (LF or CRLF)
1759 * req->lr = first non-visited byte
1760 * req->r = end of data
1761 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001764 struct http_txn *txn = &t->txn;
1765 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001769 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 /* 1: we might have to print this header in debug mode */
1772 if (unlikely((global.mode & MODE_DEBUG) &&
1773 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001774 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001776
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001777 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 eol = sol + msg->sl.rq.l;
1779 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001780
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001781 sol += hdr_idx_first_pos(&txn->hdr_idx);
1782 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001785 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001787 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1788 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 }
1790 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
1793 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001795 * If not so, we check the FD and buffer states before leaving.
1796 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1798 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001799 *
1800 */
1801
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001802 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 /*
1804 * First, let's catch bad requests.
1805 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001806 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001808
1809 /* 1: Since we are in header mode, if there's no space
1810 * left for headers, we won't be able to free more
1811 * later, so the session will never terminate. We
1812 * must terminate it now.
1813 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001814 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 /* FIXME: check if URI is set and return Status
1816 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817 */
Willy Tarreau06619262006-12-17 08:37:22 +01001818 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819 }
1820
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001821 /* 2: have we encountered a close ? */
1822 else if (req->flags & BF_READ_NULL) {
1823 txn->status = 400;
1824 client_retnclose(t, error_message(t, HTTP_ERR_400));
1825 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001826 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001827 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001828
Willy Tarreau58f10d72006-12-04 02:26:12 +01001829 if (!(t->flags & SN_ERR_MASK))
1830 t->flags |= SN_ERR_CLICL;
1831 if (!(t->flags & SN_FINST_MASK))
1832 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001833 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001834 }
1835
1836 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001837 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001838 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001839 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001840 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001841 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001842 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001843 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001844 if (!(t->flags & SN_ERR_MASK))
1845 t->flags |= SN_ERR_CLITO;
1846 if (!(t->flags & SN_FINST_MASK))
1847 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001848 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001849 }
1850
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001851 /* 4: have we encountered a read error or did we have to shutdown ? */
Willy Tarreauba392ce2008-08-16 21:13:23 +02001852 else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001853 /* we cannot return any message on error */
1854 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001855 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001856 t->fe->failed_req++;
1857 if (!(t->flags & SN_ERR_MASK))
1858 t->flags |= SN_ERR_CLICL;
1859 if (!(t->flags & SN_FINST_MASK))
1860 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001861 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001862 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001863
1864 /* just set the request timeout once at the beginning of the request */
1865 if (!tick_isset(t->txn.exp))
1866 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1867
1868 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001869 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001870 }
1871
1872
1873 /****************************************************************
1874 * More interesting part now : we know that we have a complete *
1875 * request which at least looks like HTTP. We have an indicator *
1876 * of each header's length, so we can parse them quickly. *
1877 ****************************************************************/
1878
Willy Tarreau2df28e82008-08-17 15:20:19 +02001879 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001880
Willy Tarreau9cdde232007-05-02 20:58:19 +02001881 /* ensure we keep this pointer to the beginning of the message */
1882 msg->sol = req->data + msg->som;
1883
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 /*
1885 * 1: identify the method
1886 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001887 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001888
1889 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001891 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 if (unlikely((t->fe->monitor_uri_len != 0) &&
1895 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1896 !memcmp(&req->data[msg->sl.rq.u],
1897 t->fe->monitor_uri,
1898 t->fe->monitor_uri_len))) {
1899 /*
1900 * We have found the monitor URI
1901 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001902 struct acl_cond *cond;
1903 cur_proxy = t->fe;
1904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001906
1907 /* Check if we want to fail this monitor request or not */
1908 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1909 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001910
1911 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001912 if (cond->pol == ACL_COND_UNLESS)
1913 ret = !ret;
1914
1915 if (ret) {
1916 /* we fail this request, let's return 503 service unavail */
1917 txn->status = 503;
1918 client_retnclose(t, error_message(t, HTTP_ERR_503));
1919 goto return_prx_cond;
1920 }
1921 }
1922
1923 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001924 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 client_retnclose(t, &http_200_chunk);
1926 goto return_prx_cond;
1927 }
1928
1929 /*
1930 * 3: Maybe we have to copy the original REQURI for the logs ?
1931 * Note: we cannot log anymore if the request has been
1932 * classified as invalid.
1933 */
1934 if (unlikely(t->logs.logwait & LW_REQ)) {
1935 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001936 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 int urilen = msg->sl.rq.l;
1938
1939 if (urilen >= REQURI_LEN)
1940 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001941 memcpy(txn->uri, &req->data[msg->som], urilen);
1942 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943
1944 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001945 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 } else {
1947 Alert("HTTP logging : out of memory.\n");
1948 }
1949 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001950
Willy Tarreau06619262006-12-17 08:37:22 +01001951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001952 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1953 if (unlikely(msg->sl.rq.v_l == 0)) {
1954 int delta;
1955 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001956 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 cur_end = msg->sol + msg->sl.rq.l;
1958 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 if (msg->sl.rq.u_l == 0) {
1961 /* if no URI was set, add "/" */
1962 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1963 cur_end += delta;
1964 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001965 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 /* add HTTP version */
1967 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1968 msg->eoh += delta;
1969 cur_end += delta;
1970 cur_end = (char *)http_parse_reqline(msg, req->data,
1971 HTTP_MSG_RQMETH,
1972 msg->sol, cur_end + 1,
1973 NULL, NULL);
1974 if (unlikely(!cur_end))
1975 goto return_bad_req;
1976
1977 /* we have a full HTTP/1.0 request now and we know that
1978 * we have either a CR or an LF at <ptr>.
1979 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001980 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981 }
1982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983
1984 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001985 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001986 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001987 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001988
1989 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001991 * As opposed to version 1.2, now they will be evaluated in the
1992 * filters order and not in the header order. This means that
1993 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001994 *
1995 * We can now check whether we want to switch to another
1996 * backend, in which case we will re-check the backend's
1997 * filters and various options. In order to support 3-level
1998 * switching, here's how we should proceed :
1999 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002000 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002001 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002002 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002003 * There cannot be any switch from there, so ->be cannot be
2004 * changed anymore.
2005 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002006 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002007 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002008 * The response path will be able to apply either ->be, or
2009 * ->be then ->fe filters in order to match the reverse of
2010 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002011 */
2012
Willy Tarreau06619262006-12-17 08:37:22 +01002013 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002014 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002015 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002016 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002017 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002018
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002019 /* first check whether we have some ACLs set to redirect this request */
2020 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2021 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002022
2023 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002024 if (rule->cond->pol == ACL_COND_UNLESS)
2025 ret = !ret;
2026
2027 if (ret) {
2028 struct chunk rdr = { trash, 0 };
2029 const char *msg_fmt;
2030
2031 /* build redirect message */
2032 switch(rule->code) {
2033 case 303:
2034 rdr.len = strlen(HTTP_303);
2035 msg_fmt = HTTP_303;
2036 break;
2037 case 301:
2038 rdr.len = strlen(HTTP_301);
2039 msg_fmt = HTTP_301;
2040 break;
2041 case 302:
2042 default:
2043 rdr.len = strlen(HTTP_302);
2044 msg_fmt = HTTP_302;
2045 break;
2046 }
2047
2048 if (unlikely(rdr.len > sizeof(trash)))
2049 goto return_bad_req;
2050 memcpy(rdr.str, msg_fmt, rdr.len);
2051
2052 switch(rule->type) {
2053 case REDIRECT_TYPE_PREFIX: {
2054 const char *path;
2055 int pathlen;
2056
2057 path = http_get_path(txn);
2058 /* build message using path */
2059 if (path) {
2060 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2061 } else {
2062 path = "/";
2063 pathlen = 1;
2064 }
2065
2066 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2067 goto return_bad_req;
2068
2069 /* add prefix */
2070 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2071 rdr.len += rule->rdr_len;
2072
2073 /* add path */
2074 memcpy(rdr.str + rdr.len, path, pathlen);
2075 rdr.len += pathlen;
2076 break;
2077 }
2078 case REDIRECT_TYPE_LOCATION:
2079 default:
2080 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2081 goto return_bad_req;
2082
2083 /* add location */
2084 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2085 rdr.len += rule->rdr_len;
2086 break;
2087 }
2088
2089 /* add end of headers */
2090 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2091 rdr.len += 4;
2092
2093 txn->status = rule->code;
2094 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002095 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002096 client_retnclose(t, &rdr);
2097 goto return_prx_cond;
2098 }
2099 }
2100
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002101 /* first check whether we have some ACLs set to block this request */
2102 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002103 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002104
2105 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002106 if (cond->pol == ACL_COND_UNLESS)
2107 ret = !ret;
2108
2109 if (ret) {
2110 txn->status = 403;
2111 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002112 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002113 client_retnclose(t, error_message(t, HTTP_ERR_403));
2114 goto return_prx_cond;
2115 }
2116 }
2117
Willy Tarreau06619262006-12-17 08:37:22 +01002118 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002119 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002120 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2121 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002122 }
2123
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002124 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2125 /* to ensure correct connection accounting on
2126 * the backend, we count the connection for the
2127 * one managing the queue.
2128 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002129 t->be->beconn++;
2130 if (t->be->beconn > t->be->beconn_max)
2131 t->be->beconn_max = t->be->beconn;
2132 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002133 t->flags |= SN_BE_ASSIGNED;
2134 }
2135
Willy Tarreau06619262006-12-17 08:37:22 +01002136 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002137 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002138 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002139 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002140 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002141 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002142 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002143 goto return_prx_cond;
2144 }
2145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002146 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002147 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002148 !(t->flags & SN_CONN_CLOSED)) {
2149 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002150 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002151 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002152
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002153 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002154 old_idx = 0;
2155
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002156 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2157 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002158 cur_ptr = cur_next;
2159 cur_end = cur_ptr + cur_hdr->len;
2160 cur_next = cur_end + cur_hdr->cr + 1;
2161
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002162 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2163 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002164 /* 3 possibilities :
2165 * - we have already set Connection: close,
2166 * so we remove this line.
2167 * - we have not yet set Connection: close,
2168 * but this line indicates close. We leave
2169 * it untouched and set the flag.
2170 * - we have not yet set Connection: close,
2171 * and this line indicates non-close. We
2172 * replace it.
2173 */
2174 if (t->flags & SN_CONN_CLOSED) {
2175 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002176 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002177 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002178 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2179 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002180 cur_hdr->len = 0;
2181 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002182 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2183 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2184 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002185 cur_next += delta;
2186 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002187 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002188 }
2189 t->flags |= SN_CONN_CLOSED;
2190 }
2191 }
2192 old_idx = cur_idx;
2193 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002194 }
2195 /* add request headers from the rule sets in the same order */
2196 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2197 if (unlikely(http_header_add_tail(req,
2198 &txn->req,
2199 &txn->hdr_idx,
2200 rule_set->req_add[cur_idx])) < 0)
2201 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002202 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002203
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002204 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002205 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002206 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002207 /* we have to check the URI and auth for this request.
2208 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002209 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002210 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002211 if (stats_check_uri_auth(t, rule_set))
2212 return 1;
2213 }
2214
Willy Tarreau55ea7572007-06-17 19:56:27 +02002215 /* now check whether we have some switching rules for this request */
2216 if (!(t->flags & SN_BE_ASSIGNED)) {
2217 struct switching_rule *rule;
2218
2219 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2220 int ret;
2221
2222 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002223
2224 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002225 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002226 ret = !ret;
2227
2228 if (ret) {
2229 t->be = rule->be.backend;
2230 t->be->beconn++;
2231 if (t->be->beconn > t->be->beconn_max)
2232 t->be->beconn_max = t->be->beconn;
2233 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002234
2235 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002236 t->rep->rto = t->req->wto = t->be->timeout.server;
2237 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002238 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002239 t->flags |= SN_BE_ASSIGNED;
2240 break;
2241 }
2242 }
2243 }
2244
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002245 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2246 /* No backend was set, but there was a default
2247 * backend set in the frontend, so we use it and
2248 * loop again.
2249 */
2250 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002251 t->be->beconn++;
2252 if (t->be->beconn > t->be->beconn_max)
2253 t->be->beconn_max = t->be->beconn;
2254 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002255
2256 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002257 t->rep->rto = t->req->wto = t->be->timeout.server;
2258 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002259 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002260 t->flags |= SN_BE_ASSIGNED;
2261 }
2262 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002263
Willy Tarreau58f10d72006-12-04 02:26:12 +01002264
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002265 if (!(t->flags & SN_BE_ASSIGNED)) {
2266 /* To ensure correct connection accounting on
2267 * the backend, we count the connection for the
2268 * one managing the queue.
2269 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002270 t->be->beconn++;
2271 if (t->be->beconn > t->be->beconn_max)
2272 t->be->beconn_max = t->be->beconn;
2273 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002274 t->flags |= SN_BE_ASSIGNED;
2275 }
2276
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002277 /*
2278 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002279 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002280 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002281 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002282 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002284 /*
2285 * If HTTP PROXY is set we simply get remote server address
2286 * parsing incoming request.
2287 */
2288 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2289 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002291
Willy Tarreau2a324282006-12-05 00:05:46 +01002292 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002293 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002294 * so let's do the same now.
2295 */
2296
2297 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002298 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002299 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002300 }
2301
2302
2303 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002304 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002305 * Note that doing so might move headers in the request, but
2306 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002307 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002309 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2310 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002311 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312
Willy Tarreau58f10d72006-12-04 02:26:12 +01002313
Willy Tarreau2a324282006-12-05 00:05:46 +01002314 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002315 * 9: add X-Forwarded-For if either the frontend or the backend
2316 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002317 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002318 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002319 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002320 /* Add an X-Forwarded-For header unless the source IP is
2321 * in the 'except' network range.
2322 */
2323 if ((!t->fe->except_mask.s_addr ||
2324 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2325 != t->fe->except_net.s_addr) &&
2326 (!t->be->except_mask.s_addr ||
2327 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2328 != t->be->except_net.s_addr)) {
2329 int len;
2330 unsigned char *pn;
2331 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002332
Ross Westaf72a1d2008-08-03 10:51:45 +02002333 /* Note: we rely on the backend to get the header name to be used for
2334 * x-forwarded-for, because the header is really meant for the backends.
2335 * However, if the backend did not specify any option, we have to rely
2336 * on the frontend's header name.
2337 */
2338 if (t->be->fwdfor_hdr_len) {
2339 len = t->be->fwdfor_hdr_len;
2340 memcpy(trash, t->be->fwdfor_hdr_name, len);
2341 } else {
2342 len = t->fe->fwdfor_hdr_len;
2343 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2344 }
2345 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002346
Ross Westaf72a1d2008-08-03 10:51:45 +02002347 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002348 &txn->hdr_idx, trash, len)) < 0)
2349 goto return_bad_req;
2350 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002351 }
2352 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002353 /* FIXME: for the sake of completeness, we should also support
2354 * 'except' here, although it is mostly useless in this case.
2355 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002356 int len;
2357 char pn[INET6_ADDRSTRLEN];
2358 inet_ntop(AF_INET6,
2359 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2360 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002361
2362 /* Note: we rely on the backend to get the header name to be used for
2363 * x-forwarded-for, because the header is really meant for the backends.
2364 * However, if the backend did not specify any option, we have to rely
2365 * on the frontend's header name.
2366 */
2367 if (t->be->fwdfor_hdr_len) {
2368 len = t->be->fwdfor_hdr_len;
2369 memcpy(trash, t->be->fwdfor_hdr_name, len);
2370 } else {
2371 len = t->fe->fwdfor_hdr_len;
2372 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2373 }
2374 len += sprintf(trash + len, ": %s", pn);
2375
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002376 if (unlikely(http_header_add_tail2(req, &txn->req,
2377 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002378 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002379 }
2380 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381
Willy Tarreau2a324282006-12-05 00:05:46 +01002382 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002383 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002384 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002385 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002386 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002387 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002388 if ((unlikely(msg->sl.rq.v_l != 8) ||
2389 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2390 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002391 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002392 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002393 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002394 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002395 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2396 * If not assigned, perhaps we are balancing on url_param, but this is a
2397 * POST; and the parameters are in the body, maybe scan there to find our server.
2398 * (unless headers overflowed the buffer?)
2399 */
2400 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2401 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002402 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002403 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2404 /* are there enough bytes here? total == l || r || rlim ?
2405 * len is unsigned, but eoh is int,
2406 * how many bytes of body have we received?
2407 * eoh is the first empty line of the header
2408 */
2409 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002410 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 +02002411
2412 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2413 * We can't assume responsibility for the server's decision,
2414 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2415 * We also can't change our mind later, about which server to choose, so round robin.
2416 */
2417 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2418 struct hdr_ctx ctx;
2419 ctx.idx = 0;
2420 /* Expect is allowed in 1.1, look for it */
2421 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2422 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002423 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002424 /* We can't reliablly stall and wait for data, because of
2425 * .NET clients that don't conform to rfc2616; so, no need for
2426 * the next block to check length expectations.
2427 * We could send 100 status back to the client, but then we need to
2428 * re-write headers, and send the message. And this isn't the right
2429 * place for that action.
2430 * TODO: support Expect elsewhere and delete this block.
2431 */
2432 goto end_check_maybe_wait_for_body;
2433 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002434
2435 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002436 /* nothing to do, we got enough */
2437 } else {
2438 /* limit implies we are supposed to need this many bytes
2439 * to find the parameter. Let's see how many bytes we can wait for.
2440 */
2441 long long hint = len;
2442 struct hdr_ctx ctx;
2443 ctx.idx = 0;
2444 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002445 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002446 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002447 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002448 ctx.idx = 0;
2449 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2450 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002451 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002452 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002453 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002454 hint = 0; /* parse failure, untrusted client */
2455 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002456 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002457 msg->hdr_content_len = hint;
2458 else
2459 hint = 0; /* bad client, sent negative length */
2460 }
2461 }
2462 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002463 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002464 hint = t->be->url_param_post_limit;
2465 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002466 if (len < hint)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002467 req->analysers |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002468 /* else... There are no body bytes to wait for */
2469 }
2470 }
2471 }
2472 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002473
Willy Tarreau2a324282006-12-05 00:05:46 +01002474 /*************************************************************
2475 * OK, that's finished for the headers. We have done what we *
2476 * could. Let's switch to the DATA state. *
2477 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002478
Willy Tarreaue393fe22008-08-16 22:18:07 +02002479 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002480 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002481
Willy Tarreau1fa31262007-12-03 00:36:16 +01002482 /* When a connection is tarpitted, we use the tarpit timeout,
2483 * which may be the same as the connect timeout if unspecified.
2484 * If unset, then set it to zero because we really want it to
2485 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002486 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002487 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002488 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002489 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002490 /* flush the request so that we can drop the connection early
2491 * if the client closes first.
2492 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02002493 req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2494 if (!req->wex)
2495 req->wex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002496 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002498 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002499 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002500
2501 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002502 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002503 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002504 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002505 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002506 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002507 return_prx_cond:
2508 if (!(t->flags & SN_ERR_MASK))
2509 t->flags |= SN_ERR_PRXCOND;
2510 if (!(t->flags & SN_FINST_MASK))
2511 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002512 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002513 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002514 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002515 }
2516
Willy Tarreau2df28e82008-08-17 15:20:19 +02002517 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002518 /* We have to parse the HTTP request body to find any required data.
2519 * "balance url_param check_post" should have been the only way to get
2520 * into this. We were brought here after HTTP header analysis, so all
2521 * related structures are ready.
2522 */
2523 struct http_msg *msg = &t->txn.req;
2524 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2525 long long limit = t->be->url_param_post_limit;
2526 struct hdr_ctx ctx;
2527
2528 ctx.idx = 0;
2529
2530 /* now if we have a length, we'll take the hint */
2531 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2532 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2533 unsigned int chunk = 0;
2534 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2535 char c = msg->sol[body];
2536 if (ishex(c)) {
2537 unsigned int hex = toupper(c) - '0';
2538 if (hex > 9)
2539 hex -= 'A' - '9' - 1;
2540 chunk = (chunk << 4) | hex;
2541 } else
2542 break;
2543 body++;
2544 }
2545 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002546 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002547
2548 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002549 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002550
2551 body += 2; // skip CRLF
2552
2553 /* if we support more then one chunk here, we have to do it again when assigning server
2554 * 1. how much entity data do we have? new var
2555 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2556 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2557 */
2558
2559 if (chunk < limit)
2560 limit = chunk; /* only reading one chunk */
2561 } else {
2562 if (msg->hdr_content_len < limit)
2563 limit = msg->hdr_content_len;
2564 }
2565
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002566 http_body_end:
2567 /* we leave once we know we have nothing left to do. This means that we have
2568 * enough bytes, or that we know we'll not get any more (buffer full, read
2569 * buffer closed).
2570 */
2571 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002572 req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002573 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002574 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002575 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002576 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002577 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002578
Willy Tarreau2df28e82008-08-17 15:20:19 +02002579 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2580 * probably reduce one day's debugging session.
2581 */
2582#ifdef DEBUG_DEV
2583 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY)) {
2584 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2585 __FILE__, __LINE__, req->analysers);
2586 ABORT_NOW();
2587 }
2588#endif
2589 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY;
2590
Willy Tarreaudafde432008-08-17 01:00:46 +02002591 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002592 if (unlikely(req->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02002593 goto update_state;
2594 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002595}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002596
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002597/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002598 * It normally returns zero, but may return 1 if it absolutely needs to be
2599 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002600 * t->rep->analysers. It might make sense to explode it into several other
2601 * functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002602 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002604{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002605 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002606 struct buffer *req = t->req;
2607 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002608
Willy Tarreau2df28e82008-08-17 15:20:19 +02002609 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002610 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002611 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02002612 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
2613 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02002614 req->rex, rep->wex, req->flags, rep->flags, rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002615
Willy Tarreaudafde432008-08-17 01:00:46 +02002616 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02002617 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002618 /*
2619 * Now parse the partial (or complete) lines.
2620 * We will check the response syntax, and also join multi-line
2621 * headers. An index of all the lines will be elaborated while
2622 * parsing.
2623 *
2624 * For the parsing, we use a 28 states FSM.
2625 *
2626 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002627 * rep->data + rep->som = beginning of response
2628 * rep->data + rep->eoh = end of processed headers / start of current one
2629 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002630 * rep->lr = first non-visited byte
2631 * rep->r = end of data
2632 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002633
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002634 int cur_idx;
2635 struct http_msg *msg = &txn->rsp;
2636 struct proxy *cur_proxy;
2637
2638 if (likely(rep->lr < rep->r))
2639 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2640
2641 /* 1: we might have to print this header in debug mode */
2642 if (unlikely((global.mode & MODE_DEBUG) &&
2643 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2644 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2645 char *eol, *sol;
2646
2647 sol = rep->data + msg->som;
2648 eol = sol + msg->sl.rq.l;
2649 debug_hdr("srvrep", t, sol, eol);
2650
2651 sol += hdr_idx_first_pos(&txn->hdr_idx);
2652 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2653
2654 while (cur_idx) {
2655 eol = sol + txn->hdr_idx.v[cur_idx].len;
2656 debug_hdr("srvhdr", t, sol, eol);
2657 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2658 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002659 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002662 /*
2663 * Now we quickly check if we have found a full valid response.
2664 * If not so, we check the FD and buffer states before leaving.
2665 * A full response is indicated by the fact that we have seen
2666 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2667 * responses are checked first.
2668 *
2669 * Depending on whether the client is still there or not, we
2670 * may send an error response back or not. Note that normally
2671 * we should only check for HTTP status there, and check I/O
2672 * errors somewhere else.
2673 */
2674
2675 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002676 /* Invalid response */
2677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2678 hdr_response_bad:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002679 buffer_shutr(rep);
2680 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002681 fd_delete(t->srv_fd);
2682 if (t->srv) {
2683 t->srv->cur_sess--;
2684 t->srv->failed_resp++;
2685 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002687 t->be->failed_resp++;
2688 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002689 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002690 txn->status = 502;
2691 client_return(t, error_message(t, HTTP_ERR_502));
2692 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002693 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002694 if (!(t->flags & SN_FINST_MASK))
2695 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002697 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2698 process_srv_queue(t->srv);
2699
Willy Tarreaudafde432008-08-17 01:00:46 +02002700 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002701 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002702 /* write error to client, read error or close from server */
2703 if (req->flags & BF_WRITE_ERROR ||
Willy Tarreauba392ce2008-08-16 21:13:23 +02002704 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) {
2705 buffer_shutr(rep);
2706 buffer_shutw(req);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002707 fd_delete(t->srv_fd);
2708 if (t->srv) {
2709 t->srv->cur_sess--;
2710 t->srv->failed_resp++;
2711 sess_change_server(t, NULL);
2712 }
2713 t->be->failed_resp++;
2714 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002715 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002716 txn->status = 502;
2717 client_return(t, error_message(t, HTTP_ERR_502));
2718 if (!(t->flags & SN_ERR_MASK))
2719 t->flags |= SN_ERR_SRVCL;
2720 if (!(t->flags & SN_FINST_MASK))
2721 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002722
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002723 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2724 process_srv_queue(t->srv);
2725
Willy Tarreaudafde432008-08-17 01:00:46 +02002726 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002728 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002729 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002730 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002731 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002732 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002733 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02002734 buffer_shutr(rep);
2735 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 fd_delete(t->srv_fd);
2737 if (t->srv) {
2738 t->srv->cur_sess--;
2739 t->srv->failed_resp++;
2740 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002741 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002742 t->be->failed_resp++;
2743 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002744 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002745 txn->status = 504;
2746 client_return(t, error_message(t, HTTP_ERR_504));
2747 if (!(t->flags & SN_ERR_MASK))
2748 t->flags |= SN_ERR_SRVTO;
2749 if (!(t->flags & SN_FINST_MASK))
2750 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002751
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002752 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2753 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002754 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002756 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002757 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758
Willy Tarreau21d2af32008-02-14 20:25:24 +01002759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002760 /*****************************************************************
2761 * More interesting part now : we know that we have a complete *
2762 * response which at least looks like HTTP. We have an indicator *
2763 * of each header's length, so we can parse them quickly. *
2764 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002765
Willy Tarreau2df28e82008-08-17 15:20:19 +02002766 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002767
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002768 /* ensure we keep this pointer to the beginning of the message */
2769 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002770
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002771 /*
2772 * 1: get the status code and check for cacheability.
2773 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002775 t->logs.logwait &= ~LW_RESP;
2776 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002777
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002778 switch (txn->status) {
2779 case 200:
2780 case 203:
2781 case 206:
2782 case 300:
2783 case 301:
2784 case 410:
2785 /* RFC2616 @13.4:
2786 * "A response received with a status code of
2787 * 200, 203, 206, 300, 301 or 410 MAY be stored
2788 * by a cache (...) unless a cache-control
2789 * directive prohibits caching."
2790 *
2791 * RFC2616 @9.5: POST method :
2792 * "Responses to this method are not cacheable,
2793 * unless the response includes appropriate
2794 * Cache-Control or Expires header fields."
2795 */
2796 if (likely(txn->meth != HTTP_METH_POST) &&
2797 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2798 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2799 break;
2800 default:
2801 break;
2802 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002803
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002804 /*
2805 * 2: we may need to capture headers
2806 */
2807 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2808 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2809 txn->rsp.cap, t->fe->rsp_cap);
2810
2811 /*
2812 * 3: we will have to evaluate the filters.
2813 * As opposed to version 1.2, now they will be evaluated in the
2814 * filters order and not in the header order. This means that
2815 * each filter has to be validated among all headers.
2816 *
2817 * Filters are tried with ->be first, then with ->fe if it is
2818 * different from ->be.
2819 */
2820
2821 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2822
2823 cur_proxy = t->be;
2824 while (1) {
2825 struct proxy *rule_set = cur_proxy;
2826
2827 /* try headers filters */
2828 if (rule_set->rsp_exp != NULL) {
2829 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2830 return_bad_resp:
2831 if (t->srv) {
2832 t->srv->cur_sess--;
2833 t->srv->failed_resp++;
2834 sess_change_server(t, NULL);
2835 }
2836 cur_proxy->failed_resp++;
2837 return_srv_prx_502:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002838 buffer_shutr(rep);
2839 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002840 fd_delete(t->srv_fd);
2841 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002842 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002843 txn->status = 502;
2844 client_return(t, error_message(t, HTTP_ERR_502));
2845 if (!(t->flags & SN_ERR_MASK))
2846 t->flags |= SN_ERR_PRXCOND;
2847 if (!(t->flags & SN_FINST_MASK))
2848 t->flags |= SN_FINST_H;
2849 /* We used to have a free connection slot. Since we'll never use it,
2850 * we have to inform the server that it may be used by another session.
2851 */
2852 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2853 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002854 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002855 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002857
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002858 /* has the response been denied ? */
2859 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002860 if (t->srv) {
2861 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002863 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002864 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 cur_proxy->denied_resp++;
2866 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002867 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002868
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002869 /* We might have to check for "Connection:" */
2870 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2871 !(t->flags & SN_CONN_CLOSED)) {
2872 char *cur_ptr, *cur_end, *cur_next;
2873 int cur_idx, old_idx, delta, val;
2874 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002876 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2877 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002879 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2880 cur_hdr = &txn->hdr_idx.v[cur_idx];
2881 cur_ptr = cur_next;
2882 cur_end = cur_ptr + cur_hdr->len;
2883 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2886 if (val) {
2887 /* 3 possibilities :
2888 * - we have already set Connection: close,
2889 * so we remove this line.
2890 * - we have not yet set Connection: close,
2891 * but this line indicates close. We leave
2892 * it untouched and set the flag.
2893 * - we have not yet set Connection: close,
2894 * and this line indicates non-close. We
2895 * replace it.
2896 */
2897 if (t->flags & SN_CONN_CLOSED) {
2898 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2899 txn->rsp.eoh += delta;
2900 cur_next += delta;
2901 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2902 txn->hdr_idx.used--;
2903 cur_hdr->len = 0;
2904 } else {
2905 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2906 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2907 "close", 5);
2908 cur_next += delta;
2909 cur_hdr->len += delta;
2910 txn->rsp.eoh += delta;
2911 }
2912 t->flags |= SN_CONN_CLOSED;
2913 }
2914 }
2915 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002916 }
2917 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 /* add response headers from the rule sets in the same order */
2920 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2921 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2922 rule_set->rsp_add[cur_idx])) < 0)
2923 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002924 }
2925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 /* check whether we're already working on the frontend */
2927 if (cur_proxy == t->fe)
2928 break;
2929 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002931
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002932 /*
2933 * 4: check for server cookie.
2934 */
2935 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2936 || (t->be->options & PR_O_CHK_CACHE))
2937 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002938
Willy Tarreaubaaee002006-06-26 02:48:02 +02002939
Willy Tarreaua15645d2007-03-18 16:22:39 +01002940 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002941 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002943 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2944 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002946 /*
2947 * 6: add server cookie in the response if needed
2948 */
2949 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2950 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2951 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 /* the server is known, it's not the one the client requested, we have to
2954 * insert a set-cookie here, except if we want to insert only on POST
2955 * requests and this one isn't. Note that servers which don't have cookies
2956 * (eg: some backup servers) will return a full cookie removal request.
2957 */
2958 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2959 t->be->cookie_name,
2960 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002962 if (t->be->cookie_domain)
2963 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002965 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2966 trash, len)) < 0)
2967 goto return_bad_resp;
2968 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002970 /* Here, we will tell an eventual cache on the client side that we don't
2971 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2972 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2973 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2974 */
2975 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2978
2979 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2980 "Cache-control: private", 22)) < 0)
2981 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002982 }
2983 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002986 /*
2987 * 7: check if result will be cacheable with a cookie.
2988 * We'll block the response if security checks have caught
2989 * nasty things such as a cacheable cookie.
2990 */
2991 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2992 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2993 (t->be->options & PR_O_CHK_CACHE)) {
2994
2995 /* we're in presence of a cacheable response containing
2996 * a set-cookie header. We'll block it as requested by
2997 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002998 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002999 if (t->srv) {
3000 t->srv->cur_sess--;
3001 t->srv->failed_secu++;
3002 sess_change_server(t, NULL);
3003 }
3004 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003005
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003006 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3007 t->be->id, t->srv?t->srv->id:"<dispatch>");
3008 send_log(t->be, LOG_ALERT,
3009 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3010 t->be->id, t->srv?t->srv->id:"<dispatch>");
3011 goto return_srv_prx_502;
3012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013
3014 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003015 * 8: add "Connection: close" if needed and not yet set.
3016 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003017 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003018 if (!(t->flags & SN_CONN_CLOSED) &&
3019 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3020 if ((unlikely(msg->sl.st.v_l != 8) ||
3021 unlikely(req->data[msg->som + 7] != '0')) &&
3022 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3023 "Connection: close", 17)) < 0)
3024 goto return_bad_resp;
3025 t->flags |= SN_CONN_CLOSED;
3026 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003027
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003028 /*************************************************************
3029 * OK, that's finished for the headers. We have done what we *
3030 * could. Let's switch to the DATA state. *
3031 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032
Willy Tarreaue393fe22008-08-16 22:18:07 +02003033 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003036#ifdef CONFIG_HAP_TCPSPLICE
3037 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3038 /* TCP splicing supported by both FE and BE */
3039 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3040 }
3041#endif
3042 /* if the user wants to log as soon as possible, without counting
3043 * bytes from the server, then this is the right moment. We have
3044 * to temporarily assign bytes_out to log what we currently have.
3045 */
3046 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3047 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3048 t->logs.bytes_out = txn->rsp.eoh;
3049 if (t->fe->to_log & LW_REQ)
3050 http_sess_log(t);
3051 else
3052 tcp_sess_log(t);
3053 t->logs.bytes_out = 0;
3054 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003056 /* Note: we must not try to cheat by jumping directly to DATA,
3057 * otherwise we would not let the client side wake up.
3058 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003059
Willy Tarreaudafde432008-08-17 01:00:46 +02003060 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003061 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003062
Willy Tarreau2df28e82008-08-17 15:20:19 +02003063 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3064 * probably reduce one day's debugging session.
3065 */
3066#ifdef DEBUG_DEV
3067 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3068 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3069 __FILE__, __LINE__, rep->analysers);
3070 ABORT_NOW();
3071 }
3072#endif
3073 rep->analysers &= AN_RTR_HTTP_HDR;
3074
Willy Tarreaudafde432008-08-17 01:00:46 +02003075 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02003076 if (unlikely(rep->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02003077 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003078 return 0;
3079}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003081/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003082 * Manages the client FSM and its socket. It normally returns zero, but may
3083 * return 1 if it absolutely wants to be called again.
3084 *
3085 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003086 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 */
3088int process_cli(struct session *t)
3089{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003090 struct buffer *req = t->req;
3091 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003092
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003093 DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003094 now_ms,
3095 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003096 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3097 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003098 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003099 req->flags, rep->flags,
3100 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101
Willy Tarreaudafde432008-08-17 01:00:46 +02003102 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003103 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3104 * still set this state (and will do until unix sockets are converted).
3105 */
3106 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003107 /* we can skip most of the tests at once if some conditions are not met */
3108 if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3109 (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
3110 (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3111 (!(rep->flags & BF_SHUTW) &&
3112 (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3113 goto update_timeouts;
3114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003115 /* read or write error */
3116 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003117 buffer_shutr(req);
3118 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003119 fd_delete(t->cli_fd);
3120 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003121 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003122 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003123 if (!(t->flags & SN_ERR_MASK))
3124 t->flags |= SN_ERR_CLICL;
3125 if (!(t->flags & SN_FINST_MASK)) {
3126 if (t->pend_pos)
3127 t->flags |= SN_FINST_Q;
3128 else if (t->srv_state == SV_STCONN)
3129 t->flags |= SN_FINST_C;
3130 else
3131 t->flags |= SN_FINST_D;
3132 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003133 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003134 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003135 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003136 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003137 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003138 req->flags & (BF_READ_NULL | BF_SHUTW)) {
3139 buffer_shutr(req);
3140 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003141 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003142 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003143 } else {
3144 /* output was already closed */
3145 fd_delete(t->cli_fd);
3146 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003147 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003148 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003149 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003150 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003151 /* last server read and buffer empty : we only check them when we're
3152 * allowed to forward the data.
3153 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003154 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003155 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003156 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
3157 buffer_shutw(rep);
3158 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003159 EV_FD_CLR(t->cli_fd, DIR_WR);
3160 shutdown(t->cli_fd, SHUT_WR);
3161 /* We must ensure that the read part is still alive when switching to shutw */
3162 /* FIXME: is this still true ? */
3163 EV_FD_SET(t->cli_fd, DIR_RD);
3164 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003165 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003166 } else {
3167 fd_delete(t->cli_fd);
3168 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003169 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003170 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003171 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003173 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003174 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003175 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003176 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003177 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003178 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003179 } else {
3180 /* output was already closed */
3181 fd_delete(t->cli_fd);
3182 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003183 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003184 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003185 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003186 if (!(t->flags & SN_ERR_MASK))
3187 t->flags |= SN_ERR_CLITO;
3188 if (!(t->flags & SN_FINST_MASK)) {
3189 if (t->pend_pos)
3190 t->flags |= SN_FINST_Q;
3191 else if (t->srv_state == SV_STCONN)
3192 t->flags |= SN_FINST_C;
3193 else
3194 t->flags |= SN_FINST_D;
3195 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003196 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003197 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003198 }
3199 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003200 else if (rep->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003201 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003202 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003203 EV_FD_CLR(t->cli_fd, DIR_WR);
3204 shutdown(t->cli_fd, SHUT_WR);
3205 /* We must ensure that the read part is still alive when switching to shutw */
3206 /* FIXME: is this still true ? */
3207 EV_FD_SET(t->cli_fd, DIR_RD);
3208 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003209 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003210 } else {
3211 fd_delete(t->cli_fd);
3212 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003213 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003214 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003215 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003216 if (!(t->flags & SN_ERR_MASK))
3217 t->flags |= SN_ERR_CLITO;
3218 if (!(t->flags & SN_FINST_MASK)) {
3219 if (t->pend_pos)
3220 t->flags |= SN_FINST_Q;
3221 else if (t->srv_state == SV_STCONN)
3222 t->flags |= SN_FINST_C;
3223 else
3224 t->flags |= SN_FINST_D;
3225 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003226 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003227 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003228 }
3229
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003230 update_timeouts:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003231 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003232 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003233 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003234 /* no room to read more data */
3235 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3236 /* stop reading until we get some space */
3237 req->rex = TICK_ETERNITY;
3238 }
3239 } else {
3240 EV_FD_COND_S(t->cli_fd, DIR_RD);
3241 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3242 }
3243 }
3244
3245 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003246 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003247 /* first, we may have to produce data (eg: stats).
3248 * right now, this is limited to the SHUTR state.
3249 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003250 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003251 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003252 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003253 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003254 fd_delete(t->cli_fd);
3255 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003256 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003257 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003258 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003259 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003260
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003261 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003262 if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003263 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3264 /* stop writing */
3265 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003266 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003267 } else {
3268 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003269 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003270 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003271 /* restart writing */
3272 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003273 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003274 /* FIXME: to prevent the client from expiring read timeouts during writes,
3275 * we refresh it, except if it was already infinite. */
3276 req->rex = rep->wex;
3277 }
3278 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003279 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003280 }
3281 return 0; /* other cases change nothing */
3282 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003283 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003284 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3285 int len;
3286 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);
3287 write(1, trash, len);
3288 }
3289 return 0;
3290 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003291#ifdef DEBUG_DEV
3292 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3293 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003294#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003295 return 0;
3296}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003297
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003299/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003300 * Manages the server FSM and its socket. It normally returns zero, but may
3301 * return 1 if it absolutely wants to be called again.
3302 *
Willy Tarreaua7c52762008-08-16 18:40:18 +02003303 * Note: process_srv is the ONLY function allowed to set srv_state to anything
3304 * but SV_STCLOSE. The only exception is for functions called from this
3305 * one (eg: those in backend.c).
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003306 */
3307int process_srv(struct session *t)
3308{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003309 struct http_txn *txn = &t->txn;
3310 struct buffer *req = t->req;
3311 struct buffer *rep = t->rep;
3312 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003314 DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 now_ms,
3316 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003317 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
3318 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003319 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003320 req->flags, rep->flags,
3321 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003322
Willy Tarreaudafde432008-08-17 01:00:46 +02003323 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003324 if (t->srv_state == SV_STIDLE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003325 if ((rep->flags & BF_SHUTW) ||
3326 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003327 (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003328 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003329 if (t->pend_pos)
3330 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3331 /* note that this must not return any error because it would be able to
3332 * overwrite the client_retnclose() output.
3333 */
3334 if (txn->flags & TX_CLTARPIT)
3335 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3336 else
3337 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3338
Willy Tarreauf8533202008-08-16 14:55:08 +02003339 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003340 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003341 }
Willy Tarreaud9f48362008-08-16 16:39:26 +02003342 else if (req->flags & BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003343 /* the client allows the server to connect */
3344 if (txn->flags & TX_CLTARPIT) {
3345 /* This connection is being tarpitted. The CLIENT side has
3346 * already set the connect expiration date to the right
3347 * timeout. We just have to check that it has not expired.
3348 */
Willy Tarreau507385d2008-08-17 13:04:25 +02003349 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003350 return 0;
3351
3352 /* We will set the queue timer to the time spent, just for
3353 * logging purposes. We fake a 500 server error, so that the
3354 * attacker will not suspect his connection has been tarpitted.
3355 * It will not cause trouble to the logs because we can exclude
3356 * the tarpitted connections by filtering on the 'PT' status flags.
3357 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003358 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003359 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3360 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3361 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003362 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreaudafde432008-08-17 01:00:46 +02003363 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003364 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003366 /* Right now, we will need to create a connection to the server.
3367 * We might already have tried, and got a connection pending, in
3368 * which case we will not do anything till it's pending. It's up
3369 * to any other session to release it and wake us up again.
3370 */
3371 if (t->pend_pos) {
Willy Tarreau507385d2008-08-17 13:04:25 +02003372 if (!(req->flags & BF_WRITE_TIMEOUT)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 return 0;
3374 } else {
3375 /* we've been waiting too long here */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003376 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003377 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3378 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3379 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003380 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003381 if (t->srv)
3382 t->srv->failed_conns++;
3383 t->be->failed_conns++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003384 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003385 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386 }
3387
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003388 do {
3389 /* first, get a connection */
3390 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3391 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003392
Willy Tarreaudafde432008-08-17 01:00:46 +02003393 if (srv_redispatch_connect(t)) {
3394 if (t->srv_state == SV_STIDLE)
3395 return 0;
3396 goto update_state;
3397 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003398
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003399 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3400 /* Server supporting redirection and it is possible.
3401 * Invalid requests are reported as such. It concerns all
3402 * the largest ones.
3403 */
3404 struct chunk rdr;
3405 char *path;
3406 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003407
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003408 /* 1: create the response header */
3409 rdr.len = strlen(HTTP_302);
3410 rdr.str = trash;
3411 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003412
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003413 /* 2: add the server's prefix */
3414 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3415 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003416
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003417 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3418 rdr.len += t->srv->rdr_len;
3419
3420 /* 3: add the request URI */
3421 path = http_get_path(txn);
3422 if (!path)
3423 goto cancel_redir;
3424 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3425 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3426 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003427
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003428 memcpy(rdr.str + rdr.len, path, len);
3429 rdr.len += len;
3430 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3431 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003432
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003433 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003434 trace_term(t, TT_HTTP_SRV_3);
3435
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003436 /* FIXME: we should increase a counter of redirects per server and per backend. */
3437 if (t->srv)
3438 t->srv->cum_sess++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003439 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003440 cancel_redir:
3441 txn->status = 400;
3442 t->fe->failed_req++;
3443 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3444 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003445 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaudafde432008-08-17 01:00:46 +02003446 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003447 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003448
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003449 /* try to (re-)connect to the server, and fail if we expire the
3450 * number of retries.
3451 */
3452 if (srv_retryable_connect(t)) {
3453 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003454 if (t->srv_state == SV_STIDLE)
3455 return 0;
3456 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457 }
3458 } while (1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003459 } /* end if may_forward */
3460 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003461 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003462 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003463 if ((rep->flags & BF_SHUTW) ||
3464 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003465 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003466 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003467 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003468 if (!(t->flags & SN_CONN_TAR)) {
3469 /* if we are in turn-around, we have already closed the FD */
3470 fd_delete(t->srv_fd);
3471 if (t->srv) {
3472 t->srv->cur_sess--;
3473 sess_change_server(t, NULL);
3474 }
3475 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003476
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003477 /* note that this must not return any error because it would be able to
3478 * overwrite the client_retnclose() output.
3479 */
3480 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003481 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreaudafde432008-08-17 01:00:46 +02003482 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003483 }
Willy Tarreau507385d2008-08-17 13:04:25 +02003484 if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003485 return 0; /* nothing changed */
3486 }
3487 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3488 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003489 if (t->flags & SN_CONN_TAR) {
3490 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau507385d2008-08-17 13:04:25 +02003491 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003492 return 0;
3493 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003494 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003495 else {
3496 fd_delete(t->srv_fd);
3497 if (t->srv) {
3498 t->srv->cur_sess--;
3499 sess_change_server(t, NULL);
3500 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003501
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003502 if (!(req->flags & BF_WRITE_STATUS))
3503 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3504 else
3505 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003506
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003507 /* ensure that we have enough retries left */
Willy Tarreaudafde432008-08-17 01:00:46 +02003508 if (srv_count_retry_down(t, conn_err)) {
3509 goto update_state;
3510 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003511
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003512 if (req->flags & BF_WRITE_ERROR) {
3513 /* we encountered an immediate connection error, and we
3514 * will have to retry connecting to the same server, most
3515 * likely leading to the same result. To avoid this, we
3516 * fake a connection timeout to retry after a turn-around
3517 * time of 1 second. We will wait in the previous if block.
3518 */
3519 t->flags |= SN_CONN_TAR;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003520 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003521 return 0;
3522 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003523 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003524
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003525 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3526 /* We're on our last chance, and the REDISP option was specified.
3527 * We will ignore cookie and force to balance or use the dispatcher.
3528 */
3529 /* let's try to offer this slot to anybody */
3530 if (may_dequeue_tasks(t->srv, t->be))
3531 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003532
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003533 /* it's left to the dispatcher to choose a server */
3534 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3535 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003536
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003537 /* first, get a connection */
Willy Tarreaudafde432008-08-17 01:00:46 +02003538 if (srv_redispatch_connect(t)) {
3539 if (t->srv_state == SV_STCONN)
3540 return 0;
3541 goto update_state;
3542 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003543 } else {
3544 if (t->srv)
3545 t->srv->retries++;
3546 t->be->retries++;
3547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003548
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003549 do {
3550 /* Now we will try to either reconnect to the same server or
3551 * connect to another server. If the connection gets queued
3552 * because all servers are saturated, then we will go back to
3553 * the SV_STIDLE state.
3554 */
3555 if (srv_retryable_connect(t)) {
3556 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003557 if (t->srv_state == SV_STCONN)
3558 return 0;
3559 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003560 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003561
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003562 /* we need to redispatch the connection to another server */
Willy Tarreaudafde432008-08-17 01:00:46 +02003563 if (srv_redispatch_connect(t)) {
3564 if (t->srv_state == SV_STCONN)
3565 return 0;
3566 goto update_state;
3567 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 } while (1);
3569 }
3570 else { /* no error or write 0 */
3571 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003572
Willy Tarreaue393fe22008-08-16 22:18:07 +02003573 if (req->flags & BF_EMPTY) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003574 EV_FD_CLR(t->srv_fd, DIR_WR);
3575 req->wex = TICK_ETERNITY;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003576 } else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003577 EV_FD_SET(t->srv_fd, DIR_WR);
3578 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3579 if (tick_isset(req->wex)) {
3580 /* FIXME: to prevent the server from expiring read timeouts during writes,
3581 * we refresh it. */
3582 rep->rex = req->wex;
3583 }
3584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003585
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003586 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3587 EV_FD_SET(t->srv_fd, DIR_RD);
3588 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003589 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003590
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003591 /* if the user wants to log as soon as possible, without counting
3592 bytes from the server, then this is the right moment. */
3593 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3594 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3595 tcp_sess_log(t);
3596 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003597#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003598 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3599 /* TCP splicing supported by both FE and BE */
3600 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3601 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003602#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003603 }
3604 else {
Willy Tarreau2df28e82008-08-17 15:20:19 +02003605 rep->analysers |= AN_RTR_HTTP_HDR;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003606 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003607 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3608 /* reset hdr_idx which was already initialized by the request.
3609 * right now, the http parser does it.
3610 * hdr_idx_init(&t->txn.hdr_idx);
3611 */
3612 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003613
3614 t->srv_state = SV_STDATA;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003615 if (!rep->analysers)
Willy Tarreaudafde432008-08-17 01:00:46 +02003616 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003617 req->wex = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003618 goto update_state;
3619 } /* else no error or write 0 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003620 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003621 else if (t->srv_state == SV_STDATA) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003622 /* we can skip most of the tests at once if some conditions are not met */
3623 if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
3624 (!(req->flags & BF_SHUTW) &&
3625 (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
3626 (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3627 (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
3628 goto update_timeouts;
3629
Willy Tarreaubaaee002006-06-26 02:48:02 +02003630 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003631 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003632 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003633 buffer_shutr(rep);
3634 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003635 fd_delete(t->srv_fd);
3636 if (t->srv) {
3637 t->srv->cur_sess--;
3638 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003639 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003640 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003641 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003642 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003643 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003644 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003645 if (!(t->flags & SN_ERR_MASK))
3646 t->flags |= SN_ERR_SRVCL;
3647 if (!(t->flags & SN_FINST_MASK))
3648 t->flags |= SN_FINST_D;
3649 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003650 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003651 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003652
Willy Tarreaudafde432008-08-17 01:00:46 +02003653 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003654 }
3655 /* last read, or end of client write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003656 else if (!(rep->flags & BF_SHUTR) && /* not already done */
3657 rep->flags & (BF_READ_NULL | BF_SHUTW)) {
3658 buffer_shutr(rep);
3659 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003660 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003661 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003662 } else {
3663 /* output was already closed */
3664 fd_delete(t->srv_fd);
3665 if (t->srv) {
3666 t->srv->cur_sess--;
3667 sess_change_server(t, NULL);
3668 }
3669 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003670 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003671
3672 if (may_dequeue_tasks(t->srv, t->be))
3673 process_srv_queue(t->srv);
3674 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003675 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003676 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003677 /* end of client read and no more data to send. We can forward
3678 * the close when we're allowed to forward data (anytime right
3679 * now). If we're using option forceclose, then we may also
3680 * shutdown the outgoing write channel once the response starts
3681 * coming from the server.
3682 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003683 else if (!(req->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003684 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003685 (req->flags & BF_SHUTR ||
Willy Tarreau461f6622008-08-15 23:43:19 +02003686 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003687 buffer_shutw(req);
3688 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003689 EV_FD_CLR(t->srv_fd, DIR_WR);
3690 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003691 trace_term(t, TT_HTTP_SRV_9);
Willy Tarreau461f6622008-08-15 23:43:19 +02003692 /* We must ensure that the read part is still alive when switching to shutw */
3693 /* FIXME: is this still true ? */
3694 EV_FD_SET(t->srv_fd, DIR_RD);
3695 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003696 } else {
3697 fd_delete(t->srv_fd);
3698 if (t->srv) {
3699 t->srv->cur_sess--;
3700 sess_change_server(t, NULL);
3701 }
3702 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003703 trace_term(t, TT_HTTP_SRV_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003704
Willy Tarreau461f6622008-08-15 23:43:19 +02003705 if (may_dequeue_tasks(t->srv, t->be))
3706 process_srv_queue(t->srv);
3707 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003708 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003709 }
3710 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003711 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003712 buffer_shutr(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003713 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003714 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003715 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003716 } else {
3717 fd_delete(t->srv_fd);
3718 if (t->srv) {
3719 t->srv->cur_sess--;
3720 sess_change_server(t, NULL);
3721 }
3722 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003723 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003724
3725 if (may_dequeue_tasks(t->srv, t->be))
3726 process_srv_queue(t->srv);
3727 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003728 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003729 if (!(t->flags & SN_ERR_MASK))
3730 t->flags |= SN_ERR_SRVTO;
3731 if (!(t->flags & SN_FINST_MASK))
3732 t->flags |= SN_FINST_D;
3733 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003734
3735 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736 }
3737 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003738 else if (req->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003739 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003740 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003741 EV_FD_CLR(t->srv_fd, DIR_WR);
3742 shutdown(t->srv_fd, SHUT_WR);
3743 /* We must ensure that the read part is still alive when switching to shutw */
3744 /* FIXME: is this still needed ? */
3745 EV_FD_SET(t->srv_fd, DIR_RD);
3746 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003747 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003748 } else {
3749 fd_delete(t->srv_fd);
3750 if (t->srv) {
3751 t->srv->cur_sess--;
3752 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003753 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003754 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003755 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003756
Willy Tarreau461f6622008-08-15 23:43:19 +02003757 if (may_dequeue_tasks(t->srv, t->be))
3758 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003759 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003760 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003761 if (!(t->flags & SN_ERR_MASK))
3762 t->flags |= SN_ERR_SRVTO;
3763 if (!(t->flags & SN_FINST_MASK))
3764 t->flags |= SN_FINST_D;
3765 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003766 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003767 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003768
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003769 update_timeouts:
Willy Tarreau461f6622008-08-15 23:43:19 +02003770 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003771 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003772 if (rep->flags & BF_FULL) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003773 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3774 rep->rex = TICK_ETERNITY;
3775 } else {
3776 EV_FD_COND_S(t->srv_fd, DIR_RD);
3777 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003778 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003779 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003780
Willy Tarreau461f6622008-08-15 23:43:19 +02003781 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003782 if (!(req->flags & BF_SHUTW)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003783 if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003784 /* stop writing */
3785 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3786 req->wex = TICK_ETERNITY;
3787 } else {
3788 /* buffer not empty, there are still data to be transferred */
3789 EV_FD_COND_S(t->srv_fd, DIR_WR);
3790 if (!tick_isset(req->wex)) {
3791 /* restart writing */
3792 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003793 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003794 /* FIXME: to prevent the server from expiring read timeouts during writes,
3795 * we refresh it, except if it was already infinite.
3796 */
3797 rep->rex = req->wex;
3798 }
3799 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003800 }
3801 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003802 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003803 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003804 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003805 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3806 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003807 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003808 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003809 write(1, trash, len);
3810 }
3811 return 0;
3812 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003813#ifdef DEBUG_DEV
3814 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3815 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003816#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003817 return 0;
3818}
3819
3820
3821/*
3822 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003823 * called with client socket shut down on input. Right now, only statistics can
3824 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003825 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003826 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003827 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003828 */
3829int produce_content(struct session *s)
3830{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003831 if (s->data_source == DATA_SRC_NONE) {
3832 s->flags &= ~SN_SELF_GEN;
3833 return 1;
3834 }
3835 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003836 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003837 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003838 if (ret >= 0)
3839 return ret;
3840 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003841 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003842
Willy Tarreau91861262007-10-17 17:06:05 +02003843 /* unknown data source or internal error */
3844 s->txn.status = 500;
3845 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003846 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003847 if (!(s->flags & SN_ERR_MASK))
3848 s->flags |= SN_ERR_PRXCOND;
3849 if (!(s->flags & SN_FINST_MASK))
3850 s->flags |= SN_FINST_R;
3851 s->flags &= ~SN_SELF_GEN;
3852 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003853}
3854
3855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003856/* Iterate the same filter through all request headers.
3857 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003858 * Since it can manage the switch to another backend, it updates the per-proxy
3859 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003860 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003861int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003862{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003863 char term;
3864 char *cur_ptr, *cur_end, *cur_next;
3865 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003866 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003867 struct hdr_idx_elem *cur_hdr;
3868 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003870 last_hdr = 0;
3871
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003872 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003873 old_idx = 0;
3874
3875 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003876 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003877 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003878 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003879 (exp->action == ACT_ALLOW ||
3880 exp->action == ACT_DENY ||
3881 exp->action == ACT_TARPIT))
3882 return 0;
3883
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003884 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003885 if (!cur_idx)
3886 break;
3887
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003888 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003889 cur_ptr = cur_next;
3890 cur_end = cur_ptr + cur_hdr->len;
3891 cur_next = cur_end + cur_hdr->cr + 1;
3892
3893 /* Now we have one header between cur_ptr and cur_end,
3894 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895 */
3896
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003897 /* The annoying part is that pattern matching needs
3898 * that we modify the contents to null-terminate all
3899 * strings before testing them.
3900 */
3901
3902 term = *cur_end;
3903 *cur_end = '\0';
3904
3905 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3906 switch (exp->action) {
3907 case ACT_SETBE:
3908 /* It is not possible to jump a second time.
3909 * FIXME: should we return an HTTP/500 here so that
3910 * the admin knows there's a problem ?
3911 */
3912 if (t->be != t->fe)
3913 break;
3914
3915 /* Swithing Proxy */
3916 t->be = (struct proxy *) exp->replace;
3917
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003918 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003919 * because we have associated req_cap and rsp_cap to the
3920 * frontend, and the beconn will be updated later.
3921 */
3922
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003923 t->rep->rto = t->req->wto = t->be->timeout.server;
3924 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003925 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003926 last_hdr = 1;
3927 break;
3928
3929 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003930 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003931 last_hdr = 1;
3932 break;
3933
3934 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003935 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003936 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003937 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003938 break;
3939
3940 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003941 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003942 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003943 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003944 break;
3945
3946 case ACT_REPLACE:
3947 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3948 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3949 /* FIXME: if the user adds a newline in the replacement, the
3950 * index will not be recalculated for now, and the new line
3951 * will not be counted as a new header.
3952 */
3953
3954 cur_end += delta;
3955 cur_next += delta;
3956 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003957 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003958 break;
3959
3960 case ACT_REMOVE:
3961 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3962 cur_next += delta;
3963
3964 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003965 txn->req.eoh += delta;
3966 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3967 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003968 cur_hdr->len = 0;
3969 cur_end = NULL; /* null-term has been rewritten */
3970 break;
3971
3972 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003974 if (cur_end)
3975 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003977 /* keep the link from this header to next one in case of later
3978 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 old_idx = cur_idx;
3981 }
3982 return 0;
3983}
3984
3985
3986/* Apply the filter to the request line.
3987 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3988 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003989 * Since it can manage the switch to another backend, it updates the per-proxy
3990 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991 */
3992int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3993{
3994 char term;
3995 char *cur_ptr, *cur_end;
3996 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003997 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003998 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003999
Willy Tarreau58f10d72006-12-04 02:26:12 +01004000
Willy Tarreau3d300592007-03-18 18:34:41 +01004001 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004002 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004003 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004004 (exp->action == ACT_ALLOW ||
4005 exp->action == ACT_DENY ||
4006 exp->action == ACT_TARPIT))
4007 return 0;
4008 else if (exp->action == ACT_REMOVE)
4009 return 0;
4010
4011 done = 0;
4012
Willy Tarreau9cdde232007-05-02 20:58:19 +02004013 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004014 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004015
4016 /* Now we have the request line between cur_ptr and cur_end */
4017
4018 /* The annoying part is that pattern matching needs
4019 * that we modify the contents to null-terminate all
4020 * strings before testing them.
4021 */
4022
4023 term = *cur_end;
4024 *cur_end = '\0';
4025
4026 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4027 switch (exp->action) {
4028 case ACT_SETBE:
4029 /* It is not possible to jump a second time.
4030 * FIXME: should we return an HTTP/500 here so that
4031 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004032 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 if (t->be != t->fe)
4034 break;
4035
4036 /* Swithing Proxy */
4037 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004039 /* right now, the backend switch is not too much complicated
4040 * because we have associated req_cap and rsp_cap to the
4041 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042 */
4043
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004044 t->rep->rto = t->req->wto = t->be->timeout.server;
4045 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004046 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004047 done = 1;
4048 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004050 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004051 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004052 done = 1;
4053 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004055 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004056 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004057 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 done = 1;
4059 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004062 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004063 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004064 done = 1;
4065 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 case ACT_REPLACE:
4068 *cur_end = term; /* restore the string terminator */
4069 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4070 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4071 /* FIXME: if the user adds a newline in the replacement, the
4072 * index will not be recalculated for now, and the new line
4073 * will not be counted as a new header.
4074 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004075
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004076 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004077 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004078
Willy Tarreau9cdde232007-05-02 20:58:19 +02004079 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004080 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004081 HTTP_MSG_RQMETH,
4082 cur_ptr, cur_end + 1,
4083 NULL, NULL);
4084 if (unlikely(!cur_end))
4085 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004087 /* we have a full request and we know that we have either a CR
4088 * or an LF at <ptr>.
4089 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004090 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4091 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004092 /* there is no point trying this regex on headers */
4093 return 1;
4094 }
4095 }
4096 *cur_end = term; /* restore the string terminator */
4097 return done;
4098}
Willy Tarreau97de6242006-12-27 17:18:38 +01004099
Willy Tarreau58f10d72006-12-04 02:26:12 +01004100
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004102/*
4103 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4104 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004105 * unparsable request. Since it can manage the switch to another backend, it
4106 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004107 */
4108int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4109{
Willy Tarreau3d300592007-03-18 18:34:41 +01004110 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004111 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004112 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004113 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004115 /*
4116 * The interleaving of transformations and verdicts
4117 * makes it difficult to decide to continue or stop
4118 * the evaluation.
4119 */
4120
Willy Tarreau3d300592007-03-18 18:34:41 +01004121 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004122 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4123 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4124 exp = exp->next;
4125 continue;
4126 }
4127
4128 /* Apply the filter to the request line. */
4129 ret = apply_filter_to_req_line(t, req, exp);
4130 if (unlikely(ret < 0))
4131 return -1;
4132
4133 if (likely(ret == 0)) {
4134 /* The filter did not match the request, it can be
4135 * iterated through all headers.
4136 */
4137 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004138 }
4139 exp = exp->next;
4140 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004141 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004142}
4143
4144
Willy Tarreaua15645d2007-03-18 16:22:39 +01004145
Willy Tarreau58f10d72006-12-04 02:26:12 +01004146/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004147 * Manage client-side cookie. It can impact performance by about 2% so it is
4148 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004149 */
4150void manage_client_side_cookies(struct session *t, struct buffer *req)
4151{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004152 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004153 char *p1, *p2, *p3, *p4;
4154 char *del_colon, *del_cookie, *colon;
4155 int app_cookies;
4156
4157 appsess *asession_temp = NULL;
4158 appsess local_asession;
4159
4160 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004161 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004162
Willy Tarreau2a324282006-12-05 00:05:46 +01004163 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004164 * we start with the start line.
4165 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004166 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004167 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004169 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004170 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004171 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004172
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004173 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174 cur_ptr = cur_next;
4175 cur_end = cur_ptr + cur_hdr->len;
4176 cur_next = cur_end + cur_hdr->cr + 1;
4177
4178 /* We have one full header between cur_ptr and cur_end, and the
4179 * next header starts at cur_next. We're only interested in
4180 * "Cookie:" headers.
4181 */
4182
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004183 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4184 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004185 old_idx = cur_idx;
4186 continue;
4187 }
4188
4189 /* Now look for cookies. Conforming to RFC2109, we have to support
4190 * attributes whose name begin with a '$', and associate them with
4191 * the right cookie, if we want to delete this cookie.
4192 * So there are 3 cases for each cookie read :
4193 * 1) it's a special attribute, beginning with a '$' : ignore it.
4194 * 2) it's a server id cookie that we *MAY* want to delete : save
4195 * some pointers on it (last semi-colon, beginning of cookie...)
4196 * 3) it's an application cookie : we *MAY* have to delete a previous
4197 * "special" cookie.
4198 * At the end of loop, if a "special" cookie remains, we may have to
4199 * remove it. If no application cookie persists in the header, we
4200 * *MUST* delete it
4201 */
4202
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004203 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204
Willy Tarreau58f10d72006-12-04 02:26:12 +01004205 /* del_cookie == NULL => nothing to be deleted */
4206 del_colon = del_cookie = NULL;
4207 app_cookies = 0;
4208
4209 while (p1 < cur_end) {
4210 /* skip spaces and colons, but keep an eye on these ones */
4211 while (p1 < cur_end) {
4212 if (*p1 == ';' || *p1 == ',')
4213 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004214 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004215 break;
4216 p1++;
4217 }
4218
4219 if (p1 == cur_end)
4220 break;
4221
4222 /* p1 is at the beginning of the cookie name */
4223 p2 = p1;
4224 while (p2 < cur_end && *p2 != '=')
4225 p2++;
4226
4227 if (p2 == cur_end)
4228 break;
4229
4230 p3 = p2 + 1; /* skips the '=' sign */
4231 if (p3 == cur_end)
4232 break;
4233
4234 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004235 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236 p4++;
4237
4238 /* here, we have the cookie name between p1 and p2,
4239 * and its value between p3 and p4.
4240 * we can process it :
4241 *
4242 * Cookie: NAME=VALUE;
4243 * | || || |
4244 * | || || +--> p4
4245 * | || |+-------> p3
4246 * | || +--------> p2
4247 * | |+------------> p1
4248 * | +-------------> colon
4249 * +--------------------> cur_ptr
4250 */
4251
4252 if (*p1 == '$') {
4253 /* skip this one */
4254 }
4255 else {
4256 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004257 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004258 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004259 (p4 - p1 >= t->fe->capture_namelen) &&
4260 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004261 int log_len = p4 - p1;
4262
Willy Tarreau086b3b42007-05-13 21:45:51 +02004263 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004264 Alert("HTTP logging : out of memory.\n");
4265 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004266 if (log_len > t->fe->capture_len)
4267 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004268 memcpy(txn->cli_cookie, p1, log_len);
4269 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004270 }
4271 }
4272
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004273 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4274 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004275 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004276 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 char *delim;
4278
4279 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4280 * have the server ID betweek p3 and delim, and the original cookie between
4281 * delim+1 and p4. Otherwise, delim==p4 :
4282 *
4283 * Cookie: NAME=SRV~VALUE;
4284 * | || || | |
4285 * | || || | +--> p4
4286 * | || || +--------> delim
4287 * | || |+-----------> p3
4288 * | || +------------> p2
4289 * | |+----------------> p1
4290 * | +-----------------> colon
4291 * +------------------------> cur_ptr
4292 */
4293
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004294 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004295 for (delim = p3; delim < p4; delim++)
4296 if (*delim == COOKIE_DELIM)
4297 break;
4298 }
4299 else
4300 delim = p4;
4301
4302
4303 /* Here, we'll look for the first running server which supports the cookie.
4304 * This allows to share a same cookie between several servers, for example
4305 * to dedicate backup servers to specific servers only.
4306 * However, to prevent clients from sticking to cookie-less backup server
4307 * when they have incidentely learned an empty cookie, we simply ignore
4308 * empty cookies and mark them as invalid.
4309 */
4310 if (delim == p3)
4311 srv = NULL;
4312
4313 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004314 if (srv->cookie && (srv->cklen == delim - p3) &&
4315 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004316 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004317 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004318 txn->flags &= ~TX_CK_MASK;
4319 txn->flags |= TX_CK_VALID;
4320 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004321 t->srv = srv;
4322 break;
4323 } else {
4324 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004325 txn->flags &= ~TX_CK_MASK;
4326 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004327 }
4328 }
4329 srv = srv->next;
4330 }
4331
Willy Tarreau3d300592007-03-18 18:34:41 +01004332 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004333 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004334 txn->flags &= ~TX_CK_MASK;
4335 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 }
4337
4338 /* depending on the cookie mode, we may have to either :
4339 * - delete the complete cookie if we're in insert+indirect mode, so that
4340 * the server never sees it ;
4341 * - remove the server id from the cookie value, and tag the cookie as an
4342 * application cookie so that it does not get accidentely removed later,
4343 * if we're in cookie prefix mode
4344 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004345 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 int delta; /* negative */
4347
4348 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4349 p4 += delta;
4350 cur_end += delta;
4351 cur_next += delta;
4352 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004353 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354
4355 del_cookie = del_colon = NULL;
4356 app_cookies++; /* protect the header from deletion */
4357 }
4358 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 (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 +01004360 del_cookie = p1;
4361 del_colon = colon;
4362 }
4363 } else {
4364 /* now we know that we must keep this cookie since it's
4365 * not ours. But if we wanted to delete our cookie
4366 * earlier, we cannot remove the complete header, but we
4367 * can remove the previous block itself.
4368 */
4369 app_cookies++;
4370
4371 if (del_cookie != NULL) {
4372 int delta; /* negative */
4373
4374 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4375 p4 += delta;
4376 cur_end += delta;
4377 cur_next += delta;
4378 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004379 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004380 del_cookie = del_colon = NULL;
4381 }
4382 }
4383
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004384 if ((t->be->appsession_name != NULL) &&
4385 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004386 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004387
Willy Tarreau58f10d72006-12-04 02:26:12 +01004388 /* Cool... it's the right one */
4389
4390 asession_temp = &local_asession;
4391
Willy Tarreau63963c62007-05-13 21:29:55 +02004392 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004393 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4394 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4395 return;
4396 }
4397
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4399 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004400 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004401
Willy Tarreau58f10d72006-12-04 02:26:12 +01004402 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004403 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4404 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004405 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004407 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408 Alert("Not enough memory process_cli():asession:calloc().\n");
4409 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4410 return;
4411 }
4412
4413 asession_temp->sessid = local_asession.sessid;
4414 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004415 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004416 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004417 } else {
4418 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004419 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004421 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004422 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004423 Alert("Found Application Session without matching server.\n");
4424 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004425 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 while (srv) {
4427 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004428 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004430 txn->flags &= ~TX_CK_MASK;
4431 txn->flags |= TX_CK_VALID;
4432 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004433 t->srv = srv;
4434 break;
4435 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004436 txn->flags &= ~TX_CK_MASK;
4437 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004438 }
4439 }
4440 srv = srv->next;
4441 }/* end while(srv) */
4442 }/* end else if server == NULL */
4443
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004444 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004445 asession_temp->request_count++;
4446#if defined(DEBUG_HASH)
4447 Alert("manage_client_side_cookies\n");
4448 appsession_hash_dump(&(t->be->htbl_proxy));
4449#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004450 }/* end if ((t->proxy->appsession_name != NULL) ... */
4451 }
4452
4453 /* we'll have to look for another cookie ... */
4454 p1 = p4;
4455 } /* while (p1 < cur_end) */
4456
4457 /* There's no more cookie on this line.
4458 * We may have marked the last one(s) for deletion.
4459 * We must do this now in two ways :
4460 * - if there is no app cookie, we simply delete the header ;
4461 * - if there are app cookies, we must delete the end of the
4462 * string properly, including the colon/semi-colon before
4463 * the cookie name.
4464 */
4465 if (del_cookie != NULL) {
4466 int delta;
4467 if (app_cookies) {
4468 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4469 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004470 cur_hdr->len += delta;
4471 } else {
4472 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004473
4474 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004475 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4476 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004477 cur_hdr->len = 0;
4478 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004479 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004480 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004481 }
4482
4483 /* keep the link from this header to next one */
4484 old_idx = cur_idx;
4485 } /* end of cookie processing on this header */
4486}
4487
4488
Willy Tarreaua15645d2007-03-18 16:22:39 +01004489/* Iterate the same filter through all response headers contained in <rtr>.
4490 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4491 */
4492int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4493{
4494 char term;
4495 char *cur_ptr, *cur_end, *cur_next;
4496 int cur_idx, old_idx, last_hdr;
4497 struct http_txn *txn = &t->txn;
4498 struct hdr_idx_elem *cur_hdr;
4499 int len, delta;
4500
4501 last_hdr = 0;
4502
4503 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4504 old_idx = 0;
4505
4506 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004507 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004508 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004509 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004510 (exp->action == ACT_ALLOW ||
4511 exp->action == ACT_DENY))
4512 return 0;
4513
4514 cur_idx = txn->hdr_idx.v[old_idx].next;
4515 if (!cur_idx)
4516 break;
4517
4518 cur_hdr = &txn->hdr_idx.v[cur_idx];
4519 cur_ptr = cur_next;
4520 cur_end = cur_ptr + cur_hdr->len;
4521 cur_next = cur_end + cur_hdr->cr + 1;
4522
4523 /* Now we have one header between cur_ptr and cur_end,
4524 * and the next header starts at cur_next.
4525 */
4526
4527 /* The annoying part is that pattern matching needs
4528 * that we modify the contents to null-terminate all
4529 * strings before testing them.
4530 */
4531
4532 term = *cur_end;
4533 *cur_end = '\0';
4534
4535 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4536 switch (exp->action) {
4537 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004538 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004539 last_hdr = 1;
4540 break;
4541
4542 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004543 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004544 last_hdr = 1;
4545 break;
4546
4547 case ACT_REPLACE:
4548 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4549 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4550 /* FIXME: if the user adds a newline in the replacement, the
4551 * index will not be recalculated for now, and the new line
4552 * will not be counted as a new header.
4553 */
4554
4555 cur_end += delta;
4556 cur_next += delta;
4557 cur_hdr->len += delta;
4558 txn->rsp.eoh += delta;
4559 break;
4560
4561 case ACT_REMOVE:
4562 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4563 cur_next += delta;
4564
4565 /* FIXME: this should be a separate function */
4566 txn->rsp.eoh += delta;
4567 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4568 txn->hdr_idx.used--;
4569 cur_hdr->len = 0;
4570 cur_end = NULL; /* null-term has been rewritten */
4571 break;
4572
4573 }
4574 }
4575 if (cur_end)
4576 *cur_end = term; /* restore the string terminator */
4577
4578 /* keep the link from this header to next one in case of later
4579 * removal of next header.
4580 */
4581 old_idx = cur_idx;
4582 }
4583 return 0;
4584}
4585
4586
4587/* Apply the filter to the status line in the response buffer <rtr>.
4588 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4589 * or -1 if a replacement resulted in an invalid status line.
4590 */
4591int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4592{
4593 char term;
4594 char *cur_ptr, *cur_end;
4595 int done;
4596 struct http_txn *txn = &t->txn;
4597 int len, delta;
4598
4599
Willy Tarreau3d300592007-03-18 18:34:41 +01004600 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004601 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004602 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004603 (exp->action == ACT_ALLOW ||
4604 exp->action == ACT_DENY))
4605 return 0;
4606 else if (exp->action == ACT_REMOVE)
4607 return 0;
4608
4609 done = 0;
4610
Willy Tarreau9cdde232007-05-02 20:58:19 +02004611 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4613
4614 /* Now we have the status line between cur_ptr and cur_end */
4615
4616 /* The annoying part is that pattern matching needs
4617 * that we modify the contents to null-terminate all
4618 * strings before testing them.
4619 */
4620
4621 term = *cur_end;
4622 *cur_end = '\0';
4623
4624 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4625 switch (exp->action) {
4626 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004627 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004628 done = 1;
4629 break;
4630
4631 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004632 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004633 done = 1;
4634 break;
4635
4636 case ACT_REPLACE:
4637 *cur_end = term; /* restore the string terminator */
4638 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4639 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4640 /* FIXME: if the user adds a newline in the replacement, the
4641 * index will not be recalculated for now, and the new line
4642 * will not be counted as a new header.
4643 */
4644
4645 txn->rsp.eoh += delta;
4646 cur_end += delta;
4647
Willy Tarreau9cdde232007-05-02 20:58:19 +02004648 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004649 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004650 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004651 cur_ptr, cur_end + 1,
4652 NULL, NULL);
4653 if (unlikely(!cur_end))
4654 return -1;
4655
4656 /* we have a full respnse and we know that we have either a CR
4657 * or an LF at <ptr>.
4658 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004659 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004660 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4661 /* there is no point trying this regex on headers */
4662 return 1;
4663 }
4664 }
4665 *cur_end = term; /* restore the string terminator */
4666 return done;
4667}
4668
4669
4670
4671/*
4672 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4673 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4674 * unparsable response.
4675 */
4676int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4677{
Willy Tarreau3d300592007-03-18 18:34:41 +01004678 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004679 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004680 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681 int ret;
4682
4683 /*
4684 * The interleaving of transformations and verdicts
4685 * makes it difficult to decide to continue or stop
4686 * the evaluation.
4687 */
4688
Willy Tarreau3d300592007-03-18 18:34:41 +01004689 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4691 exp->action == ACT_PASS)) {
4692 exp = exp->next;
4693 continue;
4694 }
4695
4696 /* Apply the filter to the status line. */
4697 ret = apply_filter_to_sts_line(t, rtr, exp);
4698 if (unlikely(ret < 0))
4699 return -1;
4700
4701 if (likely(ret == 0)) {
4702 /* The filter did not match the response, it can be
4703 * iterated through all headers.
4704 */
4705 apply_filter_to_resp_headers(t, rtr, exp);
4706 }
4707 exp = exp->next;
4708 }
4709 return 0;
4710}
4711
4712
4713
4714/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004715 * Manage server-side cookies. It can impact performance by about 2% so it is
4716 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004717 */
4718void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4719{
4720 struct http_txn *txn = &t->txn;
4721 char *p1, *p2, *p3, *p4;
4722
4723 appsess *asession_temp = NULL;
4724 appsess local_asession;
4725
4726 char *cur_ptr, *cur_end, *cur_next;
4727 int cur_idx, old_idx, delta;
4728
Willy Tarreaua15645d2007-03-18 16:22:39 +01004729 /* Iterate through the headers.
4730 * we start with the start line.
4731 */
4732 old_idx = 0;
4733 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4734
4735 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4736 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004737 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004738
4739 cur_hdr = &txn->hdr_idx.v[cur_idx];
4740 cur_ptr = cur_next;
4741 cur_end = cur_ptr + cur_hdr->len;
4742 cur_next = cur_end + cur_hdr->cr + 1;
4743
4744 /* We have one full header between cur_ptr and cur_end, and the
4745 * next header starts at cur_next. We're only interested in
4746 * "Cookie:" headers.
4747 */
4748
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004749 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4750 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004751 old_idx = cur_idx;
4752 continue;
4753 }
4754
4755 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004756 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004757
4758
4759 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004760 if (t->be->cookie_name == NULL &&
4761 t->be->appsession_name == NULL &&
4762 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 return;
4764
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004765 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004766
4767 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004768 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4769 break;
4770
4771 /* p1 is at the beginning of the cookie name */
4772 p2 = p1;
4773
4774 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4775 p2++;
4776
4777 if (p2 == cur_end || *p2 == ';') /* next cookie */
4778 break;
4779
4780 p3 = p2 + 1; /* skip the '=' sign */
4781 if (p3 == cur_end)
4782 break;
4783
4784 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004785 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004786 p4++;
4787
4788 /* here, we have the cookie name between p1 and p2,
4789 * and its value between p3 and p4.
4790 * we can process it.
4791 */
4792
4793 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004794 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004795 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004796 (p4 - p1 >= t->be->capture_namelen) &&
4797 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004798 int log_len = p4 - p1;
4799
Willy Tarreau086b3b42007-05-13 21:45:51 +02004800 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004801 Alert("HTTP logging : out of memory.\n");
4802 }
4803
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004804 if (log_len > t->be->capture_len)
4805 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004806 memcpy(txn->srv_cookie, p1, log_len);
4807 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004808 }
4809
4810 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004811 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4812 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004813 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004814 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004815
4816 /* If the cookie is in insert mode on a known server, we'll delete
4817 * this occurrence because we'll insert another one later.
4818 * We'll delete it too if the "indirect" option is set and we're in
4819 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004820 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4821 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004822 /* this header must be deleted */
4823 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4824 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4825 txn->hdr_idx.used--;
4826 cur_hdr->len = 0;
4827 cur_next += delta;
4828 txn->rsp.eoh += delta;
4829
Willy Tarreau3d300592007-03-18 18:34:41 +01004830 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004831 }
4832 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004833 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004834 /* replace bytes p3->p4 with the cookie name associated
4835 * with this server since we know it.
4836 */
4837 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4838 cur_hdr->len += delta;
4839 cur_next += delta;
4840 txn->rsp.eoh += delta;
4841
Willy Tarreau3d300592007-03-18 18:34:41 +01004842 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004843 }
4844 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004845 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004846 /* insert the cookie name associated with this server
4847 * before existing cookie, and insert a delimitor between them..
4848 */
4849 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4850 cur_hdr->len += delta;
4851 cur_next += delta;
4852 txn->rsp.eoh += delta;
4853
4854 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004855 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856 }
4857 }
4858 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004859 else if ((t->be->appsession_name != NULL) &&
4860 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004861
4862 /* Cool... it's the right one */
4863
4864 size_t server_id_len = strlen(t->srv->id) + 1;
4865 asession_temp = &local_asession;
4866
Willy Tarreau63963c62007-05-13 21:29:55 +02004867 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4869 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4870 return;
4871 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004872 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4873 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004874 asession_temp->serverid = NULL;
4875
4876 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004877 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4878 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004879 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880 Alert("Not enough Memory process_srv():asession:calloc().\n");
4881 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4882 return;
4883 }
4884 asession_temp->sessid = local_asession.sessid;
4885 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004886 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004887 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4888 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004889 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004890 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004891 }
4892
Willy Tarreaua15645d2007-03-18 16:22:39 +01004893 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004894 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4896 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4897 return;
4898 }
4899 asession_temp->serverid[0] = '\0';
4900 }
4901
4902 if (asession_temp->serverid[0] == '\0')
4903 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4904
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004905 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004906 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004907#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004908 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004909 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910#endif
4911 }/* end if ((t->proxy->appsession_name != NULL) ... */
4912 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4913 } /* we're now at the end of the cookie value */
4914
4915 /* keep the link from this header to next one */
4916 old_idx = cur_idx;
4917 } /* end of cookie processing on this header */
4918}
4919
4920
4921
4922/*
4923 * Check if response is cacheable or not. Updates t->flags.
4924 */
4925void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4926{
4927 struct http_txn *txn = &t->txn;
4928 char *p1, *p2;
4929
4930 char *cur_ptr, *cur_end, *cur_next;
4931 int cur_idx;
4932
Willy Tarreau5df51872007-11-25 16:20:08 +01004933 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004934 return;
4935
4936 /* Iterate through the headers.
4937 * we start with the start line.
4938 */
4939 cur_idx = 0;
4940 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4941
4942 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4943 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004944 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945
4946 cur_hdr = &txn->hdr_idx.v[cur_idx];
4947 cur_ptr = cur_next;
4948 cur_end = cur_ptr + cur_hdr->len;
4949 cur_next = cur_end + cur_hdr->cr + 1;
4950
4951 /* We have one full header between cur_ptr and cur_end, and the
4952 * next header starts at cur_next. We're only interested in
4953 * "Cookie:" headers.
4954 */
4955
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004956 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4957 if (val) {
4958 if ((cur_end - (cur_ptr + val) >= 8) &&
4959 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4960 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4961 return;
4962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004963 }
4964
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004965 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4966 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004967 continue;
4968
4969 /* OK, right now we know we have a cache-control header at cur_ptr */
4970
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004971 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004972
4973 if (p1 >= cur_end) /* no more info */
4974 continue;
4975
4976 /* p1 is at the beginning of the value */
4977 p2 = p1;
4978
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004979 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004980 p2++;
4981
4982 /* we have a complete value between p1 and p2 */
4983 if (p2 < cur_end && *p2 == '=') {
4984 /* we have something of the form no-cache="set-cookie" */
4985 if ((cur_end - p1 >= 21) &&
4986 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4987 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004988 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004989 continue;
4990 }
4991
4992 /* OK, so we know that either p2 points to the end of string or to a comma */
4993 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4994 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4995 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4996 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004997 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998 return;
4999 }
5000
5001 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005002 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005003 continue;
5004 }
5005 }
5006}
5007
5008
Willy Tarreau58f10d72006-12-04 02:26:12 +01005009/*
5010 * Try to retrieve a known appsession in the URI, then the associated server.
5011 * If the server is found, it's assigned to the session.
5012 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005013void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014{
Willy Tarreau3d300592007-03-18 18:34:41 +01005015 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016 appsess *asession_temp = NULL;
5017 appsess local_asession;
5018 char *request_line;
5019
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005020 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005021 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005022 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005023 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005024 return;
5025
5026 /* skip ';' */
5027 request_line++;
5028
5029 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005030 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031 return;
5032
5033 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005034 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005035
5036 /* First try if we already have an appsession */
5037 asession_temp = &local_asession;
5038
Willy Tarreau63963c62007-05-13 21:29:55 +02005039 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005040 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5041 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5042 return;
5043 }
5044
5045 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005046 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5047 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005048 asession_temp->serverid = NULL;
5049
5050 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005051 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5052 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005053 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005054 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005055 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056 Alert("Not enough memory process_cli():asession:calloc().\n");
5057 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5058 return;
5059 }
5060 asession_temp->sessid = local_asession.sessid;
5061 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005062 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005063 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005064 }
5065 else {
5066 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005067 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005068 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005069
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005070 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005071 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005072
Willy Tarreau58f10d72006-12-04 02:26:12 +01005073#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005074 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005075 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005076#endif
5077 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005078 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005079 Alert("Found Application Session without matching server.\n");
5080 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005081 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005082 while (srv) {
5083 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005084 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005085 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005086 txn->flags &= ~TX_CK_MASK;
5087 txn->flags |= TX_CK_VALID;
5088 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005089 t->srv = srv;
5090 break;
5091 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005092 txn->flags &= ~TX_CK_MASK;
5093 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005094 }
5095 }
5096 srv = srv->next;
5097 }
5098 }
5099}
5100
5101
Willy Tarreaub2513902006-12-17 14:52:38 +01005102/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005103 * In a GET or HEAD request, check if the requested URI matches the stats uri
5104 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005105 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005106 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005107 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005108 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005109 *
5110 * Returns 1 if the session's state changes, otherwise 0.
5111 */
5112int stats_check_uri_auth(struct session *t, struct proxy *backend)
5113{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005114 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005115 struct uri_auth *uri_auth = backend->uri_auth;
5116 struct user_auth *user;
5117 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005118 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005119
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005120 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005123 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005124 return 0;
5125
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005126 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005128 /* the URI is in h */
5129 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005130 return 0;
5131
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005132 h += uri_auth->uri_len;
5133 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5134 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005135 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005136 break;
5137 }
5138 h++;
5139 }
5140
5141 if (uri_auth->refresh) {
5142 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5143 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5144 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005145 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005146 break;
5147 }
5148 h++;
5149 }
5150 }
5151
Willy Tarreau55bb8452007-10-17 18:44:57 +02005152 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5153 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5154 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005155 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005156 break;
5157 }
5158 h++;
5159 }
5160
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005161 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5162
Willy Tarreaub2513902006-12-17 14:52:38 +01005163 /* we are in front of a interceptable URI. Let's check
5164 * if there's an authentication and if it's valid.
5165 */
5166 user = uri_auth->users;
5167 if (!user) {
5168 /* no user auth required, it's OK */
5169 authenticated = 1;
5170 } else {
5171 authenticated = 0;
5172
5173 /* a user list is defined, we have to check.
5174 * skip 21 chars for "Authorization: Basic ".
5175 */
5176
5177 /* FIXME: this should move to an earlier place */
5178 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005179 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5180 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5181 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005182 if (len > 14 &&
5183 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005184 txn->auth_hdr.str = h;
5185 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005186 break;
5187 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005188 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005189 }
5190
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005191 if (txn->auth_hdr.len < 21 ||
5192 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005193 user = NULL;
5194
5195 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005196 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5197 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005198 user->user_pwd, user->user_len)) {
5199 authenticated = 1;
5200 break;
5201 }
5202 user = user->next;
5203 }
5204 }
5205
5206 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005207 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005208
5209 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005210 msg.str = trash;
5211 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005212 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005213 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005214 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005215 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005216 if (!(t->flags & SN_ERR_MASK))
5217 t->flags |= SN_ERR_PRXCOND;
5218 if (!(t->flags & SN_FINST_MASK))
5219 t->flags |= SN_FINST_R;
5220 return 1;
5221 }
5222
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005223 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005224 * data.
5225 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005226 EV_FD_CLR(t->cli_fd, DIR_RD);
5227 buffer_shutr(t->req);
5228 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005229 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005230 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005231 t->data_source = DATA_SRC_STATS;
5232 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005233 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005234 produce_content(t);
5235 return 1;
5236}
5237
5238
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005240 * Print a debug line with a header
5241 */
5242void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5243{
5244 int len, max;
5245 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5246 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5247 max = end - start;
5248 UBOUND(max, sizeof(trash) - len - 1);
5249 len += strlcpy2(trash + len, start, max + 1);
5250 trash[len++] = '\n';
5251 write(1, trash, len);
5252}
5253
5254
Willy Tarreau8797c062007-05-07 00:55:35 +02005255/************************************************************************/
5256/* The code below is dedicated to ACL parsing and matching */
5257/************************************************************************/
5258
5259
5260
5261
5262/* 1. Check on METHOD
5263 * We use the pre-parsed method if it is known, and store its number as an
5264 * integer. If it is unknown, we use the pointer and the length.
5265 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005266static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005267{
5268 int len, meth;
5269
Willy Tarreauae8b7962007-06-09 23:10:04 +02005270 len = strlen(*text);
5271 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005272
5273 pattern->val.i = meth;
5274 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005275 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005276 if (!pattern->ptr.str)
5277 return 0;
5278 pattern->len = len;
5279 }
5280 return 1;
5281}
5282
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005283static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005284acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5285 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005286{
5287 int meth;
5288 struct http_txn *txn = l7;
5289
Willy Tarreaub6866442008-07-14 23:54:42 +02005290 if (!txn)
5291 return 0;
5292
Willy Tarreauc11416f2007-06-17 16:58:38 +02005293 if (txn->req.msg_state != HTTP_MSG_BODY)
5294 return 0;
5295
Willy Tarreau8797c062007-05-07 00:55:35 +02005296 meth = txn->meth;
5297 test->i = meth;
5298 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005299 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5300 /* ensure the indexes are not affected */
5301 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005302 test->len = txn->req.sl.rq.m_l;
5303 test->ptr = txn->req.sol;
5304 }
5305 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5306 return 1;
5307}
5308
5309static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5310{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005311 int icase;
5312
Willy Tarreau8797c062007-05-07 00:55:35 +02005313 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005314 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005315
5316 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005317 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005318
5319 /* Other method, we must compare the strings */
5320 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005321 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005322
5323 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5324 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5325 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005326 return ACL_PAT_FAIL;
5327 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005328}
5329
5330/* 2. Check on Request/Status Version
5331 * We simply compare strings here.
5332 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005333static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005334{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005335 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005336 if (!pattern->ptr.str)
5337 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005338 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005339 return 1;
5340}
5341
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005342static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005343acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5344 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005345{
5346 struct http_txn *txn = l7;
5347 char *ptr;
5348 int len;
5349
Willy Tarreaub6866442008-07-14 23:54:42 +02005350 if (!txn)
5351 return 0;
5352
Willy Tarreauc11416f2007-06-17 16:58:38 +02005353 if (txn->req.msg_state != HTTP_MSG_BODY)
5354 return 0;
5355
Willy Tarreau8797c062007-05-07 00:55:35 +02005356 len = txn->req.sl.rq.v_l;
5357 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5358
5359 while ((len-- > 0) && (*ptr++ != '/'));
5360 if (len <= 0)
5361 return 0;
5362
5363 test->ptr = ptr;
5364 test->len = len;
5365
5366 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5367 return 1;
5368}
5369
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005370static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005371acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5372 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005373{
5374 struct http_txn *txn = l7;
5375 char *ptr;
5376 int len;
5377
Willy Tarreaub6866442008-07-14 23:54:42 +02005378 if (!txn)
5379 return 0;
5380
Willy Tarreauc11416f2007-06-17 16:58:38 +02005381 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5382 return 0;
5383
Willy Tarreau8797c062007-05-07 00:55:35 +02005384 len = txn->rsp.sl.st.v_l;
5385 ptr = txn->rsp.sol;
5386
5387 while ((len-- > 0) && (*ptr++ != '/'));
5388 if (len <= 0)
5389 return 0;
5390
5391 test->ptr = ptr;
5392 test->len = len;
5393
5394 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5395 return 1;
5396}
5397
5398/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005399static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005400acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5401 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005402{
5403 struct http_txn *txn = l7;
5404 char *ptr;
5405 int len;
5406
Willy Tarreaub6866442008-07-14 23:54:42 +02005407 if (!txn)
5408 return 0;
5409
Willy Tarreauc11416f2007-06-17 16:58:38 +02005410 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5411 return 0;
5412
Willy Tarreau8797c062007-05-07 00:55:35 +02005413 len = txn->rsp.sl.st.c_l;
5414 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5415
5416 test->i = __strl2ui(ptr, len);
5417 test->flags = ACL_TEST_F_VOL_1ST;
5418 return 1;
5419}
5420
5421/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005422static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005423acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5424 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005425{
5426 struct http_txn *txn = l7;
5427
Willy Tarreaub6866442008-07-14 23:54:42 +02005428 if (!txn)
5429 return 0;
5430
Willy Tarreauc11416f2007-06-17 16:58:38 +02005431 if (txn->req.msg_state != HTTP_MSG_BODY)
5432 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005433
Willy Tarreauc11416f2007-06-17 16:58:38 +02005434 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5435 /* ensure the indexes are not affected */
5436 return 0;
5437
Willy Tarreau8797c062007-05-07 00:55:35 +02005438 test->len = txn->req.sl.rq.u_l;
5439 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5440
Willy Tarreauf3d25982007-05-08 22:45:09 +02005441 /* we do not need to set READ_ONLY because the data is in a buffer */
5442 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005443 return 1;
5444}
5445
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005446static int
5447acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5448 struct acl_expr *expr, struct acl_test *test)
5449{
5450 struct http_txn *txn = l7;
5451
Willy Tarreaub6866442008-07-14 23:54:42 +02005452 if (!txn)
5453 return 0;
5454
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005455 if (txn->req.msg_state != HTTP_MSG_BODY)
5456 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005457
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005458 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5459 /* ensure the indexes are not affected */
5460 return 0;
5461
5462 /* Parse HTTP request */
5463 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5464 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5465 test->i = AF_INET;
5466
5467 /*
5468 * If we are parsing url in frontend space, we prepare backend stage
5469 * to not parse again the same url ! optimization lazyness...
5470 */
5471 if (px->options & PR_O_HTTP_PROXY)
5472 l4->flags |= SN_ADDR_SET;
5473
5474 test->flags = ACL_TEST_F_READ_ONLY;
5475 return 1;
5476}
5477
5478static int
5479acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5480 struct acl_expr *expr, struct acl_test *test)
5481{
5482 struct http_txn *txn = l7;
5483
Willy Tarreaub6866442008-07-14 23:54:42 +02005484 if (!txn)
5485 return 0;
5486
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005487 if (txn->req.msg_state != HTTP_MSG_BODY)
5488 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005489
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005490 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5491 /* ensure the indexes are not affected */
5492 return 0;
5493
5494 /* Same optimization as url_ip */
5495 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5496 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5497
5498 if (px->options & PR_O_HTTP_PROXY)
5499 l4->flags |= SN_ADDR_SET;
5500
5501 test->flags = ACL_TEST_F_READ_ONLY;
5502 return 1;
5503}
5504
Willy Tarreauc11416f2007-06-17 16:58:38 +02005505/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5506 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5507 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005508static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005509acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005510 struct acl_expr *expr, struct acl_test *test)
5511{
5512 struct http_txn *txn = l7;
5513 struct hdr_idx *idx = &txn->hdr_idx;
5514 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005515
Willy Tarreaub6866442008-07-14 23:54:42 +02005516 if (!txn)
5517 return 0;
5518
Willy Tarreau33a7e692007-06-10 19:45:56 +02005519 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5520 /* search for header from the beginning */
5521 ctx->idx = 0;
5522
Willy Tarreau33a7e692007-06-10 19:45:56 +02005523 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5524 test->flags |= ACL_TEST_F_FETCH_MORE;
5525 test->flags |= ACL_TEST_F_VOL_HDR;
5526 test->len = ctx->vlen;
5527 test->ptr = (char *)ctx->line + ctx->val;
5528 return 1;
5529 }
5530
5531 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5532 test->flags |= ACL_TEST_F_VOL_HDR;
5533 return 0;
5534}
5535
Willy Tarreau33a7e692007-06-10 19:45:56 +02005536static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005537acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5538 struct acl_expr *expr, struct acl_test *test)
5539{
5540 struct http_txn *txn = l7;
5541
Willy Tarreaub6866442008-07-14 23:54:42 +02005542 if (!txn)
5543 return 0;
5544
Willy Tarreauc11416f2007-06-17 16:58:38 +02005545 if (txn->req.msg_state != HTTP_MSG_BODY)
5546 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005547
Willy Tarreauc11416f2007-06-17 16:58:38 +02005548 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5549 /* ensure the indexes are not affected */
5550 return 0;
5551
5552 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5553}
5554
5555static int
5556acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5557 struct acl_expr *expr, struct acl_test *test)
5558{
5559 struct http_txn *txn = l7;
5560
Willy Tarreaub6866442008-07-14 23:54:42 +02005561 if (!txn)
5562 return 0;
5563
Willy Tarreauc11416f2007-06-17 16:58:38 +02005564 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5565 return 0;
5566
5567 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5568}
5569
5570/* 6. Check on HTTP header count. The number of occurrences is returned.
5571 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5572 */
5573static int
5574acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005575 struct acl_expr *expr, struct acl_test *test)
5576{
5577 struct http_txn *txn = l7;
5578 struct hdr_idx *idx = &txn->hdr_idx;
5579 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005580 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005581
Willy Tarreaub6866442008-07-14 23:54:42 +02005582 if (!txn)
5583 return 0;
5584
Willy Tarreau33a7e692007-06-10 19:45:56 +02005585 ctx.idx = 0;
5586 cnt = 0;
5587 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5588 cnt++;
5589
5590 test->i = cnt;
5591 test->flags = ACL_TEST_F_VOL_HDR;
5592 return 1;
5593}
5594
Willy Tarreauc11416f2007-06-17 16:58:38 +02005595static int
5596acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5597 struct acl_expr *expr, struct acl_test *test)
5598{
5599 struct http_txn *txn = l7;
5600
Willy Tarreaub6866442008-07-14 23:54:42 +02005601 if (!txn)
5602 return 0;
5603
Willy Tarreauc11416f2007-06-17 16:58:38 +02005604 if (txn->req.msg_state != HTTP_MSG_BODY)
5605 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005606
Willy Tarreauc11416f2007-06-17 16:58:38 +02005607 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5608 /* ensure the indexes are not affected */
5609 return 0;
5610
5611 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5612}
5613
5614static int
5615acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5616 struct acl_expr *expr, struct acl_test *test)
5617{
5618 struct http_txn *txn = l7;
5619
Willy Tarreaub6866442008-07-14 23:54:42 +02005620 if (!txn)
5621 return 0;
5622
Willy Tarreauc11416f2007-06-17 16:58:38 +02005623 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5624 return 0;
5625
5626 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5627}
5628
Willy Tarreau33a7e692007-06-10 19:45:56 +02005629/* 7. Check on HTTP header's integer value. The integer value is returned.
5630 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005631 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005632 */
5633static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005634acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005635 struct acl_expr *expr, struct acl_test *test)
5636{
5637 struct http_txn *txn = l7;
5638 struct hdr_idx *idx = &txn->hdr_idx;
5639 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005640
Willy Tarreaub6866442008-07-14 23:54:42 +02005641 if (!txn)
5642 return 0;
5643
Willy Tarreau33a7e692007-06-10 19:45:56 +02005644 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5645 /* search for header from the beginning */
5646 ctx->idx = 0;
5647
Willy Tarreau33a7e692007-06-10 19:45:56 +02005648 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5649 test->flags |= ACL_TEST_F_FETCH_MORE;
5650 test->flags |= ACL_TEST_F_VOL_HDR;
5651 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5652 return 1;
5653 }
5654
5655 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5656 test->flags |= ACL_TEST_F_VOL_HDR;
5657 return 0;
5658}
5659
Willy Tarreauc11416f2007-06-17 16:58:38 +02005660static int
5661acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5662 struct acl_expr *expr, struct acl_test *test)
5663{
5664 struct http_txn *txn = l7;
5665
Willy Tarreaub6866442008-07-14 23:54:42 +02005666 if (!txn)
5667 return 0;
5668
Willy Tarreauc11416f2007-06-17 16:58:38 +02005669 if (txn->req.msg_state != HTTP_MSG_BODY)
5670 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005671
Willy Tarreauc11416f2007-06-17 16:58:38 +02005672 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5673 /* ensure the indexes are not affected */
5674 return 0;
5675
5676 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5677}
5678
5679static int
5680acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5681 struct acl_expr *expr, struct acl_test *test)
5682{
5683 struct http_txn *txn = l7;
5684
Willy Tarreaub6866442008-07-14 23:54:42 +02005685 if (!txn)
5686 return 0;
5687
Willy Tarreauc11416f2007-06-17 16:58:38 +02005688 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5689 return 0;
5690
5691 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5692}
5693
Willy Tarreau737b0c12007-06-10 21:28:46 +02005694/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5695 * the first '/' after the possible hostname, and ends before the possible '?'.
5696 */
5697static int
5698acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5699 struct acl_expr *expr, struct acl_test *test)
5700{
5701 struct http_txn *txn = l7;
5702 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005703
Willy Tarreaub6866442008-07-14 23:54:42 +02005704 if (!txn)
5705 return 0;
5706
Willy Tarreauc11416f2007-06-17 16:58:38 +02005707 if (txn->req.msg_state != HTTP_MSG_BODY)
5708 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005709
Willy Tarreauc11416f2007-06-17 16:58:38 +02005710 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5711 /* ensure the indexes are not affected */
5712 return 0;
5713
Willy Tarreau21d2af32008-02-14 20:25:24 +01005714 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5715 ptr = http_get_path(txn);
5716 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005717 return 0;
5718
5719 /* OK, we got the '/' ! */
5720 test->ptr = ptr;
5721
5722 while (ptr < end && *ptr != '?')
5723 ptr++;
5724
5725 test->len = ptr - test->ptr;
5726
5727 /* we do not need to set READ_ONLY because the data is in a buffer */
5728 test->flags = ACL_TEST_F_VOL_1ST;
5729 return 1;
5730}
5731
5732
Willy Tarreau8797c062007-05-07 00:55:35 +02005733
5734/************************************************************************/
5735/* All supported keywords must be declared here. */
5736/************************************************************************/
5737
5738/* Note: must not be declared <const> as its list will be overwritten */
5739static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005740 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5741 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5742 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5743 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005744
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005745 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5746 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5747 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5748 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5749 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5750 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5751 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5752 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5753 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005754
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005755 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5756 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5757 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5758 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5759 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5760 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5761 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5762 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5763 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5764 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005765
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005766 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5767 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5768 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5769 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5770 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5771 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5772 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5773 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5774 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005775
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005776 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5777 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5778 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5779 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5780 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5781 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5782 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005783
Willy Tarreauf3d25982007-05-08 22:45:09 +02005784 { NULL, NULL, NULL, NULL },
5785
5786#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005787 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5788 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5789 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5790 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5791 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5792 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5793 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5794
Willy Tarreau8797c062007-05-07 00:55:35 +02005795 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5796 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5797 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5798 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5799 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5800 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5801 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5802 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5803
5804 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5805 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5806 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5807 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5808 { NULL, NULL, NULL, NULL },
5809#endif
5810}};
5811
5812
5813__attribute__((constructor))
5814static void __http_protocol_init(void)
5815{
5816 acl_register_keywords(&acl_kws);
5817}
5818
5819
Willy Tarreau58f10d72006-12-04 02:26:12 +01005820/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005821 * Local variables:
5822 * c-indent-level: 8
5823 * c-basic-offset: 8
5824 * End:
5825 */