blob: 95f12a5c0fcf68409ccf6d9f88404334637e5aec [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200549 buffer_shutw(t->req);
550 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaudafde432008-08-17 01:00:46 +0200648/* Processes the client, server, request and response jobs of a session task,
649 * then puts it back to the wait queue in a clean state, or cleans up its
650 * resources if it must be deleted. Returns in <next> the date the task wants
651 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
652 * nothing too many times, the request and response buffers flags are monitored
653 * and each function is called only if at least another function has changed at
654 * least one flag. If one of the functions called returns non-zero, then it
655 * will be called once again after all other functions. This permits explicit
656 * external loops which may be useful for complex state machines.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 */
Willy Tarreaudafde432008-08-17 01:00:46 +0200658#define PROCESS_CLI 0x1
659#define PROCESS_SRV 0x2
660#define PROCESS_REQ 0x4
661#define PROCESS_RTR 0x8
662#define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR)
663
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200664void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
666 struct session *s = t->context;
Willy Tarreaudafde432008-08-17 01:00:46 +0200667 unsigned resync = PROCESS_ALL;
668 unsigned int rqf;
669 unsigned int rpf;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
Willy Tarreau507385d2008-08-17 13:04:25 +0200671 /* check timeout expiration only once and adjust buffer flags
672 * accordingly.
673 */
674 if (unlikely(tick_is_expired(t->expire, now_ms))) {
675 if (tick_is_expired(s->req->rex, now_ms))
676 s->req->flags |= BF_READ_TIMEOUT;
677
678 if (tick_is_expired(s->req->wex, now_ms))
679 s->req->flags |= BF_WRITE_TIMEOUT;
680
681 if (tick_is_expired(s->rep->rex, now_ms))
682 s->rep->flags |= BF_READ_TIMEOUT;
683
684 if (tick_is_expired(s->rep->wex, now_ms))
685 s->rep->flags |= BF_WRITE_TIMEOUT;
686 }
687
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 do {
Willy Tarreaudafde432008-08-17 01:00:46 +0200689 if (resync & PROCESS_REQ) {
690 resync &= ~PROCESS_REQ;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200691 if (s->req->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200692 rqf = s->req->flags;
693 rpf = s->rep->flags;
694
695 if (process_request(s))
696 resync |= PROCESS_REQ;
697
Willy Tarreau2df28e82008-08-17 15:20:19 +0200698 if (!s->req->analysers && !(s->req->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200699 s->req->flags |= BF_MAY_FORWARD;
700
701 if (rqf != s->req->flags || rpf != s->rep->flags)
702 resync |= PROCESS_ALL & ~PROCESS_REQ;
703 }
704 }
705
706 if (resync & PROCESS_RTR) {
707 resync &= ~PROCESS_RTR;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200708 if (s->rep->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200709 rqf = s->req->flags;
710 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200711
Willy Tarreaudafde432008-08-17 01:00:46 +0200712 if (process_response(s))
713 resync |= PROCESS_RTR;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200714
Willy Tarreau2df28e82008-08-17 15:20:19 +0200715 if (!s->rep->analysers && !(s->rep->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200716 s->rep->flags |= BF_MAY_FORWARD;
717
718 if (rqf != s->req->flags || rpf != s->rep->flags)
719 resync |= PROCESS_ALL & ~PROCESS_RTR;
720 }
721 }
722
723 if (resync & PROCESS_CLI) {
724 rqf = s->req->flags;
725 rpf = s->rep->flags;
726
727 resync &= ~PROCESS_CLI;
728 if (process_cli(s))
729 resync |= PROCESS_CLI;
730
731 if (rqf != s->req->flags || rpf != s->rep->flags)
732 resync |= PROCESS_ALL & ~PROCESS_CLI;
733 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200734
Willy Tarreaudafde432008-08-17 01:00:46 +0200735 if (resync & PROCESS_SRV) {
736 rqf = s->req->flags;
737 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200738
Willy Tarreaudafde432008-08-17 01:00:46 +0200739 resync &= ~PROCESS_SRV;
740 if (process_srv(s))
741 resync |= PROCESS_SRV;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200742
Willy Tarreaudafde432008-08-17 01:00:46 +0200743 if (rqf != s->req->flags || rpf != s->rep->flags)
744 resync |= PROCESS_ALL & ~PROCESS_SRV;
745 }
746 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200748 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100749
750 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
751 session_process_counters(s);
752
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200753 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
754 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreau7f875f62008-08-11 17:35:01 +0200756 /* Trick: if a request is being waiting for the server to respond,
757 * and if we know the server can timeout, we don't want the timeout
758 * to expire on the client side first, but we're still interested
759 * in passing data from the client to the server (eg: POST). Thus,
760 * we can cancel the client's request timeout if the server's
761 * request timeout is set and the server has not yet sent a response.
762 */
763
Willy Tarreauba392ce2008-08-16 21:13:23 +0200764 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200765 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200766 s->req->rex = TICK_ETERNITY;
767
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200768 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
769 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200770 if (s->req->analysers)
771 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772
773 /* restore t to its place in the task list */
774 task_queue(t);
775
Willy Tarreaua7c52762008-08-16 18:40:18 +0200776#ifdef DEBUG_DEV
777 /* this may only happen when no timeout is set or in case of an FSM bug */
778 if (!t->expire)
779 ABORT_NOW();
780#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200781 *next = t->expire;
782 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 }
784
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100785 s->fe->feconn--;
786 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 actconn--;
789
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200790 if (unlikely((global.mode & MODE_DEBUG) &&
791 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200793 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200794 s->uniq_id, s->be->id,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200795 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd,
796 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 write(1, trash, len);
798 }
799
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200800 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100801 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802
803 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200804 if (s->logs.logwait &&
805 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200806 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
807 if (s->fe->to_log & LW_REQ)
808 http_sess_log(s);
809 else
810 tcp_sess_log(s);
811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
813 /* the task MUST not be in the run queue anymore */
814 task_delete(t);
815 session_free(s);
816 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200817 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200818}
819
820
Willy Tarreau42250582007-04-01 01:30:43 +0200821extern const char sess_term_cond[8];
822extern const char sess_fin_state[8];
823extern const char *monthname[12];
824const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
825const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
826 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
827 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200828struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200829struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100830
Willy Tarreau42250582007-04-01 01:30:43 +0200831/*
832 * send a log for the session when we have enough info about it.
833 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834 */
Willy Tarreau42250582007-04-01 01:30:43 +0200835static void http_sess_log(struct session *s)
836{
837 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
838 struct proxy *fe = s->fe;
839 struct proxy *be = s->be;
840 struct proxy *prx_log;
841 struct http_txn *txn = &s->txn;
842 int tolog;
843 char *uri, *h;
844 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200845 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200846 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200847 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200848 int hdr;
849
850 if (fe->logfac1 < 0 && fe->logfac2 < 0)
851 return;
852 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100853
Willy Tarreau42250582007-04-01 01:30:43 +0200854 if (s->cli_addr.ss_family == AF_INET)
855 inet_ntop(AF_INET,
856 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
857 pn, sizeof(pn));
858 else
859 inet_ntop(AF_INET6,
860 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
861 pn, sizeof(pn));
862
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200863 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200864
865 /* FIXME: let's limit ourselves to frontend logging for now. */
866 tolog = fe->to_log;
867
868 h = tmpline;
869 if (fe->to_log & LW_REQHDR &&
870 txn->req.cap &&
871 (h < tmpline + sizeof(tmpline) - 10)) {
872 *(h++) = ' ';
873 *(h++) = '{';
874 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
875 if (hdr)
876 *(h++) = '|';
877 if (txn->req.cap[hdr] != NULL)
878 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
879 '#', hdr_encode_map, txn->req.cap[hdr]);
880 }
881 *(h++) = '}';
882 }
883
884 if (fe->to_log & LW_RSPHDR &&
885 txn->rsp.cap &&
886 (h < tmpline + sizeof(tmpline) - 7)) {
887 *(h++) = ' ';
888 *(h++) = '{';
889 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
890 if (hdr)
891 *(h++) = '|';
892 if (txn->rsp.cap[hdr] != NULL)
893 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
894 '#', hdr_encode_map, txn->rsp.cap[hdr]);
895 }
896 *(h++) = '}';
897 }
898
899 if (h < tmpline + sizeof(tmpline) - 4) {
900 *(h++) = ' ';
901 *(h++) = '"';
902 uri = txn->uri ? txn->uri : "<BADREQ>";
903 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
904 '#', url_encode_map, uri);
905 *(h++) = '"';
906 }
907 *h = '\0';
908
909 svid = (tolog & LW_SVID) ?
910 (s->data_source != DATA_SRC_STATS) ?
911 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
912
Willy Tarreau70089872008-06-13 21:12:51 +0200913 t_request = -1;
914 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
915 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
916
Willy Tarreau42250582007-04-01 01:30:43 +0200917 send_log(prx_log, LOG_INFO,
918 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
919 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100920 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200921 pn,
922 (s->cli_addr.ss_family == AF_INET) ?
923 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
924 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200925 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200926 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200927 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200928 t_request,
929 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200930 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
931 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
932 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
933 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100934 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200935 txn->cli_cookie ? txn->cli_cookie : "-",
936 txn->srv_cookie ? txn->srv_cookie : "-",
937 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
938 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
939 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
940 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
941 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100942 (s->flags & SN_REDISP)?"+":"",
943 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200944 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
945
946 s->logs.logwait = 0;
947}
948
Willy Tarreau117f59e2007-03-04 18:17:17 +0100949
950/*
951 * Capture headers from message starting at <som> according to header list
952 * <cap_hdr>, and fill the <idx> structure appropriately.
953 */
954void capture_headers(char *som, struct hdr_idx *idx,
955 char **cap, struct cap_hdr *cap_hdr)
956{
957 char *eol, *sol, *col, *sov;
958 int cur_idx;
959 struct cap_hdr *h;
960 int len;
961
962 sol = som + hdr_idx_first_pos(idx);
963 cur_idx = hdr_idx_first_idx(idx);
964
965 while (cur_idx) {
966 eol = sol + idx->v[cur_idx].len;
967
968 col = sol;
969 while (col < eol && *col != ':')
970 col++;
971
972 sov = col + 1;
973 while (sov < eol && http_is_lws[(unsigned char)*sov])
974 sov++;
975
976 for (h = cap_hdr; h; h = h->next) {
977 if ((h->namelen == col - sol) &&
978 (strncasecmp(sol, h->name, h->namelen) == 0)) {
979 if (cap[h->index] == NULL)
980 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200981 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100982
983 if (cap[h->index] == NULL) {
984 Alert("HTTP capture : out of memory.\n");
985 continue;
986 }
987
988 len = eol - sov;
989 if (len > h->len)
990 len = h->len;
991
992 memcpy(cap[h->index], sov, len);
993 cap[h->index][len]=0;
994 }
995 }
996 sol = eol + idx->v[cur_idx].cr + 1;
997 cur_idx = idx->v[cur_idx].next;
998 }
999}
1000
1001
Willy Tarreau42250582007-04-01 01:30:43 +02001002/* either we find an LF at <ptr> or we jump to <bad>.
1003 */
1004#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1005
1006/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1007 * otherwise to <http_msg_ood> with <state> set to <st>.
1008 */
1009#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1010 ptr++; \
1011 if (likely(ptr < end)) \
1012 goto good; \
1013 else { \
1014 state = (st); \
1015 goto http_msg_ood; \
1016 } \
1017 } while (0)
1018
1019
Willy Tarreaubaaee002006-06-26 02:48:02 +02001020/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001021 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1023 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1024 * will give undefined results.
1025 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1026 * and that msg->sol points to the beginning of the response.
1027 * If a complete line is found (which implies that at least one CR or LF is
1028 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1029 * returned indicating an incomplete line (which does not mean that parts have
1030 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1031 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1032 * upon next call.
1033 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001034 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1036 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001037 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001038 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001039const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1040 unsigned int state, const char *ptr, const char *end,
1041 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001042{
1043 __label__
1044 http_msg_rpver,
1045 http_msg_rpver_sp,
1046 http_msg_rpcode,
1047 http_msg_rpcode_sp,
1048 http_msg_rpreason,
1049 http_msg_rpline_eol,
1050 http_msg_ood, /* out of data */
1051 http_msg_invalid;
1052
1053 switch (state) {
1054 http_msg_rpver:
1055 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001056 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001057 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1058
1059 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001060 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1062 }
1063 goto http_msg_invalid;
1064
1065 http_msg_rpver_sp:
1066 case HTTP_MSG_RPVER_SP:
1067 if (likely(!HTTP_IS_LWS(*ptr))) {
1068 msg->sl.st.c = ptr - msg_buf;
1069 goto http_msg_rpcode;
1070 }
1071 if (likely(HTTP_IS_SPHT(*ptr)))
1072 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1073 /* so it's a CR/LF, this is invalid */
1074 goto http_msg_invalid;
1075
1076 http_msg_rpcode:
1077 case HTTP_MSG_RPCODE:
1078 if (likely(!HTTP_IS_LWS(*ptr)))
1079 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1080
1081 if (likely(HTTP_IS_SPHT(*ptr))) {
1082 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1084 }
1085
1086 /* so it's a CR/LF, so there is no reason phrase */
1087 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1088 http_msg_rsp_reason:
1089 /* FIXME: should we support HTTP responses without any reason phrase ? */
1090 msg->sl.st.r = ptr - msg_buf;
1091 msg->sl.st.r_l = 0;
1092 goto http_msg_rpline_eol;
1093
1094 http_msg_rpcode_sp:
1095 case HTTP_MSG_RPCODE_SP:
1096 if (likely(!HTTP_IS_LWS(*ptr))) {
1097 msg->sl.st.r = ptr - msg_buf;
1098 goto http_msg_rpreason;
1099 }
1100 if (likely(HTTP_IS_SPHT(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1102 /* so it's a CR/LF, so there is no reason phrase */
1103 goto http_msg_rsp_reason;
1104
1105 http_msg_rpreason:
1106 case HTTP_MSG_RPREASON:
1107 if (likely(!HTTP_IS_CRLF(*ptr)))
1108 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1109 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1110 http_msg_rpline_eol:
1111 /* We have seen the end of line. Note that we do not
1112 * necessarily have the \n yet, but at least we know that we
1113 * have EITHER \r OR \n, otherwise the response would not be
1114 * complete. We can then record the response length and return
1115 * to the caller which will be able to register it.
1116 */
1117 msg->sl.st.l = ptr - msg->sol;
1118 return ptr;
1119
1120#ifdef DEBUG_FULL
1121 default:
1122 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1123 exit(1);
1124#endif
1125 }
1126
1127 http_msg_ood:
1128 /* out of data */
1129 if (ret_state)
1130 *ret_state = state;
1131 if (ret_ptr)
1132 *ret_ptr = (char *)ptr;
1133 return NULL;
1134
1135 http_msg_invalid:
1136 /* invalid message */
1137 if (ret_state)
1138 *ret_state = HTTP_MSG_ERROR;
1139 return NULL;
1140}
1141
1142
1143/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 * This function parses a request line between <ptr> and <end>, starting with
1145 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1146 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1147 * will give undefined results.
1148 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1149 * and that msg->sol points to the beginning of the request.
1150 * If a complete line is found (which implies that at least one CR or LF is
1151 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1152 * returned indicating an incomplete line (which does not mean that parts have
1153 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1154 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1155 * upon next call.
1156 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001157 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1159 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001160 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001161 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001162const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1163 unsigned int state, const char *ptr, const char *end,
1164 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 __label__
1167 http_msg_rqmeth,
1168 http_msg_rqmeth_sp,
1169 http_msg_rquri,
1170 http_msg_rquri_sp,
1171 http_msg_rqver,
1172 http_msg_rqline_eol,
1173 http_msg_ood, /* out of data */
1174 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 switch (state) {
1177 http_msg_rqmeth:
1178 case HTTP_MSG_RQMETH:
1179 if (likely(HTTP_IS_TOKEN(*ptr)))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001183 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1185 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 if (likely(HTTP_IS_CRLF(*ptr))) {
1188 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001189 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 http_msg_req09_uri:
1191 msg->sl.rq.u = ptr - msg_buf;
1192 http_msg_req09_uri_e:
1193 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1194 http_msg_req09_ver:
1195 msg->sl.rq.v = ptr - msg_buf;
1196 msg->sl.rq.v_l = 0;
1197 goto http_msg_rqline_eol;
1198 }
1199 goto http_msg_invalid;
1200
1201 http_msg_rqmeth_sp:
1202 case HTTP_MSG_RQMETH_SP:
1203 if (likely(!HTTP_IS_LWS(*ptr))) {
1204 msg->sl.rq.u = ptr - msg_buf;
1205 goto http_msg_rquri;
1206 }
1207 if (likely(HTTP_IS_SPHT(*ptr)))
1208 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1209 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1210 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 http_msg_rquri:
1213 case HTTP_MSG_RQURI:
1214 if (likely(!HTTP_IS_LWS(*ptr)))
1215 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001216
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001217 if (likely(HTTP_IS_SPHT(*ptr))) {
1218 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001222 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1223 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001224
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 http_msg_rquri_sp:
1226 case HTTP_MSG_RQURI_SP:
1227 if (likely(!HTTP_IS_LWS(*ptr))) {
1228 msg->sl.rq.v = ptr - msg_buf;
1229 goto http_msg_rqver;
1230 }
1231 if (likely(HTTP_IS_SPHT(*ptr)))
1232 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1233 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1234 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 http_msg_rqver:
1237 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001238 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001240
1241 if (likely(HTTP_IS_CRLF(*ptr))) {
1242 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1243 http_msg_rqline_eol:
1244 /* We have seen the end of line. Note that we do not
1245 * necessarily have the \n yet, but at least we know that we
1246 * have EITHER \r OR \n, otherwise the request would not be
1247 * complete. We can then record the request length and return
1248 * to the caller which will be able to register it.
1249 */
1250 msg->sl.rq.l = ptr - msg->sol;
1251 return ptr;
1252 }
1253
1254 /* neither an HTTP_VER token nor a CRLF */
1255 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001256
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001257#ifdef DEBUG_FULL
1258 default:
1259 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1260 exit(1);
1261#endif
1262 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 http_msg_ood:
1265 /* out of data */
1266 if (ret_state)
1267 *ret_state = state;
1268 if (ret_ptr)
1269 *ret_ptr = (char *)ptr;
1270 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 http_msg_invalid:
1273 /* invalid message */
1274 if (ret_state)
1275 *ret_state = HTTP_MSG_ERROR;
1276 return NULL;
1277}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001278
1279
Willy Tarreau8973c702007-01-21 23:58:29 +01001280/*
1281 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 * when data are missing and recalled at the exact same location with no
1284 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001285 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1286 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1289{
1290 __label__
1291 http_msg_rqbefore,
1292 http_msg_rqbefore_cr,
1293 http_msg_rqmeth,
1294 http_msg_rqline_end,
1295 http_msg_hdr_first,
1296 http_msg_hdr_name,
1297 http_msg_hdr_l1_sp,
1298 http_msg_hdr_l1_lf,
1299 http_msg_hdr_l1_lws,
1300 http_msg_hdr_val,
1301 http_msg_hdr_l2_lf,
1302 http_msg_hdr_l2_lws,
1303 http_msg_complete_header,
1304 http_msg_last_lf,
1305 http_msg_ood, /* out of data */
1306 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001307
Willy Tarreaue69eada2008-01-27 00:34:10 +01001308 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001311 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 ptr = buf->lr;
1313 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 if (unlikely(ptr >= end))
1316 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 /*
1320 * First, states that are specific to the response only.
1321 * We check them first so that request and headers are
1322 * closer to each other (accessed more often).
1323 */
1324 http_msg_rpbefore:
1325 case HTTP_MSG_RPBEFORE:
1326 if (likely(HTTP_IS_TOKEN(*ptr))) {
1327 if (likely(ptr == buf->data)) {
1328 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001329 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 } else {
1331#if PARSE_PRESERVE_EMPTY_LINES
1332 /* only skip empty leading lines, don't remove them */
1333 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001334 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001335#else
1336 /* Remove empty leading lines, as recommended by
1337 * RFC2616. This takes a lot of time because we
1338 * must move all the buffer backwards, but this
1339 * is rarely needed. The method above will be
1340 * cleaner when we'll be able to start sending
1341 * the request from any place in the buffer.
1342 */
1343 buf->lr = ptr;
1344 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001345 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 msg->sol = buf->data;
1347 ptr = buf->data;
1348 end = buf->r;
1349#endif
1350 }
1351 hdr_idx_init(idx);
1352 state = HTTP_MSG_RPVER;
1353 goto http_msg_rpver;
1354 }
1355
1356 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1357 goto http_msg_invalid;
1358
1359 if (unlikely(*ptr == '\n'))
1360 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1361 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1362 /* stop here */
1363
1364 http_msg_rpbefore_cr:
1365 case HTTP_MSG_RPBEFORE_CR:
1366 EXPECT_LF_HERE(ptr, http_msg_invalid);
1367 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1368 /* stop here */
1369
1370 http_msg_rpver:
1371 case HTTP_MSG_RPVER:
1372 case HTTP_MSG_RPVER_SP:
1373 case HTTP_MSG_RPCODE:
1374 case HTTP_MSG_RPCODE_SP:
1375 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001376 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001377 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 if (unlikely(!ptr))
1379 return;
1380
1381 /* we have a full response and we know that we have either a CR
1382 * or an LF at <ptr>.
1383 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001384 //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 +01001385 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1386
1387 msg->sol = ptr;
1388 if (likely(*ptr == '\r'))
1389 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1390 goto http_msg_rpline_end;
1391
1392 http_msg_rpline_end:
1393 case HTTP_MSG_RPLINE_END:
1394 /* msg->sol must point to the first of CR or LF. */
1395 EXPECT_LF_HERE(ptr, http_msg_invalid);
1396 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1397 /* stop here */
1398
1399 /*
1400 * Second, states that are specific to the request only
1401 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 http_msg_rqbefore:
1403 case HTTP_MSG_RQBEFORE:
1404 if (likely(HTTP_IS_TOKEN(*ptr))) {
1405 if (likely(ptr == buf->data)) {
1406 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 } else {
1409#if PARSE_PRESERVE_EMPTY_LINES
1410 /* only skip empty leading lines, don't remove them */
1411 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001412 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 /* Remove empty leading lines, as recommended by
1415 * RFC2616. This takes a lot of time because we
1416 * must move all the buffer backwards, but this
1417 * is rarely needed. The method above will be
1418 * cleaner when we'll be able to start sending
1419 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001420 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 buf->lr = ptr;
1422 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001423 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->sol = buf->data;
1425 ptr = buf->data;
1426 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001427#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001429 /* we will need this when keep-alive will be supported
1430 hdr_idx_init(idx);
1431 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 state = HTTP_MSG_RQMETH;
1433 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1437 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (unlikely(*ptr == '\n'))
1440 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 http_msg_rqbefore_cr:
1445 case HTTP_MSG_RQBEFORE_CR:
1446 EXPECT_LF_HERE(ptr, http_msg_invalid);
1447 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 http_msg_rqmeth:
1451 case HTTP_MSG_RQMETH:
1452 case HTTP_MSG_RQMETH_SP:
1453 case HTTP_MSG_RQURI:
1454 case HTTP_MSG_RQURI_SP:
1455 case HTTP_MSG_RQVER:
1456 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (unlikely(!ptr))
1459 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 /* we have a full request and we know that we have either a CR
1462 * or an LF at <ptr>.
1463 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001464 //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 +01001465 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 msg->sol = ptr;
1468 if (likely(*ptr == '\r'))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 http_msg_rqline_end:
1473 case HTTP_MSG_RQLINE_END:
1474 /* check for HTTP/0.9 request : no version information available.
1475 * msg->sol must point to the first of CR or LF.
1476 */
1477 if (unlikely(msg->sl.rq.v_l == 0))
1478 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 EXPECT_LF_HERE(ptr, http_msg_invalid);
1481 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 /*
1485 * Common states below
1486 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 http_msg_hdr_first:
1488 case HTTP_MSG_HDR_FIRST:
1489 msg->sol = ptr;
1490 if (likely(!HTTP_IS_CRLF(*ptr))) {
1491 goto http_msg_hdr_name;
1492 }
1493
1494 if (likely(*ptr == '\r'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1496 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 http_msg_hdr_name:
1499 case HTTP_MSG_HDR_NAME:
1500 /* assumes msg->sol points to the first char */
1501 if (likely(HTTP_IS_TOKEN(*ptr)))
1502 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 if (likely(*ptr == ':')) {
1505 msg->col = ptr - buf->data;
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 http_msg_hdr_l1_sp:
1512 case HTTP_MSG_HDR_L1_SP:
1513 /* assumes msg->sol points to the first char and msg->col to the colon */
1514 if (likely(HTTP_IS_SPHT(*ptr)))
1515 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 /* header value can be basically anything except CR/LF */
1518 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (likely(!HTTP_IS_CRLF(*ptr))) {
1521 goto http_msg_hdr_val;
1522 }
1523
1524 if (likely(*ptr == '\r'))
1525 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1526 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 http_msg_hdr_l1_lf:
1529 case HTTP_MSG_HDR_L1_LF:
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 http_msg_hdr_l1_lws:
1534 case HTTP_MSG_HDR_L1_LWS:
1535 if (likely(HTTP_IS_SPHT(*ptr))) {
1536 /* replace HT,CR,LF with spaces */
1537 for (; buf->data+msg->sov < ptr; msg->sov++)
1538 buf->data[msg->sov] = ' ';
1539 goto http_msg_hdr_l1_sp;
1540 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001541 /* we had a header consisting only in spaces ! */
1542 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 goto http_msg_complete_header;
1544
1545 http_msg_hdr_val:
1546 case HTTP_MSG_HDR_VAL:
1547 /* assumes msg->sol points to the first char, msg->col to the
1548 * colon, and msg->sov points to the first character of the
1549 * value.
1550 */
1551 if (likely(!HTTP_IS_CRLF(*ptr)))
1552 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 msg->eol = ptr;
1555 /* Note: we could also copy eol into ->eoh so that we have the
1556 * real header end in case it ends with lots of LWS, but is this
1557 * really needed ?
1558 */
1559 if (likely(*ptr == '\r'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1561 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 http_msg_hdr_l2_lf:
1564 case HTTP_MSG_HDR_L2_LF:
1565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 http_msg_hdr_l2_lws:
1569 case HTTP_MSG_HDR_L2_LWS:
1570 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1571 /* LWS: replace HT,CR,LF with spaces */
1572 for (; msg->eol < ptr; msg->eol++)
1573 *msg->eol = ' ';
1574 goto http_msg_hdr_val;
1575 }
1576 http_msg_complete_header:
1577 /*
1578 * It was a new header, so the last one is finished.
1579 * Assumes msg->sol points to the first char, msg->col to the
1580 * colon, msg->sov points to the first character of the value
1581 * and msg->eol to the first CR or LF so we know how the line
1582 * ends. We insert last header into the index.
1583 */
1584 /*
1585 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1586 write(2, msg->sol, msg->eol-msg->sol);
1587 fprintf(stderr,"\n");
1588 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1591 idx, idx->tail) < 0))
1592 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 msg->sol = ptr;
1595 if (likely(!HTTP_IS_CRLF(*ptr))) {
1596 goto http_msg_hdr_name;
1597 }
1598
1599 if (likely(*ptr == '\r'))
1600 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1601 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 http_msg_last_lf:
1604 case HTTP_MSG_LAST_LF:
1605 /* Assumes msg->sol points to the first of either CR or LF */
1606 EXPECT_LF_HERE(ptr, http_msg_invalid);
1607 ptr++;
1608 buf->lr = ptr;
1609 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001610 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 return;
1612#ifdef DEBUG_FULL
1613 default:
1614 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1615 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001616#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 }
1618 http_msg_ood:
1619 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001620 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 buf->lr = ptr;
1622 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 http_msg_invalid:
1625 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001626 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 return;
1628}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001629
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001630/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001631 * It normally returns zero, but may return 1 if it absolutely needs to be
1632 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001633 * t->req->analysers. It might make sense to explode it into several other
1634 * functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001636int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 struct buffer *req = t->req;
1639 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001640
Willy Tarreau2df28e82008-08-17 15:20:19 +02001641 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02001642 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001643 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001644 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1645 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02001646 req->rex, rep->wex, req->flags, rep->flags, req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreaudafde432008-08-17 01:00:46 +02001648 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02001649 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001650 struct tcp_rule *rule;
1651 int partial;
1652
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001653 /* We will abort if we encounter a read error. In theory, we
1654 * should not abort if we get a close, it might be valid,
1655 * although very unlikely. FIXME: we'll abort for now, this
1656 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001657 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001658 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001659 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001660 t->fe->failed_req++;
1661 if (!(t->flags & SN_ERR_MASK))
1662 t->flags |= SN_ERR_CLICL;
1663 if (!(t->flags & SN_FINST_MASK))
1664 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001665 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001666 }
1667
1668 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001669 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001670 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001671 t->fe->failed_req++;
1672 if (!(t->flags & SN_ERR_MASK))
1673 t->flags |= SN_ERR_CLITO;
1674 if (!(t->flags & SN_FINST_MASK))
1675 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001676 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001677 }
1678
1679 /* We don't know whether we have enough data, so must proceed
1680 * this way :
1681 * - iterate through all rules in their declaration order
1682 * - if one rule returns MISS, it means the inspect delay is
1683 * not over yet, then return immediately, otherwise consider
1684 * it as a non-match.
1685 * - if one rule returns OK, then return OK
1686 * - if one rule returns KO, then return KO
1687 */
1688
Willy Tarreauffab5b42008-08-17 18:03:28 +02001689 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001690 partial = 0;
1691 else
1692 partial = ACL_PARTIAL;
1693
1694 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1695 int ret = ACL_PAT_PASS;
1696
1697 if (rule->cond) {
1698 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1699 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001700 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001701 if (!tick_isset(req->analyse_exp))
1702 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001703 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001704 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001705
Willy Tarreaub6866442008-07-14 23:54:42 +02001706 ret = acl_pass(ret);
1707 if (rule->cond->pol == ACL_COND_UNLESS)
1708 ret = !ret;
1709 }
1710
1711 if (ret) {
1712 /* we have a matching rule. */
1713 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02001714 buffer_shutr(req);
1715 buffer_shutw(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001716 fd_delete(t->cli_fd);
1717 t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001718 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001719 t->fe->failed_req++;
1720 if (!(t->flags & SN_ERR_MASK))
1721 t->flags |= SN_ERR_PRXCOND;
1722 if (!(t->flags & SN_FINST_MASK))
1723 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001724 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001725 }
1726 /* otherwise accept */
1727 break;
1728 }
1729 }
1730
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001731 /* if we get there, it means we have no rule which matches, or
1732 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001733 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001734 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001735 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001736 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001737
Willy Tarreau2df28e82008-08-17 15:20:19 +02001738 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 /*
1740 * Now parse the partial (or complete) lines.
1741 * We will check the request syntax, and also join multi-line
1742 * headers. An index of all the lines will be elaborated while
1743 * parsing.
1744 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001745 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001748 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 * req->data + req->eoh = end of processed headers / start of current one
1750 * req->data + req->eol = end of current header or line (LF or CRLF)
1751 * req->lr = first non-visited byte
1752 * req->r = end of data
1753 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001756 struct http_txn *txn = &t->txn;
1757 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001761 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* 1: we might have to print this header in debug mode */
1764 if (unlikely((global.mode & MODE_DEBUG) &&
1765 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001766 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001768
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001769 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 eol = sol + msg->sl.rq.l;
1771 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001772
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001773 sol += hdr_idx_first_pos(&txn->hdr_idx);
1774 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001777 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001779 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1780 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 }
1782 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001783
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
1785 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001787 * If not so, we check the FD and buffer states before leaving.
1788 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1790 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791 *
1792 */
1793
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001794 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 /*
1796 * First, let's catch bad requests.
1797 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001798 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001800
1801 /* 1: Since we are in header mode, if there's no space
1802 * left for headers, we won't be able to free more
1803 * later, so the session will never terminate. We
1804 * must terminate it now.
1805 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001806 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 /* FIXME: check if URI is set and return Status
1808 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001809 */
Willy Tarreau06619262006-12-17 08:37:22 +01001810 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001811 }
1812
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001813 /* 2: have we encountered a close ? */
1814 else if (req->flags & BF_READ_NULL) {
1815 txn->status = 400;
1816 client_retnclose(t, error_message(t, HTTP_ERR_400));
1817 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001818 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001819 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001820
Willy Tarreau58f10d72006-12-04 02:26:12 +01001821 if (!(t->flags & SN_ERR_MASK))
1822 t->flags |= SN_ERR_CLICL;
1823 if (!(t->flags & SN_FINST_MASK))
1824 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001825 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826 }
1827
1828 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001829 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001830 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001831 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001832 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001833 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001834 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001835 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836 if (!(t->flags & SN_ERR_MASK))
1837 t->flags |= SN_ERR_CLITO;
1838 if (!(t->flags & SN_FINST_MASK))
1839 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001840 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001841 }
1842
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001843 /* 4: have we encountered a read error or did we have to shutdown ? */
Willy Tarreauba392ce2008-08-16 21:13:23 +02001844 else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001845 /* we cannot return any message on error */
1846 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001847 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001848 t->fe->failed_req++;
1849 if (!(t->flags & SN_ERR_MASK))
1850 t->flags |= SN_ERR_CLICL;
1851 if (!(t->flags & SN_FINST_MASK))
1852 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001853 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001854 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001855
1856 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001857 if (!tick_isset(req->analyse_exp))
1858 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001859
1860 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001861 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001862 }
1863
1864
1865 /****************************************************************
1866 * More interesting part now : we know that we have a complete *
1867 * request which at least looks like HTTP. We have an indicator *
1868 * of each header's length, so we can parse them quickly. *
1869 ****************************************************************/
1870
Willy Tarreau2df28e82008-08-17 15:20:19 +02001871 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001872 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001873
Willy Tarreau9cdde232007-05-02 20:58:19 +02001874 /* ensure we keep this pointer to the beginning of the message */
1875 msg->sol = req->data + msg->som;
1876
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 /*
1878 * 1: identify the method
1879 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001880 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001881
1882 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001884 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001885 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001886 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 if (unlikely((t->fe->monitor_uri_len != 0) &&
1888 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1889 !memcmp(&req->data[msg->sl.rq.u],
1890 t->fe->monitor_uri,
1891 t->fe->monitor_uri_len))) {
1892 /*
1893 * We have found the monitor URI
1894 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001895 struct acl_cond *cond;
1896 cur_proxy = t->fe;
1897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001899
1900 /* Check if we want to fail this monitor request or not */
1901 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1902 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001903
1904 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001905 if (cond->pol == ACL_COND_UNLESS)
1906 ret = !ret;
1907
1908 if (ret) {
1909 /* we fail this request, let's return 503 service unavail */
1910 txn->status = 503;
1911 client_retnclose(t, error_message(t, HTTP_ERR_503));
1912 goto return_prx_cond;
1913 }
1914 }
1915
1916 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001917 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 client_retnclose(t, &http_200_chunk);
1919 goto return_prx_cond;
1920 }
1921
1922 /*
1923 * 3: Maybe we have to copy the original REQURI for the logs ?
1924 * Note: we cannot log anymore if the request has been
1925 * classified as invalid.
1926 */
1927 if (unlikely(t->logs.logwait & LW_REQ)) {
1928 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001929 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 int urilen = msg->sl.rq.l;
1931
1932 if (urilen >= REQURI_LEN)
1933 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001934 memcpy(txn->uri, &req->data[msg->som], urilen);
1935 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936
1937 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001938 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 } else {
1940 Alert("HTTP logging : out of memory.\n");
1941 }
1942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001943
Willy Tarreau06619262006-12-17 08:37:22 +01001944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1946 if (unlikely(msg->sl.rq.v_l == 0)) {
1947 int delta;
1948 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001949 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001950 cur_end = msg->sol + msg->sl.rq.l;
1951 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 if (msg->sl.rq.u_l == 0) {
1954 /* if no URI was set, add "/" */
1955 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1956 cur_end += delta;
1957 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001958 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 /* add HTTP version */
1960 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1961 msg->eoh += delta;
1962 cur_end += delta;
1963 cur_end = (char *)http_parse_reqline(msg, req->data,
1964 HTTP_MSG_RQMETH,
1965 msg->sol, cur_end + 1,
1966 NULL, NULL);
1967 if (unlikely(!cur_end))
1968 goto return_bad_req;
1969
1970 /* we have a full HTTP/1.0 request now and we know that
1971 * we have either a CR or an LF at <ptr>.
1972 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001973 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001974 }
1975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001976
1977 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001978 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001979 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001980 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981
1982 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001984 * As opposed to version 1.2, now they will be evaluated in the
1985 * filters order and not in the header order. This means that
1986 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001987 *
1988 * We can now check whether we want to switch to another
1989 * backend, in which case we will re-check the backend's
1990 * filters and various options. In order to support 3-level
1991 * switching, here's how we should proceed :
1992 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001993 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001994 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001995 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001996 * There cannot be any switch from there, so ->be cannot be
1997 * changed anymore.
1998 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001999 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002000 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002001 * The response path will be able to apply either ->be, or
2002 * ->be then ->fe filters in order to match the reverse of
2003 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002004 */
2005
Willy Tarreau06619262006-12-17 08:37:22 +01002006 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002007 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002008 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002009 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002010 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002011
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002012 /* first check whether we have some ACLs set to redirect this request */
2013 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2014 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002015
2016 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002017 if (rule->cond->pol == ACL_COND_UNLESS)
2018 ret = !ret;
2019
2020 if (ret) {
2021 struct chunk rdr = { trash, 0 };
2022 const char *msg_fmt;
2023
2024 /* build redirect message */
2025 switch(rule->code) {
2026 case 303:
2027 rdr.len = strlen(HTTP_303);
2028 msg_fmt = HTTP_303;
2029 break;
2030 case 301:
2031 rdr.len = strlen(HTTP_301);
2032 msg_fmt = HTTP_301;
2033 break;
2034 case 302:
2035 default:
2036 rdr.len = strlen(HTTP_302);
2037 msg_fmt = HTTP_302;
2038 break;
2039 }
2040
2041 if (unlikely(rdr.len > sizeof(trash)))
2042 goto return_bad_req;
2043 memcpy(rdr.str, msg_fmt, rdr.len);
2044
2045 switch(rule->type) {
2046 case REDIRECT_TYPE_PREFIX: {
2047 const char *path;
2048 int pathlen;
2049
2050 path = http_get_path(txn);
2051 /* build message using path */
2052 if (path) {
2053 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2054 } else {
2055 path = "/";
2056 pathlen = 1;
2057 }
2058
2059 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2060 goto return_bad_req;
2061
2062 /* add prefix */
2063 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2064 rdr.len += rule->rdr_len;
2065
2066 /* add path */
2067 memcpy(rdr.str + rdr.len, path, pathlen);
2068 rdr.len += pathlen;
2069 break;
2070 }
2071 case REDIRECT_TYPE_LOCATION:
2072 default:
2073 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2074 goto return_bad_req;
2075
2076 /* add location */
2077 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2078 rdr.len += rule->rdr_len;
2079 break;
2080 }
2081
2082 /* add end of headers */
2083 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2084 rdr.len += 4;
2085
2086 txn->status = rule->code;
2087 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002088 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002089 client_retnclose(t, &rdr);
2090 goto return_prx_cond;
2091 }
2092 }
2093
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002094 /* first check whether we have some ACLs set to block this request */
2095 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002096 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002097
2098 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002099 if (cond->pol == ACL_COND_UNLESS)
2100 ret = !ret;
2101
2102 if (ret) {
2103 txn->status = 403;
2104 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002105 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002106 client_retnclose(t, error_message(t, HTTP_ERR_403));
2107 goto return_prx_cond;
2108 }
2109 }
2110
Willy Tarreau06619262006-12-17 08:37:22 +01002111 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002112 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002113 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2114 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002115 }
2116
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002117 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2118 /* to ensure correct connection accounting on
2119 * the backend, we count the connection for the
2120 * one managing the queue.
2121 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002122 t->be->beconn++;
2123 if (t->be->beconn > t->be->beconn_max)
2124 t->be->beconn_max = t->be->beconn;
2125 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002126 t->flags |= SN_BE_ASSIGNED;
2127 }
2128
Willy Tarreau06619262006-12-17 08:37:22 +01002129 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002130 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002131 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002132 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002133 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002134 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002135 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002136 goto return_prx_cond;
2137 }
2138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002139 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002140 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002141 !(t->flags & SN_CONN_CLOSED)) {
2142 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002143 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002144 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002145
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002146 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002147 old_idx = 0;
2148
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002149 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2150 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002151 cur_ptr = cur_next;
2152 cur_end = cur_ptr + cur_hdr->len;
2153 cur_next = cur_end + cur_hdr->cr + 1;
2154
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002155 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2156 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002157 /* 3 possibilities :
2158 * - we have already set Connection: close,
2159 * so we remove this line.
2160 * - we have not yet set Connection: close,
2161 * but this line indicates close. We leave
2162 * it untouched and set the flag.
2163 * - we have not yet set Connection: close,
2164 * and this line indicates non-close. We
2165 * replace it.
2166 */
2167 if (t->flags & SN_CONN_CLOSED) {
2168 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002169 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002170 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002171 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2172 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002173 cur_hdr->len = 0;
2174 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002175 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2176 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2177 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002178 cur_next += delta;
2179 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002180 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002181 }
2182 t->flags |= SN_CONN_CLOSED;
2183 }
2184 }
2185 old_idx = cur_idx;
2186 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002187 }
2188 /* add request headers from the rule sets in the same order */
2189 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2190 if (unlikely(http_header_add_tail(req,
2191 &txn->req,
2192 &txn->hdr_idx,
2193 rule_set->req_add[cur_idx])) < 0)
2194 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002195 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002197 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002198 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002199 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002200 /* we have to check the URI and auth for this request.
2201 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002202 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002203 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002204 if (stats_check_uri_auth(t, rule_set))
2205 return 1;
2206 }
2207
Willy Tarreau55ea7572007-06-17 19:56:27 +02002208 /* now check whether we have some switching rules for this request */
2209 if (!(t->flags & SN_BE_ASSIGNED)) {
2210 struct switching_rule *rule;
2211
2212 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2213 int ret;
2214
2215 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002216
2217 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002218 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002219 ret = !ret;
2220
2221 if (ret) {
2222 t->be = rule->be.backend;
2223 t->be->beconn++;
2224 if (t->be->beconn > t->be->beconn_max)
2225 t->be->beconn_max = t->be->beconn;
2226 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002227
2228 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002229 t->rep->rto = t->req->wto = t->be->timeout.server;
2230 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002231 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002232 t->flags |= SN_BE_ASSIGNED;
2233 break;
2234 }
2235 }
2236 }
2237
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002238 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2239 /* No backend was set, but there was a default
2240 * backend set in the frontend, so we use it and
2241 * loop again.
2242 */
2243 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002244 t->be->beconn++;
2245 if (t->be->beconn > t->be->beconn_max)
2246 t->be->beconn_max = t->be->beconn;
2247 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002248
2249 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002250 t->rep->rto = t->req->wto = t->be->timeout.server;
2251 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002252 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002253 t->flags |= SN_BE_ASSIGNED;
2254 }
2255 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002256
Willy Tarreau58f10d72006-12-04 02:26:12 +01002257
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002258 if (!(t->flags & SN_BE_ASSIGNED)) {
2259 /* To ensure correct connection accounting on
2260 * the backend, we count the connection for the
2261 * one managing the queue.
2262 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002263 t->be->beconn++;
2264 if (t->be->beconn > t->be->beconn_max)
2265 t->be->beconn_max = t->be->beconn;
2266 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002267 t->flags |= SN_BE_ASSIGNED;
2268 }
2269
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002270 /*
2271 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002272 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002273 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002274 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002275 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002276
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002277 /*
2278 * If HTTP PROXY is set we simply get remote server address
2279 * parsing incoming request.
2280 */
2281 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2282 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284
Willy Tarreau2a324282006-12-05 00:05:46 +01002285 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002286 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002287 * so let's do the same now.
2288 */
2289
2290 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002291 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002292 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002293 }
2294
2295
2296 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002297 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002298 * Note that doing so might move headers in the request, but
2299 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002300 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002301 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002302 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2303 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002304 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305
Willy Tarreau58f10d72006-12-04 02:26:12 +01002306
Willy Tarreau2a324282006-12-05 00:05:46 +01002307 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002308 * 9: add X-Forwarded-For if either the frontend or the backend
2309 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002310 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002311 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002312 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002313 /* Add an X-Forwarded-For header unless the source IP is
2314 * in the 'except' network range.
2315 */
2316 if ((!t->fe->except_mask.s_addr ||
2317 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2318 != t->fe->except_net.s_addr) &&
2319 (!t->be->except_mask.s_addr ||
2320 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2321 != t->be->except_net.s_addr)) {
2322 int len;
2323 unsigned char *pn;
2324 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002325
Ross Westaf72a1d2008-08-03 10:51:45 +02002326 /* Note: we rely on the backend to get the header name to be used for
2327 * x-forwarded-for, because the header is really meant for the backends.
2328 * However, if the backend did not specify any option, we have to rely
2329 * on the frontend's header name.
2330 */
2331 if (t->be->fwdfor_hdr_len) {
2332 len = t->be->fwdfor_hdr_len;
2333 memcpy(trash, t->be->fwdfor_hdr_name, len);
2334 } else {
2335 len = t->fe->fwdfor_hdr_len;
2336 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2337 }
2338 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002339
Ross Westaf72a1d2008-08-03 10:51:45 +02002340 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002341 &txn->hdr_idx, trash, len)) < 0)
2342 goto return_bad_req;
2343 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002344 }
2345 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002346 /* FIXME: for the sake of completeness, we should also support
2347 * 'except' here, although it is mostly useless in this case.
2348 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002349 int len;
2350 char pn[INET6_ADDRSTRLEN];
2351 inet_ntop(AF_INET6,
2352 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2353 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002354
2355 /* Note: we rely on the backend to get the header name to be used for
2356 * x-forwarded-for, because the header is really meant for the backends.
2357 * However, if the backend did not specify any option, we have to rely
2358 * on the frontend's header name.
2359 */
2360 if (t->be->fwdfor_hdr_len) {
2361 len = t->be->fwdfor_hdr_len;
2362 memcpy(trash, t->be->fwdfor_hdr_name, len);
2363 } else {
2364 len = t->fe->fwdfor_hdr_len;
2365 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2366 }
2367 len += sprintf(trash + len, ": %s", pn);
2368
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002369 if (unlikely(http_header_add_tail2(req, &txn->req,
2370 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002371 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002372 }
2373 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002374
Willy Tarreau2a324282006-12-05 00:05:46 +01002375 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002376 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002377 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002378 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002379 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002380 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002381 if ((unlikely(msg->sl.rq.v_l != 8) ||
2382 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2383 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002384 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002385 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002386 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002387 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002388 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2389 * If not assigned, perhaps we are balancing on url_param, but this is a
2390 * POST; and the parameters are in the body, maybe scan there to find our server.
2391 * (unless headers overflowed the buffer?)
2392 */
2393 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2394 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002395 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002396 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2397 /* are there enough bytes here? total == l || r || rlim ?
2398 * len is unsigned, but eoh is int,
2399 * how many bytes of body have we received?
2400 * eoh is the first empty line of the header
2401 */
2402 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002403 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 +02002404
2405 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2406 * We can't assume responsibility for the server's decision,
2407 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2408 * We also can't change our mind later, about which server to choose, so round robin.
2409 */
2410 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2411 struct hdr_ctx ctx;
2412 ctx.idx = 0;
2413 /* Expect is allowed in 1.1, look for it */
2414 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2415 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002416 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002417 /* We can't reliablly stall and wait for data, because of
2418 * .NET clients that don't conform to rfc2616; so, no need for
2419 * the next block to check length expectations.
2420 * We could send 100 status back to the client, but then we need to
2421 * re-write headers, and send the message. And this isn't the right
2422 * place for that action.
2423 * TODO: support Expect elsewhere and delete this block.
2424 */
2425 goto end_check_maybe_wait_for_body;
2426 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002427
2428 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002429 /* nothing to do, we got enough */
2430 } else {
2431 /* limit implies we are supposed to need this many bytes
2432 * to find the parameter. Let's see how many bytes we can wait for.
2433 */
2434 long long hint = len;
2435 struct hdr_ctx ctx;
2436 ctx.idx = 0;
2437 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002438 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002439 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002440 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002441 ctx.idx = 0;
2442 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2443 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002444 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002445 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002446 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002447 hint = 0; /* parse failure, untrusted client */
2448 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002449 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002450 msg->hdr_content_len = hint;
2451 else
2452 hint = 0; /* bad client, sent negative length */
2453 }
2454 }
2455 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002456 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002457 hint = t->be->url_param_post_limit;
2458 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002459 if (len < hint)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002460 req->analysers |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002461 /* else... There are no body bytes to wait for */
2462 }
2463 }
2464 }
2465 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466
Willy Tarreau2a324282006-12-05 00:05:46 +01002467 /*************************************************************
2468 * OK, that's finished for the headers. We have done what we *
2469 * could. Let's switch to the DATA state. *
2470 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002471
Willy Tarreaue393fe22008-08-16 22:18:07 +02002472 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002473 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002474
Willy Tarreau1fa31262007-12-03 00:36:16 +01002475 /* When a connection is tarpitted, we use the tarpit timeout,
2476 * which may be the same as the connect timeout if unspecified.
2477 * If unset, then set it to zero because we really want it to
2478 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002479 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002480 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002481 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002482 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002483 /* flush the request so that we can drop the connection early
2484 * if the client closes first.
2485 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02002486 req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2487 if (!req->wex)
2488 req->wex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002489 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002491 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002492 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002493
2494 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002495 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002496 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002497 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002498 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002499 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002500 return_prx_cond:
2501 if (!(t->flags & SN_ERR_MASK))
2502 t->flags |= SN_ERR_PRXCOND;
2503 if (!(t->flags & SN_FINST_MASK))
2504 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002505 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002506 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002507 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002508 }
2509
Willy Tarreau2df28e82008-08-17 15:20:19 +02002510 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002511 /* We have to parse the HTTP request body to find any required data.
2512 * "balance url_param check_post" should have been the only way to get
2513 * into this. We were brought here after HTTP header analysis, so all
2514 * related structures are ready.
2515 */
2516 struct http_msg *msg = &t->txn.req;
2517 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2518 long long limit = t->be->url_param_post_limit;
2519 struct hdr_ctx ctx;
2520
2521 ctx.idx = 0;
2522
2523 /* now if we have a length, we'll take the hint */
2524 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2525 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2526 unsigned int chunk = 0;
2527 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2528 char c = msg->sol[body];
2529 if (ishex(c)) {
2530 unsigned int hex = toupper(c) - '0';
2531 if (hex > 9)
2532 hex -= 'A' - '9' - 1;
2533 chunk = (chunk << 4) | hex;
2534 } else
2535 break;
2536 body++;
2537 }
2538 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002539 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002540
2541 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002542 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002543
2544 body += 2; // skip CRLF
2545
2546 /* if we support more then one chunk here, we have to do it again when assigning server
2547 * 1. how much entity data do we have? new var
2548 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2549 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2550 */
2551
2552 if (chunk < limit)
2553 limit = chunk; /* only reading one chunk */
2554 } else {
2555 if (msg->hdr_content_len < limit)
2556 limit = msg->hdr_content_len;
2557 }
2558
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002559 http_body_end:
2560 /* we leave once we know we have nothing left to do. This means that we have
2561 * enough bytes, or that we know we'll not get any more (buffer full, read
2562 * buffer closed).
2563 */
2564 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreauc52164a2008-08-17 19:17:57 +02002565 req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT) ||
2566 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002567 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002568 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002569 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002570 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002571 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002572 else {
2573 /* Not enough data. We'll re-use the http-request
2574 * timeout here. Ideally, we should set the timeout
2575 * relative to the accept() date. We just set the
2576 * request timeout once at the beginning of the
2577 * request.
2578 */
2579 if (!tick_isset(req->analyse_exp))
2580 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2581 return 0;
2582 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002583 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002584
Willy Tarreau2df28e82008-08-17 15:20:19 +02002585 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2586 * probably reduce one day's debugging session.
2587 */
2588#ifdef DEBUG_DEV
2589 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY)) {
2590 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2591 __FILE__, __LINE__, req->analysers);
2592 ABORT_NOW();
2593 }
2594#endif
2595 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY;
2596
Willy Tarreaudafde432008-08-17 01:00:46 +02002597 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002598 if (unlikely(req->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02002599 goto update_state;
2600 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002601}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002602
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002604 * It normally returns zero, but may return 1 if it absolutely needs to be
2605 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002606 * t->rep->analysers. It might make sense to explode it into several other
2607 * functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002608 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002609int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002610{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002611 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002612 struct buffer *req = t->req;
2613 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002614
Willy Tarreau2df28e82008-08-17 15:20:19 +02002615 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002616 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002617 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02002618 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
2619 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02002620 req->rex, rep->wex, req->flags, rep->flags, rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002621
Willy Tarreaudafde432008-08-17 01:00:46 +02002622 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02002623 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002624 /*
2625 * Now parse the partial (or complete) lines.
2626 * We will check the response syntax, and also join multi-line
2627 * headers. An index of all the lines will be elaborated while
2628 * parsing.
2629 *
2630 * For the parsing, we use a 28 states FSM.
2631 *
2632 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002633 * rep->data + rep->som = beginning of response
2634 * rep->data + rep->eoh = end of processed headers / start of current one
2635 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002636 * rep->lr = first non-visited byte
2637 * rep->r = end of data
2638 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002639
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002640 int cur_idx;
2641 struct http_msg *msg = &txn->rsp;
2642 struct proxy *cur_proxy;
2643
2644 if (likely(rep->lr < rep->r))
2645 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2646
2647 /* 1: we might have to print this header in debug mode */
2648 if (unlikely((global.mode & MODE_DEBUG) &&
2649 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2650 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2651 char *eol, *sol;
2652
2653 sol = rep->data + msg->som;
2654 eol = sol + msg->sl.rq.l;
2655 debug_hdr("srvrep", t, sol, eol);
2656
2657 sol += hdr_idx_first_pos(&txn->hdr_idx);
2658 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2659
2660 while (cur_idx) {
2661 eol = sol + txn->hdr_idx.v[cur_idx].len;
2662 debug_hdr("srvhdr", t, sol, eol);
2663 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2664 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002665 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002666 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002667
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002668 /*
2669 * Now we quickly check if we have found a full valid response.
2670 * If not so, we check the FD and buffer states before leaving.
2671 * A full response is indicated by the fact that we have seen
2672 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2673 * responses are checked first.
2674 *
2675 * Depending on whether the client is still there or not, we
2676 * may send an error response back or not. Note that normally
2677 * we should only check for HTTP status there, and check I/O
2678 * errors somewhere else.
2679 */
2680
2681 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002682 /* Invalid response */
2683 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2684 hdr_response_bad:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002685 buffer_shutr(rep);
2686 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002687 fd_delete(t->srv_fd);
2688 if (t->srv) {
2689 t->srv->cur_sess--;
2690 t->srv->failed_resp++;
2691 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002692 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002693 t->be->failed_resp++;
2694 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002695 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002696 txn->status = 502;
2697 client_return(t, error_message(t, HTTP_ERR_502));
2698 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002699 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002700 if (!(t->flags & SN_FINST_MASK))
2701 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002702
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002703 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2704 process_srv_queue(t->srv);
2705
Willy Tarreaudafde432008-08-17 01:00:46 +02002706 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002707 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002708 /* write error to client, read error or close from server */
2709 if (req->flags & BF_WRITE_ERROR ||
Willy Tarreauba392ce2008-08-16 21:13:23 +02002710 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) {
2711 buffer_shutr(rep);
2712 buffer_shutw(req);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002713 fd_delete(t->srv_fd);
2714 if (t->srv) {
2715 t->srv->cur_sess--;
2716 t->srv->failed_resp++;
2717 sess_change_server(t, NULL);
2718 }
2719 t->be->failed_resp++;
2720 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002721 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002722 txn->status = 502;
2723 client_return(t, error_message(t, HTTP_ERR_502));
2724 if (!(t->flags & SN_ERR_MASK))
2725 t->flags |= SN_ERR_SRVCL;
2726 if (!(t->flags & SN_FINST_MASK))
2727 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002728
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002729 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2730 process_srv_queue(t->srv);
2731
Willy Tarreaudafde432008-08-17 01:00:46 +02002732 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002733 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002734 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002735 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002736 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002737 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002738 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002739 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02002740 buffer_shutr(rep);
2741 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002742 fd_delete(t->srv_fd);
2743 if (t->srv) {
2744 t->srv->cur_sess--;
2745 t->srv->failed_resp++;
2746 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002747 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002748 t->be->failed_resp++;
2749 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002750 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002751 txn->status = 504;
2752 client_return(t, error_message(t, HTTP_ERR_504));
2753 if (!(t->flags & SN_ERR_MASK))
2754 t->flags |= SN_ERR_SRVTO;
2755 if (!(t->flags & SN_FINST_MASK))
2756 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002757
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002758 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2759 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002760 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002761 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002762 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002764
Willy Tarreau21d2af32008-02-14 20:25:24 +01002765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002766 /*****************************************************************
2767 * More interesting part now : we know that we have a complete *
2768 * response which at least looks like HTTP. We have an indicator *
2769 * of each header's length, so we can parse them quickly. *
2770 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002771
Willy Tarreau2df28e82008-08-17 15:20:19 +02002772 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002773
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002774 /* ensure we keep this pointer to the beginning of the message */
2775 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002776
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002777 /*
2778 * 1: get the status code and check for cacheability.
2779 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002780
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002781 t->logs.logwait &= ~LW_RESP;
2782 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002783
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002784 switch (txn->status) {
2785 case 200:
2786 case 203:
2787 case 206:
2788 case 300:
2789 case 301:
2790 case 410:
2791 /* RFC2616 @13.4:
2792 * "A response received with a status code of
2793 * 200, 203, 206, 300, 301 or 410 MAY be stored
2794 * by a cache (...) unless a cache-control
2795 * directive prohibits caching."
2796 *
2797 * RFC2616 @9.5: POST method :
2798 * "Responses to this method are not cacheable,
2799 * unless the response includes appropriate
2800 * Cache-Control or Expires header fields."
2801 */
2802 if (likely(txn->meth != HTTP_METH_POST) &&
2803 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2804 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2805 break;
2806 default:
2807 break;
2808 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002809
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002810 /*
2811 * 2: we may need to capture headers
2812 */
2813 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2814 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2815 txn->rsp.cap, t->fe->rsp_cap);
2816
2817 /*
2818 * 3: we will have to evaluate the filters.
2819 * As opposed to version 1.2, now they will be evaluated in the
2820 * filters order and not in the header order. This means that
2821 * each filter has to be validated among all headers.
2822 *
2823 * Filters are tried with ->be first, then with ->fe if it is
2824 * different from ->be.
2825 */
2826
2827 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2828
2829 cur_proxy = t->be;
2830 while (1) {
2831 struct proxy *rule_set = cur_proxy;
2832
2833 /* try headers filters */
2834 if (rule_set->rsp_exp != NULL) {
2835 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2836 return_bad_resp:
2837 if (t->srv) {
2838 t->srv->cur_sess--;
2839 t->srv->failed_resp++;
2840 sess_change_server(t, NULL);
2841 }
2842 cur_proxy->failed_resp++;
2843 return_srv_prx_502:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002844 buffer_shutr(rep);
2845 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002846 fd_delete(t->srv_fd);
2847 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002848 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002849 txn->status = 502;
2850 client_return(t, error_message(t, HTTP_ERR_502));
2851 if (!(t->flags & SN_ERR_MASK))
2852 t->flags |= SN_ERR_PRXCOND;
2853 if (!(t->flags & SN_FINST_MASK))
2854 t->flags |= SN_FINST_H;
2855 /* We used to have a free connection slot. Since we'll never use it,
2856 * we have to inform the server that it may be used by another session.
2857 */
2858 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2859 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002860 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002861 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002863
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002864 /* has the response been denied ? */
2865 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002866 if (t->srv) {
2867 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002868 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002869 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002870 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002871 cur_proxy->denied_resp++;
2872 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002873 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002875 /* We might have to check for "Connection:" */
2876 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2877 !(t->flags & SN_CONN_CLOSED)) {
2878 char *cur_ptr, *cur_end, *cur_next;
2879 int cur_idx, old_idx, delta, val;
2880 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002882 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2883 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2886 cur_hdr = &txn->hdr_idx.v[cur_idx];
2887 cur_ptr = cur_next;
2888 cur_end = cur_ptr + cur_hdr->len;
2889 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002890
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002891 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2892 if (val) {
2893 /* 3 possibilities :
2894 * - we have already set Connection: close,
2895 * so we remove this line.
2896 * - we have not yet set Connection: close,
2897 * but this line indicates close. We leave
2898 * it untouched and set the flag.
2899 * - we have not yet set Connection: close,
2900 * and this line indicates non-close. We
2901 * replace it.
2902 */
2903 if (t->flags & SN_CONN_CLOSED) {
2904 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2905 txn->rsp.eoh += delta;
2906 cur_next += delta;
2907 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2908 txn->hdr_idx.used--;
2909 cur_hdr->len = 0;
2910 } else {
2911 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2912 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2913 "close", 5);
2914 cur_next += delta;
2915 cur_hdr->len += delta;
2916 txn->rsp.eoh += delta;
2917 }
2918 t->flags |= SN_CONN_CLOSED;
2919 }
2920 }
2921 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002922 }
2923 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002925 /* add response headers from the rule sets in the same order */
2926 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2927 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2928 rule_set->rsp_add[cur_idx])) < 0)
2929 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002930 }
2931
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002932 /* check whether we're already working on the frontend */
2933 if (cur_proxy == t->fe)
2934 break;
2935 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 /*
2939 * 4: check for server cookie.
2940 */
2941 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2942 || (t->be->options & PR_O_CHK_CACHE))
2943 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944
Willy Tarreaubaaee002006-06-26 02:48:02 +02002945
Willy Tarreaua15645d2007-03-18 16:22:39 +01002946 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002947 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002948 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002949 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2950 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002951
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002952 /*
2953 * 6: add server cookie in the response if needed
2954 */
2955 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2956 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2957 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 /* the server is known, it's not the one the client requested, we have to
2960 * insert a set-cookie here, except if we want to insert only on POST
2961 * requests and this one isn't. Note that servers which don't have cookies
2962 * (eg: some backup servers) will return a full cookie removal request.
2963 */
2964 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2965 t->be->cookie_name,
2966 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002968 if (t->be->cookie_domain)
2969 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002971 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2972 trash, len)) < 0)
2973 goto return_bad_resp;
2974 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002976 /* Here, we will tell an eventual cache on the client side that we don't
2977 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2978 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2979 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2980 */
2981 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002983 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2984
2985 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2986 "Cache-control: private", 22)) < 0)
2987 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002988 }
2989 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002990
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002992 /*
2993 * 7: check if result will be cacheable with a cookie.
2994 * We'll block the response if security checks have caught
2995 * nasty things such as a cacheable cookie.
2996 */
2997 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2998 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2999 (t->be->options & PR_O_CHK_CACHE)) {
3000
3001 /* we're in presence of a cacheable response containing
3002 * a set-cookie header. We'll block it as requested by
3003 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003004 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003005 if (t->srv) {
3006 t->srv->cur_sess--;
3007 t->srv->failed_secu++;
3008 sess_change_server(t, NULL);
3009 }
3010 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003011
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003012 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3013 t->be->id, t->srv?t->srv->id:"<dispatch>");
3014 send_log(t->be, LOG_ALERT,
3015 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3016 t->be->id, t->srv?t->srv->id:"<dispatch>");
3017 goto return_srv_prx_502;
3018 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003019
3020 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003021 * 8: add "Connection: close" if needed and not yet set.
3022 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003023 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003024 if (!(t->flags & SN_CONN_CLOSED) &&
3025 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3026 if ((unlikely(msg->sl.st.v_l != 8) ||
3027 unlikely(req->data[msg->som + 7] != '0')) &&
3028 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3029 "Connection: close", 17)) < 0)
3030 goto return_bad_resp;
3031 t->flags |= SN_CONN_CLOSED;
3032 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /*************************************************************
3035 * OK, that's finished for the headers. We have done what we *
3036 * could. Let's switch to the DATA state. *
3037 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038
Willy Tarreaue393fe22008-08-16 22:18:07 +02003039 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003040 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003041
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003042#ifdef CONFIG_HAP_TCPSPLICE
3043 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3044 /* TCP splicing supported by both FE and BE */
3045 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3046 }
3047#endif
3048 /* if the user wants to log as soon as possible, without counting
3049 * bytes from the server, then this is the right moment. We have
3050 * to temporarily assign bytes_out to log what we currently have.
3051 */
3052 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3053 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3054 t->logs.bytes_out = txn->rsp.eoh;
3055 if (t->fe->to_log & LW_REQ)
3056 http_sess_log(t);
3057 else
3058 tcp_sess_log(t);
3059 t->logs.bytes_out = 0;
3060 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003061
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003062 /* Note: we must not try to cheat by jumping directly to DATA,
3063 * otherwise we would not let the client side wake up.
3064 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003065
Willy Tarreaudafde432008-08-17 01:00:46 +02003066 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003067 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003068
Willy Tarreau2df28e82008-08-17 15:20:19 +02003069 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3070 * probably reduce one day's debugging session.
3071 */
3072#ifdef DEBUG_DEV
3073 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3074 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3075 __FILE__, __LINE__, rep->analysers);
3076 ABORT_NOW();
3077 }
3078#endif
3079 rep->analysers &= AN_RTR_HTTP_HDR;
3080
Willy Tarreaudafde432008-08-17 01:00:46 +02003081 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02003082 if (unlikely(rep->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02003083 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003084 return 0;
3085}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003088 * Manages the client FSM and its socket. It normally returns zero, but may
3089 * return 1 if it absolutely wants to be called again.
3090 *
3091 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003092 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003093 */
3094int process_cli(struct session *t)
3095{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003096 struct buffer *req = t->req;
3097 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003098
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003099 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 +02003100 now_ms,
3101 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003102 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3103 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 +02003104 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003105 req->flags, rep->flags,
3106 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003107
Willy Tarreaudafde432008-08-17 01:00:46 +02003108 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003109 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3110 * still set this state (and will do until unix sockets are converted).
3111 */
3112 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003113 /* we can skip most of the tests at once if some conditions are not met */
3114 if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3115 (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
3116 (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3117 (!(rep->flags & BF_SHUTW) &&
3118 (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3119 goto update_timeouts;
3120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003121 /* read or write error */
3122 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003123 buffer_shutr(req);
3124 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003125 fd_delete(t->cli_fd);
3126 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003127 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003128 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003129 if (!(t->flags & SN_ERR_MASK))
3130 t->flags |= SN_ERR_CLICL;
3131 if (!(t->flags & SN_FINST_MASK)) {
3132 if (t->pend_pos)
3133 t->flags |= SN_FINST_Q;
3134 else if (t->srv_state == SV_STCONN)
3135 t->flags |= SN_FINST_C;
3136 else
3137 t->flags |= SN_FINST_D;
3138 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003139 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003140 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003141 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003142 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003143 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003144 req->flags & (BF_READ_NULL | BF_SHUTW)) {
3145 buffer_shutr(req);
3146 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003147 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003148 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003149 } else {
3150 /* output was already closed */
3151 fd_delete(t->cli_fd);
3152 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003153 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003154 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003155 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003156 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003157 /* last server read and buffer empty : we only check them when we're
3158 * allowed to forward the data.
3159 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003160 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003161 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003162 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
3163 buffer_shutw(rep);
3164 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165 EV_FD_CLR(t->cli_fd, DIR_WR);
3166 shutdown(t->cli_fd, SHUT_WR);
3167 /* We must ensure that the read part is still alive when switching to shutw */
3168 /* FIXME: is this still true ? */
3169 EV_FD_SET(t->cli_fd, DIR_RD);
3170 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003171 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003172 } else {
3173 fd_delete(t->cli_fd);
3174 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003175 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003176 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003177 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003178 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003179 /* read timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003180 else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003181 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003182 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003183 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003184 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003185 } else {
3186 /* output was already closed */
3187 fd_delete(t->cli_fd);
3188 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003189 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003190 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003191 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003192 if (!(t->flags & SN_ERR_MASK))
3193 t->flags |= SN_ERR_CLITO;
3194 if (!(t->flags & SN_FINST_MASK)) {
3195 if (t->pend_pos)
3196 t->flags |= SN_FINST_Q;
3197 else if (t->srv_state == SV_STCONN)
3198 t->flags |= SN_FINST_C;
3199 else
3200 t->flags |= SN_FINST_D;
3201 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003202 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003203 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003204 }
3205 /* write timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003206 else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003207 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003208 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003209 EV_FD_CLR(t->cli_fd, DIR_WR);
3210 shutdown(t->cli_fd, SHUT_WR);
3211 /* We must ensure that the read part is still alive when switching to shutw */
3212 /* FIXME: is this still true ? */
3213 EV_FD_SET(t->cli_fd, DIR_RD);
3214 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003215 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003216 } else {
3217 fd_delete(t->cli_fd);
3218 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003219 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003220 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003221 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003222 if (!(t->flags & SN_ERR_MASK))
3223 t->flags |= SN_ERR_CLITO;
3224 if (!(t->flags & SN_FINST_MASK)) {
3225 if (t->pend_pos)
3226 t->flags |= SN_FINST_Q;
3227 else if (t->srv_state == SV_STCONN)
3228 t->flags |= SN_FINST_C;
3229 else
3230 t->flags |= SN_FINST_D;
3231 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003232 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003233 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003234 }
3235
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003236 update_timeouts:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003237 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003238 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003239 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003240 /* no room to read more data */
3241 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3242 /* stop reading until we get some space */
3243 req->rex = TICK_ETERNITY;
3244 }
3245 } else {
3246 EV_FD_COND_S(t->cli_fd, DIR_RD);
3247 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3248 }
3249 }
3250
3251 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003252 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003253 /* first, we may have to produce data (eg: stats).
3254 * right now, this is limited to the SHUTR state.
3255 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003256 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003257 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003258 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003259 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003260 fd_delete(t->cli_fd);
3261 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003262 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003263 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003264 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003265 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003266
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003267 /* 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 +02003268 if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003269 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3270 /* stop writing */
3271 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003272 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003273 } else {
3274 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003275 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003276 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003277 /* restart writing */
3278 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003279 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003280 /* FIXME: to prevent the client from expiring read timeouts during writes,
3281 * we refresh it, except if it was already infinite. */
3282 req->rex = rep->wex;
3283 }
3284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003285 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003286 }
3287 return 0; /* other cases change nothing */
3288 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003289 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003290 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3291 int len;
3292 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);
3293 write(1, trash, len);
3294 }
3295 return 0;
3296 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003297#ifdef DEBUG_DEV
3298 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3299 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003300#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003301 return 0;
3302}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003305/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003306 * Manages the server FSM and its socket. It normally returns zero, but may
3307 * return 1 if it absolutely wants to be called again.
3308 *
Willy Tarreaua7c52762008-08-16 18:40:18 +02003309 * Note: process_srv is the ONLY function allowed to set srv_state to anything
3310 * but SV_STCLOSE. The only exception is for functions called from this
3311 * one (eg: those in backend.c).
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003312 */
3313int process_srv(struct session *t)
3314{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 struct http_txn *txn = &t->txn;
3316 struct buffer *req = t->req;
3317 struct buffer *rep = t->rep;
3318 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003319
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003320 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 +02003321 now_ms,
3322 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003323 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
3324 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 +02003325 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003326 req->flags, rep->flags,
3327 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
Willy Tarreaudafde432008-08-17 01:00:46 +02003329 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003330 if (t->srv_state == SV_STIDLE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003331 if ((rep->flags & BF_SHUTW) ||
3332 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003333 (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003334 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003335 if (t->pend_pos)
3336 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3337 /* note that this must not return any error because it would be able to
3338 * overwrite the client_retnclose() output.
3339 */
3340 if (txn->flags & TX_CLTARPIT)
3341 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3342 else
3343 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3344
Willy Tarreauf8533202008-08-16 14:55:08 +02003345 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003346 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003347 }
Willy Tarreaud9f48362008-08-16 16:39:26 +02003348 else if (req->flags & BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003349 /* the client allows the server to connect */
3350 if (txn->flags & TX_CLTARPIT) {
3351 /* This connection is being tarpitted. The CLIENT side has
3352 * already set the connect expiration date to the right
3353 * timeout. We just have to check that it has not expired.
3354 */
Willy Tarreau507385d2008-08-17 13:04:25 +02003355 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003356 return 0;
3357
3358 /* We will set the queue timer to the time spent, just for
3359 * logging purposes. We fake a 500 server error, so that the
3360 * attacker will not suspect his connection has been tarpitted.
3361 * It will not cause trouble to the logs because we can exclude
3362 * the tarpitted connections by filtering on the 'PT' status flags.
3363 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003364 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003365 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3366 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3367 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003368 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreaudafde432008-08-17 01:00:46 +02003369 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003370 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003371
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003372 /* Right now, we will need to create a connection to the server.
3373 * We might already have tried, and got a connection pending, in
3374 * which case we will not do anything till it's pending. It's up
3375 * to any other session to release it and wake us up again.
3376 */
3377 if (t->pend_pos) {
Willy Tarreau507385d2008-08-17 13:04:25 +02003378 if (!(req->flags & BF_WRITE_TIMEOUT)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003379 return 0;
3380 } else {
3381 /* we've been waiting too long here */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003382 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003383 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3384 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3385 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003386 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003387 if (t->srv)
3388 t->srv->failed_conns++;
3389 t->be->failed_conns++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003390 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003391 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003392 }
3393
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003394 do {
3395 /* first, get a connection */
3396 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3397 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003398
Willy Tarreaudafde432008-08-17 01:00:46 +02003399 if (srv_redispatch_connect(t)) {
3400 if (t->srv_state == SV_STIDLE)
3401 return 0;
3402 goto update_state;
3403 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003404
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003405 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3406 /* Server supporting redirection and it is possible.
3407 * Invalid requests are reported as such. It concerns all
3408 * the largest ones.
3409 */
3410 struct chunk rdr;
3411 char *path;
3412 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003413
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003414 /* 1: create the response header */
3415 rdr.len = strlen(HTTP_302);
3416 rdr.str = trash;
3417 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003418
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003419 /* 2: add the server's prefix */
3420 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3421 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003422
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003423 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3424 rdr.len += t->srv->rdr_len;
3425
3426 /* 3: add the request URI */
3427 path = http_get_path(txn);
3428 if (!path)
3429 goto cancel_redir;
3430 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3431 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3432 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003434 memcpy(rdr.str + rdr.len, path, len);
3435 rdr.len += len;
3436 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3437 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003438
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003439 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003440 trace_term(t, TT_HTTP_SRV_3);
3441
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003442 /* FIXME: we should increase a counter of redirects per server and per backend. */
3443 if (t->srv)
3444 t->srv->cum_sess++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003445 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003446 cancel_redir:
3447 txn->status = 400;
3448 t->fe->failed_req++;
3449 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3450 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003451 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaudafde432008-08-17 01:00:46 +02003452 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003453 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003454
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003455 /* try to (re-)connect to the server, and fail if we expire the
3456 * number of retries.
3457 */
3458 if (srv_retryable_connect(t)) {
3459 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003460 if (t->srv_state == SV_STIDLE)
3461 return 0;
3462 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003463 }
3464 } while (1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003465 } /* end if may_forward */
3466 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003468 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003469 if ((rep->flags & BF_SHUTW) ||
3470 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003471 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003472 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003473 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003474 if (!(t->flags & SN_CONN_TAR)) {
3475 /* if we are in turn-around, we have already closed the FD */
3476 fd_delete(t->srv_fd);
3477 if (t->srv) {
3478 t->srv->cur_sess--;
3479 sess_change_server(t, NULL);
3480 }
3481 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003482
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003483 /* note that this must not return any error because it would be able to
3484 * overwrite the client_retnclose() output.
3485 */
3486 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003487 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreaudafde432008-08-17 01:00:46 +02003488 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003489 }
Willy Tarreau507385d2008-08-17 13:04:25 +02003490 if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003491 return 0; /* nothing changed */
3492 }
3493 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3494 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003495 if (t->flags & SN_CONN_TAR) {
3496 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau507385d2008-08-17 13:04:25 +02003497 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003498 return 0;
3499 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003500 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003501 else {
3502 fd_delete(t->srv_fd);
3503 if (t->srv) {
3504 t->srv->cur_sess--;
3505 sess_change_server(t, NULL);
3506 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003507
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003508 if (!(req->flags & BF_WRITE_STATUS))
3509 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3510 else
3511 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003512
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003513 /* ensure that we have enough retries left */
Willy Tarreaudafde432008-08-17 01:00:46 +02003514 if (srv_count_retry_down(t, conn_err)) {
3515 goto update_state;
3516 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003517
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003518 if (req->flags & BF_WRITE_ERROR) {
3519 /* we encountered an immediate connection error, and we
3520 * will have to retry connecting to the same server, most
3521 * likely leading to the same result. To avoid this, we
3522 * fake a connection timeout to retry after a turn-around
3523 * time of 1 second. We will wait in the previous if block.
3524 */
3525 t->flags |= SN_CONN_TAR;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003526 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003527 return 0;
3528 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003529 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003530
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003531 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3532 /* We're on our last chance, and the REDISP option was specified.
3533 * We will ignore cookie and force to balance or use the dispatcher.
3534 */
3535 /* let's try to offer this slot to anybody */
3536 if (may_dequeue_tasks(t->srv, t->be))
3537 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003538
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003539 /* it's left to the dispatcher to choose a server */
3540 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3541 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003542
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003543 /* first, get a connection */
Willy Tarreaudafde432008-08-17 01:00:46 +02003544 if (srv_redispatch_connect(t)) {
3545 if (t->srv_state == SV_STCONN)
3546 return 0;
3547 goto update_state;
3548 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003549 } else {
3550 if (t->srv)
3551 t->srv->retries++;
3552 t->be->retries++;
3553 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003554
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003555 do {
3556 /* Now we will try to either reconnect to the same server or
3557 * connect to another server. If the connection gets queued
3558 * because all servers are saturated, then we will go back to
3559 * the SV_STIDLE state.
3560 */
3561 if (srv_retryable_connect(t)) {
3562 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003563 if (t->srv_state == SV_STCONN)
3564 return 0;
3565 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003566 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003567
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 /* we need to redispatch the connection to another server */
Willy Tarreaudafde432008-08-17 01:00:46 +02003569 if (srv_redispatch_connect(t)) {
3570 if (t->srv_state == SV_STCONN)
3571 return 0;
3572 goto update_state;
3573 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003574 } while (1);
3575 }
3576 else { /* no error or write 0 */
3577 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003578
Willy Tarreaue393fe22008-08-16 22:18:07 +02003579 if (req->flags & BF_EMPTY) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003580 EV_FD_CLR(t->srv_fd, DIR_WR);
3581 req->wex = TICK_ETERNITY;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003582 } else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003583 EV_FD_SET(t->srv_fd, DIR_WR);
3584 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3585 if (tick_isset(req->wex)) {
3586 /* FIXME: to prevent the server from expiring read timeouts during writes,
3587 * we refresh it. */
3588 rep->rex = req->wex;
3589 }
3590 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003591
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003592 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3593 EV_FD_SET(t->srv_fd, DIR_RD);
3594 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003595 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003596
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003597 /* if the user wants to log as soon as possible, without counting
3598 bytes from the server, then this is the right moment. */
3599 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3600 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3601 tcp_sess_log(t);
3602 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003603#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003604 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3605 /* TCP splicing supported by both FE and BE */
3606 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3607 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003608#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003609 }
3610 else {
Willy Tarreau2df28e82008-08-17 15:20:19 +02003611 rep->analysers |= AN_RTR_HTTP_HDR;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003612 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003613 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3614 /* reset hdr_idx which was already initialized by the request.
3615 * right now, the http parser does it.
3616 * hdr_idx_init(&t->txn.hdr_idx);
3617 */
3618 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003619
3620 t->srv_state = SV_STDATA;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003621 if (!rep->analysers)
Willy Tarreaudafde432008-08-17 01:00:46 +02003622 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003623 req->wex = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003624 goto update_state;
3625 } /* else no error or write 0 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003626 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003627 else if (t->srv_state == SV_STDATA) {
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003628 /* we can skip most of the tests at once if some conditions are not met */
3629 if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) ||
3630 (!(req->flags & BF_SHUTW) &&
3631 (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) ||
3632 (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) ||
3633 (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW))))
3634 goto update_timeouts;
3635
Willy Tarreaubaaee002006-06-26 02:48:02 +02003636 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003637 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003638 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003639 buffer_shutr(rep);
3640 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003641 fd_delete(t->srv_fd);
3642 if (t->srv) {
3643 t->srv->cur_sess--;
3644 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003645 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003646 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003647 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003648 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003649 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003650 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003651 if (!(t->flags & SN_ERR_MASK))
3652 t->flags |= SN_ERR_SRVCL;
3653 if (!(t->flags & SN_FINST_MASK))
3654 t->flags |= SN_FINST_D;
3655 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003656 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003657 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003658
Willy Tarreaudafde432008-08-17 01:00:46 +02003659 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003660 }
3661 /* last read, or end of client write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003662 else if (!(rep->flags & BF_SHUTR) && /* not already done */
3663 rep->flags & (BF_READ_NULL | BF_SHUTW)) {
3664 buffer_shutr(rep);
3665 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003666 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003667 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003668 } else {
3669 /* output was already closed */
3670 fd_delete(t->srv_fd);
3671 if (t->srv) {
3672 t->srv->cur_sess--;
3673 sess_change_server(t, NULL);
3674 }
3675 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003676 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003677
3678 if (may_dequeue_tasks(t->srv, t->be))
3679 process_srv_queue(t->srv);
3680 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003681 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003683 /* end of client read and no more data to send. We can forward
3684 * the close when we're allowed to forward data (anytime right
3685 * now). If we're using option forceclose, then we may also
3686 * shutdown the outgoing write channel once the response starts
3687 * coming from the server.
3688 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003689 else if (!(req->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003690 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003691 (req->flags & BF_SHUTR ||
Willy Tarreau461f6622008-08-15 23:43:19 +02003692 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003693 buffer_shutw(req);
3694 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_9);
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 true ? */
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);
3707 }
3708 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003709 trace_term(t, TT_HTTP_SRV_10);
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);
3713 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003714 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003715 }
3716 /* read timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003717 else if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003718 buffer_shutr(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003719 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003720 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003721 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003722 } else {
3723 fd_delete(t->srv_fd);
3724 if (t->srv) {
3725 t->srv->cur_sess--;
3726 sess_change_server(t, NULL);
3727 }
3728 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003729 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003730
3731 if (may_dequeue_tasks(t->srv, t->be))
3732 process_srv_queue(t->srv);
3733 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003734 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003735 if (!(t->flags & SN_ERR_MASK))
3736 t->flags |= SN_ERR_SRVTO;
3737 if (!(t->flags & SN_FINST_MASK))
3738 t->flags |= SN_FINST_D;
3739 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003740
3741 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003742 }
3743 /* write timeout */
Willy Tarreau25009812008-08-17 18:16:38 +02003744 else if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003745 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003746 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003747 EV_FD_CLR(t->srv_fd, DIR_WR);
3748 shutdown(t->srv_fd, SHUT_WR);
3749 /* We must ensure that the read part is still alive when switching to shutw */
3750 /* FIXME: is this still needed ? */
3751 EV_FD_SET(t->srv_fd, DIR_RD);
3752 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003753 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003754 } else {
3755 fd_delete(t->srv_fd);
3756 if (t->srv) {
3757 t->srv->cur_sess--;
3758 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003760 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003761 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003762
Willy Tarreau461f6622008-08-15 23:43:19 +02003763 if (may_dequeue_tasks(t->srv, t->be))
3764 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003765 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003766 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003767 if (!(t->flags & SN_ERR_MASK))
3768 t->flags |= SN_ERR_SRVTO;
3769 if (!(t->flags & SN_FINST_MASK))
3770 t->flags |= SN_FINST_D;
3771 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003772 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003774
Willy Tarreau6d2889b2008-08-17 16:23:10 +02003775 update_timeouts:
Willy Tarreau461f6622008-08-15 23:43:19 +02003776 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003777 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003778 if (rep->flags & BF_FULL) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003779 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3780 rep->rex = TICK_ETERNITY;
3781 } else {
3782 EV_FD_COND_S(t->srv_fd, DIR_RD);
3783 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003784 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003786
Willy Tarreau461f6622008-08-15 23:43:19 +02003787 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003788 if (!(req->flags & BF_SHUTW)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003789 if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003790 /* stop writing */
3791 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3792 req->wex = TICK_ETERNITY;
3793 } else {
3794 /* buffer not empty, there are still data to be transferred */
3795 EV_FD_COND_S(t->srv_fd, DIR_WR);
3796 if (!tick_isset(req->wex)) {
3797 /* restart writing */
3798 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003799 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003800 /* FIXME: to prevent the server from expiring read timeouts during writes,
3801 * we refresh it, except if it was already infinite.
3802 */
3803 rep->rex = req->wex;
3804 }
3805 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003806 }
3807 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003808 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003809 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003810 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003811 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3812 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003813 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003814 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003815 write(1, trash, len);
3816 }
3817 return 0;
3818 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003819#ifdef DEBUG_DEV
3820 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3821 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003822#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003823 return 0;
3824}
3825
3826
3827/*
3828 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003829 * called with client socket shut down on input. Right now, only statistics can
3830 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003831 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003832 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003833 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003834 */
3835int produce_content(struct session *s)
3836{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003837 if (s->data_source == DATA_SRC_NONE) {
3838 s->flags &= ~SN_SELF_GEN;
3839 return 1;
3840 }
3841 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003842 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003843 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003844 if (ret >= 0)
3845 return ret;
3846 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003847 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003848
Willy Tarreau91861262007-10-17 17:06:05 +02003849 /* unknown data source or internal error */
3850 s->txn.status = 500;
3851 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003852 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003853 if (!(s->flags & SN_ERR_MASK))
3854 s->flags |= SN_ERR_PRXCOND;
3855 if (!(s->flags & SN_FINST_MASK))
3856 s->flags |= SN_FINST_R;
3857 s->flags &= ~SN_SELF_GEN;
3858 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003859}
3860
3861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003862/* Iterate the same filter through all request headers.
3863 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003864 * Since it can manage the switch to another backend, it updates the per-proxy
3865 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003866 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003867int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003868{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003869 char term;
3870 char *cur_ptr, *cur_end, *cur_next;
3871 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003872 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003873 struct hdr_idx_elem *cur_hdr;
3874 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003876 last_hdr = 0;
3877
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003878 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003879 old_idx = 0;
3880
3881 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003882 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003883 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003884 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003885 (exp->action == ACT_ALLOW ||
3886 exp->action == ACT_DENY ||
3887 exp->action == ACT_TARPIT))
3888 return 0;
3889
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003890 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003891 if (!cur_idx)
3892 break;
3893
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003894 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003895 cur_ptr = cur_next;
3896 cur_end = cur_ptr + cur_hdr->len;
3897 cur_next = cur_end + cur_hdr->cr + 1;
3898
3899 /* Now we have one header between cur_ptr and cur_end,
3900 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003901 */
3902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003903 /* The annoying part is that pattern matching needs
3904 * that we modify the contents to null-terminate all
3905 * strings before testing them.
3906 */
3907
3908 term = *cur_end;
3909 *cur_end = '\0';
3910
3911 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3912 switch (exp->action) {
3913 case ACT_SETBE:
3914 /* It is not possible to jump a second time.
3915 * FIXME: should we return an HTTP/500 here so that
3916 * the admin knows there's a problem ?
3917 */
3918 if (t->be != t->fe)
3919 break;
3920
3921 /* Swithing Proxy */
3922 t->be = (struct proxy *) exp->replace;
3923
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003924 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003925 * because we have associated req_cap and rsp_cap to the
3926 * frontend, and the beconn will be updated later.
3927 */
3928
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003929 t->rep->rto = t->req->wto = t->be->timeout.server;
3930 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003931 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003932 last_hdr = 1;
3933 break;
3934
3935 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003936 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003937 last_hdr = 1;
3938 break;
3939
3940 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003941 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003942 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003943 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003944 break;
3945
3946 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003947 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003948 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003949 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003950 break;
3951
3952 case ACT_REPLACE:
3953 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3954 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3955 /* FIXME: if the user adds a newline in the replacement, the
3956 * index will not be recalculated for now, and the new line
3957 * will not be counted as a new header.
3958 */
3959
3960 cur_end += delta;
3961 cur_next += delta;
3962 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003963 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003964 break;
3965
3966 case ACT_REMOVE:
3967 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3968 cur_next += delta;
3969
3970 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003971 txn->req.eoh += delta;
3972 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3973 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003974 cur_hdr->len = 0;
3975 cur_end = NULL; /* null-term has been rewritten */
3976 break;
3977
3978 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 if (cur_end)
3981 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 /* keep the link from this header to next one in case of later
3984 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003985 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 old_idx = cur_idx;
3987 }
3988 return 0;
3989}
3990
3991
3992/* Apply the filter to the request line.
3993 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3994 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003995 * Since it can manage the switch to another backend, it updates the per-proxy
3996 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003997 */
3998int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3999{
4000 char term;
4001 char *cur_ptr, *cur_end;
4002 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004003 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004004 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005
Willy Tarreau58f10d72006-12-04 02:26:12 +01004006
Willy Tarreau3d300592007-03-18 18:34:41 +01004007 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004009 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 (exp->action == ACT_ALLOW ||
4011 exp->action == ACT_DENY ||
4012 exp->action == ACT_TARPIT))
4013 return 0;
4014 else if (exp->action == ACT_REMOVE)
4015 return 0;
4016
4017 done = 0;
4018
Willy Tarreau9cdde232007-05-02 20:58:19 +02004019 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004020 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004021
4022 /* Now we have the request line between cur_ptr and cur_end */
4023
4024 /* The annoying part is that pattern matching needs
4025 * that we modify the contents to null-terminate all
4026 * strings before testing them.
4027 */
4028
4029 term = *cur_end;
4030 *cur_end = '\0';
4031
4032 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4033 switch (exp->action) {
4034 case ACT_SETBE:
4035 /* It is not possible to jump a second time.
4036 * FIXME: should we return an HTTP/500 here so that
4037 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004039 if (t->be != t->fe)
4040 break;
4041
4042 /* Swithing Proxy */
4043 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004045 /* right now, the backend switch is not too much complicated
4046 * because we have associated req_cap and rsp_cap to the
4047 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004048 */
4049
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004050 t->rep->rto = t->req->wto = t->be->timeout.server;
4051 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004052 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004053 done = 1;
4054 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004056 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004057 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 done = 1;
4059 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004062 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004063 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004064 done = 1;
4065 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004068 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004069 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004070 done = 1;
4071 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 case ACT_REPLACE:
4074 *cur_end = term; /* restore the string terminator */
4075 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4076 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4077 /* FIXME: if the user adds a newline in the replacement, the
4078 * index will not be recalculated for now, and the new line
4079 * will not be counted as a new header.
4080 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004082 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004083 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004084
Willy Tarreau9cdde232007-05-02 20:58:19 +02004085 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004086 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004087 HTTP_MSG_RQMETH,
4088 cur_ptr, cur_end + 1,
4089 NULL, NULL);
4090 if (unlikely(!cur_end))
4091 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 /* we have a full request and we know that we have either a CR
4094 * or an LF at <ptr>.
4095 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004096 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4097 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004098 /* there is no point trying this regex on headers */
4099 return 1;
4100 }
4101 }
4102 *cur_end = term; /* restore the string terminator */
4103 return done;
4104}
Willy Tarreau97de6242006-12-27 17:18:38 +01004105
Willy Tarreau58f10d72006-12-04 02:26:12 +01004106
Willy Tarreau58f10d72006-12-04 02:26:12 +01004107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004108/*
4109 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4110 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004111 * unparsable request. Since it can manage the switch to another backend, it
4112 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004113 */
4114int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4115{
Willy Tarreau3d300592007-03-18 18:34:41 +01004116 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004117 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004118 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004119 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004121 /*
4122 * The interleaving of transformations and verdicts
4123 * makes it difficult to decide to continue or stop
4124 * the evaluation.
4125 */
4126
Willy Tarreau3d300592007-03-18 18:34:41 +01004127 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004128 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4129 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4130 exp = exp->next;
4131 continue;
4132 }
4133
4134 /* Apply the filter to the request line. */
4135 ret = apply_filter_to_req_line(t, req, exp);
4136 if (unlikely(ret < 0))
4137 return -1;
4138
4139 if (likely(ret == 0)) {
4140 /* The filter did not match the request, it can be
4141 * iterated through all headers.
4142 */
4143 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004144 }
4145 exp = exp->next;
4146 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004147 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004148}
4149
4150
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151
Willy Tarreau58f10d72006-12-04 02:26:12 +01004152/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004153 * Manage client-side cookie. It can impact performance by about 2% so it is
4154 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004155 */
4156void manage_client_side_cookies(struct session *t, struct buffer *req)
4157{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004158 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004159 char *p1, *p2, *p3, *p4;
4160 char *del_colon, *del_cookie, *colon;
4161 int app_cookies;
4162
4163 appsess *asession_temp = NULL;
4164 appsess local_asession;
4165
4166 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168
Willy Tarreau2a324282006-12-05 00:05:46 +01004169 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004170 * we start with the start line.
4171 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004172 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004173 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004175 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004176 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004177 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004178
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004179 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180 cur_ptr = cur_next;
4181 cur_end = cur_ptr + cur_hdr->len;
4182 cur_next = cur_end + cur_hdr->cr + 1;
4183
4184 /* We have one full header between cur_ptr and cur_end, and the
4185 * next header starts at cur_next. We're only interested in
4186 * "Cookie:" headers.
4187 */
4188
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004189 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4190 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004191 old_idx = cur_idx;
4192 continue;
4193 }
4194
4195 /* Now look for cookies. Conforming to RFC2109, we have to support
4196 * attributes whose name begin with a '$', and associate them with
4197 * the right cookie, if we want to delete this cookie.
4198 * So there are 3 cases for each cookie read :
4199 * 1) it's a special attribute, beginning with a '$' : ignore it.
4200 * 2) it's a server id cookie that we *MAY* want to delete : save
4201 * some pointers on it (last semi-colon, beginning of cookie...)
4202 * 3) it's an application cookie : we *MAY* have to delete a previous
4203 * "special" cookie.
4204 * At the end of loop, if a "special" cookie remains, we may have to
4205 * remove it. If no application cookie persists in the header, we
4206 * *MUST* delete it
4207 */
4208
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004209 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004210
Willy Tarreau58f10d72006-12-04 02:26:12 +01004211 /* del_cookie == NULL => nothing to be deleted */
4212 del_colon = del_cookie = NULL;
4213 app_cookies = 0;
4214
4215 while (p1 < cur_end) {
4216 /* skip spaces and colons, but keep an eye on these ones */
4217 while (p1 < cur_end) {
4218 if (*p1 == ';' || *p1 == ',')
4219 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004220 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221 break;
4222 p1++;
4223 }
4224
4225 if (p1 == cur_end)
4226 break;
4227
4228 /* p1 is at the beginning of the cookie name */
4229 p2 = p1;
4230 while (p2 < cur_end && *p2 != '=')
4231 p2++;
4232
4233 if (p2 == cur_end)
4234 break;
4235
4236 p3 = p2 + 1; /* skips the '=' sign */
4237 if (p3 == cur_end)
4238 break;
4239
4240 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004241 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242 p4++;
4243
4244 /* here, we have the cookie name between p1 and p2,
4245 * and its value between p3 and p4.
4246 * we can process it :
4247 *
4248 * Cookie: NAME=VALUE;
4249 * | || || |
4250 * | || || +--> p4
4251 * | || |+-------> p3
4252 * | || +--------> p2
4253 * | |+------------> p1
4254 * | +-------------> colon
4255 * +--------------------> cur_ptr
4256 */
4257
4258 if (*p1 == '$') {
4259 /* skip this one */
4260 }
4261 else {
4262 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004263 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004264 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004265 (p4 - p1 >= t->fe->capture_namelen) &&
4266 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267 int log_len = p4 - p1;
4268
Willy Tarreau086b3b42007-05-13 21:45:51 +02004269 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004270 Alert("HTTP logging : out of memory.\n");
4271 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004272 if (log_len > t->fe->capture_len)
4273 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004274 memcpy(txn->cli_cookie, p1, log_len);
4275 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004276 }
4277 }
4278
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004279 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4280 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004281 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004282 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004283 char *delim;
4284
4285 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4286 * have the server ID betweek p3 and delim, and the original cookie between
4287 * delim+1 and p4. Otherwise, delim==p4 :
4288 *
4289 * Cookie: NAME=SRV~VALUE;
4290 * | || || | |
4291 * | || || | +--> p4
4292 * | || || +--------> delim
4293 * | || |+-----------> p3
4294 * | || +------------> p2
4295 * | |+----------------> p1
4296 * | +-----------------> colon
4297 * +------------------------> cur_ptr
4298 */
4299
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004300 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301 for (delim = p3; delim < p4; delim++)
4302 if (*delim == COOKIE_DELIM)
4303 break;
4304 }
4305 else
4306 delim = p4;
4307
4308
4309 /* Here, we'll look for the first running server which supports the cookie.
4310 * This allows to share a same cookie between several servers, for example
4311 * to dedicate backup servers to specific servers only.
4312 * However, to prevent clients from sticking to cookie-less backup server
4313 * when they have incidentely learned an empty cookie, we simply ignore
4314 * empty cookies and mark them as invalid.
4315 */
4316 if (delim == p3)
4317 srv = NULL;
4318
4319 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004320 if (srv->cookie && (srv->cklen == delim - p3) &&
4321 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004322 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004323 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004324 txn->flags &= ~TX_CK_MASK;
4325 txn->flags |= TX_CK_VALID;
4326 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004327 t->srv = srv;
4328 break;
4329 } else {
4330 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004331 txn->flags &= ~TX_CK_MASK;
4332 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004333 }
4334 }
4335 srv = srv->next;
4336 }
4337
Willy Tarreau3d300592007-03-18 18:34:41 +01004338 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004339 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004340 txn->flags &= ~TX_CK_MASK;
4341 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004342 }
4343
4344 /* depending on the cookie mode, we may have to either :
4345 * - delete the complete cookie if we're in insert+indirect mode, so that
4346 * the server never sees it ;
4347 * - remove the server id from the cookie value, and tag the cookie as an
4348 * application cookie so that it does not get accidentely removed later,
4349 * if we're in cookie prefix mode
4350 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004351 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352 int delta; /* negative */
4353
4354 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4355 p4 += delta;
4356 cur_end += delta;
4357 cur_next += delta;
4358 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004359 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004360
4361 del_cookie = del_colon = NULL;
4362 app_cookies++; /* protect the header from deletion */
4363 }
4364 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004365 (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 +01004366 del_cookie = p1;
4367 del_colon = colon;
4368 }
4369 } else {
4370 /* now we know that we must keep this cookie since it's
4371 * not ours. But if we wanted to delete our cookie
4372 * earlier, we cannot remove the complete header, but we
4373 * can remove the previous block itself.
4374 */
4375 app_cookies++;
4376
4377 if (del_cookie != NULL) {
4378 int delta; /* negative */
4379
4380 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4381 p4 += delta;
4382 cur_end += delta;
4383 cur_next += delta;
4384 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004385 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004386 del_cookie = del_colon = NULL;
4387 }
4388 }
4389
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004390 if ((t->be->appsession_name != NULL) &&
4391 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004392 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004393
Willy Tarreau58f10d72006-12-04 02:26:12 +01004394 /* Cool... it's the right one */
4395
4396 asession_temp = &local_asession;
4397
Willy Tarreau63963c62007-05-13 21:29:55 +02004398 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4400 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4401 return;
4402 }
4403
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004404 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4405 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004407
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004409 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4410 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004411 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004412 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004413 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004414 Alert("Not enough memory process_cli():asession:calloc().\n");
4415 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4416 return;
4417 }
4418
4419 asession_temp->sessid = local_asession.sessid;
4420 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004421 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004422 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004423 } else {
4424 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004425 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004427 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004428 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 Alert("Found Application Session without matching server.\n");
4430 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004431 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004432 while (srv) {
4433 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004434 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004435 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004436 txn->flags &= ~TX_CK_MASK;
4437 txn->flags |= TX_CK_VALID;
4438 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004439 t->srv = srv;
4440 break;
4441 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004442 txn->flags &= ~TX_CK_MASK;
4443 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004444 }
4445 }
4446 srv = srv->next;
4447 }/* end while(srv) */
4448 }/* end else if server == NULL */
4449
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004450 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004451 asession_temp->request_count++;
4452#if defined(DEBUG_HASH)
4453 Alert("manage_client_side_cookies\n");
4454 appsession_hash_dump(&(t->be->htbl_proxy));
4455#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004456 }/* end if ((t->proxy->appsession_name != NULL) ... */
4457 }
4458
4459 /* we'll have to look for another cookie ... */
4460 p1 = p4;
4461 } /* while (p1 < cur_end) */
4462
4463 /* There's no more cookie on this line.
4464 * We may have marked the last one(s) for deletion.
4465 * We must do this now in two ways :
4466 * - if there is no app cookie, we simply delete the header ;
4467 * - if there are app cookies, we must delete the end of the
4468 * string properly, including the colon/semi-colon before
4469 * the cookie name.
4470 */
4471 if (del_cookie != NULL) {
4472 int delta;
4473 if (app_cookies) {
4474 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4475 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004476 cur_hdr->len += delta;
4477 } else {
4478 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004479
4480 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004481 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4482 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004483 cur_hdr->len = 0;
4484 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004485 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004486 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004487 }
4488
4489 /* keep the link from this header to next one */
4490 old_idx = cur_idx;
4491 } /* end of cookie processing on this header */
4492}
4493
4494
Willy Tarreaua15645d2007-03-18 16:22:39 +01004495/* Iterate the same filter through all response headers contained in <rtr>.
4496 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4497 */
4498int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4499{
4500 char term;
4501 char *cur_ptr, *cur_end, *cur_next;
4502 int cur_idx, old_idx, last_hdr;
4503 struct http_txn *txn = &t->txn;
4504 struct hdr_idx_elem *cur_hdr;
4505 int len, delta;
4506
4507 last_hdr = 0;
4508
4509 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4510 old_idx = 0;
4511
4512 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004513 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004514 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004515 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004516 (exp->action == ACT_ALLOW ||
4517 exp->action == ACT_DENY))
4518 return 0;
4519
4520 cur_idx = txn->hdr_idx.v[old_idx].next;
4521 if (!cur_idx)
4522 break;
4523
4524 cur_hdr = &txn->hdr_idx.v[cur_idx];
4525 cur_ptr = cur_next;
4526 cur_end = cur_ptr + cur_hdr->len;
4527 cur_next = cur_end + cur_hdr->cr + 1;
4528
4529 /* Now we have one header between cur_ptr and cur_end,
4530 * and the next header starts at cur_next.
4531 */
4532
4533 /* The annoying part is that pattern matching needs
4534 * that we modify the contents to null-terminate all
4535 * strings before testing them.
4536 */
4537
4538 term = *cur_end;
4539 *cur_end = '\0';
4540
4541 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4542 switch (exp->action) {
4543 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004544 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004545 last_hdr = 1;
4546 break;
4547
4548 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004549 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004550 last_hdr = 1;
4551 break;
4552
4553 case ACT_REPLACE:
4554 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4555 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4556 /* FIXME: if the user adds a newline in the replacement, the
4557 * index will not be recalculated for now, and the new line
4558 * will not be counted as a new header.
4559 */
4560
4561 cur_end += delta;
4562 cur_next += delta;
4563 cur_hdr->len += delta;
4564 txn->rsp.eoh += delta;
4565 break;
4566
4567 case ACT_REMOVE:
4568 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4569 cur_next += delta;
4570
4571 /* FIXME: this should be a separate function */
4572 txn->rsp.eoh += delta;
4573 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4574 txn->hdr_idx.used--;
4575 cur_hdr->len = 0;
4576 cur_end = NULL; /* null-term has been rewritten */
4577 break;
4578
4579 }
4580 }
4581 if (cur_end)
4582 *cur_end = term; /* restore the string terminator */
4583
4584 /* keep the link from this header to next one in case of later
4585 * removal of next header.
4586 */
4587 old_idx = cur_idx;
4588 }
4589 return 0;
4590}
4591
4592
4593/* Apply the filter to the status line in the response buffer <rtr>.
4594 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4595 * or -1 if a replacement resulted in an invalid status line.
4596 */
4597int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4598{
4599 char term;
4600 char *cur_ptr, *cur_end;
4601 int done;
4602 struct http_txn *txn = &t->txn;
4603 int len, delta;
4604
4605
Willy Tarreau3d300592007-03-18 18:34:41 +01004606 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004607 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004608 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004609 (exp->action == ACT_ALLOW ||
4610 exp->action == ACT_DENY))
4611 return 0;
4612 else if (exp->action == ACT_REMOVE)
4613 return 0;
4614
4615 done = 0;
4616
Willy Tarreau9cdde232007-05-02 20:58:19 +02004617 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004618 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4619
4620 /* Now we have the status line between cur_ptr and cur_end */
4621
4622 /* The annoying part is that pattern matching needs
4623 * that we modify the contents to null-terminate all
4624 * strings before testing them.
4625 */
4626
4627 term = *cur_end;
4628 *cur_end = '\0';
4629
4630 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4631 switch (exp->action) {
4632 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004633 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004634 done = 1;
4635 break;
4636
4637 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004638 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004639 done = 1;
4640 break;
4641
4642 case ACT_REPLACE:
4643 *cur_end = term; /* restore the string terminator */
4644 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4645 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4646 /* FIXME: if the user adds a newline in the replacement, the
4647 * index will not be recalculated for now, and the new line
4648 * will not be counted as a new header.
4649 */
4650
4651 txn->rsp.eoh += delta;
4652 cur_end += delta;
4653
Willy Tarreau9cdde232007-05-02 20:58:19 +02004654 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004655 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004656 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004657 cur_ptr, cur_end + 1,
4658 NULL, NULL);
4659 if (unlikely(!cur_end))
4660 return -1;
4661
4662 /* we have a full respnse and we know that we have either a CR
4663 * or an LF at <ptr>.
4664 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004665 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004666 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4667 /* there is no point trying this regex on headers */
4668 return 1;
4669 }
4670 }
4671 *cur_end = term; /* restore the string terminator */
4672 return done;
4673}
4674
4675
4676
4677/*
4678 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4679 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4680 * unparsable response.
4681 */
4682int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4683{
Willy Tarreau3d300592007-03-18 18:34:41 +01004684 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004686 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687 int ret;
4688
4689 /*
4690 * The interleaving of transformations and verdicts
4691 * makes it difficult to decide to continue or stop
4692 * the evaluation.
4693 */
4694
Willy Tarreau3d300592007-03-18 18:34:41 +01004695 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004696 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4697 exp->action == ACT_PASS)) {
4698 exp = exp->next;
4699 continue;
4700 }
4701
4702 /* Apply the filter to the status line. */
4703 ret = apply_filter_to_sts_line(t, rtr, exp);
4704 if (unlikely(ret < 0))
4705 return -1;
4706
4707 if (likely(ret == 0)) {
4708 /* The filter did not match the response, it can be
4709 * iterated through all headers.
4710 */
4711 apply_filter_to_resp_headers(t, rtr, exp);
4712 }
4713 exp = exp->next;
4714 }
4715 return 0;
4716}
4717
4718
4719
4720/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004721 * Manage server-side cookies. It can impact performance by about 2% so it is
4722 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004723 */
4724void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4725{
4726 struct http_txn *txn = &t->txn;
4727 char *p1, *p2, *p3, *p4;
4728
4729 appsess *asession_temp = NULL;
4730 appsess local_asession;
4731
4732 char *cur_ptr, *cur_end, *cur_next;
4733 int cur_idx, old_idx, delta;
4734
Willy Tarreaua15645d2007-03-18 16:22:39 +01004735 /* Iterate through the headers.
4736 * we start with the start line.
4737 */
4738 old_idx = 0;
4739 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4740
4741 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4742 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004743 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004744
4745 cur_hdr = &txn->hdr_idx.v[cur_idx];
4746 cur_ptr = cur_next;
4747 cur_end = cur_ptr + cur_hdr->len;
4748 cur_next = cur_end + cur_hdr->cr + 1;
4749
4750 /* We have one full header between cur_ptr and cur_end, and the
4751 * next header starts at cur_next. We're only interested in
4752 * "Cookie:" headers.
4753 */
4754
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004755 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4756 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004757 old_idx = cur_idx;
4758 continue;
4759 }
4760
4761 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004762 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763
4764
4765 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004766 if (t->be->cookie_name == NULL &&
4767 t->be->appsession_name == NULL &&
4768 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769 return;
4770
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004771 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004772
4773 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004774 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4775 break;
4776
4777 /* p1 is at the beginning of the cookie name */
4778 p2 = p1;
4779
4780 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4781 p2++;
4782
4783 if (p2 == cur_end || *p2 == ';') /* next cookie */
4784 break;
4785
4786 p3 = p2 + 1; /* skip the '=' sign */
4787 if (p3 == cur_end)
4788 break;
4789
4790 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004791 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004792 p4++;
4793
4794 /* here, we have the cookie name between p1 and p2,
4795 * and its value between p3 and p4.
4796 * we can process it.
4797 */
4798
4799 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004800 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004801 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004802 (p4 - p1 >= t->be->capture_namelen) &&
4803 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004804 int log_len = p4 - p1;
4805
Willy Tarreau086b3b42007-05-13 21:45:51 +02004806 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004807 Alert("HTTP logging : out of memory.\n");
4808 }
4809
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004810 if (log_len > t->be->capture_len)
4811 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004812 memcpy(txn->srv_cookie, p1, log_len);
4813 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004814 }
4815
4816 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004817 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4818 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004819 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004820 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004821
4822 /* If the cookie is in insert mode on a known server, we'll delete
4823 * this occurrence because we'll insert another one later.
4824 * We'll delete it too if the "indirect" option is set and we're in
4825 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004826 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4827 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004828 /* this header must be deleted */
4829 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4830 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4831 txn->hdr_idx.used--;
4832 cur_hdr->len = 0;
4833 cur_next += delta;
4834 txn->rsp.eoh += delta;
4835
Willy Tarreau3d300592007-03-18 18:34:41 +01004836 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004837 }
4838 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004839 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840 /* replace bytes p3->p4 with the cookie name associated
4841 * with this server since we know it.
4842 */
4843 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4844 cur_hdr->len += delta;
4845 cur_next += delta;
4846 txn->rsp.eoh += delta;
4847
Willy Tarreau3d300592007-03-18 18:34:41 +01004848 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004849 }
4850 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004851 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004852 /* insert the cookie name associated with this server
4853 * before existing cookie, and insert a delimitor between them..
4854 */
4855 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4856 cur_hdr->len += delta;
4857 cur_next += delta;
4858 txn->rsp.eoh += delta;
4859
4860 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004861 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004862 }
4863 }
4864 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004865 else if ((t->be->appsession_name != NULL) &&
4866 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004867
4868 /* Cool... it's the right one */
4869
4870 size_t server_id_len = strlen(t->srv->id) + 1;
4871 asession_temp = &local_asession;
4872
Willy Tarreau63963c62007-05-13 21:29:55 +02004873 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004874 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4875 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4876 return;
4877 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004878 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4879 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880 asession_temp->serverid = NULL;
4881
4882 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004883 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4884 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004885 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004886 Alert("Not enough Memory process_srv():asession:calloc().\n");
4887 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4888 return;
4889 }
4890 asession_temp->sessid = local_asession.sessid;
4891 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004892 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004893 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4894 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004896 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004897 }
4898
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004900 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004901 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4902 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4903 return;
4904 }
4905 asession_temp->serverid[0] = '\0';
4906 }
4907
4908 if (asession_temp->serverid[0] == '\0')
4909 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4910
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004911 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004912 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004913#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004914 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004915 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004916#endif
4917 }/* end if ((t->proxy->appsession_name != NULL) ... */
4918 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4919 } /* we're now at the end of the cookie value */
4920
4921 /* keep the link from this header to next one */
4922 old_idx = cur_idx;
4923 } /* end of cookie processing on this header */
4924}
4925
4926
4927
4928/*
4929 * Check if response is cacheable or not. Updates t->flags.
4930 */
4931void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4932{
4933 struct http_txn *txn = &t->txn;
4934 char *p1, *p2;
4935
4936 char *cur_ptr, *cur_end, *cur_next;
4937 int cur_idx;
4938
Willy Tarreau5df51872007-11-25 16:20:08 +01004939 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 return;
4941
4942 /* Iterate through the headers.
4943 * we start with the start line.
4944 */
4945 cur_idx = 0;
4946 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4947
4948 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4949 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004950 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004951
4952 cur_hdr = &txn->hdr_idx.v[cur_idx];
4953 cur_ptr = cur_next;
4954 cur_end = cur_ptr + cur_hdr->len;
4955 cur_next = cur_end + cur_hdr->cr + 1;
4956
4957 /* We have one full header between cur_ptr and cur_end, and the
4958 * next header starts at cur_next. We're only interested in
4959 * "Cookie:" headers.
4960 */
4961
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004962 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4963 if (val) {
4964 if ((cur_end - (cur_ptr + val) >= 8) &&
4965 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4966 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4967 return;
4968 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004969 }
4970
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004971 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4972 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004973 continue;
4974
4975 /* OK, right now we know we have a cache-control header at cur_ptr */
4976
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004977 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004978
4979 if (p1 >= cur_end) /* no more info */
4980 continue;
4981
4982 /* p1 is at the beginning of the value */
4983 p2 = p1;
4984
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004985 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004986 p2++;
4987
4988 /* we have a complete value between p1 and p2 */
4989 if (p2 < cur_end && *p2 == '=') {
4990 /* we have something of the form no-cache="set-cookie" */
4991 if ((cur_end - p1 >= 21) &&
4992 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4993 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004994 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995 continue;
4996 }
4997
4998 /* OK, so we know that either p2 points to the end of string or to a comma */
4999 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5000 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5001 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5002 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005003 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005004 return;
5005 }
5006
5007 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005008 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005009 continue;
5010 }
5011 }
5012}
5013
5014
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015/*
5016 * Try to retrieve a known appsession in the URI, then the associated server.
5017 * If the server is found, it's assigned to the session.
5018 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005019void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005020{
Willy Tarreau3d300592007-03-18 18:34:41 +01005021 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005022 appsess *asession_temp = NULL;
5023 appsess local_asession;
5024 char *request_line;
5025
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005026 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005027 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005028 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005029 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005030 return;
5031
5032 /* skip ';' */
5033 request_line++;
5034
5035 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005036 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005037 return;
5038
5039 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005040 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005041
5042 /* First try if we already have an appsession */
5043 asession_temp = &local_asession;
5044
Willy Tarreau63963c62007-05-13 21:29:55 +02005045 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005046 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5047 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5048 return;
5049 }
5050
5051 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005052 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5053 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005054 asession_temp->serverid = NULL;
5055
5056 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005057 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5058 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005059 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005060 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005061 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005062 Alert("Not enough memory process_cli():asession:calloc().\n");
5063 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5064 return;
5065 }
5066 asession_temp->sessid = local_asession.sessid;
5067 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005068 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005069 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005070 }
5071 else {
5072 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005073 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005074 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005075
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005076 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005077 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005078
Willy Tarreau58f10d72006-12-04 02:26:12 +01005079#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005080 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005081 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005082#endif
5083 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005084 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005085 Alert("Found Application Session without matching server.\n");
5086 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005087 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088 while (srv) {
5089 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005090 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005091 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005092 txn->flags &= ~TX_CK_MASK;
5093 txn->flags |= TX_CK_VALID;
5094 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005095 t->srv = srv;
5096 break;
5097 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005098 txn->flags &= ~TX_CK_MASK;
5099 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005100 }
5101 }
5102 srv = srv->next;
5103 }
5104 }
5105}
5106
5107
Willy Tarreaub2513902006-12-17 14:52:38 +01005108/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005109 * In a GET or HEAD request, check if the requested URI matches the stats uri
5110 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005111 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005112 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005113 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005114 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005115 *
5116 * Returns 1 if the session's state changes, otherwise 0.
5117 */
5118int stats_check_uri_auth(struct session *t, struct proxy *backend)
5119{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005120 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005121 struct uri_auth *uri_auth = backend->uri_auth;
5122 struct user_auth *user;
5123 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005124 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005125
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005126 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5127
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005128 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005129 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005130 return 0;
5131
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005132 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005133
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005134 /* the URI is in h */
5135 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005136 return 0;
5137
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005138 h += uri_auth->uri_len;
5139 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5140 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005141 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005142 break;
5143 }
5144 h++;
5145 }
5146
5147 if (uri_auth->refresh) {
5148 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5149 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5150 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005151 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005152 break;
5153 }
5154 h++;
5155 }
5156 }
5157
Willy Tarreau55bb8452007-10-17 18:44:57 +02005158 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5159 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5160 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005161 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005162 break;
5163 }
5164 h++;
5165 }
5166
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005167 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5168
Willy Tarreaub2513902006-12-17 14:52:38 +01005169 /* we are in front of a interceptable URI. Let's check
5170 * if there's an authentication and if it's valid.
5171 */
5172 user = uri_auth->users;
5173 if (!user) {
5174 /* no user auth required, it's OK */
5175 authenticated = 1;
5176 } else {
5177 authenticated = 0;
5178
5179 /* a user list is defined, we have to check.
5180 * skip 21 chars for "Authorization: Basic ".
5181 */
5182
5183 /* FIXME: this should move to an earlier place */
5184 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005185 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5186 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5187 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005188 if (len > 14 &&
5189 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005190 txn->auth_hdr.str = h;
5191 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005192 break;
5193 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005194 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005195 }
5196
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005197 if (txn->auth_hdr.len < 21 ||
5198 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005199 user = NULL;
5200
5201 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005202 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5203 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005204 user->user_pwd, user->user_len)) {
5205 authenticated = 1;
5206 break;
5207 }
5208 user = user->next;
5209 }
5210 }
5211
5212 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005213 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005214
5215 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005216 msg.str = trash;
5217 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005218 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005219 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005220 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005221 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005222 if (!(t->flags & SN_ERR_MASK))
5223 t->flags |= SN_ERR_PRXCOND;
5224 if (!(t->flags & SN_FINST_MASK))
5225 t->flags |= SN_FINST_R;
5226 return 1;
5227 }
5228
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005229 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005230 * data.
5231 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005232 EV_FD_CLR(t->cli_fd, DIR_RD);
5233 buffer_shutr(t->req);
5234 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005235 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005236 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005237 t->data_source = DATA_SRC_STATS;
5238 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005239 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005240 produce_content(t);
5241 return 1;
5242}
5243
5244
Willy Tarreaubaaee002006-06-26 02:48:02 +02005245/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 * Print a debug line with a header
5247 */
5248void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5249{
5250 int len, max;
5251 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5252 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5253 max = end - start;
5254 UBOUND(max, sizeof(trash) - len - 1);
5255 len += strlcpy2(trash + len, start, max + 1);
5256 trash[len++] = '\n';
5257 write(1, trash, len);
5258}
5259
5260
Willy Tarreau8797c062007-05-07 00:55:35 +02005261/************************************************************************/
5262/* The code below is dedicated to ACL parsing and matching */
5263/************************************************************************/
5264
5265
5266
5267
5268/* 1. Check on METHOD
5269 * We use the pre-parsed method if it is known, and store its number as an
5270 * integer. If it is unknown, we use the pointer and the length.
5271 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005272static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005273{
5274 int len, meth;
5275
Willy Tarreauae8b7962007-06-09 23:10:04 +02005276 len = strlen(*text);
5277 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005278
5279 pattern->val.i = meth;
5280 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005281 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005282 if (!pattern->ptr.str)
5283 return 0;
5284 pattern->len = len;
5285 }
5286 return 1;
5287}
5288
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005289static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005290acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5291 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005292{
5293 int meth;
5294 struct http_txn *txn = l7;
5295
Willy Tarreaub6866442008-07-14 23:54:42 +02005296 if (!txn)
5297 return 0;
5298
Willy Tarreauc11416f2007-06-17 16:58:38 +02005299 if (txn->req.msg_state != HTTP_MSG_BODY)
5300 return 0;
5301
Willy Tarreau8797c062007-05-07 00:55:35 +02005302 meth = txn->meth;
5303 test->i = meth;
5304 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005305 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5306 /* ensure the indexes are not affected */
5307 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005308 test->len = txn->req.sl.rq.m_l;
5309 test->ptr = txn->req.sol;
5310 }
5311 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5312 return 1;
5313}
5314
5315static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5316{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005317 int icase;
5318
Willy Tarreau8797c062007-05-07 00:55:35 +02005319 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005320 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005321
5322 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005323 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005324
5325 /* Other method, we must compare the strings */
5326 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005327 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005328
5329 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5330 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5331 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005332 return ACL_PAT_FAIL;
5333 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005334}
5335
5336/* 2. Check on Request/Status Version
5337 * We simply compare strings here.
5338 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005339static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005340{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005341 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005342 if (!pattern->ptr.str)
5343 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005344 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005345 return 1;
5346}
5347
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005348static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005349acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5350 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005351{
5352 struct http_txn *txn = l7;
5353 char *ptr;
5354 int len;
5355
Willy Tarreaub6866442008-07-14 23:54:42 +02005356 if (!txn)
5357 return 0;
5358
Willy Tarreauc11416f2007-06-17 16:58:38 +02005359 if (txn->req.msg_state != HTTP_MSG_BODY)
5360 return 0;
5361
Willy Tarreau8797c062007-05-07 00:55:35 +02005362 len = txn->req.sl.rq.v_l;
5363 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5364
5365 while ((len-- > 0) && (*ptr++ != '/'));
5366 if (len <= 0)
5367 return 0;
5368
5369 test->ptr = ptr;
5370 test->len = len;
5371
5372 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5373 return 1;
5374}
5375
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005376static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005377acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5378 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005379{
5380 struct http_txn *txn = l7;
5381 char *ptr;
5382 int len;
5383
Willy Tarreaub6866442008-07-14 23:54:42 +02005384 if (!txn)
5385 return 0;
5386
Willy Tarreauc11416f2007-06-17 16:58:38 +02005387 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5388 return 0;
5389
Willy Tarreau8797c062007-05-07 00:55:35 +02005390 len = txn->rsp.sl.st.v_l;
5391 ptr = txn->rsp.sol;
5392
5393 while ((len-- > 0) && (*ptr++ != '/'));
5394 if (len <= 0)
5395 return 0;
5396
5397 test->ptr = ptr;
5398 test->len = len;
5399
5400 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5401 return 1;
5402}
5403
5404/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005405static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005406acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5407 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005408{
5409 struct http_txn *txn = l7;
5410 char *ptr;
5411 int len;
5412
Willy Tarreaub6866442008-07-14 23:54:42 +02005413 if (!txn)
5414 return 0;
5415
Willy Tarreauc11416f2007-06-17 16:58:38 +02005416 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5417 return 0;
5418
Willy Tarreau8797c062007-05-07 00:55:35 +02005419 len = txn->rsp.sl.st.c_l;
5420 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5421
5422 test->i = __strl2ui(ptr, len);
5423 test->flags = ACL_TEST_F_VOL_1ST;
5424 return 1;
5425}
5426
5427/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005428static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005429acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5430 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005431{
5432 struct http_txn *txn = l7;
5433
Willy Tarreaub6866442008-07-14 23:54:42 +02005434 if (!txn)
5435 return 0;
5436
Willy Tarreauc11416f2007-06-17 16:58:38 +02005437 if (txn->req.msg_state != HTTP_MSG_BODY)
5438 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005439
Willy Tarreauc11416f2007-06-17 16:58:38 +02005440 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5441 /* ensure the indexes are not affected */
5442 return 0;
5443
Willy Tarreau8797c062007-05-07 00:55:35 +02005444 test->len = txn->req.sl.rq.u_l;
5445 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5446
Willy Tarreauf3d25982007-05-08 22:45:09 +02005447 /* we do not need to set READ_ONLY because the data is in a buffer */
5448 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005449 return 1;
5450}
5451
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005452static int
5453acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5454 struct acl_expr *expr, struct acl_test *test)
5455{
5456 struct http_txn *txn = l7;
5457
Willy Tarreaub6866442008-07-14 23:54:42 +02005458 if (!txn)
5459 return 0;
5460
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005461 if (txn->req.msg_state != HTTP_MSG_BODY)
5462 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005463
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005464 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5465 /* ensure the indexes are not affected */
5466 return 0;
5467
5468 /* Parse HTTP request */
5469 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5470 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5471 test->i = AF_INET;
5472
5473 /*
5474 * If we are parsing url in frontend space, we prepare backend stage
5475 * to not parse again the same url ! optimization lazyness...
5476 */
5477 if (px->options & PR_O_HTTP_PROXY)
5478 l4->flags |= SN_ADDR_SET;
5479
5480 test->flags = ACL_TEST_F_READ_ONLY;
5481 return 1;
5482}
5483
5484static int
5485acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5486 struct acl_expr *expr, struct acl_test *test)
5487{
5488 struct http_txn *txn = l7;
5489
Willy Tarreaub6866442008-07-14 23:54:42 +02005490 if (!txn)
5491 return 0;
5492
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005493 if (txn->req.msg_state != HTTP_MSG_BODY)
5494 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005495
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005496 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5497 /* ensure the indexes are not affected */
5498 return 0;
5499
5500 /* Same optimization as url_ip */
5501 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5502 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5503
5504 if (px->options & PR_O_HTTP_PROXY)
5505 l4->flags |= SN_ADDR_SET;
5506
5507 test->flags = ACL_TEST_F_READ_ONLY;
5508 return 1;
5509}
5510
Willy Tarreauc11416f2007-06-17 16:58:38 +02005511/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5512 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5513 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005514static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005515acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005516 struct acl_expr *expr, struct acl_test *test)
5517{
5518 struct http_txn *txn = l7;
5519 struct hdr_idx *idx = &txn->hdr_idx;
5520 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005521
Willy Tarreaub6866442008-07-14 23:54:42 +02005522 if (!txn)
5523 return 0;
5524
Willy Tarreau33a7e692007-06-10 19:45:56 +02005525 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5526 /* search for header from the beginning */
5527 ctx->idx = 0;
5528
Willy Tarreau33a7e692007-06-10 19:45:56 +02005529 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5530 test->flags |= ACL_TEST_F_FETCH_MORE;
5531 test->flags |= ACL_TEST_F_VOL_HDR;
5532 test->len = ctx->vlen;
5533 test->ptr = (char *)ctx->line + ctx->val;
5534 return 1;
5535 }
5536
5537 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5538 test->flags |= ACL_TEST_F_VOL_HDR;
5539 return 0;
5540}
5541
Willy Tarreau33a7e692007-06-10 19:45:56 +02005542static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005543acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5544 struct acl_expr *expr, struct acl_test *test)
5545{
5546 struct http_txn *txn = l7;
5547
Willy Tarreaub6866442008-07-14 23:54:42 +02005548 if (!txn)
5549 return 0;
5550
Willy Tarreauc11416f2007-06-17 16:58:38 +02005551 if (txn->req.msg_state != HTTP_MSG_BODY)
5552 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005553
Willy Tarreauc11416f2007-06-17 16:58:38 +02005554 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5555 /* ensure the indexes are not affected */
5556 return 0;
5557
5558 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5559}
5560
5561static int
5562acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5563 struct acl_expr *expr, struct acl_test *test)
5564{
5565 struct http_txn *txn = l7;
5566
Willy Tarreaub6866442008-07-14 23:54:42 +02005567 if (!txn)
5568 return 0;
5569
Willy Tarreauc11416f2007-06-17 16:58:38 +02005570 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5571 return 0;
5572
5573 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5574}
5575
5576/* 6. Check on HTTP header count. The number of occurrences is returned.
5577 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5578 */
5579static int
5580acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005581 struct acl_expr *expr, struct acl_test *test)
5582{
5583 struct http_txn *txn = l7;
5584 struct hdr_idx *idx = &txn->hdr_idx;
5585 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005586 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005587
Willy Tarreaub6866442008-07-14 23:54:42 +02005588 if (!txn)
5589 return 0;
5590
Willy Tarreau33a7e692007-06-10 19:45:56 +02005591 ctx.idx = 0;
5592 cnt = 0;
5593 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5594 cnt++;
5595
5596 test->i = cnt;
5597 test->flags = ACL_TEST_F_VOL_HDR;
5598 return 1;
5599}
5600
Willy Tarreauc11416f2007-06-17 16:58:38 +02005601static int
5602acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5603 struct acl_expr *expr, struct acl_test *test)
5604{
5605 struct http_txn *txn = l7;
5606
Willy Tarreaub6866442008-07-14 23:54:42 +02005607 if (!txn)
5608 return 0;
5609
Willy Tarreauc11416f2007-06-17 16:58:38 +02005610 if (txn->req.msg_state != HTTP_MSG_BODY)
5611 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005612
Willy Tarreauc11416f2007-06-17 16:58:38 +02005613 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5614 /* ensure the indexes are not affected */
5615 return 0;
5616
5617 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5618}
5619
5620static int
5621acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5622 struct acl_expr *expr, struct acl_test *test)
5623{
5624 struct http_txn *txn = l7;
5625
Willy Tarreaub6866442008-07-14 23:54:42 +02005626 if (!txn)
5627 return 0;
5628
Willy Tarreauc11416f2007-06-17 16:58:38 +02005629 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5630 return 0;
5631
5632 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5633}
5634
Willy Tarreau33a7e692007-06-10 19:45:56 +02005635/* 7. Check on HTTP header's integer value. The integer value is returned.
5636 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005637 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005638 */
5639static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005640acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005641 struct acl_expr *expr, struct acl_test *test)
5642{
5643 struct http_txn *txn = l7;
5644 struct hdr_idx *idx = &txn->hdr_idx;
5645 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005646
Willy Tarreaub6866442008-07-14 23:54:42 +02005647 if (!txn)
5648 return 0;
5649
Willy Tarreau33a7e692007-06-10 19:45:56 +02005650 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5651 /* search for header from the beginning */
5652 ctx->idx = 0;
5653
Willy Tarreau33a7e692007-06-10 19:45:56 +02005654 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5655 test->flags |= ACL_TEST_F_FETCH_MORE;
5656 test->flags |= ACL_TEST_F_VOL_HDR;
5657 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5658 return 1;
5659 }
5660
5661 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5662 test->flags |= ACL_TEST_F_VOL_HDR;
5663 return 0;
5664}
5665
Willy Tarreauc11416f2007-06-17 16:58:38 +02005666static int
5667acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5668 struct acl_expr *expr, struct acl_test *test)
5669{
5670 struct http_txn *txn = l7;
5671
Willy Tarreaub6866442008-07-14 23:54:42 +02005672 if (!txn)
5673 return 0;
5674
Willy Tarreauc11416f2007-06-17 16:58:38 +02005675 if (txn->req.msg_state != HTTP_MSG_BODY)
5676 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005677
Willy Tarreauc11416f2007-06-17 16:58:38 +02005678 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5679 /* ensure the indexes are not affected */
5680 return 0;
5681
5682 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5683}
5684
5685static int
5686acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5687 struct acl_expr *expr, struct acl_test *test)
5688{
5689 struct http_txn *txn = l7;
5690
Willy Tarreaub6866442008-07-14 23:54:42 +02005691 if (!txn)
5692 return 0;
5693
Willy Tarreauc11416f2007-06-17 16:58:38 +02005694 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5695 return 0;
5696
5697 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5698}
5699
Willy Tarreau737b0c12007-06-10 21:28:46 +02005700/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5701 * the first '/' after the possible hostname, and ends before the possible '?'.
5702 */
5703static int
5704acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5705 struct acl_expr *expr, struct acl_test *test)
5706{
5707 struct http_txn *txn = l7;
5708 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005709
Willy Tarreaub6866442008-07-14 23:54:42 +02005710 if (!txn)
5711 return 0;
5712
Willy Tarreauc11416f2007-06-17 16:58:38 +02005713 if (txn->req.msg_state != HTTP_MSG_BODY)
5714 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005715
Willy Tarreauc11416f2007-06-17 16:58:38 +02005716 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5717 /* ensure the indexes are not affected */
5718 return 0;
5719
Willy Tarreau21d2af32008-02-14 20:25:24 +01005720 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5721 ptr = http_get_path(txn);
5722 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005723 return 0;
5724
5725 /* OK, we got the '/' ! */
5726 test->ptr = ptr;
5727
5728 while (ptr < end && *ptr != '?')
5729 ptr++;
5730
5731 test->len = ptr - test->ptr;
5732
5733 /* we do not need to set READ_ONLY because the data is in a buffer */
5734 test->flags = ACL_TEST_F_VOL_1ST;
5735 return 1;
5736}
5737
5738
Willy Tarreau8797c062007-05-07 00:55:35 +02005739
5740/************************************************************************/
5741/* All supported keywords must be declared here. */
5742/************************************************************************/
5743
5744/* Note: must not be declared <const> as its list will be overwritten */
5745static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005746 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5747 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5748 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5749 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005750
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005751 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5752 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5753 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5754 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5755 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5756 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5757 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5758 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5759 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005760
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005761 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5762 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5763 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5764 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5765 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5766 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5767 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5768 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5769 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5770 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005771
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005772 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5773 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5774 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5775 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5776 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5777 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5778 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5779 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5780 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005781
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005782 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5783 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5784 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5785 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5786 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5787 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5788 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005789
Willy Tarreauf3d25982007-05-08 22:45:09 +02005790 { NULL, NULL, NULL, NULL },
5791
5792#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005793 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5794 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5795 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5796 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5797 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5798 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5799 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5800
Willy Tarreau8797c062007-05-07 00:55:35 +02005801 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5802 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5803 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5804 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5805 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5806 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5807 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5808 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5809
5810 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5811 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5812 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5813 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5814 { NULL, NULL, NULL, NULL },
5815#endif
5816}};
5817
5818
5819__attribute__((constructor))
5820static void __http_protocol_init(void)
5821{
5822 acl_register_keywords(&acl_kws);
5823}
5824
5825
Willy Tarreau58f10d72006-12-04 02:26:12 +01005826/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005827 * Local variables:
5828 * c-indent-level: 8
5829 * c-basic-offset: 8
5830 * End:
5831 */