blob: 3a40778eed7206a8b0e2c9434f892d35ac2e0be0 [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;
691 if (s->analysis & AN_REQ_ANY) {
692 rqf = s->req->flags;
693 rpf = s->rep->flags;
694
695 if (process_request(s))
696 resync |= PROCESS_REQ;
697
698 if (!(s->analysis & AN_REQ_ANY) && !(s->req->flags & BF_MAY_FORWARD))
699 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;
708 if (s->analysis & AN_RTR_ANY) {
709 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 Tarreaudafde432008-08-17 01:00:46 +0200715 if (!(s->analysis & AN_RTR_ANY) && !(s->rep->flags & BF_MAY_FORWARD))
716 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 Tarreau67f0eea2008-08-10 22:55:22 +0200770 if (s->analysis & AN_REQ_ANY) {
771 if (s->analysis & AN_REQ_INSPECT)
772 t->expire = tick_first(t->expire, s->inspect_exp);
773 else if (s->analysis & AN_REQ_HTTP_HDR)
774 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 Tarreau45e73e32006-12-17 00:05:15 +0100797 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100799 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800 write(1, trash, len);
801 }
802
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200803 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100804 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
806 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200807 if (s->logs.logwait &&
808 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200809 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
810 if (s->fe->to_log & LW_REQ)
811 http_sess_log(s);
812 else
813 tcp_sess_log(s);
814 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815
816 /* the task MUST not be in the run queue anymore */
817 task_delete(t);
818 session_free(s);
819 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200820 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821}
822
823
Willy Tarreau42250582007-04-01 01:30:43 +0200824extern const char sess_term_cond[8];
825extern const char sess_fin_state[8];
826extern const char *monthname[12];
827const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
828const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
829 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
830 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200831struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200832struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100833
Willy Tarreau42250582007-04-01 01:30:43 +0200834/*
835 * send a log for the session when we have enough info about it.
836 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100837 */
Willy Tarreau42250582007-04-01 01:30:43 +0200838static void http_sess_log(struct session *s)
839{
840 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
841 struct proxy *fe = s->fe;
842 struct proxy *be = s->be;
843 struct proxy *prx_log;
844 struct http_txn *txn = &s->txn;
845 int tolog;
846 char *uri, *h;
847 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200848 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200849 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200850 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200851 int hdr;
852
853 if (fe->logfac1 < 0 && fe->logfac2 < 0)
854 return;
855 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100856
Willy Tarreau42250582007-04-01 01:30:43 +0200857 if (s->cli_addr.ss_family == AF_INET)
858 inet_ntop(AF_INET,
859 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
860 pn, sizeof(pn));
861 else
862 inet_ntop(AF_INET6,
863 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
864 pn, sizeof(pn));
865
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200866 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200867
868 /* FIXME: let's limit ourselves to frontend logging for now. */
869 tolog = fe->to_log;
870
871 h = tmpline;
872 if (fe->to_log & LW_REQHDR &&
873 txn->req.cap &&
874 (h < tmpline + sizeof(tmpline) - 10)) {
875 *(h++) = ' ';
876 *(h++) = '{';
877 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
878 if (hdr)
879 *(h++) = '|';
880 if (txn->req.cap[hdr] != NULL)
881 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
882 '#', hdr_encode_map, txn->req.cap[hdr]);
883 }
884 *(h++) = '}';
885 }
886
887 if (fe->to_log & LW_RSPHDR &&
888 txn->rsp.cap &&
889 (h < tmpline + sizeof(tmpline) - 7)) {
890 *(h++) = ' ';
891 *(h++) = '{';
892 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
893 if (hdr)
894 *(h++) = '|';
895 if (txn->rsp.cap[hdr] != NULL)
896 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
897 '#', hdr_encode_map, txn->rsp.cap[hdr]);
898 }
899 *(h++) = '}';
900 }
901
902 if (h < tmpline + sizeof(tmpline) - 4) {
903 *(h++) = ' ';
904 *(h++) = '"';
905 uri = txn->uri ? txn->uri : "<BADREQ>";
906 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
907 '#', url_encode_map, uri);
908 *(h++) = '"';
909 }
910 *h = '\0';
911
912 svid = (tolog & LW_SVID) ?
913 (s->data_source != DATA_SRC_STATS) ?
914 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
915
Willy Tarreau70089872008-06-13 21:12:51 +0200916 t_request = -1;
917 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
918 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
919
Willy Tarreau42250582007-04-01 01:30:43 +0200920 send_log(prx_log, LOG_INFO,
921 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
922 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100923 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200924 pn,
925 (s->cli_addr.ss_family == AF_INET) ?
926 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
927 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200928 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200929 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200930 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200931 t_request,
932 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200933 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
934 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
935 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
936 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100937 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200938 txn->cli_cookie ? txn->cli_cookie : "-",
939 txn->srv_cookie ? txn->srv_cookie : "-",
940 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
941 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
942 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
943 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
944 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100945 (s->flags & SN_REDISP)?"+":"",
946 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200947 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
948
949 s->logs.logwait = 0;
950}
951
Willy Tarreau117f59e2007-03-04 18:17:17 +0100952
953/*
954 * Capture headers from message starting at <som> according to header list
955 * <cap_hdr>, and fill the <idx> structure appropriately.
956 */
957void capture_headers(char *som, struct hdr_idx *idx,
958 char **cap, struct cap_hdr *cap_hdr)
959{
960 char *eol, *sol, *col, *sov;
961 int cur_idx;
962 struct cap_hdr *h;
963 int len;
964
965 sol = som + hdr_idx_first_pos(idx);
966 cur_idx = hdr_idx_first_idx(idx);
967
968 while (cur_idx) {
969 eol = sol + idx->v[cur_idx].len;
970
971 col = sol;
972 while (col < eol && *col != ':')
973 col++;
974
975 sov = col + 1;
976 while (sov < eol && http_is_lws[(unsigned char)*sov])
977 sov++;
978
979 for (h = cap_hdr; h; h = h->next) {
980 if ((h->namelen == col - sol) &&
981 (strncasecmp(sol, h->name, h->namelen) == 0)) {
982 if (cap[h->index] == NULL)
983 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200984 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100985
986 if (cap[h->index] == NULL) {
987 Alert("HTTP capture : out of memory.\n");
988 continue;
989 }
990
991 len = eol - sov;
992 if (len > h->len)
993 len = h->len;
994
995 memcpy(cap[h->index], sov, len);
996 cap[h->index][len]=0;
997 }
998 }
999 sol = eol + idx->v[cur_idx].cr + 1;
1000 cur_idx = idx->v[cur_idx].next;
1001 }
1002}
1003
1004
Willy Tarreau42250582007-04-01 01:30:43 +02001005/* either we find an LF at <ptr> or we jump to <bad>.
1006 */
1007#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1008
1009/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1010 * otherwise to <http_msg_ood> with <state> set to <st>.
1011 */
1012#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1013 ptr++; \
1014 if (likely(ptr < end)) \
1015 goto good; \
1016 else { \
1017 state = (st); \
1018 goto http_msg_ood; \
1019 } \
1020 } while (0)
1021
1022
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001024 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001025 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1026 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1027 * will give undefined results.
1028 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1029 * and that msg->sol points to the beginning of the response.
1030 * If a complete line is found (which implies that at least one CR or LF is
1031 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1032 * returned indicating an incomplete line (which does not mean that parts have
1033 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1034 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1035 * upon next call.
1036 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001037 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001038 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1039 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001040 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001041 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001042const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1043 unsigned int state, const char *ptr, const char *end,
1044 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001045{
1046 __label__
1047 http_msg_rpver,
1048 http_msg_rpver_sp,
1049 http_msg_rpcode,
1050 http_msg_rpcode_sp,
1051 http_msg_rpreason,
1052 http_msg_rpline_eol,
1053 http_msg_ood, /* out of data */
1054 http_msg_invalid;
1055
1056 switch (state) {
1057 http_msg_rpver:
1058 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001059 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001060 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1061
1062 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001063 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1065 }
1066 goto http_msg_invalid;
1067
1068 http_msg_rpver_sp:
1069 case HTTP_MSG_RPVER_SP:
1070 if (likely(!HTTP_IS_LWS(*ptr))) {
1071 msg->sl.st.c = ptr - msg_buf;
1072 goto http_msg_rpcode;
1073 }
1074 if (likely(HTTP_IS_SPHT(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1076 /* so it's a CR/LF, this is invalid */
1077 goto http_msg_invalid;
1078
1079 http_msg_rpcode:
1080 case HTTP_MSG_RPCODE:
1081 if (likely(!HTTP_IS_LWS(*ptr)))
1082 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1083
1084 if (likely(HTTP_IS_SPHT(*ptr))) {
1085 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1086 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1087 }
1088
1089 /* so it's a CR/LF, so there is no reason phrase */
1090 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1091 http_msg_rsp_reason:
1092 /* FIXME: should we support HTTP responses without any reason phrase ? */
1093 msg->sl.st.r = ptr - msg_buf;
1094 msg->sl.st.r_l = 0;
1095 goto http_msg_rpline_eol;
1096
1097 http_msg_rpcode_sp:
1098 case HTTP_MSG_RPCODE_SP:
1099 if (likely(!HTTP_IS_LWS(*ptr))) {
1100 msg->sl.st.r = ptr - msg_buf;
1101 goto http_msg_rpreason;
1102 }
1103 if (likely(HTTP_IS_SPHT(*ptr)))
1104 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1105 /* so it's a CR/LF, so there is no reason phrase */
1106 goto http_msg_rsp_reason;
1107
1108 http_msg_rpreason:
1109 case HTTP_MSG_RPREASON:
1110 if (likely(!HTTP_IS_CRLF(*ptr)))
1111 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1112 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1113 http_msg_rpline_eol:
1114 /* We have seen the end of line. Note that we do not
1115 * necessarily have the \n yet, but at least we know that we
1116 * have EITHER \r OR \n, otherwise the response would not be
1117 * complete. We can then record the response length and return
1118 * to the caller which will be able to register it.
1119 */
1120 msg->sl.st.l = ptr - msg->sol;
1121 return ptr;
1122
1123#ifdef DEBUG_FULL
1124 default:
1125 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1126 exit(1);
1127#endif
1128 }
1129
1130 http_msg_ood:
1131 /* out of data */
1132 if (ret_state)
1133 *ret_state = state;
1134 if (ret_ptr)
1135 *ret_ptr = (char *)ptr;
1136 return NULL;
1137
1138 http_msg_invalid:
1139 /* invalid message */
1140 if (ret_state)
1141 *ret_state = HTTP_MSG_ERROR;
1142 return NULL;
1143}
1144
1145
1146/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 * This function parses a request line between <ptr> and <end>, starting with
1148 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1149 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1150 * will give undefined results.
1151 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1152 * and that msg->sol points to the beginning of the request.
1153 * If a complete line is found (which implies that at least one CR or LF is
1154 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1155 * returned indicating an incomplete line (which does not mean that parts have
1156 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1157 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1158 * upon next call.
1159 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001160 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1162 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001163 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001165const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1166 unsigned int state, const char *ptr, const char *end,
1167 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 __label__
1170 http_msg_rqmeth,
1171 http_msg_rqmeth_sp,
1172 http_msg_rquri,
1173 http_msg_rquri_sp,
1174 http_msg_rqver,
1175 http_msg_rqline_eol,
1176 http_msg_ood, /* out of data */
1177 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 switch (state) {
1180 http_msg_rqmeth:
1181 case HTTP_MSG_RQMETH:
1182 if (likely(HTTP_IS_TOKEN(*ptr)))
1183 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001186 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1188 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 if (likely(HTTP_IS_CRLF(*ptr))) {
1191 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001192 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 http_msg_req09_uri:
1194 msg->sl.rq.u = ptr - msg_buf;
1195 http_msg_req09_uri_e:
1196 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1197 http_msg_req09_ver:
1198 msg->sl.rq.v = ptr - msg_buf;
1199 msg->sl.rq.v_l = 0;
1200 goto http_msg_rqline_eol;
1201 }
1202 goto http_msg_invalid;
1203
1204 http_msg_rqmeth_sp:
1205 case HTTP_MSG_RQMETH_SP:
1206 if (likely(!HTTP_IS_LWS(*ptr))) {
1207 msg->sl.rq.u = ptr - msg_buf;
1208 goto http_msg_rquri;
1209 }
1210 if (likely(HTTP_IS_SPHT(*ptr)))
1211 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1212 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1213 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 http_msg_rquri:
1216 case HTTP_MSG_RQURI:
1217 if (likely(!HTTP_IS_LWS(*ptr)))
1218 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001220 if (likely(HTTP_IS_SPHT(*ptr))) {
1221 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1222 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001224
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1226 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001227
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 http_msg_rquri_sp:
1229 case HTTP_MSG_RQURI_SP:
1230 if (likely(!HTTP_IS_LWS(*ptr))) {
1231 msg->sl.rq.v = ptr - msg_buf;
1232 goto http_msg_rqver;
1233 }
1234 if (likely(HTTP_IS_SPHT(*ptr)))
1235 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1236 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1237 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001238
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 http_msg_rqver:
1240 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001241 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001243
1244 if (likely(HTTP_IS_CRLF(*ptr))) {
1245 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1246 http_msg_rqline_eol:
1247 /* We have seen the end of line. Note that we do not
1248 * necessarily have the \n yet, but at least we know that we
1249 * have EITHER \r OR \n, otherwise the request would not be
1250 * complete. We can then record the request length and return
1251 * to the caller which will be able to register it.
1252 */
1253 msg->sl.rq.l = ptr - msg->sol;
1254 return ptr;
1255 }
1256
1257 /* neither an HTTP_VER token nor a CRLF */
1258 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260#ifdef DEBUG_FULL
1261 default:
1262 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1263 exit(1);
1264#endif
1265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 http_msg_ood:
1268 /* out of data */
1269 if (ret_state)
1270 *ret_state = state;
1271 if (ret_ptr)
1272 *ret_ptr = (char *)ptr;
1273 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 http_msg_invalid:
1276 /* invalid message */
1277 if (ret_state)
1278 *ret_state = HTTP_MSG_ERROR;
1279 return NULL;
1280}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
1282
Willy Tarreau8973c702007-01-21 23:58:29 +01001283/*
1284 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001285 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001286 * when data are missing and recalled at the exact same location with no
1287 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001288 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1289 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1292{
1293 __label__
1294 http_msg_rqbefore,
1295 http_msg_rqbefore_cr,
1296 http_msg_rqmeth,
1297 http_msg_rqline_end,
1298 http_msg_hdr_first,
1299 http_msg_hdr_name,
1300 http_msg_hdr_l1_sp,
1301 http_msg_hdr_l1_lf,
1302 http_msg_hdr_l1_lws,
1303 http_msg_hdr_val,
1304 http_msg_hdr_l2_lf,
1305 http_msg_hdr_l2_lws,
1306 http_msg_complete_header,
1307 http_msg_last_lf,
1308 http_msg_ood, /* out of data */
1309 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreaue69eada2008-01-27 00:34:10 +01001311 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001313
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 ptr = buf->lr;
1316 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 if (unlikely(ptr >= end))
1319 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 /*
1323 * First, states that are specific to the response only.
1324 * We check them first so that request and headers are
1325 * closer to each other (accessed more often).
1326 */
1327 http_msg_rpbefore:
1328 case HTTP_MSG_RPBEFORE:
1329 if (likely(HTTP_IS_TOKEN(*ptr))) {
1330 if (likely(ptr == buf->data)) {
1331 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001332 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 } else {
1334#if PARSE_PRESERVE_EMPTY_LINES
1335 /* only skip empty leading lines, don't remove them */
1336 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338#else
1339 /* Remove empty leading lines, as recommended by
1340 * RFC2616. This takes a lot of time because we
1341 * must move all the buffer backwards, but this
1342 * is rarely needed. The method above will be
1343 * cleaner when we'll be able to start sending
1344 * the request from any place in the buffer.
1345 */
1346 buf->lr = ptr;
1347 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001348 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 msg->sol = buf->data;
1350 ptr = buf->data;
1351 end = buf->r;
1352#endif
1353 }
1354 hdr_idx_init(idx);
1355 state = HTTP_MSG_RPVER;
1356 goto http_msg_rpver;
1357 }
1358
1359 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1360 goto http_msg_invalid;
1361
1362 if (unlikely(*ptr == '\n'))
1363 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1365 /* stop here */
1366
1367 http_msg_rpbefore_cr:
1368 case HTTP_MSG_RPBEFORE_CR:
1369 EXPECT_LF_HERE(ptr, http_msg_invalid);
1370 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1371 /* stop here */
1372
1373 http_msg_rpver:
1374 case HTTP_MSG_RPVER:
1375 case HTTP_MSG_RPVER_SP:
1376 case HTTP_MSG_RPCODE:
1377 case HTTP_MSG_RPCODE_SP:
1378 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001379 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001380 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 if (unlikely(!ptr))
1382 return;
1383
1384 /* we have a full response and we know that we have either a CR
1385 * or an LF at <ptr>.
1386 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001387 //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 +01001388 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1389
1390 msg->sol = ptr;
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1393 goto http_msg_rpline_end;
1394
1395 http_msg_rpline_end:
1396 case HTTP_MSG_RPLINE_END:
1397 /* msg->sol must point to the first of CR or LF. */
1398 EXPECT_LF_HERE(ptr, http_msg_invalid);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1400 /* stop here */
1401
1402 /*
1403 * Second, states that are specific to the request only
1404 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 http_msg_rqbefore:
1406 case HTTP_MSG_RQBEFORE:
1407 if (likely(HTTP_IS_TOKEN(*ptr))) {
1408 if (likely(ptr == buf->data)) {
1409 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001410 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 } else {
1412#if PARSE_PRESERVE_EMPTY_LINES
1413 /* only skip empty leading lines, don't remove them */
1414 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001415 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 /* Remove empty leading lines, as recommended by
1418 * RFC2616. This takes a lot of time because we
1419 * must move all the buffer backwards, but this
1420 * is rarely needed. The method above will be
1421 * cleaner when we'll be able to start sending
1422 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 buf->lr = ptr;
1425 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001426 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 msg->sol = buf->data;
1428 ptr = buf->data;
1429 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001430#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001432 /* we will need this when keep-alive will be supported
1433 hdr_idx_init(idx);
1434 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 state = HTTP_MSG_RQMETH;
1436 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1440 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (unlikely(*ptr == '\n'))
1443 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_rqbefore_cr:
1448 case HTTP_MSG_RQBEFORE_CR:
1449 EXPECT_LF_HERE(ptr, http_msg_invalid);
1450 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 http_msg_rqmeth:
1454 case HTTP_MSG_RQMETH:
1455 case HTTP_MSG_RQMETH_SP:
1456 case HTTP_MSG_RQURI:
1457 case HTTP_MSG_RQURI_SP:
1458 case HTTP_MSG_RQVER:
1459 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001460 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (unlikely(!ptr))
1462 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 /* we have a full request and we know that we have either a CR
1465 * or an LF at <ptr>.
1466 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001467 //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 +01001468 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 msg->sol = ptr;
1471 if (likely(*ptr == '\r'))
1472 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 http_msg_rqline_end:
1476 case HTTP_MSG_RQLINE_END:
1477 /* check for HTTP/0.9 request : no version information available.
1478 * msg->sol must point to the first of CR or LF.
1479 */
1480 if (unlikely(msg->sl.rq.v_l == 0))
1481 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 EXPECT_LF_HERE(ptr, http_msg_invalid);
1484 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 /*
1488 * Common states below
1489 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 http_msg_hdr_first:
1491 case HTTP_MSG_HDR_FIRST:
1492 msg->sol = ptr;
1493 if (likely(!HTTP_IS_CRLF(*ptr))) {
1494 goto http_msg_hdr_name;
1495 }
1496
1497 if (likely(*ptr == '\r'))
1498 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1499 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 http_msg_hdr_name:
1502 case HTTP_MSG_HDR_NAME:
1503 /* assumes msg->sol points to the first char */
1504 if (likely(HTTP_IS_TOKEN(*ptr)))
1505 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (likely(*ptr == ':')) {
1508 msg->col = ptr - buf->data;
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 http_msg_hdr_l1_sp:
1515 case HTTP_MSG_HDR_L1_SP:
1516 /* assumes msg->sol points to the first char and msg->col to the colon */
1517 if (likely(HTTP_IS_SPHT(*ptr)))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 /* header value can be basically anything except CR/LF */
1521 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (likely(!HTTP_IS_CRLF(*ptr))) {
1524 goto http_msg_hdr_val;
1525 }
1526
1527 if (likely(*ptr == '\r'))
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1529 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 http_msg_hdr_l1_lf:
1532 case HTTP_MSG_HDR_L1_LF:
1533 EXPECT_LF_HERE(ptr, http_msg_invalid);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 http_msg_hdr_l1_lws:
1537 case HTTP_MSG_HDR_L1_LWS:
1538 if (likely(HTTP_IS_SPHT(*ptr))) {
1539 /* replace HT,CR,LF with spaces */
1540 for (; buf->data+msg->sov < ptr; msg->sov++)
1541 buf->data[msg->sov] = ' ';
1542 goto http_msg_hdr_l1_sp;
1543 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001544 /* we had a header consisting only in spaces ! */
1545 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 goto http_msg_complete_header;
1547
1548 http_msg_hdr_val:
1549 case HTTP_MSG_HDR_VAL:
1550 /* assumes msg->sol points to the first char, msg->col to the
1551 * colon, and msg->sov points to the first character of the
1552 * value.
1553 */
1554 if (likely(!HTTP_IS_CRLF(*ptr)))
1555 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 msg->eol = ptr;
1558 /* Note: we could also copy eol into ->eoh so that we have the
1559 * real header end in case it ends with lots of LWS, but is this
1560 * really needed ?
1561 */
1562 if (likely(*ptr == '\r'))
1563 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1564 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 http_msg_hdr_l2_lf:
1567 case HTTP_MSG_HDR_L2_LF:
1568 EXPECT_LF_HERE(ptr, http_msg_invalid);
1569 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 http_msg_hdr_l2_lws:
1572 case HTTP_MSG_HDR_L2_LWS:
1573 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1574 /* LWS: replace HT,CR,LF with spaces */
1575 for (; msg->eol < ptr; msg->eol++)
1576 *msg->eol = ' ';
1577 goto http_msg_hdr_val;
1578 }
1579 http_msg_complete_header:
1580 /*
1581 * It was a new header, so the last one is finished.
1582 * Assumes msg->sol points to the first char, msg->col to the
1583 * colon, msg->sov points to the first character of the value
1584 * and msg->eol to the first CR or LF so we know how the line
1585 * ends. We insert last header into the index.
1586 */
1587 /*
1588 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1589 write(2, msg->sol, msg->eol-msg->sol);
1590 fprintf(stderr,"\n");
1591 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1594 idx, idx->tail) < 0))
1595 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 msg->sol = ptr;
1598 if (likely(!HTTP_IS_CRLF(*ptr))) {
1599 goto http_msg_hdr_name;
1600 }
1601
1602 if (likely(*ptr == '\r'))
1603 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1604 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 http_msg_last_lf:
1607 case HTTP_MSG_LAST_LF:
1608 /* Assumes msg->sol points to the first of either CR or LF */
1609 EXPECT_LF_HERE(ptr, http_msg_invalid);
1610 ptr++;
1611 buf->lr = ptr;
1612 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001613 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 return;
1615#ifdef DEBUG_FULL
1616 default:
1617 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1618 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001619#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 }
1621 http_msg_ood:
1622 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001623 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 buf->lr = ptr;
1625 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 http_msg_invalid:
1628 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001629 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 return;
1631}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001632
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001633/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001634 * It normally returns zero, but may return 1 if it absolutely needs to be
1635 * called again after other functions. It relies on buffers flags, and updates
1636 * t->analysis. It might make sense to explode it into several other functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001638int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 struct buffer *req = t->req;
1641 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001642
Willy Tarreaue46ab552008-08-13 19:19:37 +02001643 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
1644 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001645 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001646 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1647 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001648 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001649
Willy Tarreaudafde432008-08-17 01:00:46 +02001650 update_state:
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001651 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001652 struct tcp_rule *rule;
1653 int partial;
1654
1655 /* We will abort if we encounter a read error. In theory,
1656 * we should not abort if we get a close, it might be
1657 * valid, also very unlikely. FIXME: we'll abort for now,
1658 * this will be easier to change later.
1659 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001660 if (req->flags & BF_READ_ERROR) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001661 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001662 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001663 t->fe->failed_req++;
1664 if (!(t->flags & SN_ERR_MASK))
1665 t->flags |= SN_ERR_CLICL;
1666 if (!(t->flags & SN_FINST_MASK))
1667 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001668 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001669 }
1670
1671 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001672 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001673 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001674 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001675 t->fe->failed_req++;
1676 if (!(t->flags & SN_ERR_MASK))
1677 t->flags |= SN_ERR_CLITO;
1678 if (!(t->flags & SN_FINST_MASK))
1679 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001680 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001681 }
1682
1683 /* We don't know whether we have enough data, so must proceed
1684 * this way :
1685 * - iterate through all rules in their declaration order
1686 * - if one rule returns MISS, it means the inspect delay is
1687 * not over yet, then return immediately, otherwise consider
1688 * it as a non-match.
1689 * - if one rule returns OK, then return OK
1690 * - if one rule returns KO, then return KO
1691 */
1692
Willy Tarreauba392ce2008-08-16 21:13:23 +02001693 if (req->flags & (BF_READ_NULL | BF_SHUTR) ||
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001694 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001695 partial = 0;
1696 else
1697 partial = ACL_PARTIAL;
1698
1699 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1700 int ret = ACL_PAT_PASS;
1701
1702 if (rule->cond) {
1703 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1704 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001705 /* just set the request timeout once at the beginning of the request */
1706 if (!tick_isset(t->inspect_exp))
1707 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001708 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001709 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001710
Willy Tarreaub6866442008-07-14 23:54:42 +02001711 ret = acl_pass(ret);
1712 if (rule->cond->pol == ACL_COND_UNLESS)
1713 ret = !ret;
1714 }
1715
1716 if (ret) {
1717 /* we have a matching rule. */
1718 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02001719 buffer_shutr(req);
1720 buffer_shutw(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001721 fd_delete(t->cli_fd);
1722 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001723 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001724 t->fe->failed_req++;
1725 if (!(t->flags & SN_ERR_MASK))
1726 t->flags |= SN_ERR_PRXCOND;
1727 if (!(t->flags & SN_FINST_MASK))
1728 t->flags |= SN_FINST_R;
1729 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02001730 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001731 }
1732 /* otherwise accept */
1733 break;
1734 }
1735 }
1736
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001737 /* if we get there, it means we have no rule which matches, or
1738 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001739 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001740 t->analysis &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001741 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001743
1744 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 /*
1746 * Now parse the partial (or complete) lines.
1747 * We will check the request syntax, and also join multi-line
1748 * headers. An index of all the lines will be elaborated while
1749 * parsing.
1750 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001751 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001754 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 * req->data + req->eoh = end of processed headers / start of current one
1756 * req->data + req->eol = end of current header or line (LF or CRLF)
1757 * req->lr = first non-visited byte
1758 * req->r = end of data
1759 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001762 struct http_txn *txn = &t->txn;
1763 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001767 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 /* 1: we might have to print this header in debug mode */
1770 if (unlikely((global.mode & MODE_DEBUG) &&
1771 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001772 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001774
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001775 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 eol = sol + msg->sl.rq.l;
1777 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001778
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001779 sol += hdr_idx_first_pos(&txn->hdr_idx);
1780 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001783 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001785 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1786 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 }
1788 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreau58f10d72006-12-04 02:26:12 +01001790
1791 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793 * If not so, we check the FD and buffer states before leaving.
1794 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1796 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001797 *
1798 */
1799
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001800 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 /*
1802 * First, let's catch bad requests.
1803 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001804 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001806
1807 /* 1: Since we are in header mode, if there's no space
1808 * left for headers, we won't be able to free more
1809 * later, so the session will never terminate. We
1810 * must terminate it now.
1811 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001812 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 /* FIXME: check if URI is set and return Status
1814 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001815 */
Willy Tarreau06619262006-12-17 08:37:22 +01001816 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817 }
1818
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001819 /* 2: have we encountered a close ? */
1820 else if (req->flags & BF_READ_NULL) {
1821 txn->status = 400;
1822 client_retnclose(t, error_message(t, HTTP_ERR_400));
1823 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001824 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001825 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001826
Willy Tarreau58f10d72006-12-04 02:26:12 +01001827 if (!(t->flags & SN_ERR_MASK))
1828 t->flags |= SN_ERR_CLICL;
1829 if (!(t->flags & SN_FINST_MASK))
1830 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001831 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001832 }
1833
1834 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001835 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001837 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001838 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001839 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001840 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001841 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001842 if (!(t->flags & SN_ERR_MASK))
1843 t->flags |= SN_ERR_CLITO;
1844 if (!(t->flags & SN_FINST_MASK))
1845 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001846 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001847 }
1848
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001849 /* 4: have we encountered a read error or did we have to shutdown ? */
Willy Tarreauba392ce2008-08-16 21:13:23 +02001850 else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001851 /* we cannot return any message on error */
1852 msg->msg_state = HTTP_MSG_ERROR;
1853 t->analysis &= ~AN_REQ_ANY;
1854 t->fe->failed_req++;
1855 if (!(t->flags & SN_ERR_MASK))
1856 t->flags |= SN_ERR_CLICL;
1857 if (!(t->flags & SN_FINST_MASK))
1858 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001859 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001860 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001861
1862 /* just set the request timeout once at the beginning of the request */
1863 if (!tick_isset(t->txn.exp))
1864 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1865
1866 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001867 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001868 }
1869
1870
1871 /****************************************************************
1872 * More interesting part now : we know that we have a complete *
1873 * request which at least looks like HTTP. We have an indicator *
1874 * of each header's length, so we can parse them quickly. *
1875 ****************************************************************/
1876
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001877 t->analysis &= ~AN_REQ_HTTP_HDR;
1878
Willy Tarreau9cdde232007-05-02 20:58:19 +02001879 /* ensure we keep this pointer to the beginning of the message */
1880 msg->sol = req->data + msg->som;
1881
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 /*
1883 * 1: identify the method
1884 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001885 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001886
1887 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001889 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001891 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 if (unlikely((t->fe->monitor_uri_len != 0) &&
1893 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1894 !memcmp(&req->data[msg->sl.rq.u],
1895 t->fe->monitor_uri,
1896 t->fe->monitor_uri_len))) {
1897 /*
1898 * We have found the monitor URI
1899 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001900 struct acl_cond *cond;
1901 cur_proxy = t->fe;
1902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001904
1905 /* Check if we want to fail this monitor request or not */
1906 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1907 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001908
1909 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001910 if (cond->pol == ACL_COND_UNLESS)
1911 ret = !ret;
1912
1913 if (ret) {
1914 /* we fail this request, let's return 503 service unavail */
1915 txn->status = 503;
1916 client_retnclose(t, error_message(t, HTTP_ERR_503));
1917 goto return_prx_cond;
1918 }
1919 }
1920
1921 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001922 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 client_retnclose(t, &http_200_chunk);
1924 goto return_prx_cond;
1925 }
1926
1927 /*
1928 * 3: Maybe we have to copy the original REQURI for the logs ?
1929 * Note: we cannot log anymore if the request has been
1930 * classified as invalid.
1931 */
1932 if (unlikely(t->logs.logwait & LW_REQ)) {
1933 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001934 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 int urilen = msg->sl.rq.l;
1936
1937 if (urilen >= REQURI_LEN)
1938 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001939 memcpy(txn->uri, &req->data[msg->som], urilen);
1940 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001941
1942 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001943 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 } else {
1945 Alert("HTTP logging : out of memory.\n");
1946 }
1947 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001948
Willy Tarreau06619262006-12-17 08:37:22 +01001949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001950 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1951 if (unlikely(msg->sl.rq.v_l == 0)) {
1952 int delta;
1953 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001954 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001955 cur_end = msg->sol + msg->sl.rq.l;
1956 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001958 if (msg->sl.rq.u_l == 0) {
1959 /* if no URI was set, add "/" */
1960 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1961 cur_end += delta;
1962 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001963 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 /* add HTTP version */
1965 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1966 msg->eoh += delta;
1967 cur_end += delta;
1968 cur_end = (char *)http_parse_reqline(msg, req->data,
1969 HTTP_MSG_RQMETH,
1970 msg->sol, cur_end + 1,
1971 NULL, NULL);
1972 if (unlikely(!cur_end))
1973 goto return_bad_req;
1974
1975 /* we have a full HTTP/1.0 request now and we know that
1976 * we have either a CR or an LF at <ptr>.
1977 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001978 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001979 }
1980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001981
1982 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001983 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001984 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001985 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001986
1987 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001989 * As opposed to version 1.2, now they will be evaluated in the
1990 * filters order and not in the header order. This means that
1991 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001992 *
1993 * We can now check whether we want to switch to another
1994 * backend, in which case we will re-check the backend's
1995 * filters and various options. In order to support 3-level
1996 * switching, here's how we should proceed :
1997 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001998 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001999 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002000 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002001 * There cannot be any switch from there, so ->be cannot be
2002 * changed anymore.
2003 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002004 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002005 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002006 * The response path will be able to apply either ->be, or
2007 * ->be then ->fe filters in order to match the reverse of
2008 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002009 */
2010
Willy Tarreau06619262006-12-17 08:37:22 +01002011 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002012 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002013 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002014 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002015 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002016
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002017 /* first check whether we have some ACLs set to redirect this request */
2018 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2019 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002020
2021 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002022 if (rule->cond->pol == ACL_COND_UNLESS)
2023 ret = !ret;
2024
2025 if (ret) {
2026 struct chunk rdr = { trash, 0 };
2027 const char *msg_fmt;
2028
2029 /* build redirect message */
2030 switch(rule->code) {
2031 case 303:
2032 rdr.len = strlen(HTTP_303);
2033 msg_fmt = HTTP_303;
2034 break;
2035 case 301:
2036 rdr.len = strlen(HTTP_301);
2037 msg_fmt = HTTP_301;
2038 break;
2039 case 302:
2040 default:
2041 rdr.len = strlen(HTTP_302);
2042 msg_fmt = HTTP_302;
2043 break;
2044 }
2045
2046 if (unlikely(rdr.len > sizeof(trash)))
2047 goto return_bad_req;
2048 memcpy(rdr.str, msg_fmt, rdr.len);
2049
2050 switch(rule->type) {
2051 case REDIRECT_TYPE_PREFIX: {
2052 const char *path;
2053 int pathlen;
2054
2055 path = http_get_path(txn);
2056 /* build message using path */
2057 if (path) {
2058 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2059 } else {
2060 path = "/";
2061 pathlen = 1;
2062 }
2063
2064 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2065 goto return_bad_req;
2066
2067 /* add prefix */
2068 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2069 rdr.len += rule->rdr_len;
2070
2071 /* add path */
2072 memcpy(rdr.str + rdr.len, path, pathlen);
2073 rdr.len += pathlen;
2074 break;
2075 }
2076 case REDIRECT_TYPE_LOCATION:
2077 default:
2078 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2079 goto return_bad_req;
2080
2081 /* add location */
2082 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2083 rdr.len += rule->rdr_len;
2084 break;
2085 }
2086
2087 /* add end of headers */
2088 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2089 rdr.len += 4;
2090
2091 txn->status = rule->code;
2092 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002093 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002094 client_retnclose(t, &rdr);
2095 goto return_prx_cond;
2096 }
2097 }
2098
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002099 /* first check whether we have some ACLs set to block this request */
2100 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002101 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002102
2103 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002104 if (cond->pol == ACL_COND_UNLESS)
2105 ret = !ret;
2106
2107 if (ret) {
2108 txn->status = 403;
2109 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002110 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002111 client_retnclose(t, error_message(t, HTTP_ERR_403));
2112 goto return_prx_cond;
2113 }
2114 }
2115
Willy Tarreau06619262006-12-17 08:37:22 +01002116 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002117 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002118 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2119 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002120 }
2121
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002122 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2123 /* to ensure correct connection accounting on
2124 * the backend, we count the connection for the
2125 * one managing the queue.
2126 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002127 t->be->beconn++;
2128 if (t->be->beconn > t->be->beconn_max)
2129 t->be->beconn_max = t->be->beconn;
2130 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002131 t->flags |= SN_BE_ASSIGNED;
2132 }
2133
Willy Tarreau06619262006-12-17 08:37:22 +01002134 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002135 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002136 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002137 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002138 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002139 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002140 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002141 goto return_prx_cond;
2142 }
2143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002144 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002145 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002146 !(t->flags & SN_CONN_CLOSED)) {
2147 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002148 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002149 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002150
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002151 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002152 old_idx = 0;
2153
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002154 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2155 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002156 cur_ptr = cur_next;
2157 cur_end = cur_ptr + cur_hdr->len;
2158 cur_next = cur_end + cur_hdr->cr + 1;
2159
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002160 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2161 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002162 /* 3 possibilities :
2163 * - we have already set Connection: close,
2164 * so we remove this line.
2165 * - we have not yet set Connection: close,
2166 * but this line indicates close. We leave
2167 * it untouched and set the flag.
2168 * - we have not yet set Connection: close,
2169 * and this line indicates non-close. We
2170 * replace it.
2171 */
2172 if (t->flags & SN_CONN_CLOSED) {
2173 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002174 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002175 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002176 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2177 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002178 cur_hdr->len = 0;
2179 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002180 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2181 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2182 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002183 cur_next += delta;
2184 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002185 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002186 }
2187 t->flags |= SN_CONN_CLOSED;
2188 }
2189 }
2190 old_idx = cur_idx;
2191 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002192 }
2193 /* add request headers from the rule sets in the same order */
2194 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2195 if (unlikely(http_header_add_tail(req,
2196 &txn->req,
2197 &txn->hdr_idx,
2198 rule_set->req_add[cur_idx])) < 0)
2199 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002200 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002202 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002203 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002204 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002205 /* we have to check the URI and auth for this request.
2206 * FIXME!!! that one is rather dangerous, we want to
2207 * make it follow standard rules (eg: clear t->analysis).
2208 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002209 if (stats_check_uri_auth(t, rule_set))
2210 return 1;
2211 }
2212
Willy Tarreau55ea7572007-06-17 19:56:27 +02002213 /* now check whether we have some switching rules for this request */
2214 if (!(t->flags & SN_BE_ASSIGNED)) {
2215 struct switching_rule *rule;
2216
2217 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2218 int ret;
2219
2220 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002221
2222 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002223 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002224 ret = !ret;
2225
2226 if (ret) {
2227 t->be = rule->be.backend;
2228 t->be->beconn++;
2229 if (t->be->beconn > t->be->beconn_max)
2230 t->be->beconn_max = t->be->beconn;
2231 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002232
2233 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002234 t->rep->rto = t->req->wto = t->be->timeout.server;
2235 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002236 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002237 t->flags |= SN_BE_ASSIGNED;
2238 break;
2239 }
2240 }
2241 }
2242
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002243 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2244 /* No backend was set, but there was a default
2245 * backend set in the frontend, so we use it and
2246 * loop again.
2247 */
2248 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002249 t->be->beconn++;
2250 if (t->be->beconn > t->be->beconn_max)
2251 t->be->beconn_max = t->be->beconn;
2252 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002253
2254 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002255 t->rep->rto = t->req->wto = t->be->timeout.server;
2256 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002257 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002258 t->flags |= SN_BE_ASSIGNED;
2259 }
2260 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002261
Willy Tarreau58f10d72006-12-04 02:26:12 +01002262
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002263 if (!(t->flags & SN_BE_ASSIGNED)) {
2264 /* To ensure correct connection accounting on
2265 * the backend, we count the connection for the
2266 * one managing the queue.
2267 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002268 t->be->beconn++;
2269 if (t->be->beconn > t->be->beconn_max)
2270 t->be->beconn_max = t->be->beconn;
2271 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002272 t->flags |= SN_BE_ASSIGNED;
2273 }
2274
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002275 /*
2276 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002277 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002278 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002279 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002280 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002281
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002282 /*
2283 * If HTTP PROXY is set we simply get remote server address
2284 * parsing incoming request.
2285 */
2286 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2287 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2288 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002289
Willy Tarreau2a324282006-12-05 00:05:46 +01002290 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002291 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002292 * so let's do the same now.
2293 */
2294
2295 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002296 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002297 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002298 }
2299
2300
2301 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002302 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002303 * Note that doing so might move headers in the request, but
2304 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002305 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002306 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002307 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2308 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002309 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau58f10d72006-12-04 02:26:12 +01002311
Willy Tarreau2a324282006-12-05 00:05:46 +01002312 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002313 * 9: add X-Forwarded-For if either the frontend or the backend
2314 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002315 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002316 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002317 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002318 /* Add an X-Forwarded-For header unless the source IP is
2319 * in the 'except' network range.
2320 */
2321 if ((!t->fe->except_mask.s_addr ||
2322 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2323 != t->fe->except_net.s_addr) &&
2324 (!t->be->except_mask.s_addr ||
2325 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2326 != t->be->except_net.s_addr)) {
2327 int len;
2328 unsigned char *pn;
2329 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002330
Ross Westaf72a1d2008-08-03 10:51:45 +02002331 /* Note: we rely on the backend to get the header name to be used for
2332 * x-forwarded-for, because the header is really meant for the backends.
2333 * However, if the backend did not specify any option, we have to rely
2334 * on the frontend's header name.
2335 */
2336 if (t->be->fwdfor_hdr_len) {
2337 len = t->be->fwdfor_hdr_len;
2338 memcpy(trash, t->be->fwdfor_hdr_name, len);
2339 } else {
2340 len = t->fe->fwdfor_hdr_len;
2341 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2342 }
2343 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002344
Ross Westaf72a1d2008-08-03 10:51:45 +02002345 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002346 &txn->hdr_idx, trash, len)) < 0)
2347 goto return_bad_req;
2348 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002349 }
2350 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002351 /* FIXME: for the sake of completeness, we should also support
2352 * 'except' here, although it is mostly useless in this case.
2353 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002354 int len;
2355 char pn[INET6_ADDRSTRLEN];
2356 inet_ntop(AF_INET6,
2357 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2358 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002359
2360 /* Note: we rely on the backend to get the header name to be used for
2361 * x-forwarded-for, because the header is really meant for the backends.
2362 * However, if the backend did not specify any option, we have to rely
2363 * on the frontend's header name.
2364 */
2365 if (t->be->fwdfor_hdr_len) {
2366 len = t->be->fwdfor_hdr_len;
2367 memcpy(trash, t->be->fwdfor_hdr_name, len);
2368 } else {
2369 len = t->fe->fwdfor_hdr_len;
2370 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2371 }
2372 len += sprintf(trash + len, ": %s", pn);
2373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002374 if (unlikely(http_header_add_tail2(req, &txn->req,
2375 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002376 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002377 }
2378 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379
Willy Tarreau2a324282006-12-05 00:05:46 +01002380 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002381 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002382 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002383 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002384 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002385 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002386 if ((unlikely(msg->sl.rq.v_l != 8) ||
2387 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2388 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002389 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002390 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002391 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002392 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002393 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2394 * If not assigned, perhaps we are balancing on url_param, but this is a
2395 * POST; and the parameters are in the body, maybe scan there to find our server.
2396 * (unless headers overflowed the buffer?)
2397 */
2398 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2399 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002400 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002401 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2402 /* are there enough bytes here? total == l || r || rlim ?
2403 * len is unsigned, but eoh is int,
2404 * how many bytes of body have we received?
2405 * eoh is the first empty line of the header
2406 */
2407 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002408 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 +02002409
2410 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2411 * We can't assume responsibility for the server's decision,
2412 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2413 * We also can't change our mind later, about which server to choose, so round robin.
2414 */
2415 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2416 struct hdr_ctx ctx;
2417 ctx.idx = 0;
2418 /* Expect is allowed in 1.1, look for it */
2419 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2420 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002421 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002422 /* We can't reliablly stall and wait for data, because of
2423 * .NET clients that don't conform to rfc2616; so, no need for
2424 * the next block to check length expectations.
2425 * We could send 100 status back to the client, but then we need to
2426 * re-write headers, and send the message. And this isn't the right
2427 * place for that action.
2428 * TODO: support Expect elsewhere and delete this block.
2429 */
2430 goto end_check_maybe_wait_for_body;
2431 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002432
2433 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002434 /* nothing to do, we got enough */
2435 } else {
2436 /* limit implies we are supposed to need this many bytes
2437 * to find the parameter. Let's see how many bytes we can wait for.
2438 */
2439 long long hint = len;
2440 struct hdr_ctx ctx;
2441 ctx.idx = 0;
2442 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002443 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002444 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002445 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002446 ctx.idx = 0;
2447 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2448 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002449 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002450 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002451 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002452 hint = 0; /* parse failure, untrusted client */
2453 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002454 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002455 msg->hdr_content_len = hint;
2456 else
2457 hint = 0; /* bad client, sent negative length */
2458 }
2459 }
2460 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002461 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002462 hint = t->be->url_param_post_limit;
2463 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002464 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002465 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002466 /* else... There are no body bytes to wait for */
2467 }
2468 }
2469 }
2470 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002471
Willy Tarreau2a324282006-12-05 00:05:46 +01002472 /*************************************************************
2473 * OK, that's finished for the headers. We have done what we *
2474 * could. Let's switch to the DATA state. *
2475 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002476
Willy Tarreaue393fe22008-08-16 22:18:07 +02002477 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002478 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479
Willy Tarreau1fa31262007-12-03 00:36:16 +01002480 /* When a connection is tarpitted, we use the tarpit timeout,
2481 * which may be the same as the connect timeout if unspecified.
2482 * If unset, then set it to zero because we really want it to
2483 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002484 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002485 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002486 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002487 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002488 /* flush the request so that we can drop the connection early
2489 * if the client closes first.
2490 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02002491 req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2492 if (!req->wex)
2493 req->wex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002494 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002496 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002497 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002498
2499 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002500 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002501 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002502 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002503 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002504 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002505 return_prx_cond:
2506 if (!(t->flags & SN_ERR_MASK))
2507 t->flags |= SN_ERR_PRXCOND;
2508 if (!(t->flags & SN_FINST_MASK))
2509 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002510 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002511 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002512 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002513 }
2514
2515 if (t->analysis & AN_REQ_HTTP_BODY) {
2516 /* We have to parse the HTTP request body to find any required data.
2517 * "balance url_param check_post" should have been the only way to get
2518 * into this. We were brought here after HTTP header analysis, so all
2519 * related structures are ready.
2520 */
2521 struct http_msg *msg = &t->txn.req;
2522 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2523 long long limit = t->be->url_param_post_limit;
2524 struct hdr_ctx ctx;
2525
2526 ctx.idx = 0;
2527
2528 /* now if we have a length, we'll take the hint */
2529 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2530 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2531 unsigned int chunk = 0;
2532 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2533 char c = msg->sol[body];
2534 if (ishex(c)) {
2535 unsigned int hex = toupper(c) - '0';
2536 if (hex > 9)
2537 hex -= 'A' - '9' - 1;
2538 chunk = (chunk << 4) | hex;
2539 } else
2540 break;
2541 body++;
2542 }
2543 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002544 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002545
2546 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002547 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002548
2549 body += 2; // skip CRLF
2550
2551 /* if we support more then one chunk here, we have to do it again when assigning server
2552 * 1. how much entity data do we have? new var
2553 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2554 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2555 */
2556
2557 if (chunk < limit)
2558 limit = chunk; /* only reading one chunk */
2559 } else {
2560 if (msg->hdr_content_len < limit)
2561 limit = msg->hdr_content_len;
2562 }
2563
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002564 http_body_end:
2565 /* we leave once we know we have nothing left to do. This means that we have
2566 * enough bytes, or that we know we'll not get any more (buffer full, read
2567 * buffer closed).
2568 */
2569 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002570 req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002571 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002572 t->logs.tv_request = now; /* update the request timer to reflect full request */
2573 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002574 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002575 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002576
Willy Tarreaudafde432008-08-17 01:00:46 +02002577 /* we want to leave with that clean */
2578 if (unlikely(t->analysis & AN_REQ_ANY))
2579 goto update_state;
2580 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002581}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002582
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002583/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002584 * It normally returns zero, but may return 1 if it absolutely needs to be
2585 * called again after other functions. It relies on buffers flags, and updates
2586 * t->analysis. It might make sense to explode it into several other functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002587 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002588int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002589{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002590 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002591 struct buffer *req = t->req;
2592 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002593
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002594 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002595 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002596 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02002597 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
2598 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 +02002599 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002600
Willy Tarreaudafde432008-08-17 01:00:46 +02002601 update_state:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002602 if (t->analysis & AN_RTR_HTTP_HDR) { /* receiving server headers */
2603 /*
2604 * Now parse the partial (or complete) lines.
2605 * We will check the response syntax, and also join multi-line
2606 * headers. An index of all the lines will be elaborated while
2607 * parsing.
2608 *
2609 * For the parsing, we use a 28 states FSM.
2610 *
2611 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002612 * rep->data + rep->som = beginning of response
2613 * rep->data + rep->eoh = end of processed headers / start of current one
2614 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002615 * rep->lr = first non-visited byte
2616 * rep->r = end of data
2617 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002618
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002619 int cur_idx;
2620 struct http_msg *msg = &txn->rsp;
2621 struct proxy *cur_proxy;
2622
2623 if (likely(rep->lr < rep->r))
2624 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2625
2626 /* 1: we might have to print this header in debug mode */
2627 if (unlikely((global.mode & MODE_DEBUG) &&
2628 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2629 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2630 char *eol, *sol;
2631
2632 sol = rep->data + msg->som;
2633 eol = sol + msg->sl.rq.l;
2634 debug_hdr("srvrep", t, sol, eol);
2635
2636 sol += hdr_idx_first_pos(&txn->hdr_idx);
2637 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2638
2639 while (cur_idx) {
2640 eol = sol + txn->hdr_idx.v[cur_idx].len;
2641 debug_hdr("srvhdr", t, sol, eol);
2642 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2643 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002644 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002646
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002647 /*
2648 * Now we quickly check if we have found a full valid response.
2649 * If not so, we check the FD and buffer states before leaving.
2650 * A full response is indicated by the fact that we have seen
2651 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2652 * responses are checked first.
2653 *
2654 * Depending on whether the client is still there or not, we
2655 * may send an error response back or not. Note that normally
2656 * we should only check for HTTP status there, and check I/O
2657 * errors somewhere else.
2658 */
2659
2660 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002661 /* Invalid response */
2662 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2663 hdr_response_bad:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002664 buffer_shutr(rep);
2665 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002666 fd_delete(t->srv_fd);
2667 if (t->srv) {
2668 t->srv->cur_sess--;
2669 t->srv->failed_resp++;
2670 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002672 t->be->failed_resp++;
2673 t->srv_state = SV_STCLOSE;
2674 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002675 txn->status = 502;
2676 client_return(t, error_message(t, HTTP_ERR_502));
2677 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002678 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002679 if (!(t->flags & SN_FINST_MASK))
2680 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002682 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2683 process_srv_queue(t->srv);
2684
Willy Tarreaudafde432008-08-17 01:00:46 +02002685 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002687 /* write error to client, read error or close from server */
2688 if (req->flags & BF_WRITE_ERROR ||
Willy Tarreauba392ce2008-08-16 21:13:23 +02002689 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) {
2690 buffer_shutr(rep);
2691 buffer_shutw(req);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002692 fd_delete(t->srv_fd);
2693 if (t->srv) {
2694 t->srv->cur_sess--;
2695 t->srv->failed_resp++;
2696 sess_change_server(t, NULL);
2697 }
2698 t->be->failed_resp++;
2699 t->srv_state = SV_STCLOSE;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002700 t->analysis &= ~AN_RTR_ANY;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002701 txn->status = 502;
2702 client_return(t, error_message(t, HTTP_ERR_502));
2703 if (!(t->flags & SN_ERR_MASK))
2704 t->flags |= SN_ERR_SRVCL;
2705 if (!(t->flags & SN_FINST_MASK))
2706 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002707
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002708 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2709 process_srv_queue(t->srv);
2710
Willy Tarreaudafde432008-08-17 01:00:46 +02002711 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002712 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002713 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002714 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002715 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002716 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002717 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002718 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02002719 buffer_shutr(rep);
2720 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002721 fd_delete(t->srv_fd);
2722 if (t->srv) {
2723 t->srv->cur_sess--;
2724 t->srv->failed_resp++;
2725 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002726 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002727 t->be->failed_resp++;
2728 t->srv_state = SV_STCLOSE;
2729 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002730 txn->status = 504;
2731 client_return(t, error_message(t, HTTP_ERR_504));
2732 if (!(t->flags & SN_ERR_MASK))
2733 t->flags |= SN_ERR_SRVTO;
2734 if (!(t->flags & SN_FINST_MASK))
2735 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002736
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002737 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2738 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002739 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002740 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002741 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002742 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002743
Willy Tarreau21d2af32008-02-14 20:25:24 +01002744
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002745 /*****************************************************************
2746 * More interesting part now : we know that we have a complete *
2747 * response which at least looks like HTTP. We have an indicator *
2748 * of each header's length, so we can parse them quickly. *
2749 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002750
Willy Tarreaua7c52762008-08-16 18:40:18 +02002751 t->analysis &= ~AN_RTR_HTTP_HDR;
2752
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002753 /* ensure we keep this pointer to the beginning of the message */
2754 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002755
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002756 /*
2757 * 1: get the status code and check for cacheability.
2758 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002760 t->logs.logwait &= ~LW_RESP;
2761 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002762
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 switch (txn->status) {
2764 case 200:
2765 case 203:
2766 case 206:
2767 case 300:
2768 case 301:
2769 case 410:
2770 /* RFC2616 @13.4:
2771 * "A response received with a status code of
2772 * 200, 203, 206, 300, 301 or 410 MAY be stored
2773 * by a cache (...) unless a cache-control
2774 * directive prohibits caching."
2775 *
2776 * RFC2616 @9.5: POST method :
2777 * "Responses to this method are not cacheable,
2778 * unless the response includes appropriate
2779 * Cache-Control or Expires header fields."
2780 */
2781 if (likely(txn->meth != HTTP_METH_POST) &&
2782 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2783 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2784 break;
2785 default:
2786 break;
2787 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002788
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002789 /*
2790 * 2: we may need to capture headers
2791 */
2792 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2793 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2794 txn->rsp.cap, t->fe->rsp_cap);
2795
2796 /*
2797 * 3: we will have to evaluate the filters.
2798 * As opposed to version 1.2, now they will be evaluated in the
2799 * filters order and not in the header order. This means that
2800 * each filter has to be validated among all headers.
2801 *
2802 * Filters are tried with ->be first, then with ->fe if it is
2803 * different from ->be.
2804 */
2805
2806 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2807
2808 cur_proxy = t->be;
2809 while (1) {
2810 struct proxy *rule_set = cur_proxy;
2811
2812 /* try headers filters */
2813 if (rule_set->rsp_exp != NULL) {
2814 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2815 return_bad_resp:
2816 if (t->srv) {
2817 t->srv->cur_sess--;
2818 t->srv->failed_resp++;
2819 sess_change_server(t, NULL);
2820 }
2821 cur_proxy->failed_resp++;
2822 return_srv_prx_502:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002823 buffer_shutr(rep);
2824 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002825 fd_delete(t->srv_fd);
2826 t->srv_state = SV_STCLOSE;
2827 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002828 txn->status = 502;
2829 client_return(t, error_message(t, HTTP_ERR_502));
2830 if (!(t->flags & SN_ERR_MASK))
2831 t->flags |= SN_ERR_PRXCOND;
2832 if (!(t->flags & SN_FINST_MASK))
2833 t->flags |= SN_FINST_H;
2834 /* We used to have a free connection slot. Since we'll never use it,
2835 * we have to inform the server that it may be used by another session.
2836 */
2837 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2838 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002839 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002840 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002841 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002842
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002843 /* has the response been denied ? */
2844 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002845 if (t->srv) {
2846 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002847 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002848 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002849 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002850 cur_proxy->denied_resp++;
2851 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002852 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002853
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002854 /* We might have to check for "Connection:" */
2855 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2856 !(t->flags & SN_CONN_CLOSED)) {
2857 char *cur_ptr, *cur_end, *cur_next;
2858 int cur_idx, old_idx, delta, val;
2859 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2862 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002863
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002864 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2865 cur_hdr = &txn->hdr_idx.v[cur_idx];
2866 cur_ptr = cur_next;
2867 cur_end = cur_ptr + cur_hdr->len;
2868 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002870 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2871 if (val) {
2872 /* 3 possibilities :
2873 * - we have already set Connection: close,
2874 * so we remove this line.
2875 * - we have not yet set Connection: close,
2876 * but this line indicates close. We leave
2877 * it untouched and set the flag.
2878 * - we have not yet set Connection: close,
2879 * and this line indicates non-close. We
2880 * replace it.
2881 */
2882 if (t->flags & SN_CONN_CLOSED) {
2883 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2884 txn->rsp.eoh += delta;
2885 cur_next += delta;
2886 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2887 txn->hdr_idx.used--;
2888 cur_hdr->len = 0;
2889 } else {
2890 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2891 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2892 "close", 5);
2893 cur_next += delta;
2894 cur_hdr->len += delta;
2895 txn->rsp.eoh += delta;
2896 }
2897 t->flags |= SN_CONN_CLOSED;
2898 }
2899 }
2900 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002901 }
2902 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002904 /* add response headers from the rule sets in the same order */
2905 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2906 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2907 rule_set->rsp_add[cur_idx])) < 0)
2908 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002909 }
2910
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002911 /* check whether we're already working on the frontend */
2912 if (cur_proxy == t->fe)
2913 break;
2914 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002917 /*
2918 * 4: check for server cookie.
2919 */
2920 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2921 || (t->be->options & PR_O_CHK_CACHE))
2922 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002923
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002927 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002928 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2929 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002930
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 /*
2932 * 6: add server cookie in the response if needed
2933 */
2934 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2935 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2936 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 /* the server is known, it's not the one the client requested, we have to
2939 * insert a set-cookie here, except if we want to insert only on POST
2940 * requests and this one isn't. Note that servers which don't have cookies
2941 * (eg: some backup servers) will return a full cookie removal request.
2942 */
2943 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2944 t->be->cookie_name,
2945 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002947 if (t->be->cookie_domain)
2948 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002950 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2951 trash, len)) < 0)
2952 goto return_bad_resp;
2953 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955 /* Here, we will tell an eventual cache on the client side that we don't
2956 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2957 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2958 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2959 */
2960 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002962 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2963
2964 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2965 "Cache-control: private", 22)) < 0)
2966 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002967 }
2968 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002971 /*
2972 * 7: check if result will be cacheable with a cookie.
2973 * We'll block the response if security checks have caught
2974 * nasty things such as a cacheable cookie.
2975 */
2976 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2977 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2978 (t->be->options & PR_O_CHK_CACHE)) {
2979
2980 /* we're in presence of a cacheable response containing
2981 * a set-cookie header. We'll block it as requested by
2982 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002983 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002984 if (t->srv) {
2985 t->srv->cur_sess--;
2986 t->srv->failed_secu++;
2987 sess_change_server(t, NULL);
2988 }
2989 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002990
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002991 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2992 t->be->id, t->srv?t->srv->id:"<dispatch>");
2993 send_log(t->be, LOG_ALERT,
2994 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2995 t->be->id, t->srv?t->srv->id:"<dispatch>");
2996 goto return_srv_prx_502;
2997 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002998
2999 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003000 * 8: add "Connection: close" if needed and not yet set.
3001 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003002 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003003 if (!(t->flags & SN_CONN_CLOSED) &&
3004 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3005 if ((unlikely(msg->sl.st.v_l != 8) ||
3006 unlikely(req->data[msg->som + 7] != '0')) &&
3007 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3008 "Connection: close", 17)) < 0)
3009 goto return_bad_resp;
3010 t->flags |= SN_CONN_CLOSED;
3011 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003012
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003013 /*************************************************************
3014 * OK, that's finished for the headers. We have done what we *
3015 * could. Let's switch to the DATA state. *
3016 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017
Willy Tarreaue393fe22008-08-16 22:18:07 +02003018 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003019 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003021#ifdef CONFIG_HAP_TCPSPLICE
3022 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3023 /* TCP splicing supported by both FE and BE */
3024 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3025 }
3026#endif
3027 /* if the user wants to log as soon as possible, without counting
3028 * bytes from the server, then this is the right moment. We have
3029 * to temporarily assign bytes_out to log what we currently have.
3030 */
3031 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3032 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3033 t->logs.bytes_out = txn->rsp.eoh;
3034 if (t->fe->to_log & LW_REQ)
3035 http_sess_log(t);
3036 else
3037 tcp_sess_log(t);
3038 t->logs.bytes_out = 0;
3039 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003041 /* Note: we must not try to cheat by jumping directly to DATA,
3042 * otherwise we would not let the client side wake up.
3043 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003044
Willy Tarreaudafde432008-08-17 01:00:46 +02003045 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003046 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003047
3048 /* we want to leave with that clean */
3049 if (unlikely(t->analysis & AN_RTR_ANY))
3050 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003051 return 0;
3052}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003054/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003055 * Manages the client FSM and its socket. It normally returns zero, but may
3056 * return 1 if it absolutely wants to be called again.
3057 *
3058 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003059 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003060 */
3061int process_cli(struct session *t)
3062{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003063 struct buffer *req = t->req;
3064 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003065
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003066 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 +02003067 now_ms,
3068 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003069 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3070 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 +02003071 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003072 req->flags, rep->flags,
3073 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003074
Willy Tarreaudafde432008-08-17 01:00:46 +02003075 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003076 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3077 * still set this state (and will do until unix sockets are converted).
3078 */
3079 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003080 /* read or write error */
3081 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003082 buffer_shutr(req);
3083 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003084 fd_delete(t->cli_fd);
3085 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003086 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 if (!(t->flags & SN_ERR_MASK))
3088 t->flags |= SN_ERR_CLICL;
3089 if (!(t->flags & SN_FINST_MASK)) {
3090 if (t->analysis & AN_REQ_ANY)
3091 t->flags |= SN_FINST_R;
3092 else if (t->pend_pos)
3093 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003094 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003095 t->flags |= SN_FINST_C;
3096 else
3097 t->flags |= SN_FINST_D;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003098 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003099 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003100 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003101 /* last read, or end of server write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003102 else if (!(req->flags & BF_SHUTR) && /* already done */
3103 req->flags & (BF_READ_NULL | BF_SHUTW)) {
3104 buffer_shutr(req);
3105 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003106 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003107 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003108 } else {
3109 /* output was already closed */
3110 fd_delete(t->cli_fd);
3111 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003112 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003113 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003114 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003115 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003116 /* last server read and buffer empty : we only check them when we're
3117 * allowed to forward the data.
3118 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003119 else if (!(rep->flags & BF_SHUTW) && /* already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003120 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003121 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
3122 buffer_shutw(rep);
3123 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003124 EV_FD_CLR(t->cli_fd, DIR_WR);
3125 shutdown(t->cli_fd, SHUT_WR);
3126 /* We must ensure that the read part is still alive when switching to shutw */
3127 /* FIXME: is this still true ? */
3128 EV_FD_SET(t->cli_fd, DIR_RD);
3129 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003130 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003131 } else {
3132 fd_delete(t->cli_fd);
3133 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003134 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003136 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003137 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003138 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003139 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003140 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003141 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003142 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003143 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003144 } else {
3145 /* output was already closed */
3146 fd_delete(t->cli_fd);
3147 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003148 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003149 }
3150 if (!(t->flags & SN_ERR_MASK))
3151 t->flags |= SN_ERR_CLITO;
3152 if (!(t->flags & SN_FINST_MASK)) {
3153 if (t->analysis & AN_REQ_ANY)
3154 t->flags |= SN_FINST_R;
3155 else if (t->pend_pos)
3156 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003157 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003158 t->flags |= SN_FINST_C;
3159 else
3160 t->flags |= SN_FINST_D;
3161 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003162 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003163 }
3164 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003165 else if (rep->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003166 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003167 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003168 EV_FD_CLR(t->cli_fd, DIR_WR);
3169 shutdown(t->cli_fd, SHUT_WR);
3170 /* We must ensure that the read part is still alive when switching to shutw */
3171 /* FIXME: is this still true ? */
3172 EV_FD_SET(t->cli_fd, DIR_RD);
3173 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003174 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003175 } else {
3176 fd_delete(t->cli_fd);
3177 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003178 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003179 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003181 if (!(t->flags & SN_ERR_MASK))
3182 t->flags |= SN_ERR_CLITO;
3183 if (!(t->flags & SN_FINST_MASK)) {
3184 if (t->analysis & AN_REQ_ANY)
3185 t->flags |= SN_FINST_R;
3186 else if (t->pend_pos)
3187 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003188 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003189 t->flags |= SN_FINST_C;
3190 else
3191 t->flags |= SN_FINST_D;
3192 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003193 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003194 }
3195
3196 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003197 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003198 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003199 /* no room to read more data */
3200 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3201 /* stop reading until we get some space */
3202 req->rex = TICK_ETERNITY;
3203 }
3204 } else {
3205 EV_FD_COND_S(t->cli_fd, DIR_RD);
3206 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3207 }
3208 }
3209
3210 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003211 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003212 /* first, we may have to produce data (eg: stats).
3213 * right now, this is limited to the SHUTR state.
3214 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003215 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003216 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003217 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003218 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 fd_delete(t->cli_fd);
3220 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003221 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003222 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003224 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003225
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003226 /* 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 +02003227 if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003228 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3229 /* stop writing */
3230 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003231 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003232 } else {
3233 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003234 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003235 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003236 /* restart writing */
3237 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003238 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003239 /* FIXME: to prevent the client from expiring read timeouts during writes,
3240 * we refresh it, except if it was already infinite. */
3241 req->rex = rep->wex;
3242 }
3243 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003244 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003245 }
3246 return 0; /* other cases change nothing */
3247 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003248 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003249 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3250 int len;
3251 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);
3252 write(1, trash, len);
3253 }
3254 return 0;
3255 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003256#ifdef DEBUG_DEV
3257 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3258 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003259#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003260 return 0;
3261}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262
Willy Tarreaubaaee002006-06-26 02:48:02 +02003263
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003265 * Manages the server FSM and its socket. It normally returns zero, but may
3266 * return 1 if it absolutely wants to be called again.
3267 *
Willy Tarreaua7c52762008-08-16 18:40:18 +02003268 * Note: process_srv is the ONLY function allowed to set srv_state to anything
3269 * but SV_STCLOSE. The only exception is for functions called from this
3270 * one (eg: those in backend.c).
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003271 */
3272int process_srv(struct session *t)
3273{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003274 struct http_txn *txn = &t->txn;
3275 struct buffer *req = t->req;
3276 struct buffer *rep = t->rep;
3277 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003278
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003279 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 +02003280 now_ms,
3281 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003282 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
3283 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 +02003284 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003285 req->flags, rep->flags,
3286 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreaudafde432008-08-17 01:00:46 +02003288 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003289 if (t->srv_state == SV_STIDLE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003290 if ((rep->flags & BF_SHUTW) ||
3291 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003292 (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003293 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003294 if (t->pend_pos)
3295 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3296 /* note that this must not return any error because it would be able to
3297 * overwrite the client_retnclose() output.
3298 */
3299 if (txn->flags & TX_CLTARPIT)
3300 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3301 else
3302 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3303
Willy Tarreauf8533202008-08-16 14:55:08 +02003304 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003305 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003306 }
Willy Tarreaud9f48362008-08-16 16:39:26 +02003307 else if (req->flags & BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003308 /* the client allows the server to connect */
3309 if (txn->flags & TX_CLTARPIT) {
3310 /* This connection is being tarpitted. The CLIENT side has
3311 * already set the connect expiration date to the right
3312 * timeout. We just have to check that it has not expired.
3313 */
Willy Tarreau507385d2008-08-17 13:04:25 +02003314 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 return 0;
3316
3317 /* We will set the queue timer to the time spent, just for
3318 * logging purposes. We fake a 500 server error, so that the
3319 * attacker will not suspect his connection has been tarpitted.
3320 * It will not cause trouble to the logs because we can exclude
3321 * the tarpitted connections by filtering on the 'PT' status flags.
3322 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003323 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003324 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3325 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3326 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003327 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreaudafde432008-08-17 01:00:46 +02003328 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003329 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003330
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003331 /* Right now, we will need to create a connection to the server.
3332 * We might already have tried, and got a connection pending, in
3333 * which case we will not do anything till it's pending. It's up
3334 * to any other session to release it and wake us up again.
3335 */
3336 if (t->pend_pos) {
Willy Tarreau507385d2008-08-17 13:04:25 +02003337 if (!(req->flags & BF_WRITE_TIMEOUT)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003338 return 0;
3339 } else {
3340 /* we've been waiting too long here */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003341 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003342 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3343 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3344 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003345 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003346 if (t->srv)
3347 t->srv->failed_conns++;
3348 t->be->failed_conns++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003349 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003350 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003351 }
3352
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003353 do {
3354 /* first, get a connection */
3355 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3356 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003357
Willy Tarreaudafde432008-08-17 01:00:46 +02003358 if (srv_redispatch_connect(t)) {
3359 if (t->srv_state == SV_STIDLE)
3360 return 0;
3361 goto update_state;
3362 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003363
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003364 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3365 /* Server supporting redirection and it is possible.
3366 * Invalid requests are reported as such. It concerns all
3367 * the largest ones.
3368 */
3369 struct chunk rdr;
3370 char *path;
3371 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003372
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 /* 1: create the response header */
3374 rdr.len = strlen(HTTP_302);
3375 rdr.str = trash;
3376 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003377
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003378 /* 2: add the server's prefix */
3379 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3380 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003381
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003382 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3383 rdr.len += t->srv->rdr_len;
3384
3385 /* 3: add the request URI */
3386 path = http_get_path(txn);
3387 if (!path)
3388 goto cancel_redir;
3389 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3390 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3391 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003392
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003393 memcpy(rdr.str + rdr.len, path, len);
3394 rdr.len += len;
3395 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3396 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003398 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003399 trace_term(t, TT_HTTP_SRV_3);
3400
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003401 /* FIXME: we should increase a counter of redirects per server and per backend. */
3402 if (t->srv)
3403 t->srv->cum_sess++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003404 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003405 cancel_redir:
3406 txn->status = 400;
3407 t->fe->failed_req++;
3408 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3409 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003410 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaudafde432008-08-17 01:00:46 +02003411 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003414 /* try to (re-)connect to the server, and fail if we expire the
3415 * number of retries.
3416 */
3417 if (srv_retryable_connect(t)) {
3418 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003419 if (t->srv_state == SV_STIDLE)
3420 return 0;
3421 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003422 }
3423 } while (1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003424 } /* end if may_forward */
3425 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003426 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003427 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003428 if ((rep->flags & BF_SHUTW) ||
3429 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003430 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003431 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003432 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003433 if (!(t->flags & SN_CONN_TAR)) {
3434 /* if we are in turn-around, we have already closed the FD */
3435 fd_delete(t->srv_fd);
3436 if (t->srv) {
3437 t->srv->cur_sess--;
3438 sess_change_server(t, NULL);
3439 }
3440 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003441
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003442 /* note that this must not return any error because it would be able to
3443 * overwrite the client_retnclose() output.
3444 */
3445 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003446 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreaudafde432008-08-17 01:00:46 +02003447 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003448 }
Willy Tarreau507385d2008-08-17 13:04:25 +02003449 if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 return 0; /* nothing changed */
3451 }
3452 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3453 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003454 if (t->flags & SN_CONN_TAR) {
3455 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau507385d2008-08-17 13:04:25 +02003456 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457 return 0;
3458 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003460 else {
3461 fd_delete(t->srv_fd);
3462 if (t->srv) {
3463 t->srv->cur_sess--;
3464 sess_change_server(t, NULL);
3465 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003466
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 if (!(req->flags & BF_WRITE_STATUS))
3468 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3469 else
3470 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003471
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003472 /* ensure that we have enough retries left */
Willy Tarreaudafde432008-08-17 01:00:46 +02003473 if (srv_count_retry_down(t, conn_err)) {
3474 goto update_state;
3475 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003477 if (req->flags & BF_WRITE_ERROR) {
3478 /* we encountered an immediate connection error, and we
3479 * will have to retry connecting to the same server, most
3480 * likely leading to the same result. To avoid this, we
3481 * fake a connection timeout to retry after a turn-around
3482 * time of 1 second. We will wait in the previous if block.
3483 */
3484 t->flags |= SN_CONN_TAR;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003485 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003486 return 0;
3487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003490 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3491 /* We're on our last chance, and the REDISP option was specified.
3492 * We will ignore cookie and force to balance or use the dispatcher.
3493 */
3494 /* let's try to offer this slot to anybody */
3495 if (may_dequeue_tasks(t->srv, t->be))
3496 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003497
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003498 /* it's left to the dispatcher to choose a server */
3499 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3500 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003501
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003502 /* first, get a connection */
Willy Tarreaudafde432008-08-17 01:00:46 +02003503 if (srv_redispatch_connect(t)) {
3504 if (t->srv_state == SV_STCONN)
3505 return 0;
3506 goto update_state;
3507 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003508 } else {
3509 if (t->srv)
3510 t->srv->retries++;
3511 t->be->retries++;
3512 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003514 do {
3515 /* Now we will try to either reconnect to the same server or
3516 * connect to another server. If the connection gets queued
3517 * because all servers are saturated, then we will go back to
3518 * the SV_STIDLE state.
3519 */
3520 if (srv_retryable_connect(t)) {
3521 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003522 if (t->srv_state == SV_STCONN)
3523 return 0;
3524 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003525 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003526
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003527 /* we need to redispatch the connection to another server */
Willy Tarreaudafde432008-08-17 01:00:46 +02003528 if (srv_redispatch_connect(t)) {
3529 if (t->srv_state == SV_STCONN)
3530 return 0;
3531 goto update_state;
3532 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003533 } while (1);
3534 }
3535 else { /* no error or write 0 */
3536 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003537
Willy Tarreaue393fe22008-08-16 22:18:07 +02003538 if (req->flags & BF_EMPTY) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003539 EV_FD_CLR(t->srv_fd, DIR_WR);
3540 req->wex = TICK_ETERNITY;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003541 } else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003542 EV_FD_SET(t->srv_fd, DIR_WR);
3543 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3544 if (tick_isset(req->wex)) {
3545 /* FIXME: to prevent the server from expiring read timeouts during writes,
3546 * we refresh it. */
3547 rep->rex = req->wex;
3548 }
3549 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003551 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3552 EV_FD_SET(t->srv_fd, DIR_RD);
3553 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003554 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003555
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003556 /* if the user wants to log as soon as possible, without counting
3557 bytes from the server, then this is the right moment. */
3558 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3559 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3560 tcp_sess_log(t);
3561 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003562#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003563 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3564 /* TCP splicing supported by both FE and BE */
3565 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3566 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003567#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 }
3569 else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003570 t->analysis |= AN_RTR_HTTP_HDR;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003571 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003572 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3573 /* reset hdr_idx which was already initialized by the request.
3574 * right now, the http parser does it.
3575 * hdr_idx_init(&t->txn.hdr_idx);
3576 */
3577 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003578
3579 t->srv_state = SV_STDATA;
3580 if (!(t->analysis & AN_RTR_ANY))
3581 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003582 req->wex = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003583 goto update_state;
3584 } /* else no error or write 0 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003585 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003586 else if (t->srv_state == SV_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003587 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003588 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003589 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003590 buffer_shutr(rep);
3591 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003592 fd_delete(t->srv_fd);
3593 if (t->srv) {
3594 t->srv->cur_sess--;
3595 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003596 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003597 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003598 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003599 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003600 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003601 if (!(t->flags & SN_ERR_MASK))
3602 t->flags |= SN_ERR_SRVCL;
3603 if (!(t->flags & SN_FINST_MASK))
3604 t->flags |= SN_FINST_D;
Willy Tarreau461f6622008-08-15 23:43:19 +02003605
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003606 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003607 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003608
Willy Tarreaudafde432008-08-17 01:00:46 +02003609 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003610 }
3611 /* last read, or end of client write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003612 else if (!(rep->flags & BF_SHUTR) && /* not already done */
3613 rep->flags & (BF_READ_NULL | BF_SHUTW)) {
3614 buffer_shutr(rep);
3615 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003616 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003617 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003618 } else {
3619 /* output was already closed */
3620 fd_delete(t->srv_fd);
3621 if (t->srv) {
3622 t->srv->cur_sess--;
3623 sess_change_server(t, NULL);
3624 }
3625 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003626 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003627
3628 if (may_dequeue_tasks(t->srv, t->be))
3629 process_srv_queue(t->srv);
3630 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003631 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003632 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003633 /* end of client read and no more data to send. We can forward
3634 * the close when we're allowed to forward data (anytime right
3635 * now). If we're using option forceclose, then we may also
3636 * shutdown the outgoing write channel once the response starts
3637 * coming from the server.
3638 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003639 else if (!(req->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003640 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003641 (req->flags & BF_SHUTR ||
Willy Tarreau461f6622008-08-15 23:43:19 +02003642 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003643 buffer_shutw(req);
3644 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003645 EV_FD_CLR(t->srv_fd, DIR_WR);
3646 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003647 trace_term(t, TT_HTTP_SRV_9);
Willy Tarreau461f6622008-08-15 23:43:19 +02003648 /* We must ensure that the read part is still alive when switching to shutw */
3649 /* FIXME: is this still true ? */
3650 EV_FD_SET(t->srv_fd, DIR_RD);
3651 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003652 } else {
3653 fd_delete(t->srv_fd);
3654 if (t->srv) {
3655 t->srv->cur_sess--;
3656 sess_change_server(t, NULL);
3657 }
3658 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003659 trace_term(t, TT_HTTP_SRV_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003660
Willy Tarreau461f6622008-08-15 23:43:19 +02003661 if (may_dequeue_tasks(t->srv, t->be))
3662 process_srv_queue(t->srv);
3663 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003664 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003665 }
3666 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003667 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003668 buffer_shutr(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003669 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003670 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003671 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003672 } else {
3673 fd_delete(t->srv_fd);
3674 if (t->srv) {
3675 t->srv->cur_sess--;
3676 sess_change_server(t, NULL);
3677 }
3678 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003679 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003680
3681 if (may_dequeue_tasks(t->srv, t->be))
3682 process_srv_queue(t->srv);
3683 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003684 if (!(t->flags & SN_ERR_MASK))
3685 t->flags |= SN_ERR_SRVTO;
3686 if (!(t->flags & SN_FINST_MASK))
3687 t->flags |= SN_FINST_D;
Willy Tarreaudafde432008-08-17 01:00:46 +02003688
3689 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003690 }
3691 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003692 else if (req->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003693 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003694 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003695 EV_FD_CLR(t->srv_fd, DIR_WR);
3696 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003697 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003698 /* We must ensure that the read part is still alive when switching to shutw */
3699 /* FIXME: is this still needed ? */
3700 EV_FD_SET(t->srv_fd, DIR_RD);
3701 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003702 } else {
3703 fd_delete(t->srv_fd);
3704 if (t->srv) {
3705 t->srv->cur_sess--;
3706 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003707 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003708 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003709 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003710
Willy Tarreau461f6622008-08-15 23:43:19 +02003711 if (may_dequeue_tasks(t->srv, t->be))
3712 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003714 if (!(t->flags & SN_ERR_MASK))
3715 t->flags |= SN_ERR_SRVTO;
3716 if (!(t->flags & SN_FINST_MASK))
3717 t->flags |= SN_FINST_D;
Willy Tarreaudafde432008-08-17 01:00:46 +02003718
3719 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003720 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003721
Willy Tarreau461f6622008-08-15 23:43:19 +02003722 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003723 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003724 if (rep->flags & BF_FULL) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003725 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3726 rep->rex = TICK_ETERNITY;
3727 } else {
3728 EV_FD_COND_S(t->srv_fd, DIR_RD);
3729 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003730 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003731 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003732
Willy Tarreau461f6622008-08-15 23:43:19 +02003733 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003734 if (!(req->flags & BF_SHUTW)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003735 if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003736 /* stop writing */
3737 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3738 req->wex = TICK_ETERNITY;
3739 } else {
3740 /* buffer not empty, there are still data to be transferred */
3741 EV_FD_COND_S(t->srv_fd, DIR_WR);
3742 if (!tick_isset(req->wex)) {
3743 /* restart writing */
3744 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003745 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003746 /* FIXME: to prevent the server from expiring read timeouts during writes,
3747 * we refresh it, except if it was already infinite.
3748 */
3749 rep->rex = req->wex;
3750 }
3751 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003752 }
3753 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003754 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003755 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003756 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003757 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3758 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003759 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003760 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761 write(1, trash, len);
3762 }
3763 return 0;
3764 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003765#ifdef DEBUG_DEV
3766 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3767 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003768#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003769 return 0;
3770}
3771
3772
3773/*
3774 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003775 * called with client socket shut down on input. Right now, only statistics can
3776 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003777 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003778 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003779 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003780 */
3781int produce_content(struct session *s)
3782{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003783 if (s->data_source == DATA_SRC_NONE) {
3784 s->flags &= ~SN_SELF_GEN;
3785 return 1;
3786 }
3787 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003788 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003789 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003790 if (ret >= 0)
3791 return ret;
3792 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003793 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003794
Willy Tarreau91861262007-10-17 17:06:05 +02003795 /* unknown data source or internal error */
3796 s->txn.status = 500;
3797 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003798 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003799 if (!(s->flags & SN_ERR_MASK))
3800 s->flags |= SN_ERR_PRXCOND;
3801 if (!(s->flags & SN_FINST_MASK))
3802 s->flags |= SN_FINST_R;
3803 s->flags &= ~SN_SELF_GEN;
3804 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003805}
3806
3807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003808/* Iterate the same filter through all request headers.
3809 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003810 * Since it can manage the switch to another backend, it updates the per-proxy
3811 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003812 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003813int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003814{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003815 char term;
3816 char *cur_ptr, *cur_end, *cur_next;
3817 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003818 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003819 struct hdr_idx_elem *cur_hdr;
3820 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003822 last_hdr = 0;
3823
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003824 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003825 old_idx = 0;
3826
3827 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003828 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003829 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003830 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003831 (exp->action == ACT_ALLOW ||
3832 exp->action == ACT_DENY ||
3833 exp->action == ACT_TARPIT))
3834 return 0;
3835
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003836 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003837 if (!cur_idx)
3838 break;
3839
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003840 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003841 cur_ptr = cur_next;
3842 cur_end = cur_ptr + cur_hdr->len;
3843 cur_next = cur_end + cur_hdr->cr + 1;
3844
3845 /* Now we have one header between cur_ptr and cur_end,
3846 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003847 */
3848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 /* The annoying part is that pattern matching needs
3850 * that we modify the contents to null-terminate all
3851 * strings before testing them.
3852 */
3853
3854 term = *cur_end;
3855 *cur_end = '\0';
3856
3857 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3858 switch (exp->action) {
3859 case ACT_SETBE:
3860 /* It is not possible to jump a second time.
3861 * FIXME: should we return an HTTP/500 here so that
3862 * the admin knows there's a problem ?
3863 */
3864 if (t->be != t->fe)
3865 break;
3866
3867 /* Swithing Proxy */
3868 t->be = (struct proxy *) exp->replace;
3869
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003870 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003871 * because we have associated req_cap and rsp_cap to the
3872 * frontend, and the beconn will be updated later.
3873 */
3874
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003875 t->rep->rto = t->req->wto = t->be->timeout.server;
3876 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003877 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003878 last_hdr = 1;
3879 break;
3880
3881 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003882 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003883 last_hdr = 1;
3884 break;
3885
3886 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003887 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003888 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003889 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003890 break;
3891
3892 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003893 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003894 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003895 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003896 break;
3897
3898 case ACT_REPLACE:
3899 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3900 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3901 /* FIXME: if the user adds a newline in the replacement, the
3902 * index will not be recalculated for now, and the new line
3903 * will not be counted as a new header.
3904 */
3905
3906 cur_end += delta;
3907 cur_next += delta;
3908 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003909 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003910 break;
3911
3912 case ACT_REMOVE:
3913 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3914 cur_next += delta;
3915
3916 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003917 txn->req.eoh += delta;
3918 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3919 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003920 cur_hdr->len = 0;
3921 cur_end = NULL; /* null-term has been rewritten */
3922 break;
3923
3924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003925 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003926 if (cur_end)
3927 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003928
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003929 /* keep the link from this header to next one in case of later
3930 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003931 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003932 old_idx = cur_idx;
3933 }
3934 return 0;
3935}
3936
3937
3938/* Apply the filter to the request line.
3939 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3940 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003941 * Since it can manage the switch to another backend, it updates the per-proxy
3942 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003943 */
3944int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3945{
3946 char term;
3947 char *cur_ptr, *cur_end;
3948 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003949 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003950 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003951
Willy Tarreau58f10d72006-12-04 02:26:12 +01003952
Willy Tarreau3d300592007-03-18 18:34:41 +01003953 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003954 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003955 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003956 (exp->action == ACT_ALLOW ||
3957 exp->action == ACT_DENY ||
3958 exp->action == ACT_TARPIT))
3959 return 0;
3960 else if (exp->action == ACT_REMOVE)
3961 return 0;
3962
3963 done = 0;
3964
Willy Tarreau9cdde232007-05-02 20:58:19 +02003965 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003966 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003967
3968 /* Now we have the request line between cur_ptr and cur_end */
3969
3970 /* The annoying part is that pattern matching needs
3971 * that we modify the contents to null-terminate all
3972 * strings before testing them.
3973 */
3974
3975 term = *cur_end;
3976 *cur_end = '\0';
3977
3978 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3979 switch (exp->action) {
3980 case ACT_SETBE:
3981 /* It is not possible to jump a second time.
3982 * FIXME: should we return an HTTP/500 here so that
3983 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003984 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003985 if (t->be != t->fe)
3986 break;
3987
3988 /* Swithing Proxy */
3989 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003990
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991 /* right now, the backend switch is not too much complicated
3992 * because we have associated req_cap and rsp_cap to the
3993 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003994 */
3995
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003996 t->rep->rto = t->req->wto = t->be->timeout.server;
3997 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003998 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003999 done = 1;
4000 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004001
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004002 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004003 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004004 done = 1;
4005 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004006
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004007 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004008 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004009 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 done = 1;
4011 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004014 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004015 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 done = 1;
4017 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004019 case ACT_REPLACE:
4020 *cur_end = term; /* restore the string terminator */
4021 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4022 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4023 /* FIXME: if the user adds a newline in the replacement, the
4024 * index will not be recalculated for now, and the new line
4025 * will not be counted as a new header.
4026 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004027
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004028 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004029 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004030
Willy Tarreau9cdde232007-05-02 20:58:19 +02004031 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004032 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 HTTP_MSG_RQMETH,
4034 cur_ptr, cur_end + 1,
4035 NULL, NULL);
4036 if (unlikely(!cur_end))
4037 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004039 /* we have a full request and we know that we have either a CR
4040 * or an LF at <ptr>.
4041 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004042 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4043 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004044 /* there is no point trying this regex on headers */
4045 return 1;
4046 }
4047 }
4048 *cur_end = term; /* restore the string terminator */
4049 return done;
4050}
Willy Tarreau97de6242006-12-27 17:18:38 +01004051
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052
Willy Tarreau58f10d72006-12-04 02:26:12 +01004053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004054/*
4055 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4056 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004057 * unparsable request. Since it can manage the switch to another backend, it
4058 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004059 */
4060int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4061{
Willy Tarreau3d300592007-03-18 18:34:41 +01004062 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004064 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 /*
4068 * The interleaving of transformations and verdicts
4069 * makes it difficult to decide to continue or stop
4070 * the evaluation.
4071 */
4072
Willy Tarreau3d300592007-03-18 18:34:41 +01004073 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004074 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4075 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4076 exp = exp->next;
4077 continue;
4078 }
4079
4080 /* Apply the filter to the request line. */
4081 ret = apply_filter_to_req_line(t, req, exp);
4082 if (unlikely(ret < 0))
4083 return -1;
4084
4085 if (likely(ret == 0)) {
4086 /* The filter did not match the request, it can be
4087 * iterated through all headers.
4088 */
4089 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004090 }
4091 exp = exp->next;
4092 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004094}
4095
4096
Willy Tarreaua15645d2007-03-18 16:22:39 +01004097
Willy Tarreau58f10d72006-12-04 02:26:12 +01004098/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004099 * Manage client-side cookie. It can impact performance by about 2% so it is
4100 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101 */
4102void manage_client_side_cookies(struct session *t, struct buffer *req)
4103{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004104 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004105 char *p1, *p2, *p3, *p4;
4106 char *del_colon, *del_cookie, *colon;
4107 int app_cookies;
4108
4109 appsess *asession_temp = NULL;
4110 appsess local_asession;
4111
4112 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004113 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114
Willy Tarreau2a324282006-12-05 00:05:46 +01004115 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004116 * we start with the start line.
4117 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004118 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004119 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004121 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004122 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004123 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004125 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004126 cur_ptr = cur_next;
4127 cur_end = cur_ptr + cur_hdr->len;
4128 cur_next = cur_end + cur_hdr->cr + 1;
4129
4130 /* We have one full header between cur_ptr and cur_end, and the
4131 * next header starts at cur_next. We're only interested in
4132 * "Cookie:" headers.
4133 */
4134
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004135 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4136 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004137 old_idx = cur_idx;
4138 continue;
4139 }
4140
4141 /* Now look for cookies. Conforming to RFC2109, we have to support
4142 * attributes whose name begin with a '$', and associate them with
4143 * the right cookie, if we want to delete this cookie.
4144 * So there are 3 cases for each cookie read :
4145 * 1) it's a special attribute, beginning with a '$' : ignore it.
4146 * 2) it's a server id cookie that we *MAY* want to delete : save
4147 * some pointers on it (last semi-colon, beginning of cookie...)
4148 * 3) it's an application cookie : we *MAY* have to delete a previous
4149 * "special" cookie.
4150 * At the end of loop, if a "special" cookie remains, we may have to
4151 * remove it. If no application cookie persists in the header, we
4152 * *MUST* delete it
4153 */
4154
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004155 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156
Willy Tarreau58f10d72006-12-04 02:26:12 +01004157 /* del_cookie == NULL => nothing to be deleted */
4158 del_colon = del_cookie = NULL;
4159 app_cookies = 0;
4160
4161 while (p1 < cur_end) {
4162 /* skip spaces and colons, but keep an eye on these ones */
4163 while (p1 < cur_end) {
4164 if (*p1 == ';' || *p1 == ',')
4165 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004166 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 break;
4168 p1++;
4169 }
4170
4171 if (p1 == cur_end)
4172 break;
4173
4174 /* p1 is at the beginning of the cookie name */
4175 p2 = p1;
4176 while (p2 < cur_end && *p2 != '=')
4177 p2++;
4178
4179 if (p2 == cur_end)
4180 break;
4181
4182 p3 = p2 + 1; /* skips the '=' sign */
4183 if (p3 == cur_end)
4184 break;
4185
4186 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004187 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188 p4++;
4189
4190 /* here, we have the cookie name between p1 and p2,
4191 * and its value between p3 and p4.
4192 * we can process it :
4193 *
4194 * Cookie: NAME=VALUE;
4195 * | || || |
4196 * | || || +--> p4
4197 * | || |+-------> p3
4198 * | || +--------> p2
4199 * | |+------------> p1
4200 * | +-------------> colon
4201 * +--------------------> cur_ptr
4202 */
4203
4204 if (*p1 == '$') {
4205 /* skip this one */
4206 }
4207 else {
4208 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004209 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004210 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004211 (p4 - p1 >= t->fe->capture_namelen) &&
4212 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004213 int log_len = p4 - p1;
4214
Willy Tarreau086b3b42007-05-13 21:45:51 +02004215 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216 Alert("HTTP logging : out of memory.\n");
4217 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004218 if (log_len > t->fe->capture_len)
4219 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004220 memcpy(txn->cli_cookie, p1, log_len);
4221 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 }
4223 }
4224
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004225 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4226 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004228 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004229 char *delim;
4230
4231 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4232 * have the server ID betweek p3 and delim, and the original cookie between
4233 * delim+1 and p4. Otherwise, delim==p4 :
4234 *
4235 * Cookie: NAME=SRV~VALUE;
4236 * | || || | |
4237 * | || || | +--> p4
4238 * | || || +--------> delim
4239 * | || |+-----------> p3
4240 * | || +------------> p2
4241 * | |+----------------> p1
4242 * | +-----------------> colon
4243 * +------------------------> cur_ptr
4244 */
4245
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004246 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004247 for (delim = p3; delim < p4; delim++)
4248 if (*delim == COOKIE_DELIM)
4249 break;
4250 }
4251 else
4252 delim = p4;
4253
4254
4255 /* Here, we'll look for the first running server which supports the cookie.
4256 * This allows to share a same cookie between several servers, for example
4257 * to dedicate backup servers to specific servers only.
4258 * However, to prevent clients from sticking to cookie-less backup server
4259 * when they have incidentely learned an empty cookie, we simply ignore
4260 * empty cookies and mark them as invalid.
4261 */
4262 if (delim == p3)
4263 srv = NULL;
4264
4265 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004266 if (srv->cookie && (srv->cklen == delim - p3) &&
4267 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004268 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004270 txn->flags &= ~TX_CK_MASK;
4271 txn->flags |= TX_CK_VALID;
4272 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 t->srv = srv;
4274 break;
4275 } else {
4276 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004277 txn->flags &= ~TX_CK_MASK;
4278 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004279 }
4280 }
4281 srv = srv->next;
4282 }
4283
Willy Tarreau3d300592007-03-18 18:34:41 +01004284 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004285 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004286 txn->flags &= ~TX_CK_MASK;
4287 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004288 }
4289
4290 /* depending on the cookie mode, we may have to either :
4291 * - delete the complete cookie if we're in insert+indirect mode, so that
4292 * the server never sees it ;
4293 * - remove the server id from the cookie value, and tag the cookie as an
4294 * application cookie so that it does not get accidentely removed later,
4295 * if we're in cookie prefix mode
4296 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004297 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004298 int delta; /* negative */
4299
4300 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4301 p4 += delta;
4302 cur_end += delta;
4303 cur_next += delta;
4304 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004305 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306
4307 del_cookie = del_colon = NULL;
4308 app_cookies++; /* protect the header from deletion */
4309 }
4310 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004311 (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 +01004312 del_cookie = p1;
4313 del_colon = colon;
4314 }
4315 } else {
4316 /* now we know that we must keep this cookie since it's
4317 * not ours. But if we wanted to delete our cookie
4318 * earlier, we cannot remove the complete header, but we
4319 * can remove the previous block itself.
4320 */
4321 app_cookies++;
4322
4323 if (del_cookie != NULL) {
4324 int delta; /* negative */
4325
4326 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4327 p4 += delta;
4328 cur_end += delta;
4329 cur_next += delta;
4330 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004331 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004332 del_cookie = del_colon = NULL;
4333 }
4334 }
4335
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004336 if ((t->be->appsession_name != NULL) &&
4337 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004338 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004339
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 /* Cool... it's the right one */
4341
4342 asession_temp = &local_asession;
4343
Willy Tarreau63963c62007-05-13 21:29:55 +02004344 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004345 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4346 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4347 return;
4348 }
4349
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4351 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004353
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004355 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4356 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004357 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004358 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004359 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004360 Alert("Not enough memory process_cli():asession:calloc().\n");
4361 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4362 return;
4363 }
4364
4365 asession_temp->sessid = local_asession.sessid;
4366 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004367 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004368 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004369 } else {
4370 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004371 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004373 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004374 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375 Alert("Found Application Session without matching server.\n");
4376 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004377 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004378 while (srv) {
4379 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004380 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004381 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004382 txn->flags &= ~TX_CK_MASK;
4383 txn->flags |= TX_CK_VALID;
4384 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004385 t->srv = srv;
4386 break;
4387 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004388 txn->flags &= ~TX_CK_MASK;
4389 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004390 }
4391 }
4392 srv = srv->next;
4393 }/* end while(srv) */
4394 }/* end else if server == NULL */
4395
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004396 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004397 asession_temp->request_count++;
4398#if defined(DEBUG_HASH)
4399 Alert("manage_client_side_cookies\n");
4400 appsession_hash_dump(&(t->be->htbl_proxy));
4401#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004402 }/* end if ((t->proxy->appsession_name != NULL) ... */
4403 }
4404
4405 /* we'll have to look for another cookie ... */
4406 p1 = p4;
4407 } /* while (p1 < cur_end) */
4408
4409 /* There's no more cookie on this line.
4410 * We may have marked the last one(s) for deletion.
4411 * We must do this now in two ways :
4412 * - if there is no app cookie, we simply delete the header ;
4413 * - if there are app cookies, we must delete the end of the
4414 * string properly, including the colon/semi-colon before
4415 * the cookie name.
4416 */
4417 if (del_cookie != NULL) {
4418 int delta;
4419 if (app_cookies) {
4420 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4421 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004422 cur_hdr->len += delta;
4423 } else {
4424 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004425
4426 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004427 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4428 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 cur_hdr->len = 0;
4430 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004431 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004432 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004433 }
4434
4435 /* keep the link from this header to next one */
4436 old_idx = cur_idx;
4437 } /* end of cookie processing on this header */
4438}
4439
4440
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441/* Iterate the same filter through all response headers contained in <rtr>.
4442 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4443 */
4444int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4445{
4446 char term;
4447 char *cur_ptr, *cur_end, *cur_next;
4448 int cur_idx, old_idx, last_hdr;
4449 struct http_txn *txn = &t->txn;
4450 struct hdr_idx_elem *cur_hdr;
4451 int len, delta;
4452
4453 last_hdr = 0;
4454
4455 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4456 old_idx = 0;
4457
4458 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004459 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004460 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004461 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004462 (exp->action == ACT_ALLOW ||
4463 exp->action == ACT_DENY))
4464 return 0;
4465
4466 cur_idx = txn->hdr_idx.v[old_idx].next;
4467 if (!cur_idx)
4468 break;
4469
4470 cur_hdr = &txn->hdr_idx.v[cur_idx];
4471 cur_ptr = cur_next;
4472 cur_end = cur_ptr + cur_hdr->len;
4473 cur_next = cur_end + cur_hdr->cr + 1;
4474
4475 /* Now we have one header between cur_ptr and cur_end,
4476 * and the next header starts at cur_next.
4477 */
4478
4479 /* The annoying part is that pattern matching needs
4480 * that we modify the contents to null-terminate all
4481 * strings before testing them.
4482 */
4483
4484 term = *cur_end;
4485 *cur_end = '\0';
4486
4487 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4488 switch (exp->action) {
4489 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004490 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004491 last_hdr = 1;
4492 break;
4493
4494 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004495 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004496 last_hdr = 1;
4497 break;
4498
4499 case ACT_REPLACE:
4500 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4501 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4502 /* FIXME: if the user adds a newline in the replacement, the
4503 * index will not be recalculated for now, and the new line
4504 * will not be counted as a new header.
4505 */
4506
4507 cur_end += delta;
4508 cur_next += delta;
4509 cur_hdr->len += delta;
4510 txn->rsp.eoh += delta;
4511 break;
4512
4513 case ACT_REMOVE:
4514 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4515 cur_next += delta;
4516
4517 /* FIXME: this should be a separate function */
4518 txn->rsp.eoh += delta;
4519 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4520 txn->hdr_idx.used--;
4521 cur_hdr->len = 0;
4522 cur_end = NULL; /* null-term has been rewritten */
4523 break;
4524
4525 }
4526 }
4527 if (cur_end)
4528 *cur_end = term; /* restore the string terminator */
4529
4530 /* keep the link from this header to next one in case of later
4531 * removal of next header.
4532 */
4533 old_idx = cur_idx;
4534 }
4535 return 0;
4536}
4537
4538
4539/* Apply the filter to the status line in the response buffer <rtr>.
4540 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4541 * or -1 if a replacement resulted in an invalid status line.
4542 */
4543int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4544{
4545 char term;
4546 char *cur_ptr, *cur_end;
4547 int done;
4548 struct http_txn *txn = &t->txn;
4549 int len, delta;
4550
4551
Willy Tarreau3d300592007-03-18 18:34:41 +01004552 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004553 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004554 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004555 (exp->action == ACT_ALLOW ||
4556 exp->action == ACT_DENY))
4557 return 0;
4558 else if (exp->action == ACT_REMOVE)
4559 return 0;
4560
4561 done = 0;
4562
Willy Tarreau9cdde232007-05-02 20:58:19 +02004563 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004564 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4565
4566 /* Now we have the status line between cur_ptr and cur_end */
4567
4568 /* The annoying part is that pattern matching needs
4569 * that we modify the contents to null-terminate all
4570 * strings before testing them.
4571 */
4572
4573 term = *cur_end;
4574 *cur_end = '\0';
4575
4576 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4577 switch (exp->action) {
4578 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004579 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004580 done = 1;
4581 break;
4582
4583 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004584 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004585 done = 1;
4586 break;
4587
4588 case ACT_REPLACE:
4589 *cur_end = term; /* restore the string terminator */
4590 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4591 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4592 /* FIXME: if the user adds a newline in the replacement, the
4593 * index will not be recalculated for now, and the new line
4594 * will not be counted as a new header.
4595 */
4596
4597 txn->rsp.eoh += delta;
4598 cur_end += delta;
4599
Willy Tarreau9cdde232007-05-02 20:58:19 +02004600 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004601 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004602 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004603 cur_ptr, cur_end + 1,
4604 NULL, NULL);
4605 if (unlikely(!cur_end))
4606 return -1;
4607
4608 /* we have a full respnse and we know that we have either a CR
4609 * or an LF at <ptr>.
4610 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004611 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4613 /* there is no point trying this regex on headers */
4614 return 1;
4615 }
4616 }
4617 *cur_end = term; /* restore the string terminator */
4618 return done;
4619}
4620
4621
4622
4623/*
4624 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4625 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4626 * unparsable response.
4627 */
4628int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4629{
Willy Tarreau3d300592007-03-18 18:34:41 +01004630 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004631 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004632 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004633 int ret;
4634
4635 /*
4636 * The interleaving of transformations and verdicts
4637 * makes it difficult to decide to continue or stop
4638 * the evaluation.
4639 */
4640
Willy Tarreau3d300592007-03-18 18:34:41 +01004641 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004642 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4643 exp->action == ACT_PASS)) {
4644 exp = exp->next;
4645 continue;
4646 }
4647
4648 /* Apply the filter to the status line. */
4649 ret = apply_filter_to_sts_line(t, rtr, exp);
4650 if (unlikely(ret < 0))
4651 return -1;
4652
4653 if (likely(ret == 0)) {
4654 /* The filter did not match the response, it can be
4655 * iterated through all headers.
4656 */
4657 apply_filter_to_resp_headers(t, rtr, exp);
4658 }
4659 exp = exp->next;
4660 }
4661 return 0;
4662}
4663
4664
4665
4666/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004667 * Manage server-side cookies. It can impact performance by about 2% so it is
4668 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004669 */
4670void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4671{
4672 struct http_txn *txn = &t->txn;
4673 char *p1, *p2, *p3, *p4;
4674
4675 appsess *asession_temp = NULL;
4676 appsess local_asession;
4677
4678 char *cur_ptr, *cur_end, *cur_next;
4679 int cur_idx, old_idx, delta;
4680
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681 /* Iterate through the headers.
4682 * we start with the start line.
4683 */
4684 old_idx = 0;
4685 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4686
4687 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4688 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004689 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690
4691 cur_hdr = &txn->hdr_idx.v[cur_idx];
4692 cur_ptr = cur_next;
4693 cur_end = cur_ptr + cur_hdr->len;
4694 cur_next = cur_end + cur_hdr->cr + 1;
4695
4696 /* We have one full header between cur_ptr and cur_end, and the
4697 * next header starts at cur_next. We're only interested in
4698 * "Cookie:" headers.
4699 */
4700
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004701 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4702 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703 old_idx = cur_idx;
4704 continue;
4705 }
4706
4707 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004708 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004709
4710
4711 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004712 if (t->be->cookie_name == NULL &&
4713 t->be->appsession_name == NULL &&
4714 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715 return;
4716
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004717 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004718
4719 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4721 break;
4722
4723 /* p1 is at the beginning of the cookie name */
4724 p2 = p1;
4725
4726 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4727 p2++;
4728
4729 if (p2 == cur_end || *p2 == ';') /* next cookie */
4730 break;
4731
4732 p3 = p2 + 1; /* skip the '=' sign */
4733 if (p3 == cur_end)
4734 break;
4735
4736 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004737 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004738 p4++;
4739
4740 /* here, we have the cookie name between p1 and p2,
4741 * and its value between p3 and p4.
4742 * we can process it.
4743 */
4744
4745 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004746 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004747 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004748 (p4 - p1 >= t->be->capture_namelen) &&
4749 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004750 int log_len = p4 - p1;
4751
Willy Tarreau086b3b42007-05-13 21:45:51 +02004752 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004753 Alert("HTTP logging : out of memory.\n");
4754 }
4755
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004756 if (log_len > t->be->capture_len)
4757 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004758 memcpy(txn->srv_cookie, p1, log_len);
4759 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004760 }
4761
4762 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004763 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4764 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004765 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004766 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767
4768 /* If the cookie is in insert mode on a known server, we'll delete
4769 * this occurrence because we'll insert another one later.
4770 * We'll delete it too if the "indirect" option is set and we're in
4771 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004772 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4773 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004774 /* this header must be deleted */
4775 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4776 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4777 txn->hdr_idx.used--;
4778 cur_hdr->len = 0;
4779 cur_next += delta;
4780 txn->rsp.eoh += delta;
4781
Willy Tarreau3d300592007-03-18 18:34:41 +01004782 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004783 }
4784 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004785 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004786 /* replace bytes p3->p4 with the cookie name associated
4787 * with this server since we know it.
4788 */
4789 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4790 cur_hdr->len += delta;
4791 cur_next += delta;
4792 txn->rsp.eoh += delta;
4793
Willy Tarreau3d300592007-03-18 18:34:41 +01004794 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004795 }
4796 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004797 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004798 /* insert the cookie name associated with this server
4799 * before existing cookie, and insert a delimitor between them..
4800 */
4801 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4802 cur_hdr->len += delta;
4803 cur_next += delta;
4804 txn->rsp.eoh += delta;
4805
4806 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004807 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004808 }
4809 }
4810 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004811 else if ((t->be->appsession_name != NULL) &&
4812 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004813
4814 /* Cool... it's the right one */
4815
4816 size_t server_id_len = strlen(t->srv->id) + 1;
4817 asession_temp = &local_asession;
4818
Willy Tarreau63963c62007-05-13 21:29:55 +02004819 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004820 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4821 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4822 return;
4823 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004824 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4825 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004826 asession_temp->serverid = NULL;
4827
4828 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004829 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4830 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004831 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004832 Alert("Not enough Memory process_srv():asession:calloc().\n");
4833 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4834 return;
4835 }
4836 asession_temp->sessid = local_asession.sessid;
4837 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004838 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004839 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4840 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004841 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004842 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004843 }
4844
Willy Tarreaua15645d2007-03-18 16:22:39 +01004845 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004846 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004847 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4848 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4849 return;
4850 }
4851 asession_temp->serverid[0] = '\0';
4852 }
4853
4854 if (asession_temp->serverid[0] == '\0')
4855 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4856
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004857 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004858 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004859#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004860 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004861 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004862#endif
4863 }/* end if ((t->proxy->appsession_name != NULL) ... */
4864 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4865 } /* we're now at the end of the cookie value */
4866
4867 /* keep the link from this header to next one */
4868 old_idx = cur_idx;
4869 } /* end of cookie processing on this header */
4870}
4871
4872
4873
4874/*
4875 * Check if response is cacheable or not. Updates t->flags.
4876 */
4877void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4878{
4879 struct http_txn *txn = &t->txn;
4880 char *p1, *p2;
4881
4882 char *cur_ptr, *cur_end, *cur_next;
4883 int cur_idx;
4884
Willy Tarreau5df51872007-11-25 16:20:08 +01004885 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004886 return;
4887
4888 /* Iterate through the headers.
4889 * we start with the start line.
4890 */
4891 cur_idx = 0;
4892 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4893
4894 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4895 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004896 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004897
4898 cur_hdr = &txn->hdr_idx.v[cur_idx];
4899 cur_ptr = cur_next;
4900 cur_end = cur_ptr + cur_hdr->len;
4901 cur_next = cur_end + cur_hdr->cr + 1;
4902
4903 /* We have one full header between cur_ptr and cur_end, and the
4904 * next header starts at cur_next. We're only interested in
4905 * "Cookie:" headers.
4906 */
4907
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004908 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4909 if (val) {
4910 if ((cur_end - (cur_ptr + val) >= 8) &&
4911 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4912 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4913 return;
4914 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004915 }
4916
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004917 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4918 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004919 continue;
4920
4921 /* OK, right now we know we have a cache-control header at cur_ptr */
4922
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004923 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004924
4925 if (p1 >= cur_end) /* no more info */
4926 continue;
4927
4928 /* p1 is at the beginning of the value */
4929 p2 = p1;
4930
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004931 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004932 p2++;
4933
4934 /* we have a complete value between p1 and p2 */
4935 if (p2 < cur_end && *p2 == '=') {
4936 /* we have something of the form no-cache="set-cookie" */
4937 if ((cur_end - p1 >= 21) &&
4938 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4939 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004940 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004941 continue;
4942 }
4943
4944 /* OK, so we know that either p2 points to the end of string or to a comma */
4945 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4946 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4947 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4948 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004949 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004950 return;
4951 }
4952
4953 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004954 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004955 continue;
4956 }
4957 }
4958}
4959
4960
Willy Tarreau58f10d72006-12-04 02:26:12 +01004961/*
4962 * Try to retrieve a known appsession in the URI, then the associated server.
4963 * If the server is found, it's assigned to the session.
4964 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004965void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966{
Willy Tarreau3d300592007-03-18 18:34:41 +01004967 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004968 appsess *asession_temp = NULL;
4969 appsess local_asession;
4970 char *request_line;
4971
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004972 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004973 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004975 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976 return;
4977
4978 /* skip ';' */
4979 request_line++;
4980
4981 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004982 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004983 return;
4984
4985 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004986 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987
4988 /* First try if we already have an appsession */
4989 asession_temp = &local_asession;
4990
Willy Tarreau63963c62007-05-13 21:29:55 +02004991 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004992 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4993 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4994 return;
4995 }
4996
4997 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004998 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4999 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005000 asession_temp->serverid = NULL;
5001
5002 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005003 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5004 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005005 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005006 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005007 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005008 Alert("Not enough memory process_cli():asession:calloc().\n");
5009 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5010 return;
5011 }
5012 asession_temp->sessid = local_asession.sessid;
5013 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005014 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005015 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016 }
5017 else {
5018 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005019 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005020 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005021
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005022 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005023 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005024
Willy Tarreau58f10d72006-12-04 02:26:12 +01005025#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005026 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005027 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005028#endif
5029 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005030 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031 Alert("Found Application Session without matching server.\n");
5032 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005033 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005034 while (srv) {
5035 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005036 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005037 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005038 txn->flags &= ~TX_CK_MASK;
5039 txn->flags |= TX_CK_VALID;
5040 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005041 t->srv = srv;
5042 break;
5043 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005044 txn->flags &= ~TX_CK_MASK;
5045 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005046 }
5047 }
5048 srv = srv->next;
5049 }
5050 }
5051}
5052
5053
Willy Tarreaub2513902006-12-17 14:52:38 +01005054/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005055 * In a GET or HEAD request, check if the requested URI matches the stats uri
5056 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005057 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005058 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005059 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005060 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005061 *
5062 * Returns 1 if the session's state changes, otherwise 0.
5063 */
5064int stats_check_uri_auth(struct session *t, struct proxy *backend)
5065{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005066 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005067 struct uri_auth *uri_auth = backend->uri_auth;
5068 struct user_auth *user;
5069 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005070 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005071
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005072 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005074 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005075 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005076 return 0;
5077
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005078 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005079
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005080 /* the URI is in h */
5081 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005082 return 0;
5083
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005084 h += uri_auth->uri_len;
5085 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5086 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005087 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005088 break;
5089 }
5090 h++;
5091 }
5092
5093 if (uri_auth->refresh) {
5094 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5095 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5096 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005097 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005098 break;
5099 }
5100 h++;
5101 }
5102 }
5103
Willy Tarreau55bb8452007-10-17 18:44:57 +02005104 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5105 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5106 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005107 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005108 break;
5109 }
5110 h++;
5111 }
5112
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005113 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5114
Willy Tarreaub2513902006-12-17 14:52:38 +01005115 /* we are in front of a interceptable URI. Let's check
5116 * if there's an authentication and if it's valid.
5117 */
5118 user = uri_auth->users;
5119 if (!user) {
5120 /* no user auth required, it's OK */
5121 authenticated = 1;
5122 } else {
5123 authenticated = 0;
5124
5125 /* a user list is defined, we have to check.
5126 * skip 21 chars for "Authorization: Basic ".
5127 */
5128
5129 /* FIXME: this should move to an earlier place */
5130 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005131 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5132 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5133 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005134 if (len > 14 &&
5135 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005136 txn->auth_hdr.str = h;
5137 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005138 break;
5139 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005140 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005141 }
5142
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005143 if (txn->auth_hdr.len < 21 ||
5144 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005145 user = NULL;
5146
5147 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005148 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5149 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005150 user->user_pwd, user->user_len)) {
5151 authenticated = 1;
5152 break;
5153 }
5154 user = user->next;
5155 }
5156 }
5157
5158 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005159 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005160
5161 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005162 msg.str = trash;
5163 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005164 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005165 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005166 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005167 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005168 if (!(t->flags & SN_ERR_MASK))
5169 t->flags |= SN_ERR_PRXCOND;
5170 if (!(t->flags & SN_FINST_MASK))
5171 t->flags |= SN_FINST_R;
5172 return 1;
5173 }
5174
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005175 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005176 * data.
5177 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005178 EV_FD_CLR(t->cli_fd, DIR_RD);
5179 buffer_shutr(t->req);
5180 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005181 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005182 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005183 t->data_source = DATA_SRC_STATS;
5184 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005185 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005186 produce_content(t);
5187 return 1;
5188}
5189
5190
Willy Tarreaubaaee002006-06-26 02:48:02 +02005191/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005192 * Print a debug line with a header
5193 */
5194void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5195{
5196 int len, max;
5197 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5198 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5199 max = end - start;
5200 UBOUND(max, sizeof(trash) - len - 1);
5201 len += strlcpy2(trash + len, start, max + 1);
5202 trash[len++] = '\n';
5203 write(1, trash, len);
5204}
5205
5206
Willy Tarreau8797c062007-05-07 00:55:35 +02005207/************************************************************************/
5208/* The code below is dedicated to ACL parsing and matching */
5209/************************************************************************/
5210
5211
5212
5213
5214/* 1. Check on METHOD
5215 * We use the pre-parsed method if it is known, and store its number as an
5216 * integer. If it is unknown, we use the pointer and the length.
5217 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005218static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005219{
5220 int len, meth;
5221
Willy Tarreauae8b7962007-06-09 23:10:04 +02005222 len = strlen(*text);
5223 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005224
5225 pattern->val.i = meth;
5226 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005227 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005228 if (!pattern->ptr.str)
5229 return 0;
5230 pattern->len = len;
5231 }
5232 return 1;
5233}
5234
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005235static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005236acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5237 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005238{
5239 int meth;
5240 struct http_txn *txn = l7;
5241
Willy Tarreaub6866442008-07-14 23:54:42 +02005242 if (!txn)
5243 return 0;
5244
Willy Tarreauc11416f2007-06-17 16:58:38 +02005245 if (txn->req.msg_state != HTTP_MSG_BODY)
5246 return 0;
5247
Willy Tarreau8797c062007-05-07 00:55:35 +02005248 meth = txn->meth;
5249 test->i = meth;
5250 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005251 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5252 /* ensure the indexes are not affected */
5253 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005254 test->len = txn->req.sl.rq.m_l;
5255 test->ptr = txn->req.sol;
5256 }
5257 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5258 return 1;
5259}
5260
5261static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5262{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005263 int icase;
5264
Willy Tarreau8797c062007-05-07 00:55:35 +02005265 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005266 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005267
5268 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005269 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005270
5271 /* Other method, we must compare the strings */
5272 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005273 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005274
5275 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5276 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5277 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005278 return ACL_PAT_FAIL;
5279 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005280}
5281
5282/* 2. Check on Request/Status Version
5283 * We simply compare strings here.
5284 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005285static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005286{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005287 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005288 if (!pattern->ptr.str)
5289 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005290 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005291 return 1;
5292}
5293
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005294static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005295acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5296 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005297{
5298 struct http_txn *txn = l7;
5299 char *ptr;
5300 int len;
5301
Willy Tarreaub6866442008-07-14 23:54:42 +02005302 if (!txn)
5303 return 0;
5304
Willy Tarreauc11416f2007-06-17 16:58:38 +02005305 if (txn->req.msg_state != HTTP_MSG_BODY)
5306 return 0;
5307
Willy Tarreau8797c062007-05-07 00:55:35 +02005308 len = txn->req.sl.rq.v_l;
5309 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5310
5311 while ((len-- > 0) && (*ptr++ != '/'));
5312 if (len <= 0)
5313 return 0;
5314
5315 test->ptr = ptr;
5316 test->len = len;
5317
5318 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5319 return 1;
5320}
5321
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005322static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005323acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5324 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005325{
5326 struct http_txn *txn = l7;
5327 char *ptr;
5328 int len;
5329
Willy Tarreaub6866442008-07-14 23:54:42 +02005330 if (!txn)
5331 return 0;
5332
Willy Tarreauc11416f2007-06-17 16:58:38 +02005333 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5334 return 0;
5335
Willy Tarreau8797c062007-05-07 00:55:35 +02005336 len = txn->rsp.sl.st.v_l;
5337 ptr = txn->rsp.sol;
5338
5339 while ((len-- > 0) && (*ptr++ != '/'));
5340 if (len <= 0)
5341 return 0;
5342
5343 test->ptr = ptr;
5344 test->len = len;
5345
5346 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5347 return 1;
5348}
5349
5350/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005351static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005352acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5353 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005354{
5355 struct http_txn *txn = l7;
5356 char *ptr;
5357 int len;
5358
Willy Tarreaub6866442008-07-14 23:54:42 +02005359 if (!txn)
5360 return 0;
5361
Willy Tarreauc11416f2007-06-17 16:58:38 +02005362 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5363 return 0;
5364
Willy Tarreau8797c062007-05-07 00:55:35 +02005365 len = txn->rsp.sl.st.c_l;
5366 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5367
5368 test->i = __strl2ui(ptr, len);
5369 test->flags = ACL_TEST_F_VOL_1ST;
5370 return 1;
5371}
5372
5373/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005374static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005375acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5376 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005377{
5378 struct http_txn *txn = l7;
5379
Willy Tarreaub6866442008-07-14 23:54:42 +02005380 if (!txn)
5381 return 0;
5382
Willy Tarreauc11416f2007-06-17 16:58:38 +02005383 if (txn->req.msg_state != HTTP_MSG_BODY)
5384 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005385
Willy Tarreauc11416f2007-06-17 16:58:38 +02005386 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5387 /* ensure the indexes are not affected */
5388 return 0;
5389
Willy Tarreau8797c062007-05-07 00:55:35 +02005390 test->len = txn->req.sl.rq.u_l;
5391 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5392
Willy Tarreauf3d25982007-05-08 22:45:09 +02005393 /* we do not need to set READ_ONLY because the data is in a buffer */
5394 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005395 return 1;
5396}
5397
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005398static int
5399acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5400 struct acl_expr *expr, struct acl_test *test)
5401{
5402 struct http_txn *txn = l7;
5403
Willy Tarreaub6866442008-07-14 23:54:42 +02005404 if (!txn)
5405 return 0;
5406
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005407 if (txn->req.msg_state != HTTP_MSG_BODY)
5408 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005409
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005410 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5411 /* ensure the indexes are not affected */
5412 return 0;
5413
5414 /* Parse HTTP request */
5415 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5416 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5417 test->i = AF_INET;
5418
5419 /*
5420 * If we are parsing url in frontend space, we prepare backend stage
5421 * to not parse again the same url ! optimization lazyness...
5422 */
5423 if (px->options & PR_O_HTTP_PROXY)
5424 l4->flags |= SN_ADDR_SET;
5425
5426 test->flags = ACL_TEST_F_READ_ONLY;
5427 return 1;
5428}
5429
5430static int
5431acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5432 struct acl_expr *expr, struct acl_test *test)
5433{
5434 struct http_txn *txn = l7;
5435
Willy Tarreaub6866442008-07-14 23:54:42 +02005436 if (!txn)
5437 return 0;
5438
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005439 if (txn->req.msg_state != HTTP_MSG_BODY)
5440 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005441
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005442 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5443 /* ensure the indexes are not affected */
5444 return 0;
5445
5446 /* Same optimization as url_ip */
5447 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5448 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5449
5450 if (px->options & PR_O_HTTP_PROXY)
5451 l4->flags |= SN_ADDR_SET;
5452
5453 test->flags = ACL_TEST_F_READ_ONLY;
5454 return 1;
5455}
5456
Willy Tarreauc11416f2007-06-17 16:58:38 +02005457/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5458 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5459 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005460static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005461acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005462 struct acl_expr *expr, struct acl_test *test)
5463{
5464 struct http_txn *txn = l7;
5465 struct hdr_idx *idx = &txn->hdr_idx;
5466 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005467
Willy Tarreaub6866442008-07-14 23:54:42 +02005468 if (!txn)
5469 return 0;
5470
Willy Tarreau33a7e692007-06-10 19:45:56 +02005471 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5472 /* search for header from the beginning */
5473 ctx->idx = 0;
5474
Willy Tarreau33a7e692007-06-10 19:45:56 +02005475 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5476 test->flags |= ACL_TEST_F_FETCH_MORE;
5477 test->flags |= ACL_TEST_F_VOL_HDR;
5478 test->len = ctx->vlen;
5479 test->ptr = (char *)ctx->line + ctx->val;
5480 return 1;
5481 }
5482
5483 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5484 test->flags |= ACL_TEST_F_VOL_HDR;
5485 return 0;
5486}
5487
Willy Tarreau33a7e692007-06-10 19:45:56 +02005488static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005489acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5490 struct acl_expr *expr, struct acl_test *test)
5491{
5492 struct http_txn *txn = l7;
5493
Willy Tarreaub6866442008-07-14 23:54:42 +02005494 if (!txn)
5495 return 0;
5496
Willy Tarreauc11416f2007-06-17 16:58:38 +02005497 if (txn->req.msg_state != HTTP_MSG_BODY)
5498 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005499
Willy Tarreauc11416f2007-06-17 16:58:38 +02005500 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5501 /* ensure the indexes are not affected */
5502 return 0;
5503
5504 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5505}
5506
5507static int
5508acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5509 struct acl_expr *expr, struct acl_test *test)
5510{
5511 struct http_txn *txn = l7;
5512
Willy Tarreaub6866442008-07-14 23:54:42 +02005513 if (!txn)
5514 return 0;
5515
Willy Tarreauc11416f2007-06-17 16:58:38 +02005516 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5517 return 0;
5518
5519 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5520}
5521
5522/* 6. Check on HTTP header count. The number of occurrences is returned.
5523 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5524 */
5525static int
5526acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005527 struct acl_expr *expr, struct acl_test *test)
5528{
5529 struct http_txn *txn = l7;
5530 struct hdr_idx *idx = &txn->hdr_idx;
5531 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005532 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005533
Willy Tarreaub6866442008-07-14 23:54:42 +02005534 if (!txn)
5535 return 0;
5536
Willy Tarreau33a7e692007-06-10 19:45:56 +02005537 ctx.idx = 0;
5538 cnt = 0;
5539 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5540 cnt++;
5541
5542 test->i = cnt;
5543 test->flags = ACL_TEST_F_VOL_HDR;
5544 return 1;
5545}
5546
Willy Tarreauc11416f2007-06-17 16:58:38 +02005547static int
5548acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5549 struct acl_expr *expr, struct acl_test *test)
5550{
5551 struct http_txn *txn = l7;
5552
Willy Tarreaub6866442008-07-14 23:54:42 +02005553 if (!txn)
5554 return 0;
5555
Willy Tarreauc11416f2007-06-17 16:58:38 +02005556 if (txn->req.msg_state != HTTP_MSG_BODY)
5557 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005558
Willy Tarreauc11416f2007-06-17 16:58:38 +02005559 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5560 /* ensure the indexes are not affected */
5561 return 0;
5562
5563 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5564}
5565
5566static int
5567acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5568 struct acl_expr *expr, struct acl_test *test)
5569{
5570 struct http_txn *txn = l7;
5571
Willy Tarreaub6866442008-07-14 23:54:42 +02005572 if (!txn)
5573 return 0;
5574
Willy Tarreauc11416f2007-06-17 16:58:38 +02005575 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5576 return 0;
5577
5578 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5579}
5580
Willy Tarreau33a7e692007-06-10 19:45:56 +02005581/* 7. Check on HTTP header's integer value. The integer value is returned.
5582 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005583 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005584 */
5585static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005586acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005587 struct acl_expr *expr, struct acl_test *test)
5588{
5589 struct http_txn *txn = l7;
5590 struct hdr_idx *idx = &txn->hdr_idx;
5591 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005592
Willy Tarreaub6866442008-07-14 23:54:42 +02005593 if (!txn)
5594 return 0;
5595
Willy Tarreau33a7e692007-06-10 19:45:56 +02005596 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5597 /* search for header from the beginning */
5598 ctx->idx = 0;
5599
Willy Tarreau33a7e692007-06-10 19:45:56 +02005600 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5601 test->flags |= ACL_TEST_F_FETCH_MORE;
5602 test->flags |= ACL_TEST_F_VOL_HDR;
5603 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5604 return 1;
5605 }
5606
5607 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5608 test->flags |= ACL_TEST_F_VOL_HDR;
5609 return 0;
5610}
5611
Willy Tarreauc11416f2007-06-17 16:58:38 +02005612static int
5613acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5614 struct acl_expr *expr, struct acl_test *test)
5615{
5616 struct http_txn *txn = l7;
5617
Willy Tarreaub6866442008-07-14 23:54:42 +02005618 if (!txn)
5619 return 0;
5620
Willy Tarreauc11416f2007-06-17 16:58:38 +02005621 if (txn->req.msg_state != HTTP_MSG_BODY)
5622 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005623
Willy Tarreauc11416f2007-06-17 16:58:38 +02005624 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5625 /* ensure the indexes are not affected */
5626 return 0;
5627
5628 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5629}
5630
5631static int
5632acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5633 struct acl_expr *expr, struct acl_test *test)
5634{
5635 struct http_txn *txn = l7;
5636
Willy Tarreaub6866442008-07-14 23:54:42 +02005637 if (!txn)
5638 return 0;
5639
Willy Tarreauc11416f2007-06-17 16:58:38 +02005640 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5641 return 0;
5642
5643 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5644}
5645
Willy Tarreau737b0c12007-06-10 21:28:46 +02005646/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5647 * the first '/' after the possible hostname, and ends before the possible '?'.
5648 */
5649static int
5650acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5651 struct acl_expr *expr, struct acl_test *test)
5652{
5653 struct http_txn *txn = l7;
5654 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005655
Willy Tarreaub6866442008-07-14 23:54:42 +02005656 if (!txn)
5657 return 0;
5658
Willy Tarreauc11416f2007-06-17 16:58:38 +02005659 if (txn->req.msg_state != HTTP_MSG_BODY)
5660 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005661
Willy Tarreauc11416f2007-06-17 16:58:38 +02005662 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5663 /* ensure the indexes are not affected */
5664 return 0;
5665
Willy Tarreau21d2af32008-02-14 20:25:24 +01005666 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5667 ptr = http_get_path(txn);
5668 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005669 return 0;
5670
5671 /* OK, we got the '/' ! */
5672 test->ptr = ptr;
5673
5674 while (ptr < end && *ptr != '?')
5675 ptr++;
5676
5677 test->len = ptr - test->ptr;
5678
5679 /* we do not need to set READ_ONLY because the data is in a buffer */
5680 test->flags = ACL_TEST_F_VOL_1ST;
5681 return 1;
5682}
5683
5684
Willy Tarreau8797c062007-05-07 00:55:35 +02005685
5686/************************************************************************/
5687/* All supported keywords must be declared here. */
5688/************************************************************************/
5689
5690/* Note: must not be declared <const> as its list will be overwritten */
5691static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005692 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5693 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5694 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5695 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005696
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005697 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5698 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5699 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5700 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5701 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5702 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5703 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5704 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5705 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005706
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005707 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5708 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5709 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5710 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5711 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5712 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5713 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5714 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5715 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5716 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005717
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005718 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5719 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5720 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5721 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5722 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5723 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5724 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5725 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5726 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005727
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005728 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5729 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5730 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5731 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5732 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5733 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5734 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005735
Willy Tarreauf3d25982007-05-08 22:45:09 +02005736 { NULL, NULL, NULL, NULL },
5737
5738#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005739 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5740 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5741 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5742 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5743 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5744 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5745 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5746
Willy Tarreau8797c062007-05-07 00:55:35 +02005747 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5748 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5749 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5750 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5751 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5752 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5753 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5754 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5755
5756 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5757 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5758 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5759 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5760 { NULL, NULL, NULL, NULL },
5761#endif
5762}};
5763
5764
5765__attribute__((constructor))
5766static void __http_protocol_init(void)
5767{
5768 acl_register_keywords(&acl_kws);
5769}
5770
5771
Willy Tarreau58f10d72006-12-04 02:26:12 +01005772/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005773 * Local variables:
5774 * c-indent-level: 8
5775 * c-basic-offset: 8
5776 * End:
5777 */