blob: 6802b5ae656280fc9beb370a5fc7ced1089456c0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200549 buffer_shutw(t->req);
550 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaudafde432008-08-17 01:00:46 +0200648/* Processes the client, server, request and response jobs of a session task,
649 * then puts it back to the wait queue in a clean state, or cleans up its
650 * resources if it must be deleted. Returns in <next> the date the task wants
651 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
652 * nothing too many times, the request and response buffers flags are monitored
653 * and each function is called only if at least another function has changed at
654 * least one flag. If one of the functions called returns non-zero, then it
655 * will be called once again after all other functions. This permits explicit
656 * external loops which may be useful for complex state machines.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 */
Willy Tarreaudafde432008-08-17 01:00:46 +0200658#define PROCESS_CLI 0x1
659#define PROCESS_SRV 0x2
660#define PROCESS_REQ 0x4
661#define PROCESS_RTR 0x8
662#define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR)
663
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200664void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
666 struct session *s = t->context;
Willy Tarreaudafde432008-08-17 01:00:46 +0200667 unsigned resync = PROCESS_ALL;
668 unsigned int rqf;
669 unsigned int rpf;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
Willy Tarreau507385d2008-08-17 13:04:25 +0200671 /* check timeout expiration only once and adjust buffer flags
672 * accordingly.
673 */
674 if (unlikely(tick_is_expired(t->expire, now_ms))) {
675 if (tick_is_expired(s->req->rex, now_ms))
676 s->req->flags |= BF_READ_TIMEOUT;
677
678 if (tick_is_expired(s->req->wex, now_ms))
679 s->req->flags |= BF_WRITE_TIMEOUT;
680
681 if (tick_is_expired(s->rep->rex, now_ms))
682 s->rep->flags |= BF_READ_TIMEOUT;
683
684 if (tick_is_expired(s->rep->wex, now_ms))
685 s->rep->flags |= BF_WRITE_TIMEOUT;
686 }
687
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 do {
Willy Tarreaudafde432008-08-17 01:00:46 +0200689 if (resync & PROCESS_REQ) {
690 resync &= ~PROCESS_REQ;
691 if (s->analysis & AN_REQ_ANY) {
692 rqf = s->req->flags;
693 rpf = s->rep->flags;
694
695 if (process_request(s))
696 resync |= PROCESS_REQ;
697
698 if (!(s->analysis & AN_REQ_ANY) && !(s->req->flags & BF_MAY_FORWARD))
699 s->req->flags |= BF_MAY_FORWARD;
700
701 if (rqf != s->req->flags || rpf != s->rep->flags)
702 resync |= PROCESS_ALL & ~PROCESS_REQ;
703 }
704 }
705
706 if (resync & PROCESS_RTR) {
707 resync &= ~PROCESS_RTR;
708 if (s->analysis & AN_RTR_ANY) {
709 rqf = s->req->flags;
710 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200711
Willy Tarreaudafde432008-08-17 01:00:46 +0200712 if (process_response(s))
713 resync |= PROCESS_RTR;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200714
Willy Tarreaudafde432008-08-17 01:00:46 +0200715 if (!(s->analysis & AN_RTR_ANY) && !(s->rep->flags & BF_MAY_FORWARD))
716 s->rep->flags |= BF_MAY_FORWARD;
717
718 if (rqf != s->req->flags || rpf != s->rep->flags)
719 resync |= PROCESS_ALL & ~PROCESS_RTR;
720 }
721 }
722
723 if (resync & PROCESS_CLI) {
724 rqf = s->req->flags;
725 rpf = s->rep->flags;
726
727 resync &= ~PROCESS_CLI;
728 if (process_cli(s))
729 resync |= PROCESS_CLI;
730
731 if (rqf != s->req->flags || rpf != s->rep->flags)
732 resync |= PROCESS_ALL & ~PROCESS_CLI;
733 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200734
Willy Tarreaudafde432008-08-17 01:00:46 +0200735 if (resync & PROCESS_SRV) {
736 rqf = s->req->flags;
737 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200738
Willy Tarreaudafde432008-08-17 01:00:46 +0200739 resync &= ~PROCESS_SRV;
740 if (process_srv(s))
741 resync |= PROCESS_SRV;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200742
Willy Tarreaudafde432008-08-17 01:00:46 +0200743 if (rqf != s->req->flags || rpf != s->rep->flags)
744 resync |= PROCESS_ALL & ~PROCESS_SRV;
745 }
746 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200748 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100749
750 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
751 session_process_counters(s);
752
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200753 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
754 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreau7f875f62008-08-11 17:35:01 +0200756 /* Trick: if a request is being waiting for the server to respond,
757 * and if we know the server can timeout, we don't want the timeout
758 * to expire on the client side first, but we're still interested
759 * in passing data from the client to the server (eg: POST). Thus,
760 * we can cancel the client's request timeout if the server's
761 * request timeout is set and the server has not yet sent a response.
762 */
763
Willy Tarreauba392ce2008-08-16 21:13:23 +0200764 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200765 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200766 s->req->rex = TICK_ETERNITY;
767
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200768 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
769 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200770 if (s->analysis & AN_REQ_ANY) {
771 if (s->analysis & AN_REQ_INSPECT)
772 t->expire = tick_first(t->expire, s->inspect_exp);
773 else if (s->analysis & AN_REQ_HTTP_HDR)
774 t->expire = tick_first(t->expire, s->txn.exp);
775 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776
777 /* restore t to its place in the task list */
778 task_queue(t);
779
Willy Tarreaua7c52762008-08-16 18:40:18 +0200780#ifdef DEBUG_DEV
781 /* this may only happen when no timeout is set or in case of an FSM bug */
782 if (!t->expire)
783 ABORT_NOW();
784#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200785 *next = t->expire;
786 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
788
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100789 s->fe->feconn--;
790 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200791 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 actconn--;
793
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200794 if (unlikely((global.mode & MODE_DEBUG) &&
795 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200797 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 s->uniq_id, s->be->id,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200799 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd,
800 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 write(1, trash, len);
802 }
803
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200804 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100805 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806
807 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200808 if (s->logs.logwait &&
809 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200810 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
811 if (s->fe->to_log & LW_REQ)
812 http_sess_log(s);
813 else
814 tcp_sess_log(s);
815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816
817 /* the task MUST not be in the run queue anymore */
818 task_delete(t);
819 session_free(s);
820 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200821 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822}
823
824
Willy Tarreau42250582007-04-01 01:30:43 +0200825extern const char sess_term_cond[8];
826extern const char sess_fin_state[8];
827extern const char *monthname[12];
828const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
829const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
830 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
831 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200832struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200833struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834
Willy Tarreau42250582007-04-01 01:30:43 +0200835/*
836 * send a log for the session when we have enough info about it.
837 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100838 */
Willy Tarreau42250582007-04-01 01:30:43 +0200839static void http_sess_log(struct session *s)
840{
841 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
842 struct proxy *fe = s->fe;
843 struct proxy *be = s->be;
844 struct proxy *prx_log;
845 struct http_txn *txn = &s->txn;
846 int tolog;
847 char *uri, *h;
848 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200849 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200850 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200851 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200852 int hdr;
853
854 if (fe->logfac1 < 0 && fe->logfac2 < 0)
855 return;
856 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100857
Willy Tarreau42250582007-04-01 01:30:43 +0200858 if (s->cli_addr.ss_family == AF_INET)
859 inet_ntop(AF_INET,
860 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
861 pn, sizeof(pn));
862 else
863 inet_ntop(AF_INET6,
864 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
865 pn, sizeof(pn));
866
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200867 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200868
869 /* FIXME: let's limit ourselves to frontend logging for now. */
870 tolog = fe->to_log;
871
872 h = tmpline;
873 if (fe->to_log & LW_REQHDR &&
874 txn->req.cap &&
875 (h < tmpline + sizeof(tmpline) - 10)) {
876 *(h++) = ' ';
877 *(h++) = '{';
878 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
879 if (hdr)
880 *(h++) = '|';
881 if (txn->req.cap[hdr] != NULL)
882 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
883 '#', hdr_encode_map, txn->req.cap[hdr]);
884 }
885 *(h++) = '}';
886 }
887
888 if (fe->to_log & LW_RSPHDR &&
889 txn->rsp.cap &&
890 (h < tmpline + sizeof(tmpline) - 7)) {
891 *(h++) = ' ';
892 *(h++) = '{';
893 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
894 if (hdr)
895 *(h++) = '|';
896 if (txn->rsp.cap[hdr] != NULL)
897 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
898 '#', hdr_encode_map, txn->rsp.cap[hdr]);
899 }
900 *(h++) = '}';
901 }
902
903 if (h < tmpline + sizeof(tmpline) - 4) {
904 *(h++) = ' ';
905 *(h++) = '"';
906 uri = txn->uri ? txn->uri : "<BADREQ>";
907 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
908 '#', url_encode_map, uri);
909 *(h++) = '"';
910 }
911 *h = '\0';
912
913 svid = (tolog & LW_SVID) ?
914 (s->data_source != DATA_SRC_STATS) ?
915 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
916
Willy Tarreau70089872008-06-13 21:12:51 +0200917 t_request = -1;
918 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
919 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
920
Willy Tarreau42250582007-04-01 01:30:43 +0200921 send_log(prx_log, LOG_INFO,
922 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
923 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100924 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200925 pn,
926 (s->cli_addr.ss_family == AF_INET) ?
927 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
928 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200929 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200930 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200931 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200932 t_request,
933 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200934 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
935 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
936 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
937 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100938 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200939 txn->cli_cookie ? txn->cli_cookie : "-",
940 txn->srv_cookie ? txn->srv_cookie : "-",
941 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
942 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
943 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
944 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
945 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100946 (s->flags & SN_REDISP)?"+":"",
947 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200948 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
949
950 s->logs.logwait = 0;
951}
952
Willy Tarreau117f59e2007-03-04 18:17:17 +0100953
954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001043const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1044 unsigned int state, const char *ptr, const char *end,
1045 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
1047 __label__
1048 http_msg_rpver,
1049 http_msg_rpver_sp,
1050 http_msg_rpcode,
1051 http_msg_rpcode_sp,
1052 http_msg_rpreason,
1053 http_msg_rpline_eol,
1054 http_msg_ood, /* out of data */
1055 http_msg_invalid;
1056
1057 switch (state) {
1058 http_msg_rpver:
1059 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001060 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1062
1063 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001064 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001065 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1066 }
1067 goto http_msg_invalid;
1068
1069 http_msg_rpver_sp:
1070 case HTTP_MSG_RPVER_SP:
1071 if (likely(!HTTP_IS_LWS(*ptr))) {
1072 msg->sl.st.c = ptr - msg_buf;
1073 goto http_msg_rpcode;
1074 }
1075 if (likely(HTTP_IS_SPHT(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1077 /* so it's a CR/LF, this is invalid */
1078 goto http_msg_invalid;
1079
1080 http_msg_rpcode:
1081 case HTTP_MSG_RPCODE:
1082 if (likely(!HTTP_IS_LWS(*ptr)))
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1084
1085 if (likely(HTTP_IS_SPHT(*ptr))) {
1086 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1087 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1088 }
1089
1090 /* so it's a CR/LF, so there is no reason phrase */
1091 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1092 http_msg_rsp_reason:
1093 /* FIXME: should we support HTTP responses without any reason phrase ? */
1094 msg->sl.st.r = ptr - msg_buf;
1095 msg->sl.st.r_l = 0;
1096 goto http_msg_rpline_eol;
1097
1098 http_msg_rpcode_sp:
1099 case HTTP_MSG_RPCODE_SP:
1100 if (likely(!HTTP_IS_LWS(*ptr))) {
1101 msg->sl.st.r = ptr - msg_buf;
1102 goto http_msg_rpreason;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1106 /* so it's a CR/LF, so there is no reason phrase */
1107 goto http_msg_rsp_reason;
1108
1109 http_msg_rpreason:
1110 case HTTP_MSG_RPREASON:
1111 if (likely(!HTTP_IS_CRLF(*ptr)))
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1113 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1114 http_msg_rpline_eol:
1115 /* We have seen the end of line. Note that we do not
1116 * necessarily have the \n yet, but at least we know that we
1117 * have EITHER \r OR \n, otherwise the response would not be
1118 * complete. We can then record the response length and return
1119 * to the caller which will be able to register it.
1120 */
1121 msg->sl.st.l = ptr - msg->sol;
1122 return ptr;
1123
1124#ifdef DEBUG_FULL
1125 default:
1126 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1127 exit(1);
1128#endif
1129 }
1130
1131 http_msg_ood:
1132 /* out of data */
1133 if (ret_state)
1134 *ret_state = state;
1135 if (ret_ptr)
1136 *ret_ptr = (char *)ptr;
1137 return NULL;
1138
1139 http_msg_invalid:
1140 /* invalid message */
1141 if (ret_state)
1142 *ret_state = HTTP_MSG_ERROR;
1143 return NULL;
1144}
1145
1146
1147/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 * This function parses a request line between <ptr> and <end>, starting with
1149 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1150 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1151 * will give undefined results.
1152 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1153 * and that msg->sol points to the beginning of the request.
1154 * If a complete line is found (which implies that at least one CR or LF is
1155 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1156 * returned indicating an incomplete line (which does not mean that parts have
1157 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1158 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1159 * upon next call.
1160 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001161 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1163 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001164 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001166const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1167 unsigned int state, const char *ptr, const char *end,
1168 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 __label__
1171 http_msg_rqmeth,
1172 http_msg_rqmeth_sp,
1173 http_msg_rquri,
1174 http_msg_rquri_sp,
1175 http_msg_rqver,
1176 http_msg_rqline_eol,
1177 http_msg_ood, /* out of data */
1178 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 switch (state) {
1181 http_msg_rqmeth:
1182 case HTTP_MSG_RQMETH:
1183 if (likely(HTTP_IS_TOKEN(*ptr)))
1184 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001187 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1189 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001191 if (likely(HTTP_IS_CRLF(*ptr))) {
1192 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001193 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_req09_uri:
1195 msg->sl.rq.u = ptr - msg_buf;
1196 http_msg_req09_uri_e:
1197 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1198 http_msg_req09_ver:
1199 msg->sl.rq.v = ptr - msg_buf;
1200 msg->sl.rq.v_l = 0;
1201 goto http_msg_rqline_eol;
1202 }
1203 goto http_msg_invalid;
1204
1205 http_msg_rqmeth_sp:
1206 case HTTP_MSG_RQMETH_SP:
1207 if (likely(!HTTP_IS_LWS(*ptr))) {
1208 msg->sl.rq.u = ptr - msg_buf;
1209 goto http_msg_rquri;
1210 }
1211 if (likely(HTTP_IS_SPHT(*ptr)))
1212 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1213 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1214 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001215
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 http_msg_rquri:
1217 case HTTP_MSG_RQURI:
1218 if (likely(!HTTP_IS_LWS(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001220
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221 if (likely(HTTP_IS_SPHT(*ptr))) {
1222 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1227 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 http_msg_rquri_sp:
1230 case HTTP_MSG_RQURI_SP:
1231 if (likely(!HTTP_IS_LWS(*ptr))) {
1232 msg->sl.rq.v = ptr - msg_buf;
1233 goto http_msg_rqver;
1234 }
1235 if (likely(HTTP_IS_SPHT(*ptr)))
1236 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1237 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1238 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 http_msg_rqver:
1241 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001242 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244
1245 if (likely(HTTP_IS_CRLF(*ptr))) {
1246 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1247 http_msg_rqline_eol:
1248 /* We have seen the end of line. Note that we do not
1249 * necessarily have the \n yet, but at least we know that we
1250 * have EITHER \r OR \n, otherwise the request would not be
1251 * complete. We can then record the request length and return
1252 * to the caller which will be able to register it.
1253 */
1254 msg->sl.rq.l = ptr - msg->sol;
1255 return ptr;
1256 }
1257
1258 /* neither an HTTP_VER token nor a CRLF */
1259 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261#ifdef DEBUG_FULL
1262 default:
1263 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1264 exit(1);
1265#endif
1266 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 http_msg_ood:
1269 /* out of data */
1270 if (ret_state)
1271 *ret_state = state;
1272 if (ret_ptr)
1273 *ret_ptr = (char *)ptr;
1274 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 http_msg_invalid:
1277 /* invalid message */
1278 if (ret_state)
1279 *ret_state = HTTP_MSG_ERROR;
1280 return NULL;
1281}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
1283
Willy Tarreau8973c702007-01-21 23:58:29 +01001284/*
1285 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 * when data are missing and recalled at the exact same location with no
1288 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1290 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1293{
1294 __label__
1295 http_msg_rqbefore,
1296 http_msg_rqbefore_cr,
1297 http_msg_rqmeth,
1298 http_msg_rqline_end,
1299 http_msg_hdr_first,
1300 http_msg_hdr_name,
1301 http_msg_hdr_l1_sp,
1302 http_msg_hdr_l1_lf,
1303 http_msg_hdr_l1_lws,
1304 http_msg_hdr_val,
1305 http_msg_hdr_l2_lf,
1306 http_msg_hdr_l2_lws,
1307 http_msg_complete_header,
1308 http_msg_last_lf,
1309 http_msg_ood, /* out of data */
1310 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001311
Willy Tarreaue69eada2008-01-27 00:34:10 +01001312 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001315 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 ptr = buf->lr;
1317 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (unlikely(ptr >= end))
1320 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 /*
1324 * First, states that are specific to the response only.
1325 * We check them first so that request and headers are
1326 * closer to each other (accessed more often).
1327 */
1328 http_msg_rpbefore:
1329 case HTTP_MSG_RPBEFORE:
1330 if (likely(HTTP_IS_TOKEN(*ptr))) {
1331 if (likely(ptr == buf->data)) {
1332 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001333 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 } else {
1335#if PARSE_PRESERVE_EMPTY_LINES
1336 /* only skip empty leading lines, don't remove them */
1337 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001338 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001339#else
1340 /* Remove empty leading lines, as recommended by
1341 * RFC2616. This takes a lot of time because we
1342 * must move all the buffer backwards, but this
1343 * is rarely needed. The method above will be
1344 * cleaner when we'll be able to start sending
1345 * the request from any place in the buffer.
1346 */
1347 buf->lr = ptr;
1348 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001349 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 msg->sol = buf->data;
1351 ptr = buf->data;
1352 end = buf->r;
1353#endif
1354 }
1355 hdr_idx_init(idx);
1356 state = HTTP_MSG_RPVER;
1357 goto http_msg_rpver;
1358 }
1359
1360 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1361 goto http_msg_invalid;
1362
1363 if (unlikely(*ptr == '\n'))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1365 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1366 /* stop here */
1367
1368 http_msg_rpbefore_cr:
1369 case HTTP_MSG_RPBEFORE_CR:
1370 EXPECT_LF_HERE(ptr, http_msg_invalid);
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1372 /* stop here */
1373
1374 http_msg_rpver:
1375 case HTTP_MSG_RPVER:
1376 case HTTP_MSG_RPVER_SP:
1377 case HTTP_MSG_RPCODE:
1378 case HTTP_MSG_RPCODE_SP:
1379 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001380 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001381 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 if (unlikely(!ptr))
1383 return;
1384
1385 /* we have a full response and we know that we have either a CR
1386 * or an LF at <ptr>.
1387 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001388 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1390
1391 msg->sol = ptr;
1392 if (likely(*ptr == '\r'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1394 goto http_msg_rpline_end;
1395
1396 http_msg_rpline_end:
1397 case HTTP_MSG_RPLINE_END:
1398 /* msg->sol must point to the first of CR or LF. */
1399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1401 /* stop here */
1402
1403 /*
1404 * Second, states that are specific to the request only
1405 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 http_msg_rqbefore:
1407 case HTTP_MSG_RQBEFORE:
1408 if (likely(HTTP_IS_TOKEN(*ptr))) {
1409 if (likely(ptr == buf->data)) {
1410 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 } else {
1413#if PARSE_PRESERVE_EMPTY_LINES
1414 /* only skip empty leading lines, don't remove them */
1415 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001416 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 /* Remove empty leading lines, as recommended by
1419 * RFC2616. This takes a lot of time because we
1420 * must move all the buffer backwards, but this
1421 * is rarely needed. The method above will be
1422 * cleaner when we'll be able to start sending
1423 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 buf->lr = ptr;
1426 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001427 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 msg->sol = buf->data;
1429 ptr = buf->data;
1430 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001433 /* we will need this when keep-alive will be supported
1434 hdr_idx_init(idx);
1435 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 state = HTTP_MSG_RQMETH;
1437 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1441 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (unlikely(*ptr == '\n'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1445 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 http_msg_rqbefore_cr:
1449 case HTTP_MSG_RQBEFORE_CR:
1450 EXPECT_LF_HERE(ptr, http_msg_invalid);
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 http_msg_rqmeth:
1455 case HTTP_MSG_RQMETH:
1456 case HTTP_MSG_RQMETH_SP:
1457 case HTTP_MSG_RQURI:
1458 case HTTP_MSG_RQURI_SP:
1459 case HTTP_MSG_RQVER:
1460 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001461 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(!ptr))
1463 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* we have a full request and we know that we have either a CR
1466 * or an LF at <ptr>.
1467 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 msg->sol = ptr;
1472 if (likely(*ptr == '\r'))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 http_msg_rqline_end:
1477 case HTTP_MSG_RQLINE_END:
1478 /* check for HTTP/0.9 request : no version information available.
1479 * msg->sol must point to the first of CR or LF.
1480 */
1481 if (unlikely(msg->sl.rq.v_l == 0))
1482 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 /*
1489 * Common states below
1490 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_hdr_first:
1492 case HTTP_MSG_HDR_FIRST:
1493 msg->sol = ptr;
1494 if (likely(!HTTP_IS_CRLF(*ptr))) {
1495 goto http_msg_hdr_name;
1496 }
1497
1498 if (likely(*ptr == '\r'))
1499 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1500 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 http_msg_hdr_name:
1503 case HTTP_MSG_HDR_NAME:
1504 /* assumes msg->sol points to the first char */
1505 if (likely(HTTP_IS_TOKEN(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(*ptr == ':')) {
1509 msg->col = ptr - buf->data;
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 http_msg_hdr_l1_sp:
1516 case HTTP_MSG_HDR_L1_SP:
1517 /* assumes msg->sol points to the first char and msg->col to the colon */
1518 if (likely(HTTP_IS_SPHT(*ptr)))
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 /* header value can be basically anything except CR/LF */
1522 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (likely(!HTTP_IS_CRLF(*ptr))) {
1525 goto http_msg_hdr_val;
1526 }
1527
1528 if (likely(*ptr == '\r'))
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1530 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 http_msg_hdr_l1_lf:
1533 case HTTP_MSG_HDR_L1_LF:
1534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_hdr_l1_lws:
1538 case HTTP_MSG_HDR_L1_LWS:
1539 if (likely(HTTP_IS_SPHT(*ptr))) {
1540 /* replace HT,CR,LF with spaces */
1541 for (; buf->data+msg->sov < ptr; msg->sov++)
1542 buf->data[msg->sov] = ' ';
1543 goto http_msg_hdr_l1_sp;
1544 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001545 /* we had a header consisting only in spaces ! */
1546 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 goto http_msg_complete_header;
1548
1549 http_msg_hdr_val:
1550 case HTTP_MSG_HDR_VAL:
1551 /* assumes msg->sol points to the first char, msg->col to the
1552 * colon, and msg->sov points to the first character of the
1553 * value.
1554 */
1555 if (likely(!HTTP_IS_CRLF(*ptr)))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 msg->eol = ptr;
1559 /* Note: we could also copy eol into ->eoh so that we have the
1560 * real header end in case it ends with lots of LWS, but is this
1561 * really needed ?
1562 */
1563 if (likely(*ptr == '\r'))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1565 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 http_msg_hdr_l2_lf:
1568 case HTTP_MSG_HDR_L2_LF:
1569 EXPECT_LF_HERE(ptr, http_msg_invalid);
1570 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 http_msg_hdr_l2_lws:
1573 case HTTP_MSG_HDR_L2_LWS:
1574 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1575 /* LWS: replace HT,CR,LF with spaces */
1576 for (; msg->eol < ptr; msg->eol++)
1577 *msg->eol = ' ';
1578 goto http_msg_hdr_val;
1579 }
1580 http_msg_complete_header:
1581 /*
1582 * It was a new header, so the last one is finished.
1583 * Assumes msg->sol points to the first char, msg->col to the
1584 * colon, msg->sov points to the first character of the value
1585 * and msg->eol to the first CR or LF so we know how the line
1586 * ends. We insert last header into the index.
1587 */
1588 /*
1589 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1590 write(2, msg->sol, msg->eol-msg->sol);
1591 fprintf(stderr,"\n");
1592 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1595 idx, idx->tail) < 0))
1596 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 msg->sol = ptr;
1599 if (likely(!HTTP_IS_CRLF(*ptr))) {
1600 goto http_msg_hdr_name;
1601 }
1602
1603 if (likely(*ptr == '\r'))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1605 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 http_msg_last_lf:
1608 case HTTP_MSG_LAST_LF:
1609 /* Assumes msg->sol points to the first of either CR or LF */
1610 EXPECT_LF_HERE(ptr, http_msg_invalid);
1611 ptr++;
1612 buf->lr = ptr;
1613 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001614 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 return;
1616#ifdef DEBUG_FULL
1617 default:
1618 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1619 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001620#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 }
1622 http_msg_ood:
1623 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 buf->lr = ptr;
1626 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 http_msg_invalid:
1629 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001630 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 return;
1632}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001633
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001634/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001635 * It normally returns zero, but may return 1 if it absolutely needs to be
1636 * called again after other functions. It relies on buffers flags, and updates
1637 * t->analysis. It might make sense to explode it into several other functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001639int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 struct buffer *req = t->req;
1642 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001643
Willy Tarreaue46ab552008-08-13 19:19:37 +02001644 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
1645 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001646 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001647 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1648 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001649 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaudafde432008-08-17 01:00:46 +02001651 update_state:
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001652 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001653 struct tcp_rule *rule;
1654 int partial;
1655
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001656 /* We will abort if we encounter a read error. In theory, we
1657 * should not abort if we get a close, it might be valid,
1658 * although very unlikely. FIXME: we'll abort for now, this
1659 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001660 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001661 if (req->flags & BF_READ_ERROR) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001662 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001663 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001664 t->fe->failed_req++;
1665 if (!(t->flags & SN_ERR_MASK))
1666 t->flags |= SN_ERR_CLICL;
1667 if (!(t->flags & SN_FINST_MASK))
1668 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001669 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001670 }
1671
1672 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001673 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001674 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001675 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001676 t->fe->failed_req++;
1677 if (!(t->flags & SN_ERR_MASK))
1678 t->flags |= SN_ERR_CLITO;
1679 if (!(t->flags & SN_FINST_MASK))
1680 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001681 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001682 }
1683
1684 /* We don't know whether we have enough data, so must proceed
1685 * this way :
1686 * - iterate through all rules in their declaration order
1687 * - if one rule returns MISS, it means the inspect delay is
1688 * not over yet, then return immediately, otherwise consider
1689 * it as a non-match.
1690 * - if one rule returns OK, then return OK
1691 * - if one rule returns KO, then return KO
1692 */
1693
Willy Tarreauba392ce2008-08-16 21:13:23 +02001694 if (req->flags & (BF_READ_NULL | BF_SHUTR) ||
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001695 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001696 partial = 0;
1697 else
1698 partial = ACL_PARTIAL;
1699
1700 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1701 int ret = ACL_PAT_PASS;
1702
1703 if (rule->cond) {
1704 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1705 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001706 /* just set the request timeout once at the beginning of the request */
1707 if (!tick_isset(t->inspect_exp))
1708 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001709 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001710 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001711
Willy Tarreaub6866442008-07-14 23:54:42 +02001712 ret = acl_pass(ret);
1713 if (rule->cond->pol == ACL_COND_UNLESS)
1714 ret = !ret;
1715 }
1716
1717 if (ret) {
1718 /* we have a matching rule. */
1719 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02001720 buffer_shutr(req);
1721 buffer_shutw(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001722 fd_delete(t->cli_fd);
1723 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001724 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001725 t->fe->failed_req++;
1726 if (!(t->flags & SN_ERR_MASK))
1727 t->flags |= SN_ERR_PRXCOND;
1728 if (!(t->flags & SN_FINST_MASK))
1729 t->flags |= SN_FINST_R;
1730 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02001731 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001732 }
1733 /* otherwise accept */
1734 break;
1735 }
1736 }
1737
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001738 /* if we get there, it means we have no rule which matches, or
1739 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001740 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001741 t->analysis &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001743 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001744
1745 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 /*
1747 * Now parse the partial (or complete) lines.
1748 * We will check the request syntax, and also join multi-line
1749 * headers. An index of all the lines will be elaborated while
1750 * parsing.
1751 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001752 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 * req->data + req->eoh = end of processed headers / start of current one
1757 * req->data + req->eol = end of current header or line (LF or CRLF)
1758 * req->lr = first non-visited byte
1759 * req->r = end of data
1760 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001763 struct http_txn *txn = &t->txn;
1764 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001768 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 /* 1: we might have to print this header in debug mode */
1771 if (unlikely((global.mode & MODE_DEBUG) &&
1772 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001773 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001776 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 eol = sol + msg->sl.rq.l;
1778 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001779
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001780 sol += hdr_idx_first_pos(&txn->hdr_idx);
1781 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001784 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001786 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1787 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 }
1789 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001790
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
1792 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001794 * If not so, we check the FD and buffer states before leaving.
1795 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1797 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798 *
1799 */
1800
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001801 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001802 /*
1803 * First, let's catch bad requests.
1804 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001805 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001807
1808 /* 1: Since we are in header mode, if there's no space
1809 * left for headers, we won't be able to free more
1810 * later, so the session will never terminate. We
1811 * must terminate it now.
1812 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001813 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 /* FIXME: check if URI is set and return Status
1815 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816 */
Willy Tarreau06619262006-12-17 08:37:22 +01001817 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001818 }
1819
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001820 /* 2: have we encountered a close ? */
1821 else if (req->flags & BF_READ_NULL) {
1822 txn->status = 400;
1823 client_retnclose(t, error_message(t, HTTP_ERR_400));
1824 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001825 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001826 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001827
Willy Tarreau58f10d72006-12-04 02:26:12 +01001828 if (!(t->flags & SN_ERR_MASK))
1829 t->flags |= SN_ERR_CLICL;
1830 if (!(t->flags & SN_FINST_MASK))
1831 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001832 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001833 }
1834
1835 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001836 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001837 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001838 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001839 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001840 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001841 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001842 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001843 if (!(t->flags & SN_ERR_MASK))
1844 t->flags |= SN_ERR_CLITO;
1845 if (!(t->flags & SN_FINST_MASK))
1846 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001847 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848 }
1849
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001850 /* 4: have we encountered a read error or did we have to shutdown ? */
Willy Tarreauba392ce2008-08-16 21:13:23 +02001851 else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001852 /* we cannot return any message on error */
1853 msg->msg_state = HTTP_MSG_ERROR;
1854 t->analysis &= ~AN_REQ_ANY;
1855 t->fe->failed_req++;
1856 if (!(t->flags & SN_ERR_MASK))
1857 t->flags |= SN_ERR_CLICL;
1858 if (!(t->flags & SN_FINST_MASK))
1859 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001860 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001861 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001862
1863 /* just set the request timeout once at the beginning of the request */
1864 if (!tick_isset(t->txn.exp))
1865 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1866
1867 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001868 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869 }
1870
1871
1872 /****************************************************************
1873 * More interesting part now : we know that we have a complete *
1874 * request which at least looks like HTTP. We have an indicator *
1875 * of each header's length, so we can parse them quickly. *
1876 ****************************************************************/
1877
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001878 t->analysis &= ~AN_REQ_HTTP_HDR;
1879
Willy Tarreau9cdde232007-05-02 20:58:19 +02001880 /* ensure we keep this pointer to the beginning of the message */
1881 msg->sol = req->data + msg->som;
1882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 /*
1884 * 1: identify the method
1885 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001886 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001887
1888 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001890 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001892 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 if (unlikely((t->fe->monitor_uri_len != 0) &&
1894 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1895 !memcmp(&req->data[msg->sl.rq.u],
1896 t->fe->monitor_uri,
1897 t->fe->monitor_uri_len))) {
1898 /*
1899 * We have found the monitor URI
1900 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001901 struct acl_cond *cond;
1902 cur_proxy = t->fe;
1903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001904 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001905
1906 /* Check if we want to fail this monitor request or not */
1907 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1908 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001909
1910 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001911 if (cond->pol == ACL_COND_UNLESS)
1912 ret = !ret;
1913
1914 if (ret) {
1915 /* we fail this request, let's return 503 service unavail */
1916 txn->status = 503;
1917 client_retnclose(t, error_message(t, HTTP_ERR_503));
1918 goto return_prx_cond;
1919 }
1920 }
1921
1922 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001923 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 client_retnclose(t, &http_200_chunk);
1925 goto return_prx_cond;
1926 }
1927
1928 /*
1929 * 3: Maybe we have to copy the original REQURI for the logs ?
1930 * Note: we cannot log anymore if the request has been
1931 * classified as invalid.
1932 */
1933 if (unlikely(t->logs.logwait & LW_REQ)) {
1934 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001935 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 int urilen = msg->sl.rq.l;
1937
1938 if (urilen >= REQURI_LEN)
1939 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001940 memcpy(txn->uri, &req->data[msg->som], urilen);
1941 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942
1943 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001944 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 } else {
1946 Alert("HTTP logging : out of memory.\n");
1947 }
1948 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001949
Willy Tarreau06619262006-12-17 08:37:22 +01001950
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001951 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1952 if (unlikely(msg->sl.rq.v_l == 0)) {
1953 int delta;
1954 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001955 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 cur_end = msg->sol + msg->sl.rq.l;
1957 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 if (msg->sl.rq.u_l == 0) {
1960 /* if no URI was set, add "/" */
1961 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1962 cur_end += delta;
1963 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001964 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001965 /* add HTTP version */
1966 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1967 msg->eoh += delta;
1968 cur_end += delta;
1969 cur_end = (char *)http_parse_reqline(msg, req->data,
1970 HTTP_MSG_RQMETH,
1971 msg->sol, cur_end + 1,
1972 NULL, NULL);
1973 if (unlikely(!cur_end))
1974 goto return_bad_req;
1975
1976 /* we have a full HTTP/1.0 request now and we know that
1977 * we have either a CR or an LF at <ptr>.
1978 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001979 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001980 }
1981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001982
1983 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001984 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001985 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001986 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001987
1988 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001990 * As opposed to version 1.2, now they will be evaluated in the
1991 * filters order and not in the header order. This means that
1992 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001993 *
1994 * We can now check whether we want to switch to another
1995 * backend, in which case we will re-check the backend's
1996 * filters and various options. In order to support 3-level
1997 * switching, here's how we should proceed :
1998 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001999 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002000 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002001 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002002 * There cannot be any switch from there, so ->be cannot be
2003 * changed anymore.
2004 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002005 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002006 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002007 * The response path will be able to apply either ->be, or
2008 * ->be then ->fe filters in order to match the reverse of
2009 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002010 */
2011
Willy Tarreau06619262006-12-17 08:37:22 +01002012 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002013 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002014 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002015 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002016 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002017
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002018 /* first check whether we have some ACLs set to redirect this request */
2019 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2020 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002021
2022 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002023 if (rule->cond->pol == ACL_COND_UNLESS)
2024 ret = !ret;
2025
2026 if (ret) {
2027 struct chunk rdr = { trash, 0 };
2028 const char *msg_fmt;
2029
2030 /* build redirect message */
2031 switch(rule->code) {
2032 case 303:
2033 rdr.len = strlen(HTTP_303);
2034 msg_fmt = HTTP_303;
2035 break;
2036 case 301:
2037 rdr.len = strlen(HTTP_301);
2038 msg_fmt = HTTP_301;
2039 break;
2040 case 302:
2041 default:
2042 rdr.len = strlen(HTTP_302);
2043 msg_fmt = HTTP_302;
2044 break;
2045 }
2046
2047 if (unlikely(rdr.len > sizeof(trash)))
2048 goto return_bad_req;
2049 memcpy(rdr.str, msg_fmt, rdr.len);
2050
2051 switch(rule->type) {
2052 case REDIRECT_TYPE_PREFIX: {
2053 const char *path;
2054 int pathlen;
2055
2056 path = http_get_path(txn);
2057 /* build message using path */
2058 if (path) {
2059 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2060 } else {
2061 path = "/";
2062 pathlen = 1;
2063 }
2064
2065 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2066 goto return_bad_req;
2067
2068 /* add prefix */
2069 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2070 rdr.len += rule->rdr_len;
2071
2072 /* add path */
2073 memcpy(rdr.str + rdr.len, path, pathlen);
2074 rdr.len += pathlen;
2075 break;
2076 }
2077 case REDIRECT_TYPE_LOCATION:
2078 default:
2079 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2080 goto return_bad_req;
2081
2082 /* add location */
2083 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2084 rdr.len += rule->rdr_len;
2085 break;
2086 }
2087
2088 /* add end of headers */
2089 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2090 rdr.len += 4;
2091
2092 txn->status = rule->code;
2093 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002094 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002095 client_retnclose(t, &rdr);
2096 goto return_prx_cond;
2097 }
2098 }
2099
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002100 /* first check whether we have some ACLs set to block this request */
2101 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002102 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002103
2104 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002105 if (cond->pol == ACL_COND_UNLESS)
2106 ret = !ret;
2107
2108 if (ret) {
2109 txn->status = 403;
2110 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002111 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002112 client_retnclose(t, error_message(t, HTTP_ERR_403));
2113 goto return_prx_cond;
2114 }
2115 }
2116
Willy Tarreau06619262006-12-17 08:37:22 +01002117 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002118 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002119 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2120 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002121 }
2122
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002123 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2124 /* to ensure correct connection accounting on
2125 * the backend, we count the connection for the
2126 * one managing the queue.
2127 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002128 t->be->beconn++;
2129 if (t->be->beconn > t->be->beconn_max)
2130 t->be->beconn_max = t->be->beconn;
2131 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002132 t->flags |= SN_BE_ASSIGNED;
2133 }
2134
Willy Tarreau06619262006-12-17 08:37:22 +01002135 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002136 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002137 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002138 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002139 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002140 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002141 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002142 goto return_prx_cond;
2143 }
2144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002145 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002146 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002147 !(t->flags & SN_CONN_CLOSED)) {
2148 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002149 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002150 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002151
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002152 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002153 old_idx = 0;
2154
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002155 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2156 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002157 cur_ptr = cur_next;
2158 cur_end = cur_ptr + cur_hdr->len;
2159 cur_next = cur_end + cur_hdr->cr + 1;
2160
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002161 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2162 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002163 /* 3 possibilities :
2164 * - we have already set Connection: close,
2165 * so we remove this line.
2166 * - we have not yet set Connection: close,
2167 * but this line indicates close. We leave
2168 * it untouched and set the flag.
2169 * - we have not yet set Connection: close,
2170 * and this line indicates non-close. We
2171 * replace it.
2172 */
2173 if (t->flags & SN_CONN_CLOSED) {
2174 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002175 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002176 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002177 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2178 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002179 cur_hdr->len = 0;
2180 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002181 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2182 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2183 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002184 cur_next += delta;
2185 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002186 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002187 }
2188 t->flags |= SN_CONN_CLOSED;
2189 }
2190 }
2191 old_idx = cur_idx;
2192 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002193 }
2194 /* add request headers from the rule sets in the same order */
2195 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2196 if (unlikely(http_header_add_tail(req,
2197 &txn->req,
2198 &txn->hdr_idx,
2199 rule_set->req_add[cur_idx])) < 0)
2200 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002201 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002202
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002203 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002204 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002205 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002206 /* we have to check the URI and auth for this request.
2207 * FIXME!!! that one is rather dangerous, we want to
2208 * make it follow standard rules (eg: clear t->analysis).
2209 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002210 if (stats_check_uri_auth(t, rule_set))
2211 return 1;
2212 }
2213
Willy Tarreau55ea7572007-06-17 19:56:27 +02002214 /* now check whether we have some switching rules for this request */
2215 if (!(t->flags & SN_BE_ASSIGNED)) {
2216 struct switching_rule *rule;
2217
2218 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2219 int ret;
2220
2221 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002222
2223 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002224 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002225 ret = !ret;
2226
2227 if (ret) {
2228 t->be = rule->be.backend;
2229 t->be->beconn++;
2230 if (t->be->beconn > t->be->beconn_max)
2231 t->be->beconn_max = t->be->beconn;
2232 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002233
2234 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002235 t->rep->rto = t->req->wto = t->be->timeout.server;
2236 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002237 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002238 t->flags |= SN_BE_ASSIGNED;
2239 break;
2240 }
2241 }
2242 }
2243
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002244 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2245 /* No backend was set, but there was a default
2246 * backend set in the frontend, so we use it and
2247 * loop again.
2248 */
2249 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002250 t->be->beconn++;
2251 if (t->be->beconn > t->be->beconn_max)
2252 t->be->beconn_max = t->be->beconn;
2253 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002254
2255 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002256 t->rep->rto = t->req->wto = t->be->timeout.server;
2257 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002258 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002259 t->flags |= SN_BE_ASSIGNED;
2260 }
2261 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002262
Willy Tarreau58f10d72006-12-04 02:26:12 +01002263
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002264 if (!(t->flags & SN_BE_ASSIGNED)) {
2265 /* To ensure correct connection accounting on
2266 * the backend, we count the connection for the
2267 * one managing the queue.
2268 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002269 t->be->beconn++;
2270 if (t->be->beconn > t->be->beconn_max)
2271 t->be->beconn_max = t->be->beconn;
2272 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002273 t->flags |= SN_BE_ASSIGNED;
2274 }
2275
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002276 /*
2277 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002278 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002279 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002280 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002281 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002282
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002283 /*
2284 * If HTTP PROXY is set we simply get remote server address
2285 * parsing incoming request.
2286 */
2287 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2288 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreau2a324282006-12-05 00:05:46 +01002291 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002292 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002293 * so let's do the same now.
2294 */
2295
2296 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002297 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002298 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002299 }
2300
2301
2302 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002303 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002304 * Note that doing so might move headers in the request, but
2305 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002306 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002307 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002308 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2309 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002310 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002311
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312
Willy Tarreau2a324282006-12-05 00:05:46 +01002313 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002314 * 9: add X-Forwarded-For if either the frontend or the backend
2315 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002316 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002317 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002318 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002319 /* Add an X-Forwarded-For header unless the source IP is
2320 * in the 'except' network range.
2321 */
2322 if ((!t->fe->except_mask.s_addr ||
2323 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2324 != t->fe->except_net.s_addr) &&
2325 (!t->be->except_mask.s_addr ||
2326 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2327 != t->be->except_net.s_addr)) {
2328 int len;
2329 unsigned char *pn;
2330 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002331
Ross Westaf72a1d2008-08-03 10:51:45 +02002332 /* Note: we rely on the backend to get the header name to be used for
2333 * x-forwarded-for, because the header is really meant for the backends.
2334 * However, if the backend did not specify any option, we have to rely
2335 * on the frontend's header name.
2336 */
2337 if (t->be->fwdfor_hdr_len) {
2338 len = t->be->fwdfor_hdr_len;
2339 memcpy(trash, t->be->fwdfor_hdr_name, len);
2340 } else {
2341 len = t->fe->fwdfor_hdr_len;
2342 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2343 }
2344 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002345
Ross Westaf72a1d2008-08-03 10:51:45 +02002346 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002347 &txn->hdr_idx, trash, len)) < 0)
2348 goto return_bad_req;
2349 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002350 }
2351 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002352 /* FIXME: for the sake of completeness, we should also support
2353 * 'except' here, although it is mostly useless in this case.
2354 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002355 int len;
2356 char pn[INET6_ADDRSTRLEN];
2357 inet_ntop(AF_INET6,
2358 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2359 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002360
2361 /* Note: we rely on the backend to get the header name to be used for
2362 * x-forwarded-for, because the header is really meant for the backends.
2363 * However, if the backend did not specify any option, we have to rely
2364 * on the frontend's header name.
2365 */
2366 if (t->be->fwdfor_hdr_len) {
2367 len = t->be->fwdfor_hdr_len;
2368 memcpy(trash, t->be->fwdfor_hdr_name, len);
2369 } else {
2370 len = t->fe->fwdfor_hdr_len;
2371 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2372 }
2373 len += sprintf(trash + len, ": %s", pn);
2374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002375 if (unlikely(http_header_add_tail2(req, &txn->req,
2376 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002377 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002378 }
2379 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002380
Willy Tarreau2a324282006-12-05 00:05:46 +01002381 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002382 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002383 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002384 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002385 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002386 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002387 if ((unlikely(msg->sl.rq.v_l != 8) ||
2388 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2389 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002390 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002391 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002392 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002393 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002394 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2395 * If not assigned, perhaps we are balancing on url_param, but this is a
2396 * POST; and the parameters are in the body, maybe scan there to find our server.
2397 * (unless headers overflowed the buffer?)
2398 */
2399 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2400 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002401 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002402 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2403 /* are there enough bytes here? total == l || r || rlim ?
2404 * len is unsigned, but eoh is int,
2405 * how many bytes of body have we received?
2406 * eoh is the first empty line of the header
2407 */
2408 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002409 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 +02002410
2411 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2412 * We can't assume responsibility for the server's decision,
2413 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2414 * We also can't change our mind later, about which server to choose, so round robin.
2415 */
2416 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2417 struct hdr_ctx ctx;
2418 ctx.idx = 0;
2419 /* Expect is allowed in 1.1, look for it */
2420 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2421 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002422 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002423 /* We can't reliablly stall and wait for data, because of
2424 * .NET clients that don't conform to rfc2616; so, no need for
2425 * the next block to check length expectations.
2426 * We could send 100 status back to the client, but then we need to
2427 * re-write headers, and send the message. And this isn't the right
2428 * place for that action.
2429 * TODO: support Expect elsewhere and delete this block.
2430 */
2431 goto end_check_maybe_wait_for_body;
2432 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002433
2434 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002435 /* nothing to do, we got enough */
2436 } else {
2437 /* limit implies we are supposed to need this many bytes
2438 * to find the parameter. Let's see how many bytes we can wait for.
2439 */
2440 long long hint = len;
2441 struct hdr_ctx ctx;
2442 ctx.idx = 0;
2443 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002444 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002445 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002446 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002447 ctx.idx = 0;
2448 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2449 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002450 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002451 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002452 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002453 hint = 0; /* parse failure, untrusted client */
2454 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002455 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002456 msg->hdr_content_len = hint;
2457 else
2458 hint = 0; /* bad client, sent negative length */
2459 }
2460 }
2461 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002462 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002463 hint = t->be->url_param_post_limit;
2464 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002465 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002466 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002467 /* else... There are no body bytes to wait for */
2468 }
2469 }
2470 }
2471 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002472
Willy Tarreau2a324282006-12-05 00:05:46 +01002473 /*************************************************************
2474 * OK, that's finished for the headers. We have done what we *
2475 * could. Let's switch to the DATA state. *
2476 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002477
Willy Tarreaue393fe22008-08-16 22:18:07 +02002478 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002479 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002480
Willy Tarreau1fa31262007-12-03 00:36:16 +01002481 /* When a connection is tarpitted, we use the tarpit timeout,
2482 * which may be the same as the connect timeout if unspecified.
2483 * If unset, then set it to zero because we really want it to
2484 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002485 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002486 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002487 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002488 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002489 /* flush the request so that we can drop the connection early
2490 * if the client closes first.
2491 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02002492 req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2493 if (!req->wex)
2494 req->wex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002495 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002497 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002498 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002499
2500 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002501 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002502 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002503 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002504 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002505 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002506 return_prx_cond:
2507 if (!(t->flags & SN_ERR_MASK))
2508 t->flags |= SN_ERR_PRXCOND;
2509 if (!(t->flags & SN_FINST_MASK))
2510 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002511 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002512 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002513 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002514 }
2515
2516 if (t->analysis & AN_REQ_HTTP_BODY) {
2517 /* We have to parse the HTTP request body to find any required data.
2518 * "balance url_param check_post" should have been the only way to get
2519 * into this. We were brought here after HTTP header analysis, so all
2520 * related structures are ready.
2521 */
2522 struct http_msg *msg = &t->txn.req;
2523 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2524 long long limit = t->be->url_param_post_limit;
2525 struct hdr_ctx ctx;
2526
2527 ctx.idx = 0;
2528
2529 /* now if we have a length, we'll take the hint */
2530 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2531 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2532 unsigned int chunk = 0;
2533 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2534 char c = msg->sol[body];
2535 if (ishex(c)) {
2536 unsigned int hex = toupper(c) - '0';
2537 if (hex > 9)
2538 hex -= 'A' - '9' - 1;
2539 chunk = (chunk << 4) | hex;
2540 } else
2541 break;
2542 body++;
2543 }
2544 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002545 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002546
2547 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002548 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002549
2550 body += 2; // skip CRLF
2551
2552 /* if we support more then one chunk here, we have to do it again when assigning server
2553 * 1. how much entity data do we have? new var
2554 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2555 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2556 */
2557
2558 if (chunk < limit)
2559 limit = chunk; /* only reading one chunk */
2560 } else {
2561 if (msg->hdr_content_len < limit)
2562 limit = msg->hdr_content_len;
2563 }
2564
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002565 http_body_end:
2566 /* we leave once we know we have nothing left to do. This means that we have
2567 * enough bytes, or that we know we'll not get any more (buffer full, read
2568 * buffer closed).
2569 */
2570 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002571 req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002572 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002573 t->logs.tv_request = now; /* update the request timer to reflect full request */
2574 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002575 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002576 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002577
Willy Tarreaudafde432008-08-17 01:00:46 +02002578 /* we want to leave with that clean */
2579 if (unlikely(t->analysis & AN_REQ_ANY))
2580 goto update_state;
2581 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002582}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002583
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002584/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002585 * It normally returns zero, but may return 1 if it absolutely needs to be
2586 * called again after other functions. It relies on buffers flags, and updates
2587 * t->analysis. It might make sense to explode it into several other functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002588 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002589int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002590{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002591 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002592 struct buffer *req = t->req;
2593 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002594
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002595 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002596 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002597 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02002598 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
2599 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 +02002600 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002601
Willy Tarreaudafde432008-08-17 01:00:46 +02002602 update_state:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603 if (t->analysis & AN_RTR_HTTP_HDR) { /* receiving server headers */
2604 /*
2605 * Now parse the partial (or complete) lines.
2606 * We will check the response syntax, and also join multi-line
2607 * headers. An index of all the lines will be elaborated while
2608 * parsing.
2609 *
2610 * For the parsing, we use a 28 states FSM.
2611 *
2612 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002613 * rep->data + rep->som = beginning of response
2614 * rep->data + rep->eoh = end of processed headers / start of current one
2615 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002616 * rep->lr = first non-visited byte
2617 * rep->r = end of data
2618 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002619
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002620 int cur_idx;
2621 struct http_msg *msg = &txn->rsp;
2622 struct proxy *cur_proxy;
2623
2624 if (likely(rep->lr < rep->r))
2625 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2626
2627 /* 1: we might have to print this header in debug mode */
2628 if (unlikely((global.mode & MODE_DEBUG) &&
2629 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2630 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2631 char *eol, *sol;
2632
2633 sol = rep->data + msg->som;
2634 eol = sol + msg->sl.rq.l;
2635 debug_hdr("srvrep", t, sol, eol);
2636
2637 sol += hdr_idx_first_pos(&txn->hdr_idx);
2638 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2639
2640 while (cur_idx) {
2641 eol = sol + txn->hdr_idx.v[cur_idx].len;
2642 debug_hdr("srvhdr", t, sol, eol);
2643 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2644 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002646 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002648 /*
2649 * Now we quickly check if we have found a full valid response.
2650 * If not so, we check the FD and buffer states before leaving.
2651 * A full response is indicated by the fact that we have seen
2652 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2653 * responses are checked first.
2654 *
2655 * Depending on whether the client is still there or not, we
2656 * may send an error response back or not. Note that normally
2657 * we should only check for HTTP status there, and check I/O
2658 * errors somewhere else.
2659 */
2660
2661 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002662 /* Invalid response */
2663 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2664 hdr_response_bad:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002665 buffer_shutr(rep);
2666 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002667 fd_delete(t->srv_fd);
2668 if (t->srv) {
2669 t->srv->cur_sess--;
2670 t->srv->failed_resp++;
2671 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002673 t->be->failed_resp++;
2674 t->srv_state = SV_STCLOSE;
2675 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002676 txn->status = 502;
2677 client_return(t, error_message(t, HTTP_ERR_502));
2678 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002679 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002680 if (!(t->flags & SN_FINST_MASK))
2681 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002682
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002683 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2684 process_srv_queue(t->srv);
2685
Willy Tarreaudafde432008-08-17 01:00:46 +02002686 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002687 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002688 /* write error to client, read error or close from server */
2689 if (req->flags & BF_WRITE_ERROR ||
Willy Tarreauba392ce2008-08-16 21:13:23 +02002690 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) {
2691 buffer_shutr(rep);
2692 buffer_shutw(req);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002693 fd_delete(t->srv_fd);
2694 if (t->srv) {
2695 t->srv->cur_sess--;
2696 t->srv->failed_resp++;
2697 sess_change_server(t, NULL);
2698 }
2699 t->be->failed_resp++;
2700 t->srv_state = SV_STCLOSE;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002701 t->analysis &= ~AN_RTR_ANY;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002702 txn->status = 502;
2703 client_return(t, error_message(t, HTTP_ERR_502));
2704 if (!(t->flags & SN_ERR_MASK))
2705 t->flags |= SN_ERR_SRVCL;
2706 if (!(t->flags & SN_FINST_MASK))
2707 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002708
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002709 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2710 process_srv_queue(t->srv);
2711
Willy Tarreaudafde432008-08-17 01:00:46 +02002712 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002713 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002714 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002715 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002716 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002717 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002718 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002719 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02002720 buffer_shutr(rep);
2721 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002722 fd_delete(t->srv_fd);
2723 if (t->srv) {
2724 t->srv->cur_sess--;
2725 t->srv->failed_resp++;
2726 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002727 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002728 t->be->failed_resp++;
2729 t->srv_state = SV_STCLOSE;
2730 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002731 txn->status = 504;
2732 client_return(t, error_message(t, HTTP_ERR_504));
2733 if (!(t->flags & SN_ERR_MASK))
2734 t->flags |= SN_ERR_SRVTO;
2735 if (!(t->flags & SN_FINST_MASK))
2736 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002737
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002738 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2739 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002740 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002741 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002742 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002743 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002744
Willy Tarreau21d2af32008-02-14 20:25:24 +01002745
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002746 /*****************************************************************
2747 * More interesting part now : we know that we have a complete *
2748 * response which at least looks like HTTP. We have an indicator *
2749 * of each header's length, so we can parse them quickly. *
2750 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002751
Willy Tarreaua7c52762008-08-16 18:40:18 +02002752 t->analysis &= ~AN_RTR_HTTP_HDR;
2753
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002754 /* ensure we keep this pointer to the beginning of the message */
2755 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002756
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002757 /*
2758 * 1: get the status code and check for cacheability.
2759 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002760
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002761 t->logs.logwait &= ~LW_RESP;
2762 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002764 switch (txn->status) {
2765 case 200:
2766 case 203:
2767 case 206:
2768 case 300:
2769 case 301:
2770 case 410:
2771 /* RFC2616 @13.4:
2772 * "A response received with a status code of
2773 * 200, 203, 206, 300, 301 or 410 MAY be stored
2774 * by a cache (...) unless a cache-control
2775 * directive prohibits caching."
2776 *
2777 * RFC2616 @9.5: POST method :
2778 * "Responses to this method are not cacheable,
2779 * unless the response includes appropriate
2780 * Cache-Control or Expires header fields."
2781 */
2782 if (likely(txn->meth != HTTP_METH_POST) &&
2783 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2784 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2785 break;
2786 default:
2787 break;
2788 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002789
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002790 /*
2791 * 2: we may need to capture headers
2792 */
2793 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2794 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2795 txn->rsp.cap, t->fe->rsp_cap);
2796
2797 /*
2798 * 3: we will have to evaluate the filters.
2799 * As opposed to version 1.2, now they will be evaluated in the
2800 * filters order and not in the header order. This means that
2801 * each filter has to be validated among all headers.
2802 *
2803 * Filters are tried with ->be first, then with ->fe if it is
2804 * different from ->be.
2805 */
2806
2807 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2808
2809 cur_proxy = t->be;
2810 while (1) {
2811 struct proxy *rule_set = cur_proxy;
2812
2813 /* try headers filters */
2814 if (rule_set->rsp_exp != NULL) {
2815 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2816 return_bad_resp:
2817 if (t->srv) {
2818 t->srv->cur_sess--;
2819 t->srv->failed_resp++;
2820 sess_change_server(t, NULL);
2821 }
2822 cur_proxy->failed_resp++;
2823 return_srv_prx_502:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002824 buffer_shutr(rep);
2825 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002826 fd_delete(t->srv_fd);
2827 t->srv_state = SV_STCLOSE;
2828 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002829 txn->status = 502;
2830 client_return(t, error_message(t, HTTP_ERR_502));
2831 if (!(t->flags & SN_ERR_MASK))
2832 t->flags |= SN_ERR_PRXCOND;
2833 if (!(t->flags & SN_FINST_MASK))
2834 t->flags |= SN_FINST_H;
2835 /* We used to have a free connection slot. Since we'll never use it,
2836 * we have to inform the server that it may be used by another session.
2837 */
2838 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2839 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002840 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002841 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002842 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002844 /* has the response been denied ? */
2845 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002846 if (t->srv) {
2847 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002848 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002849 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002850 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002851 cur_proxy->denied_resp++;
2852 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002853 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002855 /* We might have to check for "Connection:" */
2856 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2857 !(t->flags & SN_CONN_CLOSED)) {
2858 char *cur_ptr, *cur_end, *cur_next;
2859 int cur_idx, old_idx, delta, val;
2860 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002861
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2863 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002864
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2866 cur_hdr = &txn->hdr_idx.v[cur_idx];
2867 cur_ptr = cur_next;
2868 cur_end = cur_ptr + cur_hdr->len;
2869 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002871 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2872 if (val) {
2873 /* 3 possibilities :
2874 * - we have already set Connection: close,
2875 * so we remove this line.
2876 * - we have not yet set Connection: close,
2877 * but this line indicates close. We leave
2878 * it untouched and set the flag.
2879 * - we have not yet set Connection: close,
2880 * and this line indicates non-close. We
2881 * replace it.
2882 */
2883 if (t->flags & SN_CONN_CLOSED) {
2884 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2885 txn->rsp.eoh += delta;
2886 cur_next += delta;
2887 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2888 txn->hdr_idx.used--;
2889 cur_hdr->len = 0;
2890 } else {
2891 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2892 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2893 "close", 5);
2894 cur_next += delta;
2895 cur_hdr->len += delta;
2896 txn->rsp.eoh += delta;
2897 }
2898 t->flags |= SN_CONN_CLOSED;
2899 }
2900 }
2901 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002902 }
2903 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002905 /* add response headers from the rule sets in the same order */
2906 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2907 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2908 rule_set->rsp_add[cur_idx])) < 0)
2909 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002910 }
2911
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002912 /* check whether we're already working on the frontend */
2913 if (cur_proxy == t->fe)
2914 break;
2915 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002917
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002918 /*
2919 * 4: check for server cookie.
2920 */
2921 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2922 || (t->be->options & PR_O_CHK_CACHE))
2923 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924
Willy Tarreaubaaee002006-06-26 02:48:02 +02002925
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002927 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002928 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002929 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2930 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002931
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002932 /*
2933 * 6: add server cookie in the response if needed
2934 */
2935 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2936 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2937 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002938
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002939 /* the server is known, it's not the one the client requested, we have to
2940 * insert a set-cookie here, except if we want to insert only on POST
2941 * requests and this one isn't. Note that servers which don't have cookies
2942 * (eg: some backup servers) will return a full cookie removal request.
2943 */
2944 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2945 t->be->cookie_name,
2946 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 if (t->be->cookie_domain)
2949 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002950
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002951 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2952 trash, len)) < 0)
2953 goto return_bad_resp;
2954 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002955
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002956 /* Here, we will tell an eventual cache on the client side that we don't
2957 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2958 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2959 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2960 */
2961 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002962
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002963 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2964
2965 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2966 "Cache-control: private", 22)) < 0)
2967 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968 }
2969 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002972 /*
2973 * 7: check if result will be cacheable with a cookie.
2974 * We'll block the response if security checks have caught
2975 * nasty things such as a cacheable cookie.
2976 */
2977 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2978 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2979 (t->be->options & PR_O_CHK_CACHE)) {
2980
2981 /* we're in presence of a cacheable response containing
2982 * a set-cookie header. We'll block it as requested by
2983 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002984 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002985 if (t->srv) {
2986 t->srv->cur_sess--;
2987 t->srv->failed_secu++;
2988 sess_change_server(t, NULL);
2989 }
2990 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002991
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002992 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2993 t->be->id, t->srv?t->srv->id:"<dispatch>");
2994 send_log(t->be, LOG_ALERT,
2995 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2996 t->be->id, t->srv?t->srv->id:"<dispatch>");
2997 goto return_srv_prx_502;
2998 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002999
3000 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003001 * 8: add "Connection: close" if needed and not yet set.
3002 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003003 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003004 if (!(t->flags & SN_CONN_CLOSED) &&
3005 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3006 if ((unlikely(msg->sl.st.v_l != 8) ||
3007 unlikely(req->data[msg->som + 7] != '0')) &&
3008 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3009 "Connection: close", 17)) < 0)
3010 goto return_bad_resp;
3011 t->flags |= SN_CONN_CLOSED;
3012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003014 /*************************************************************
3015 * OK, that's finished for the headers. We have done what we *
3016 * could. Let's switch to the DATA state. *
3017 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018
Willy Tarreaue393fe22008-08-16 22:18:07 +02003019 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003021
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003022#ifdef CONFIG_HAP_TCPSPLICE
3023 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3024 /* TCP splicing supported by both FE and BE */
3025 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3026 }
3027#endif
3028 /* if the user wants to log as soon as possible, without counting
3029 * bytes from the server, then this is the right moment. We have
3030 * to temporarily assign bytes_out to log what we currently have.
3031 */
3032 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3033 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3034 t->logs.bytes_out = txn->rsp.eoh;
3035 if (t->fe->to_log & LW_REQ)
3036 http_sess_log(t);
3037 else
3038 tcp_sess_log(t);
3039 t->logs.bytes_out = 0;
3040 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003041
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003042 /* Note: we must not try to cheat by jumping directly to DATA,
3043 * otherwise we would not let the client side wake up.
3044 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003045
Willy Tarreaudafde432008-08-17 01:00:46 +02003046 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003047 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003048
3049 /* we want to leave with that clean */
3050 if (unlikely(t->analysis & AN_RTR_ANY))
3051 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003052 return 0;
3053}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003054
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003055/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003056 * Manages the client FSM and its socket. It normally returns zero, but may
3057 * return 1 if it absolutely wants to be called again.
3058 *
3059 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003060 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003061 */
3062int process_cli(struct session *t)
3063{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003064 struct buffer *req = t->req;
3065 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003066
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003067 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 +02003068 now_ms,
3069 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003070 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3071 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 +02003072 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003073 req->flags, rep->flags,
3074 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003075
Willy Tarreaudafde432008-08-17 01:00:46 +02003076 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003077 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3078 * still set this state (and will do until unix sockets are converted).
3079 */
3080 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003081 /* read or write error */
3082 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003083 buffer_shutr(req);
3084 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003085 fd_delete(t->cli_fd);
3086 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003087 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003088 if (!(t->analysis & AN_REQ_ANY)) {
3089 if (!(t->flags & SN_ERR_MASK))
3090 t->flags |= SN_ERR_CLICL;
3091 if (!(t->flags & SN_FINST_MASK)) {
3092 if (t->pend_pos)
3093 t->flags |= SN_FINST_Q;
3094 else if (t->srv_state == SV_STCONN)
3095 t->flags |= SN_FINST_C;
3096 else
3097 t->flags |= SN_FINST_D;
3098 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003099 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003100 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003102 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003103 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003104 req->flags & (BF_READ_NULL | BF_SHUTW)) {
3105 buffer_shutr(req);
3106 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003108 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003109 } else {
3110 /* output was already closed */
3111 fd_delete(t->cli_fd);
3112 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003113 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003114 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003115 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003116 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003117 /* last server read and buffer empty : we only check them when we're
3118 * allowed to forward the data.
3119 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003120 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003121 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003122 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
3123 buffer_shutw(rep);
3124 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003125 EV_FD_CLR(t->cli_fd, DIR_WR);
3126 shutdown(t->cli_fd, SHUT_WR);
3127 /* We must ensure that the read part is still alive when switching to shutw */
3128 /* FIXME: is this still true ? */
3129 EV_FD_SET(t->cli_fd, DIR_RD);
3130 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003131 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003132 } else {
3133 fd_delete(t->cli_fd);
3134 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003135 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003136 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003137 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003138 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003139 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003140 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003141 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003142 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003143 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003144 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003145 } else {
3146 /* output was already closed */
3147 fd_delete(t->cli_fd);
3148 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003149 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003150 }
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003151 if (!(t->analysis & AN_REQ_ANY)) {
3152 if (!(t->flags & SN_ERR_MASK))
3153 t->flags |= SN_ERR_CLITO;
3154 if (!(t->flags & SN_FINST_MASK)) {
3155 if (t->pend_pos)
3156 t->flags |= SN_FINST_Q;
3157 else if (t->srv_state == SV_STCONN)
3158 t->flags |= SN_FINST_C;
3159 else
3160 t->flags |= SN_FINST_D;
3161 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003162 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003163 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003164 }
3165 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003166 else if (rep->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003167 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003168 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003169 EV_FD_CLR(t->cli_fd, DIR_WR);
3170 shutdown(t->cli_fd, SHUT_WR);
3171 /* We must ensure that the read part is still alive when switching to shutw */
3172 /* FIXME: is this still true ? */
3173 EV_FD_SET(t->cli_fd, DIR_RD);
3174 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003175 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003176 } else {
3177 fd_delete(t->cli_fd);
3178 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003179 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003180 }
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003181 if (!(t->analysis & AN_REQ_ANY)) {
3182 if (!(t->flags & SN_ERR_MASK))
3183 t->flags |= SN_ERR_CLITO;
3184 if (!(t->flags & SN_FINST_MASK)) {
3185 if (t->pend_pos)
3186 t->flags |= SN_FINST_Q;
3187 else if (t->srv_state == SV_STCONN)
3188 t->flags |= SN_FINST_C;
3189 else
3190 t->flags |= SN_FINST_D;
3191 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003192 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003193 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003194 }
3195
3196 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003197 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003198 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003199 /* no room to read more data */
3200 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3201 /* stop reading until we get some space */
3202 req->rex = TICK_ETERNITY;
3203 }
3204 } else {
3205 EV_FD_COND_S(t->cli_fd, DIR_RD);
3206 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3207 }
3208 }
3209
3210 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003211 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003212 /* first, we may have to produce data (eg: stats).
3213 * right now, this is limited to the SHUTR state.
3214 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003215 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003216 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003217 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003218 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 fd_delete(t->cli_fd);
3220 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003221 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003222 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003224 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003225
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003226 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003227 if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003228 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3229 /* stop writing */
3230 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003231 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003232 } else {
3233 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003234 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003235 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003236 /* restart writing */
3237 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003238 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003239 /* FIXME: to prevent the client from expiring read timeouts during writes,
3240 * we refresh it, except if it was already infinite. */
3241 req->rex = rep->wex;
3242 }
3243 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003244 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003245 }
3246 return 0; /* other cases change nothing */
3247 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003248 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003249 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3250 int len;
3251 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
3252 write(1, trash, len);
3253 }
3254 return 0;
3255 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003256#ifdef DEBUG_DEV
3257 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3258 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003259#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003260 return 0;
3261}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262
Willy Tarreaubaaee002006-06-26 02:48:02 +02003263
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003265 * Manages the server FSM and its socket. It normally returns zero, but may
3266 * return 1 if it absolutely wants to be called again.
3267 *
Willy Tarreaua7c52762008-08-16 18:40:18 +02003268 * Note: process_srv is the ONLY function allowed to set srv_state to anything
3269 * but SV_STCLOSE. The only exception is for functions called from this
3270 * one (eg: those in backend.c).
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003271 */
3272int process_srv(struct session *t)
3273{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003274 struct http_txn *txn = &t->txn;
3275 struct buffer *req = t->req;
3276 struct buffer *rep = t->rep;
3277 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003278
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003279 DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003280 now_ms,
3281 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003282 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
3283 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003284 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003285 req->flags, rep->flags,
3286 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreaudafde432008-08-17 01:00:46 +02003288 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003289 if (t->srv_state == SV_STIDLE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003290 if ((rep->flags & BF_SHUTW) ||
3291 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003292 (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003293 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003294 if (t->pend_pos)
3295 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3296 /* note that this must not return any error because it would be able to
3297 * overwrite the client_retnclose() output.
3298 */
3299 if (txn->flags & TX_CLTARPIT)
3300 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3301 else
3302 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3303
Willy Tarreauf8533202008-08-16 14:55:08 +02003304 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003305 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003306 }
Willy Tarreaud9f48362008-08-16 16:39:26 +02003307 else if (req->flags & BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003308 /* the client allows the server to connect */
3309 if (txn->flags & TX_CLTARPIT) {
3310 /* This connection is being tarpitted. The CLIENT side has
3311 * already set the connect expiration date to the right
3312 * timeout. We just have to check that it has not expired.
3313 */
Willy Tarreau507385d2008-08-17 13:04:25 +02003314 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003315 return 0;
3316
3317 /* We will set the queue timer to the time spent, just for
3318 * logging purposes. We fake a 500 server error, so that the
3319 * attacker will not suspect his connection has been tarpitted.
3320 * It will not cause trouble to the logs because we can exclude
3321 * the tarpitted connections by filtering on the 'PT' status flags.
3322 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003323 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003324 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3325 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3326 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003327 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreaudafde432008-08-17 01:00:46 +02003328 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003329 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003330
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003331 /* Right now, we will need to create a connection to the server.
3332 * We might already have tried, and got a connection pending, in
3333 * which case we will not do anything till it's pending. It's up
3334 * to any other session to release it and wake us up again.
3335 */
3336 if (t->pend_pos) {
Willy Tarreau507385d2008-08-17 13:04:25 +02003337 if (!(req->flags & BF_WRITE_TIMEOUT)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003338 return 0;
3339 } else {
3340 /* we've been waiting too long here */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003341 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003342 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3343 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3344 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003345 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003346 if (t->srv)
3347 t->srv->failed_conns++;
3348 t->be->failed_conns++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003349 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003350 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003351 }
3352
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003353 do {
3354 /* first, get a connection */
3355 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3356 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003357
Willy Tarreaudafde432008-08-17 01:00:46 +02003358 if (srv_redispatch_connect(t)) {
3359 if (t->srv_state == SV_STIDLE)
3360 return 0;
3361 goto update_state;
3362 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003363
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003364 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3365 /* Server supporting redirection and it is possible.
3366 * Invalid requests are reported as such. It concerns all
3367 * the largest ones.
3368 */
3369 struct chunk rdr;
3370 char *path;
3371 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003372
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 /* 1: create the response header */
3374 rdr.len = strlen(HTTP_302);
3375 rdr.str = trash;
3376 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003377
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003378 /* 2: add the server's prefix */
3379 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3380 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003381
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003382 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3383 rdr.len += t->srv->rdr_len;
3384
3385 /* 3: add the request URI */
3386 path = http_get_path(txn);
3387 if (!path)
3388 goto cancel_redir;
3389 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3390 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3391 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003392
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003393 memcpy(rdr.str + rdr.len, path, len);
3394 rdr.len += len;
3395 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3396 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003398 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003399 trace_term(t, TT_HTTP_SRV_3);
3400
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003401 /* FIXME: we should increase a counter of redirects per server and per backend. */
3402 if (t->srv)
3403 t->srv->cum_sess++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003404 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003405 cancel_redir:
3406 txn->status = 400;
3407 t->fe->failed_req++;
3408 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3409 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003410 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaudafde432008-08-17 01:00:46 +02003411 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003414 /* try to (re-)connect to the server, and fail if we expire the
3415 * number of retries.
3416 */
3417 if (srv_retryable_connect(t)) {
3418 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003419 if (t->srv_state == SV_STIDLE)
3420 return 0;
3421 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003422 }
3423 } while (1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003424 } /* end if may_forward */
3425 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003426 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003427 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003428 if ((rep->flags & BF_SHUTW) ||
3429 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003430 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003431 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003432 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003433 if (!(t->flags & SN_CONN_TAR)) {
3434 /* if we are in turn-around, we have already closed the FD */
3435 fd_delete(t->srv_fd);
3436 if (t->srv) {
3437 t->srv->cur_sess--;
3438 sess_change_server(t, NULL);
3439 }
3440 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003441
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003442 /* note that this must not return any error because it would be able to
3443 * overwrite the client_retnclose() output.
3444 */
3445 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003446 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreaudafde432008-08-17 01:00:46 +02003447 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003448 }
Willy Tarreau507385d2008-08-17 13:04:25 +02003449 if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 return 0; /* nothing changed */
3451 }
3452 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3453 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003454 if (t->flags & SN_CONN_TAR) {
3455 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau507385d2008-08-17 13:04:25 +02003456 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457 return 0;
3458 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003460 else {
3461 fd_delete(t->srv_fd);
3462 if (t->srv) {
3463 t->srv->cur_sess--;
3464 sess_change_server(t, NULL);
3465 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003466
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 if (!(req->flags & BF_WRITE_STATUS))
3468 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3469 else
3470 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003471
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003472 /* ensure that we have enough retries left */
Willy Tarreaudafde432008-08-17 01:00:46 +02003473 if (srv_count_retry_down(t, conn_err)) {
3474 goto update_state;
3475 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003477 if (req->flags & BF_WRITE_ERROR) {
3478 /* we encountered an immediate connection error, and we
3479 * will have to retry connecting to the same server, most
3480 * likely leading to the same result. To avoid this, we
3481 * fake a connection timeout to retry after a turn-around
3482 * time of 1 second. We will wait in the previous if block.
3483 */
3484 t->flags |= SN_CONN_TAR;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003485 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003486 return 0;
3487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003490 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3491 /* We're on our last chance, and the REDISP option was specified.
3492 * We will ignore cookie and force to balance or use the dispatcher.
3493 */
3494 /* let's try to offer this slot to anybody */
3495 if (may_dequeue_tasks(t->srv, t->be))
3496 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003497
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003498 /* it's left to the dispatcher to choose a server */
3499 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3500 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003501
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003502 /* first, get a connection */
Willy Tarreaudafde432008-08-17 01:00:46 +02003503 if (srv_redispatch_connect(t)) {
3504 if (t->srv_state == SV_STCONN)
3505 return 0;
3506 goto update_state;
3507 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003508 } else {
3509 if (t->srv)
3510 t->srv->retries++;
3511 t->be->retries++;
3512 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003514 do {
3515 /* Now we will try to either reconnect to the same server or
3516 * connect to another server. If the connection gets queued
3517 * because all servers are saturated, then we will go back to
3518 * the SV_STIDLE state.
3519 */
3520 if (srv_retryable_connect(t)) {
3521 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003522 if (t->srv_state == SV_STCONN)
3523 return 0;
3524 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003525 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003526
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003527 /* we need to redispatch the connection to another server */
Willy Tarreaudafde432008-08-17 01:00:46 +02003528 if (srv_redispatch_connect(t)) {
3529 if (t->srv_state == SV_STCONN)
3530 return 0;
3531 goto update_state;
3532 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003533 } while (1);
3534 }
3535 else { /* no error or write 0 */
3536 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003537
Willy Tarreaue393fe22008-08-16 22:18:07 +02003538 if (req->flags & BF_EMPTY) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003539 EV_FD_CLR(t->srv_fd, DIR_WR);
3540 req->wex = TICK_ETERNITY;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003541 } else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003542 EV_FD_SET(t->srv_fd, DIR_WR);
3543 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3544 if (tick_isset(req->wex)) {
3545 /* FIXME: to prevent the server from expiring read timeouts during writes,
3546 * we refresh it. */
3547 rep->rex = req->wex;
3548 }
3549 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003551 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3552 EV_FD_SET(t->srv_fd, DIR_RD);
3553 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003554 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003555
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003556 /* if the user wants to log as soon as possible, without counting
3557 bytes from the server, then this is the right moment. */
3558 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3559 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3560 tcp_sess_log(t);
3561 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003562#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003563 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3564 /* TCP splicing supported by both FE and BE */
3565 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3566 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003567#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 }
3569 else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003570 t->analysis |= AN_RTR_HTTP_HDR;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003571 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003572 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3573 /* reset hdr_idx which was already initialized by the request.
3574 * right now, the http parser does it.
3575 * hdr_idx_init(&t->txn.hdr_idx);
3576 */
3577 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003578
3579 t->srv_state = SV_STDATA;
3580 if (!(t->analysis & AN_RTR_ANY))
3581 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003582 req->wex = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003583 goto update_state;
3584 } /* else no error or write 0 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003585 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003586 else if (t->srv_state == SV_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003587 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003588 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003589 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003590 buffer_shutr(rep);
3591 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003592 fd_delete(t->srv_fd);
3593 if (t->srv) {
3594 t->srv->cur_sess--;
3595 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003596 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003597 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003598 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003599 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003600 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003601 if (!(t->analysis & AN_RTR_ANY)) {
3602 if (!(t->flags & SN_ERR_MASK))
3603 t->flags |= SN_ERR_SRVCL;
3604 if (!(t->flags & SN_FINST_MASK))
3605 t->flags |= SN_FINST_D;
3606 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003607 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003608 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003609
Willy Tarreaudafde432008-08-17 01:00:46 +02003610 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003611 }
3612 /* last read, or end of client write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003613 else if (!(rep->flags & BF_SHUTR) && /* not already done */
3614 rep->flags & (BF_READ_NULL | BF_SHUTW)) {
3615 buffer_shutr(rep);
3616 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003617 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003618 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003619 } else {
3620 /* output was already closed */
3621 fd_delete(t->srv_fd);
3622 if (t->srv) {
3623 t->srv->cur_sess--;
3624 sess_change_server(t, NULL);
3625 }
3626 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003627 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003628
3629 if (may_dequeue_tasks(t->srv, t->be))
3630 process_srv_queue(t->srv);
3631 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003632 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003634 /* end of client read and no more data to send. We can forward
3635 * the close when we're allowed to forward data (anytime right
3636 * now). If we're using option forceclose, then we may also
3637 * shutdown the outgoing write channel once the response starts
3638 * coming from the server.
3639 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003640 else if (!(req->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003641 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003642 (req->flags & BF_SHUTR ||
Willy Tarreau461f6622008-08-15 23:43:19 +02003643 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003644 buffer_shutw(req);
3645 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003646 EV_FD_CLR(t->srv_fd, DIR_WR);
3647 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003648 trace_term(t, TT_HTTP_SRV_9);
Willy Tarreau461f6622008-08-15 23:43:19 +02003649 /* We must ensure that the read part is still alive when switching to shutw */
3650 /* FIXME: is this still true ? */
3651 EV_FD_SET(t->srv_fd, DIR_RD);
3652 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003653 } else {
3654 fd_delete(t->srv_fd);
3655 if (t->srv) {
3656 t->srv->cur_sess--;
3657 sess_change_server(t, NULL);
3658 }
3659 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003660 trace_term(t, TT_HTTP_SRV_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003661
Willy Tarreau461f6622008-08-15 23:43:19 +02003662 if (may_dequeue_tasks(t->srv, t->be))
3663 process_srv_queue(t->srv);
3664 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003665 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003666 }
3667 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003668 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003669 buffer_shutr(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003670 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003671 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003672 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003673 } else {
3674 fd_delete(t->srv_fd);
3675 if (t->srv) {
3676 t->srv->cur_sess--;
3677 sess_change_server(t, NULL);
3678 }
3679 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003680 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003681
3682 if (may_dequeue_tasks(t->srv, t->be))
3683 process_srv_queue(t->srv);
3684 }
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003685 if (!(t->analysis & AN_RTR_ANY)) {
3686 if (!(t->flags & SN_ERR_MASK))
3687 t->flags |= SN_ERR_SRVTO;
3688 if (!(t->flags & SN_FINST_MASK))
3689 t->flags |= SN_FINST_D;
3690 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003691
3692 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003693 }
3694 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003695 else if (req->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003696 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003697 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003698 EV_FD_CLR(t->srv_fd, DIR_WR);
3699 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003700 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003701 /* We must ensure that the read part is still alive when switching to shutw */
3702 /* FIXME: is this still needed ? */
3703 EV_FD_SET(t->srv_fd, DIR_RD);
3704 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003705 } else {
3706 fd_delete(t->srv_fd);
3707 if (t->srv) {
3708 t->srv->cur_sess--;
3709 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003710 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003711 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003712 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003713
Willy Tarreau461f6622008-08-15 23:43:19 +02003714 if (may_dequeue_tasks(t->srv, t->be))
3715 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003716 }
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003717 if (!(t->analysis & AN_RTR_ANY)) {
3718 if (!(t->flags & SN_ERR_MASK))
3719 t->flags |= SN_ERR_SRVTO;
3720 if (!(t->flags & SN_FINST_MASK))
3721 t->flags |= SN_FINST_D;
3722 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003723 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003724 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003725
Willy Tarreau461f6622008-08-15 23:43:19 +02003726 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003727 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003728 if (rep->flags & BF_FULL) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003729 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3730 rep->rex = TICK_ETERNITY;
3731 } else {
3732 EV_FD_COND_S(t->srv_fd, DIR_RD);
3733 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003734 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003735 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736
Willy Tarreau461f6622008-08-15 23:43:19 +02003737 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003738 if (!(req->flags & BF_SHUTW)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003739 if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003740 /* stop writing */
3741 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3742 req->wex = TICK_ETERNITY;
3743 } else {
3744 /* buffer not empty, there are still data to be transferred */
3745 EV_FD_COND_S(t->srv_fd, DIR_WR);
3746 if (!tick_isset(req->wex)) {
3747 /* restart writing */
3748 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003749 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003750 /* FIXME: to prevent the server from expiring read timeouts during writes,
3751 * we refresh it, except if it was already infinite.
3752 */
3753 rep->rex = req->wex;
3754 }
3755 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003756 }
3757 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003758 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003760 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3762 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003763 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003764 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003765 write(1, trash, len);
3766 }
3767 return 0;
3768 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003769#ifdef DEBUG_DEV
3770 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3771 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003772#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003773 return 0;
3774}
3775
3776
3777/*
3778 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003779 * called with client socket shut down on input. Right now, only statistics can
3780 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003782 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003783 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003784 */
3785int produce_content(struct session *s)
3786{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003787 if (s->data_source == DATA_SRC_NONE) {
3788 s->flags &= ~SN_SELF_GEN;
3789 return 1;
3790 }
3791 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003792 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003793 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003794 if (ret >= 0)
3795 return ret;
3796 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003797 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003798
Willy Tarreau91861262007-10-17 17:06:05 +02003799 /* unknown data source or internal error */
3800 s->txn.status = 500;
3801 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003802 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003803 if (!(s->flags & SN_ERR_MASK))
3804 s->flags |= SN_ERR_PRXCOND;
3805 if (!(s->flags & SN_FINST_MASK))
3806 s->flags |= SN_FINST_R;
3807 s->flags &= ~SN_SELF_GEN;
3808 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003809}
3810
3811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003812/* Iterate the same filter through all request headers.
3813 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003814 * Since it can manage the switch to another backend, it updates the per-proxy
3815 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003816 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003817int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003818{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003819 char term;
3820 char *cur_ptr, *cur_end, *cur_next;
3821 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003822 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003823 struct hdr_idx_elem *cur_hdr;
3824 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003826 last_hdr = 0;
3827
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003828 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003829 old_idx = 0;
3830
3831 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003832 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003833 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003834 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003835 (exp->action == ACT_ALLOW ||
3836 exp->action == ACT_DENY ||
3837 exp->action == ACT_TARPIT))
3838 return 0;
3839
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003840 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003841 if (!cur_idx)
3842 break;
3843
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003844 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845 cur_ptr = cur_next;
3846 cur_end = cur_ptr + cur_hdr->len;
3847 cur_next = cur_end + cur_hdr->cr + 1;
3848
3849 /* Now we have one header between cur_ptr and cur_end,
3850 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851 */
3852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003853 /* The annoying part is that pattern matching needs
3854 * that we modify the contents to null-terminate all
3855 * strings before testing them.
3856 */
3857
3858 term = *cur_end;
3859 *cur_end = '\0';
3860
3861 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3862 switch (exp->action) {
3863 case ACT_SETBE:
3864 /* It is not possible to jump a second time.
3865 * FIXME: should we return an HTTP/500 here so that
3866 * the admin knows there's a problem ?
3867 */
3868 if (t->be != t->fe)
3869 break;
3870
3871 /* Swithing Proxy */
3872 t->be = (struct proxy *) exp->replace;
3873
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003874 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003875 * because we have associated req_cap and rsp_cap to the
3876 * frontend, and the beconn will be updated later.
3877 */
3878
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003879 t->rep->rto = t->req->wto = t->be->timeout.server;
3880 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003881 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003882 last_hdr = 1;
3883 break;
3884
3885 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003886 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003887 last_hdr = 1;
3888 break;
3889
3890 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003891 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003892 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003893 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003894 break;
3895
3896 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003897 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003898 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003899 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003900 break;
3901
3902 case ACT_REPLACE:
3903 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3904 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3905 /* FIXME: if the user adds a newline in the replacement, the
3906 * index will not be recalculated for now, and the new line
3907 * will not be counted as a new header.
3908 */
3909
3910 cur_end += delta;
3911 cur_next += delta;
3912 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003913 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003914 break;
3915
3916 case ACT_REMOVE:
3917 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3918 cur_next += delta;
3919
3920 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003921 txn->req.eoh += delta;
3922 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3923 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003924 cur_hdr->len = 0;
3925 cur_end = NULL; /* null-term has been rewritten */
3926 break;
3927
3928 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003929 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003930 if (cur_end)
3931 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003933 /* keep the link from this header to next one in case of later
3934 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003935 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003936 old_idx = cur_idx;
3937 }
3938 return 0;
3939}
3940
3941
3942/* Apply the filter to the request line.
3943 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3944 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003945 * Since it can manage the switch to another backend, it updates the per-proxy
3946 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003947 */
3948int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3949{
3950 char term;
3951 char *cur_ptr, *cur_end;
3952 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003953 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003954 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003955
Willy Tarreau58f10d72006-12-04 02:26:12 +01003956
Willy Tarreau3d300592007-03-18 18:34:41 +01003957 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003958 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003959 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003960 (exp->action == ACT_ALLOW ||
3961 exp->action == ACT_DENY ||
3962 exp->action == ACT_TARPIT))
3963 return 0;
3964 else if (exp->action == ACT_REMOVE)
3965 return 0;
3966
3967 done = 0;
3968
Willy Tarreau9cdde232007-05-02 20:58:19 +02003969 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003970 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003971
3972 /* Now we have the request line between cur_ptr and cur_end */
3973
3974 /* The annoying part is that pattern matching needs
3975 * that we modify the contents to null-terminate all
3976 * strings before testing them.
3977 */
3978
3979 term = *cur_end;
3980 *cur_end = '\0';
3981
3982 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3983 switch (exp->action) {
3984 case ACT_SETBE:
3985 /* It is not possible to jump a second time.
3986 * FIXME: should we return an HTTP/500 here so that
3987 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003988 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003989 if (t->be != t->fe)
3990 break;
3991
3992 /* Swithing Proxy */
3993 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003995 /* right now, the backend switch is not too much complicated
3996 * because we have associated req_cap and rsp_cap to the
3997 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003998 */
3999
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004000 t->rep->rto = t->req->wto = t->be->timeout.server;
4001 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004002 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004003 done = 1;
4004 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004006 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004007 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008 done = 1;
4009 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004010
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004011 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004012 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004013 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004014 done = 1;
4015 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004017 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004018 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004019 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 done = 1;
4021 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004023 case ACT_REPLACE:
4024 *cur_end = term; /* restore the string terminator */
4025 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4026 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4027 /* FIXME: if the user adds a newline in the replacement, the
4028 * index will not be recalculated for now, and the new line
4029 * will not be counted as a new header.
4030 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004031
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004032 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004034
Willy Tarreau9cdde232007-05-02 20:58:19 +02004035 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004036 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004037 HTTP_MSG_RQMETH,
4038 cur_ptr, cur_end + 1,
4039 NULL, NULL);
4040 if (unlikely(!cur_end))
4041 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004043 /* we have a full request and we know that we have either a CR
4044 * or an LF at <ptr>.
4045 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004046 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4047 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004048 /* there is no point trying this regex on headers */
4049 return 1;
4050 }
4051 }
4052 *cur_end = term; /* restore the string terminator */
4053 return done;
4054}
Willy Tarreau97de6242006-12-27 17:18:38 +01004055
Willy Tarreau58f10d72006-12-04 02:26:12 +01004056
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058/*
4059 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4060 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004061 * unparsable request. Since it can manage the switch to another backend, it
4062 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 */
4064int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4065{
Willy Tarreau3d300592007-03-18 18:34:41 +01004066 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004068 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004069 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004071 /*
4072 * The interleaving of transformations and verdicts
4073 * makes it difficult to decide to continue or stop
4074 * the evaluation.
4075 */
4076
Willy Tarreau3d300592007-03-18 18:34:41 +01004077 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004078 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4079 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4080 exp = exp->next;
4081 continue;
4082 }
4083
4084 /* Apply the filter to the request line. */
4085 ret = apply_filter_to_req_line(t, req, exp);
4086 if (unlikely(ret < 0))
4087 return -1;
4088
4089 if (likely(ret == 0)) {
4090 /* The filter did not match the request, it can be
4091 * iterated through all headers.
4092 */
4093 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004094 }
4095 exp = exp->next;
4096 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004097 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004098}
4099
4100
Willy Tarreaua15645d2007-03-18 16:22:39 +01004101
Willy Tarreau58f10d72006-12-04 02:26:12 +01004102/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004103 * Manage client-side cookie. It can impact performance by about 2% so it is
4104 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004105 */
4106void manage_client_side_cookies(struct session *t, struct buffer *req)
4107{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004108 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004109 char *p1, *p2, *p3, *p4;
4110 char *del_colon, *del_cookie, *colon;
4111 int app_cookies;
4112
4113 appsess *asession_temp = NULL;
4114 appsess local_asession;
4115
4116 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004117 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004118
Willy Tarreau2a324282006-12-05 00:05:46 +01004119 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120 * we start with the start line.
4121 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004122 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004123 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004125 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004126 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004127 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004128
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004129 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004130 cur_ptr = cur_next;
4131 cur_end = cur_ptr + cur_hdr->len;
4132 cur_next = cur_end + cur_hdr->cr + 1;
4133
4134 /* We have one full header between cur_ptr and cur_end, and the
4135 * next header starts at cur_next. We're only interested in
4136 * "Cookie:" headers.
4137 */
4138
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004139 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4140 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 old_idx = cur_idx;
4142 continue;
4143 }
4144
4145 /* Now look for cookies. Conforming to RFC2109, we have to support
4146 * attributes whose name begin with a '$', and associate them with
4147 * the right cookie, if we want to delete this cookie.
4148 * So there are 3 cases for each cookie read :
4149 * 1) it's a special attribute, beginning with a '$' : ignore it.
4150 * 2) it's a server id cookie that we *MAY* want to delete : save
4151 * some pointers on it (last semi-colon, beginning of cookie...)
4152 * 3) it's an application cookie : we *MAY* have to delete a previous
4153 * "special" cookie.
4154 * At the end of loop, if a "special" cookie remains, we may have to
4155 * remove it. If no application cookie persists in the header, we
4156 * *MUST* delete it
4157 */
4158
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004159 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004160
Willy Tarreau58f10d72006-12-04 02:26:12 +01004161 /* del_cookie == NULL => nothing to be deleted */
4162 del_colon = del_cookie = NULL;
4163 app_cookies = 0;
4164
4165 while (p1 < cur_end) {
4166 /* skip spaces and colons, but keep an eye on these ones */
4167 while (p1 < cur_end) {
4168 if (*p1 == ';' || *p1 == ',')
4169 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004170 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171 break;
4172 p1++;
4173 }
4174
4175 if (p1 == cur_end)
4176 break;
4177
4178 /* p1 is at the beginning of the cookie name */
4179 p2 = p1;
4180 while (p2 < cur_end && *p2 != '=')
4181 p2++;
4182
4183 if (p2 == cur_end)
4184 break;
4185
4186 p3 = p2 + 1; /* skips the '=' sign */
4187 if (p3 == cur_end)
4188 break;
4189
4190 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004191 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004192 p4++;
4193
4194 /* here, we have the cookie name between p1 and p2,
4195 * and its value between p3 and p4.
4196 * we can process it :
4197 *
4198 * Cookie: NAME=VALUE;
4199 * | || || |
4200 * | || || +--> p4
4201 * | || |+-------> p3
4202 * | || +--------> p2
4203 * | |+------------> p1
4204 * | +-------------> colon
4205 * +--------------------> cur_ptr
4206 */
4207
4208 if (*p1 == '$') {
4209 /* skip this one */
4210 }
4211 else {
4212 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004213 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004214 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004215 (p4 - p1 >= t->fe->capture_namelen) &&
4216 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004217 int log_len = p4 - p1;
4218
Willy Tarreau086b3b42007-05-13 21:45:51 +02004219 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004220 Alert("HTTP logging : out of memory.\n");
4221 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004222 if (log_len > t->fe->capture_len)
4223 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004224 memcpy(txn->cli_cookie, p1, log_len);
4225 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226 }
4227 }
4228
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004229 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4230 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004232 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004233 char *delim;
4234
4235 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4236 * have the server ID betweek p3 and delim, and the original cookie between
4237 * delim+1 and p4. Otherwise, delim==p4 :
4238 *
4239 * Cookie: NAME=SRV~VALUE;
4240 * | || || | |
4241 * | || || | +--> p4
4242 * | || || +--------> delim
4243 * | || |+-----------> p3
4244 * | || +------------> p2
4245 * | |+----------------> p1
4246 * | +-----------------> colon
4247 * +------------------------> cur_ptr
4248 */
4249
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004250 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004251 for (delim = p3; delim < p4; delim++)
4252 if (*delim == COOKIE_DELIM)
4253 break;
4254 }
4255 else
4256 delim = p4;
4257
4258
4259 /* Here, we'll look for the first running server which supports the cookie.
4260 * This allows to share a same cookie between several servers, for example
4261 * to dedicate backup servers to specific servers only.
4262 * However, to prevent clients from sticking to cookie-less backup server
4263 * when they have incidentely learned an empty cookie, we simply ignore
4264 * empty cookies and mark them as invalid.
4265 */
4266 if (delim == p3)
4267 srv = NULL;
4268
4269 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004270 if (srv->cookie && (srv->cklen == delim - p3) &&
4271 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004272 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004274 txn->flags &= ~TX_CK_MASK;
4275 txn->flags |= TX_CK_VALID;
4276 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 t->srv = srv;
4278 break;
4279 } else {
4280 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004281 txn->flags &= ~TX_CK_MASK;
4282 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004283 }
4284 }
4285 srv = srv->next;
4286 }
4287
Willy Tarreau3d300592007-03-18 18:34:41 +01004288 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004290 txn->flags &= ~TX_CK_MASK;
4291 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004292 }
4293
4294 /* depending on the cookie mode, we may have to either :
4295 * - delete the complete cookie if we're in insert+indirect mode, so that
4296 * the server never sees it ;
4297 * - remove the server id from the cookie value, and tag the cookie as an
4298 * application cookie so that it does not get accidentely removed later,
4299 * if we're in cookie prefix mode
4300 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004301 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004302 int delta; /* negative */
4303
4304 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4305 p4 += delta;
4306 cur_end += delta;
4307 cur_next += delta;
4308 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004309 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004310
4311 del_cookie = del_colon = NULL;
4312 app_cookies++; /* protect the header from deletion */
4313 }
4314 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004315 (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 +01004316 del_cookie = p1;
4317 del_colon = colon;
4318 }
4319 } else {
4320 /* now we know that we must keep this cookie since it's
4321 * not ours. But if we wanted to delete our cookie
4322 * earlier, we cannot remove the complete header, but we
4323 * can remove the previous block itself.
4324 */
4325 app_cookies++;
4326
4327 if (del_cookie != NULL) {
4328 int delta; /* negative */
4329
4330 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4331 p4 += delta;
4332 cur_end += delta;
4333 cur_next += delta;
4334 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004335 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 del_cookie = del_colon = NULL;
4337 }
4338 }
4339
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004340 if ((t->be->appsession_name != NULL) &&
4341 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004342 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004343
Willy Tarreau58f10d72006-12-04 02:26:12 +01004344 /* Cool... it's the right one */
4345
4346 asession_temp = &local_asession;
4347
Willy Tarreau63963c62007-05-13 21:29:55 +02004348 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004349 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4350 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4351 return;
4352 }
4353
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004354 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4355 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004356 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004357
Willy Tarreau58f10d72006-12-04 02:26:12 +01004358 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004359 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4360 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004361 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004362 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004363 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004364 Alert("Not enough memory process_cli():asession:calloc().\n");
4365 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4366 return;
4367 }
4368
4369 asession_temp->sessid = local_asession.sessid;
4370 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004371 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004372 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004373 } else {
4374 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004375 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004376 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004377 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004378 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004379 Alert("Found Application Session without matching server.\n");
4380 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004381 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004382 while (srv) {
4383 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004384 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004385 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004386 txn->flags &= ~TX_CK_MASK;
4387 txn->flags |= TX_CK_VALID;
4388 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004389 t->srv = srv;
4390 break;
4391 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004392 txn->flags &= ~TX_CK_MASK;
4393 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004394 }
4395 }
4396 srv = srv->next;
4397 }/* end while(srv) */
4398 }/* end else if server == NULL */
4399
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004400 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004401 asession_temp->request_count++;
4402#if defined(DEBUG_HASH)
4403 Alert("manage_client_side_cookies\n");
4404 appsession_hash_dump(&(t->be->htbl_proxy));
4405#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 }/* end if ((t->proxy->appsession_name != NULL) ... */
4407 }
4408
4409 /* we'll have to look for another cookie ... */
4410 p1 = p4;
4411 } /* while (p1 < cur_end) */
4412
4413 /* There's no more cookie on this line.
4414 * We may have marked the last one(s) for deletion.
4415 * We must do this now in two ways :
4416 * - if there is no app cookie, we simply delete the header ;
4417 * - if there are app cookies, we must delete the end of the
4418 * string properly, including the colon/semi-colon before
4419 * the cookie name.
4420 */
4421 if (del_cookie != NULL) {
4422 int delta;
4423 if (app_cookies) {
4424 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4425 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004426 cur_hdr->len += delta;
4427 } else {
4428 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429
4430 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004431 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4432 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004433 cur_hdr->len = 0;
4434 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004435 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004436 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004437 }
4438
4439 /* keep the link from this header to next one */
4440 old_idx = cur_idx;
4441 } /* end of cookie processing on this header */
4442}
4443
4444
Willy Tarreaua15645d2007-03-18 16:22:39 +01004445/* Iterate the same filter through all response headers contained in <rtr>.
4446 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4447 */
4448int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4449{
4450 char term;
4451 char *cur_ptr, *cur_end, *cur_next;
4452 int cur_idx, old_idx, last_hdr;
4453 struct http_txn *txn = &t->txn;
4454 struct hdr_idx_elem *cur_hdr;
4455 int len, delta;
4456
4457 last_hdr = 0;
4458
4459 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4460 old_idx = 0;
4461
4462 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004463 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004464 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004465 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004466 (exp->action == ACT_ALLOW ||
4467 exp->action == ACT_DENY))
4468 return 0;
4469
4470 cur_idx = txn->hdr_idx.v[old_idx].next;
4471 if (!cur_idx)
4472 break;
4473
4474 cur_hdr = &txn->hdr_idx.v[cur_idx];
4475 cur_ptr = cur_next;
4476 cur_end = cur_ptr + cur_hdr->len;
4477 cur_next = cur_end + cur_hdr->cr + 1;
4478
4479 /* Now we have one header between cur_ptr and cur_end,
4480 * and the next header starts at cur_next.
4481 */
4482
4483 /* The annoying part is that pattern matching needs
4484 * that we modify the contents to null-terminate all
4485 * strings before testing them.
4486 */
4487
4488 term = *cur_end;
4489 *cur_end = '\0';
4490
4491 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4492 switch (exp->action) {
4493 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004494 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004495 last_hdr = 1;
4496 break;
4497
4498 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004499 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004500 last_hdr = 1;
4501 break;
4502
4503 case ACT_REPLACE:
4504 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4505 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4506 /* FIXME: if the user adds a newline in the replacement, the
4507 * index will not be recalculated for now, and the new line
4508 * will not be counted as a new header.
4509 */
4510
4511 cur_end += delta;
4512 cur_next += delta;
4513 cur_hdr->len += delta;
4514 txn->rsp.eoh += delta;
4515 break;
4516
4517 case ACT_REMOVE:
4518 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4519 cur_next += delta;
4520
4521 /* FIXME: this should be a separate function */
4522 txn->rsp.eoh += delta;
4523 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4524 txn->hdr_idx.used--;
4525 cur_hdr->len = 0;
4526 cur_end = NULL; /* null-term has been rewritten */
4527 break;
4528
4529 }
4530 }
4531 if (cur_end)
4532 *cur_end = term; /* restore the string terminator */
4533
4534 /* keep the link from this header to next one in case of later
4535 * removal of next header.
4536 */
4537 old_idx = cur_idx;
4538 }
4539 return 0;
4540}
4541
4542
4543/* Apply the filter to the status line in the response buffer <rtr>.
4544 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4545 * or -1 if a replacement resulted in an invalid status line.
4546 */
4547int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4548{
4549 char term;
4550 char *cur_ptr, *cur_end;
4551 int done;
4552 struct http_txn *txn = &t->txn;
4553 int len, delta;
4554
4555
Willy Tarreau3d300592007-03-18 18:34:41 +01004556 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004557 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004558 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004559 (exp->action == ACT_ALLOW ||
4560 exp->action == ACT_DENY))
4561 return 0;
4562 else if (exp->action == ACT_REMOVE)
4563 return 0;
4564
4565 done = 0;
4566
Willy Tarreau9cdde232007-05-02 20:58:19 +02004567 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004568 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4569
4570 /* Now we have the status line between cur_ptr and cur_end */
4571
4572 /* The annoying part is that pattern matching needs
4573 * that we modify the contents to null-terminate all
4574 * strings before testing them.
4575 */
4576
4577 term = *cur_end;
4578 *cur_end = '\0';
4579
4580 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4581 switch (exp->action) {
4582 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004583 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004584 done = 1;
4585 break;
4586
4587 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004588 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004589 done = 1;
4590 break;
4591
4592 case ACT_REPLACE:
4593 *cur_end = term; /* restore the string terminator */
4594 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4595 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4596 /* FIXME: if the user adds a newline in the replacement, the
4597 * index will not be recalculated for now, and the new line
4598 * will not be counted as a new header.
4599 */
4600
4601 txn->rsp.eoh += delta;
4602 cur_end += delta;
4603
Willy Tarreau9cdde232007-05-02 20:58:19 +02004604 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004605 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004606 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004607 cur_ptr, cur_end + 1,
4608 NULL, NULL);
4609 if (unlikely(!cur_end))
4610 return -1;
4611
4612 /* we have a full respnse and we know that we have either a CR
4613 * or an LF at <ptr>.
4614 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004615 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004616 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4617 /* there is no point trying this regex on headers */
4618 return 1;
4619 }
4620 }
4621 *cur_end = term; /* restore the string terminator */
4622 return done;
4623}
4624
4625
4626
4627/*
4628 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4629 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4630 * unparsable response.
4631 */
4632int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4633{
Willy Tarreau3d300592007-03-18 18:34:41 +01004634 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004635 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004636 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004637 int ret;
4638
4639 /*
4640 * The interleaving of transformations and verdicts
4641 * makes it difficult to decide to continue or stop
4642 * the evaluation.
4643 */
4644
Willy Tarreau3d300592007-03-18 18:34:41 +01004645 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004646 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4647 exp->action == ACT_PASS)) {
4648 exp = exp->next;
4649 continue;
4650 }
4651
4652 /* Apply the filter to the status line. */
4653 ret = apply_filter_to_sts_line(t, rtr, exp);
4654 if (unlikely(ret < 0))
4655 return -1;
4656
4657 if (likely(ret == 0)) {
4658 /* The filter did not match the response, it can be
4659 * iterated through all headers.
4660 */
4661 apply_filter_to_resp_headers(t, rtr, exp);
4662 }
4663 exp = exp->next;
4664 }
4665 return 0;
4666}
4667
4668
4669
4670/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004671 * Manage server-side cookies. It can impact performance by about 2% so it is
4672 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673 */
4674void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4675{
4676 struct http_txn *txn = &t->txn;
4677 char *p1, *p2, *p3, *p4;
4678
4679 appsess *asession_temp = NULL;
4680 appsess local_asession;
4681
4682 char *cur_ptr, *cur_end, *cur_next;
4683 int cur_idx, old_idx, delta;
4684
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 /* Iterate through the headers.
4686 * we start with the start line.
4687 */
4688 old_idx = 0;
4689 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4690
4691 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4692 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004693 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694
4695 cur_hdr = &txn->hdr_idx.v[cur_idx];
4696 cur_ptr = cur_next;
4697 cur_end = cur_ptr + cur_hdr->len;
4698 cur_next = cur_end + cur_hdr->cr + 1;
4699
4700 /* We have one full header between cur_ptr and cur_end, and the
4701 * next header starts at cur_next. We're only interested in
4702 * "Cookie:" headers.
4703 */
4704
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004705 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4706 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004707 old_idx = cur_idx;
4708 continue;
4709 }
4710
4711 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004712 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004713
4714
4715 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004716 if (t->be->cookie_name == NULL &&
4717 t->be->appsession_name == NULL &&
4718 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004719 return;
4720
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004721 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004722
4723 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004724 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4725 break;
4726
4727 /* p1 is at the beginning of the cookie name */
4728 p2 = p1;
4729
4730 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4731 p2++;
4732
4733 if (p2 == cur_end || *p2 == ';') /* next cookie */
4734 break;
4735
4736 p3 = p2 + 1; /* skip the '=' sign */
4737 if (p3 == cur_end)
4738 break;
4739
4740 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004741 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004742 p4++;
4743
4744 /* here, we have the cookie name between p1 and p2,
4745 * and its value between p3 and p4.
4746 * we can process it.
4747 */
4748
4749 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004750 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004751 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004752 (p4 - p1 >= t->be->capture_namelen) &&
4753 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004754 int log_len = p4 - p1;
4755
Willy Tarreau086b3b42007-05-13 21:45:51 +02004756 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004757 Alert("HTTP logging : out of memory.\n");
4758 }
4759
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004760 if (log_len > t->be->capture_len)
4761 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004762 memcpy(txn->srv_cookie, p1, log_len);
4763 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004764 }
4765
4766 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004767 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4768 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004770 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004771
4772 /* If the cookie is in insert mode on a known server, we'll delete
4773 * this occurrence because we'll insert another one later.
4774 * We'll delete it too if the "indirect" option is set and we're in
4775 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004776 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4777 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004778 /* this header must be deleted */
4779 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4780 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4781 txn->hdr_idx.used--;
4782 cur_hdr->len = 0;
4783 cur_next += delta;
4784 txn->rsp.eoh += delta;
4785
Willy Tarreau3d300592007-03-18 18:34:41 +01004786 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004787 }
4788 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004789 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004790 /* replace bytes p3->p4 with the cookie name associated
4791 * with this server since we know it.
4792 */
4793 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4794 cur_hdr->len += delta;
4795 cur_next += delta;
4796 txn->rsp.eoh += delta;
4797
Willy Tarreau3d300592007-03-18 18:34:41 +01004798 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799 }
4800 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004801 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004802 /* insert the cookie name associated with this server
4803 * before existing cookie, and insert a delimitor between them..
4804 */
4805 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4806 cur_hdr->len += delta;
4807 cur_next += delta;
4808 txn->rsp.eoh += delta;
4809
4810 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004811 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812 }
4813 }
4814 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004815 else if ((t->be->appsession_name != NULL) &&
4816 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004817
4818 /* Cool... it's the right one */
4819
4820 size_t server_id_len = strlen(t->srv->id) + 1;
4821 asession_temp = &local_asession;
4822
Willy Tarreau63963c62007-05-13 21:29:55 +02004823 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004824 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4825 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4826 return;
4827 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004828 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4829 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004830 asession_temp->serverid = NULL;
4831
4832 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004833 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4834 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004835 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004836 Alert("Not enough Memory process_srv():asession:calloc().\n");
4837 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4838 return;
4839 }
4840 asession_temp->sessid = local_asession.sessid;
4841 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004842 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004843 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4844 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004845 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004846 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004847 }
4848
Willy Tarreaua15645d2007-03-18 16:22:39 +01004849 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004850 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4852 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4853 return;
4854 }
4855 asession_temp->serverid[0] = '\0';
4856 }
4857
4858 if (asession_temp->serverid[0] == '\0')
4859 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4860
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004861 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004862 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004863#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004864 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004865 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004866#endif
4867 }/* end if ((t->proxy->appsession_name != NULL) ... */
4868 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4869 } /* we're now at the end of the cookie value */
4870
4871 /* keep the link from this header to next one */
4872 old_idx = cur_idx;
4873 } /* end of cookie processing on this header */
4874}
4875
4876
4877
4878/*
4879 * Check if response is cacheable or not. Updates t->flags.
4880 */
4881void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4882{
4883 struct http_txn *txn = &t->txn;
4884 char *p1, *p2;
4885
4886 char *cur_ptr, *cur_end, *cur_next;
4887 int cur_idx;
4888
Willy Tarreau5df51872007-11-25 16:20:08 +01004889 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004890 return;
4891
4892 /* Iterate through the headers.
4893 * we start with the start line.
4894 */
4895 cur_idx = 0;
4896 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4897
4898 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4899 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004900 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004901
4902 cur_hdr = &txn->hdr_idx.v[cur_idx];
4903 cur_ptr = cur_next;
4904 cur_end = cur_ptr + cur_hdr->len;
4905 cur_next = cur_end + cur_hdr->cr + 1;
4906
4907 /* We have one full header between cur_ptr and cur_end, and the
4908 * next header starts at cur_next. We're only interested in
4909 * "Cookie:" headers.
4910 */
4911
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004912 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4913 if (val) {
4914 if ((cur_end - (cur_ptr + val) >= 8) &&
4915 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4916 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4917 return;
4918 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004919 }
4920
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004921 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4922 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004923 continue;
4924
4925 /* OK, right now we know we have a cache-control header at cur_ptr */
4926
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004927 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004928
4929 if (p1 >= cur_end) /* no more info */
4930 continue;
4931
4932 /* p1 is at the beginning of the value */
4933 p2 = p1;
4934
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004935 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004936 p2++;
4937
4938 /* we have a complete value between p1 and p2 */
4939 if (p2 < cur_end && *p2 == '=') {
4940 /* we have something of the form no-cache="set-cookie" */
4941 if ((cur_end - p1 >= 21) &&
4942 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4943 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004944 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945 continue;
4946 }
4947
4948 /* OK, so we know that either p2 points to the end of string or to a comma */
4949 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4950 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4951 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4952 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004953 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004954 return;
4955 }
4956
4957 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004958 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004959 continue;
4960 }
4961 }
4962}
4963
4964
Willy Tarreau58f10d72006-12-04 02:26:12 +01004965/*
4966 * Try to retrieve a known appsession in the URI, then the associated server.
4967 * If the server is found, it's assigned to the session.
4968 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004969void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004970{
Willy Tarreau3d300592007-03-18 18:34:41 +01004971 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004972 appsess *asession_temp = NULL;
4973 appsess local_asession;
4974 char *request_line;
4975
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004976 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004977 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004978 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004979 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004980 return;
4981
4982 /* skip ';' */
4983 request_line++;
4984
4985 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004986 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987 return;
4988
4989 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004990 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991
4992 /* First try if we already have an appsession */
4993 asession_temp = &local_asession;
4994
Willy Tarreau63963c62007-05-13 21:29:55 +02004995 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004996 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4997 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4998 return;
4999 }
5000
5001 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005002 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5003 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005004 asession_temp->serverid = NULL;
5005
5006 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005007 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5008 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005009 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005010 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005011 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005012 Alert("Not enough memory process_cli():asession:calloc().\n");
5013 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5014 return;
5015 }
5016 asession_temp->sessid = local_asession.sessid;
5017 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005018 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005019 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005020 }
5021 else {
5022 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005023 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005024 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005025
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005026 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005027 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005028
Willy Tarreau58f10d72006-12-04 02:26:12 +01005029#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005030 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005031 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005032#endif
5033 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005034 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005035 Alert("Found Application Session without matching server.\n");
5036 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005037 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005038 while (srv) {
5039 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005040 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005041 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005042 txn->flags &= ~TX_CK_MASK;
5043 txn->flags |= TX_CK_VALID;
5044 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005045 t->srv = srv;
5046 break;
5047 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005048 txn->flags &= ~TX_CK_MASK;
5049 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005050 }
5051 }
5052 srv = srv->next;
5053 }
5054 }
5055}
5056
5057
Willy Tarreaub2513902006-12-17 14:52:38 +01005058/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005059 * In a GET or HEAD request, check if the requested URI matches the stats uri
5060 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005061 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005062 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005063 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005064 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005065 *
5066 * Returns 1 if the session's state changes, otherwise 0.
5067 */
5068int stats_check_uri_auth(struct session *t, struct proxy *backend)
5069{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005070 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005071 struct uri_auth *uri_auth = backend->uri_auth;
5072 struct user_auth *user;
5073 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005074 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005075
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005076 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005079 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005080 return 0;
5081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005082 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005083
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005084 /* the URI is in h */
5085 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005086 return 0;
5087
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005088 h += uri_auth->uri_len;
5089 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5090 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005091 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005092 break;
5093 }
5094 h++;
5095 }
5096
5097 if (uri_auth->refresh) {
5098 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5099 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5100 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005101 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005102 break;
5103 }
5104 h++;
5105 }
5106 }
5107
Willy Tarreau55bb8452007-10-17 18:44:57 +02005108 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5109 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5110 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005111 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005112 break;
5113 }
5114 h++;
5115 }
5116
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005117 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5118
Willy Tarreaub2513902006-12-17 14:52:38 +01005119 /* we are in front of a interceptable URI. Let's check
5120 * if there's an authentication and if it's valid.
5121 */
5122 user = uri_auth->users;
5123 if (!user) {
5124 /* no user auth required, it's OK */
5125 authenticated = 1;
5126 } else {
5127 authenticated = 0;
5128
5129 /* a user list is defined, we have to check.
5130 * skip 21 chars for "Authorization: Basic ".
5131 */
5132
5133 /* FIXME: this should move to an earlier place */
5134 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005135 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5136 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5137 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005138 if (len > 14 &&
5139 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005140 txn->auth_hdr.str = h;
5141 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005142 break;
5143 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005144 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005145 }
5146
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005147 if (txn->auth_hdr.len < 21 ||
5148 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005149 user = NULL;
5150
5151 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005152 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5153 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005154 user->user_pwd, user->user_len)) {
5155 authenticated = 1;
5156 break;
5157 }
5158 user = user->next;
5159 }
5160 }
5161
5162 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005163 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005164
5165 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005166 msg.str = trash;
5167 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005168 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005169 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005170 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005171 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005172 if (!(t->flags & SN_ERR_MASK))
5173 t->flags |= SN_ERR_PRXCOND;
5174 if (!(t->flags & SN_FINST_MASK))
5175 t->flags |= SN_FINST_R;
5176 return 1;
5177 }
5178
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005179 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005180 * data.
5181 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005182 EV_FD_CLR(t->cli_fd, DIR_RD);
5183 buffer_shutr(t->req);
5184 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005185 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005186 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005187 t->data_source = DATA_SRC_STATS;
5188 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005189 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005190 produce_content(t);
5191 return 1;
5192}
5193
5194
Willy Tarreaubaaee002006-06-26 02:48:02 +02005195/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005196 * Print a debug line with a header
5197 */
5198void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5199{
5200 int len, max;
5201 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5202 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5203 max = end - start;
5204 UBOUND(max, sizeof(trash) - len - 1);
5205 len += strlcpy2(trash + len, start, max + 1);
5206 trash[len++] = '\n';
5207 write(1, trash, len);
5208}
5209
5210
Willy Tarreau8797c062007-05-07 00:55:35 +02005211/************************************************************************/
5212/* The code below is dedicated to ACL parsing and matching */
5213/************************************************************************/
5214
5215
5216
5217
5218/* 1. Check on METHOD
5219 * We use the pre-parsed method if it is known, and store its number as an
5220 * integer. If it is unknown, we use the pointer and the length.
5221 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005222static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005223{
5224 int len, meth;
5225
Willy Tarreauae8b7962007-06-09 23:10:04 +02005226 len = strlen(*text);
5227 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005228
5229 pattern->val.i = meth;
5230 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005231 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005232 if (!pattern->ptr.str)
5233 return 0;
5234 pattern->len = len;
5235 }
5236 return 1;
5237}
5238
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005239static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005240acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5241 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005242{
5243 int meth;
5244 struct http_txn *txn = l7;
5245
Willy Tarreaub6866442008-07-14 23:54:42 +02005246 if (!txn)
5247 return 0;
5248
Willy Tarreauc11416f2007-06-17 16:58:38 +02005249 if (txn->req.msg_state != HTTP_MSG_BODY)
5250 return 0;
5251
Willy Tarreau8797c062007-05-07 00:55:35 +02005252 meth = txn->meth;
5253 test->i = meth;
5254 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005255 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5256 /* ensure the indexes are not affected */
5257 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005258 test->len = txn->req.sl.rq.m_l;
5259 test->ptr = txn->req.sol;
5260 }
5261 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5262 return 1;
5263}
5264
5265static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5266{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005267 int icase;
5268
Willy Tarreau8797c062007-05-07 00:55:35 +02005269 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005270 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005271
5272 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005273 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005274
5275 /* Other method, we must compare the strings */
5276 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005277 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005278
5279 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5280 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5281 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005282 return ACL_PAT_FAIL;
5283 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005284}
5285
5286/* 2. Check on Request/Status Version
5287 * We simply compare strings here.
5288 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005289static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005290{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005291 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005292 if (!pattern->ptr.str)
5293 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005294 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005295 return 1;
5296}
5297
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005298static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005299acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5300 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005301{
5302 struct http_txn *txn = l7;
5303 char *ptr;
5304 int len;
5305
Willy Tarreaub6866442008-07-14 23:54:42 +02005306 if (!txn)
5307 return 0;
5308
Willy Tarreauc11416f2007-06-17 16:58:38 +02005309 if (txn->req.msg_state != HTTP_MSG_BODY)
5310 return 0;
5311
Willy Tarreau8797c062007-05-07 00:55:35 +02005312 len = txn->req.sl.rq.v_l;
5313 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5314
5315 while ((len-- > 0) && (*ptr++ != '/'));
5316 if (len <= 0)
5317 return 0;
5318
5319 test->ptr = ptr;
5320 test->len = len;
5321
5322 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5323 return 1;
5324}
5325
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005326static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005327acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5328 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005329{
5330 struct http_txn *txn = l7;
5331 char *ptr;
5332 int len;
5333
Willy Tarreaub6866442008-07-14 23:54:42 +02005334 if (!txn)
5335 return 0;
5336
Willy Tarreauc11416f2007-06-17 16:58:38 +02005337 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5338 return 0;
5339
Willy Tarreau8797c062007-05-07 00:55:35 +02005340 len = txn->rsp.sl.st.v_l;
5341 ptr = txn->rsp.sol;
5342
5343 while ((len-- > 0) && (*ptr++ != '/'));
5344 if (len <= 0)
5345 return 0;
5346
5347 test->ptr = ptr;
5348 test->len = len;
5349
5350 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5351 return 1;
5352}
5353
5354/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005355static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005356acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5357 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005358{
5359 struct http_txn *txn = l7;
5360 char *ptr;
5361 int len;
5362
Willy Tarreaub6866442008-07-14 23:54:42 +02005363 if (!txn)
5364 return 0;
5365
Willy Tarreauc11416f2007-06-17 16:58:38 +02005366 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5367 return 0;
5368
Willy Tarreau8797c062007-05-07 00:55:35 +02005369 len = txn->rsp.sl.st.c_l;
5370 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5371
5372 test->i = __strl2ui(ptr, len);
5373 test->flags = ACL_TEST_F_VOL_1ST;
5374 return 1;
5375}
5376
5377/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005378static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005379acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5380 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005381{
5382 struct http_txn *txn = l7;
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->req.msg_state != HTTP_MSG_BODY)
5388 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005389
Willy Tarreauc11416f2007-06-17 16:58:38 +02005390 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5391 /* ensure the indexes are not affected */
5392 return 0;
5393
Willy Tarreau8797c062007-05-07 00:55:35 +02005394 test->len = txn->req.sl.rq.u_l;
5395 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5396
Willy Tarreauf3d25982007-05-08 22:45:09 +02005397 /* we do not need to set READ_ONLY because the data is in a buffer */
5398 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005399 return 1;
5400}
5401
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005402static int
5403acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5404 struct acl_expr *expr, struct acl_test *test)
5405{
5406 struct http_txn *txn = l7;
5407
Willy Tarreaub6866442008-07-14 23:54:42 +02005408 if (!txn)
5409 return 0;
5410
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005411 if (txn->req.msg_state != HTTP_MSG_BODY)
5412 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005413
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005414 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5415 /* ensure the indexes are not affected */
5416 return 0;
5417
5418 /* Parse HTTP request */
5419 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5420 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5421 test->i = AF_INET;
5422
5423 /*
5424 * If we are parsing url in frontend space, we prepare backend stage
5425 * to not parse again the same url ! optimization lazyness...
5426 */
5427 if (px->options & PR_O_HTTP_PROXY)
5428 l4->flags |= SN_ADDR_SET;
5429
5430 test->flags = ACL_TEST_F_READ_ONLY;
5431 return 1;
5432}
5433
5434static int
5435acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5436 struct acl_expr *expr, struct acl_test *test)
5437{
5438 struct http_txn *txn = l7;
5439
Willy Tarreaub6866442008-07-14 23:54:42 +02005440 if (!txn)
5441 return 0;
5442
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005443 if (txn->req.msg_state != HTTP_MSG_BODY)
5444 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005445
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005446 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5447 /* ensure the indexes are not affected */
5448 return 0;
5449
5450 /* Same optimization as url_ip */
5451 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5452 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5453
5454 if (px->options & PR_O_HTTP_PROXY)
5455 l4->flags |= SN_ADDR_SET;
5456
5457 test->flags = ACL_TEST_F_READ_ONLY;
5458 return 1;
5459}
5460
Willy Tarreauc11416f2007-06-17 16:58:38 +02005461/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5462 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5463 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005464static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005465acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005466 struct acl_expr *expr, struct acl_test *test)
5467{
5468 struct http_txn *txn = l7;
5469 struct hdr_idx *idx = &txn->hdr_idx;
5470 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005471
Willy Tarreaub6866442008-07-14 23:54:42 +02005472 if (!txn)
5473 return 0;
5474
Willy Tarreau33a7e692007-06-10 19:45:56 +02005475 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5476 /* search for header from the beginning */
5477 ctx->idx = 0;
5478
Willy Tarreau33a7e692007-06-10 19:45:56 +02005479 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5480 test->flags |= ACL_TEST_F_FETCH_MORE;
5481 test->flags |= ACL_TEST_F_VOL_HDR;
5482 test->len = ctx->vlen;
5483 test->ptr = (char *)ctx->line + ctx->val;
5484 return 1;
5485 }
5486
5487 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5488 test->flags |= ACL_TEST_F_VOL_HDR;
5489 return 0;
5490}
5491
Willy Tarreau33a7e692007-06-10 19:45:56 +02005492static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005493acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5494 struct acl_expr *expr, struct acl_test *test)
5495{
5496 struct http_txn *txn = l7;
5497
Willy Tarreaub6866442008-07-14 23:54:42 +02005498 if (!txn)
5499 return 0;
5500
Willy Tarreauc11416f2007-06-17 16:58:38 +02005501 if (txn->req.msg_state != HTTP_MSG_BODY)
5502 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005503
Willy Tarreauc11416f2007-06-17 16:58:38 +02005504 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5505 /* ensure the indexes are not affected */
5506 return 0;
5507
5508 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5509}
5510
5511static int
5512acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5513 struct acl_expr *expr, struct acl_test *test)
5514{
5515 struct http_txn *txn = l7;
5516
Willy Tarreaub6866442008-07-14 23:54:42 +02005517 if (!txn)
5518 return 0;
5519
Willy Tarreauc11416f2007-06-17 16:58:38 +02005520 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5521 return 0;
5522
5523 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5524}
5525
5526/* 6. Check on HTTP header count. The number of occurrences is returned.
5527 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5528 */
5529static int
5530acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005531 struct acl_expr *expr, struct acl_test *test)
5532{
5533 struct http_txn *txn = l7;
5534 struct hdr_idx *idx = &txn->hdr_idx;
5535 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005536 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005537
Willy Tarreaub6866442008-07-14 23:54:42 +02005538 if (!txn)
5539 return 0;
5540
Willy Tarreau33a7e692007-06-10 19:45:56 +02005541 ctx.idx = 0;
5542 cnt = 0;
5543 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5544 cnt++;
5545
5546 test->i = cnt;
5547 test->flags = ACL_TEST_F_VOL_HDR;
5548 return 1;
5549}
5550
Willy Tarreauc11416f2007-06-17 16:58:38 +02005551static int
5552acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5553 struct acl_expr *expr, struct acl_test *test)
5554{
5555 struct http_txn *txn = l7;
5556
Willy Tarreaub6866442008-07-14 23:54:42 +02005557 if (!txn)
5558 return 0;
5559
Willy Tarreauc11416f2007-06-17 16:58:38 +02005560 if (txn->req.msg_state != HTTP_MSG_BODY)
5561 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005562
Willy Tarreauc11416f2007-06-17 16:58:38 +02005563 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5564 /* ensure the indexes are not affected */
5565 return 0;
5566
5567 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5568}
5569
5570static int
5571acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5572 struct acl_expr *expr, struct acl_test *test)
5573{
5574 struct http_txn *txn = l7;
5575
Willy Tarreaub6866442008-07-14 23:54:42 +02005576 if (!txn)
5577 return 0;
5578
Willy Tarreauc11416f2007-06-17 16:58:38 +02005579 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5580 return 0;
5581
5582 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5583}
5584
Willy Tarreau33a7e692007-06-10 19:45:56 +02005585/* 7. Check on HTTP header's integer value. The integer value is returned.
5586 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005587 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005588 */
5589static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005590acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005591 struct acl_expr *expr, struct acl_test *test)
5592{
5593 struct http_txn *txn = l7;
5594 struct hdr_idx *idx = &txn->hdr_idx;
5595 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005596
Willy Tarreaub6866442008-07-14 23:54:42 +02005597 if (!txn)
5598 return 0;
5599
Willy Tarreau33a7e692007-06-10 19:45:56 +02005600 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5601 /* search for header from the beginning */
5602 ctx->idx = 0;
5603
Willy Tarreau33a7e692007-06-10 19:45:56 +02005604 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5605 test->flags |= ACL_TEST_F_FETCH_MORE;
5606 test->flags |= ACL_TEST_F_VOL_HDR;
5607 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5608 return 1;
5609 }
5610
5611 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5612 test->flags |= ACL_TEST_F_VOL_HDR;
5613 return 0;
5614}
5615
Willy Tarreauc11416f2007-06-17 16:58:38 +02005616static int
5617acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5618 struct acl_expr *expr, struct acl_test *test)
5619{
5620 struct http_txn *txn = l7;
5621
Willy Tarreaub6866442008-07-14 23:54:42 +02005622 if (!txn)
5623 return 0;
5624
Willy Tarreauc11416f2007-06-17 16:58:38 +02005625 if (txn->req.msg_state != HTTP_MSG_BODY)
5626 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005627
Willy Tarreauc11416f2007-06-17 16:58:38 +02005628 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5629 /* ensure the indexes are not affected */
5630 return 0;
5631
5632 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5633}
5634
5635static int
5636acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5637 struct acl_expr *expr, struct acl_test *test)
5638{
5639 struct http_txn *txn = l7;
5640
Willy Tarreaub6866442008-07-14 23:54:42 +02005641 if (!txn)
5642 return 0;
5643
Willy Tarreauc11416f2007-06-17 16:58:38 +02005644 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5645 return 0;
5646
5647 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5648}
5649
Willy Tarreau737b0c12007-06-10 21:28:46 +02005650/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5651 * the first '/' after the possible hostname, and ends before the possible '?'.
5652 */
5653static int
5654acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5655 struct acl_expr *expr, struct acl_test *test)
5656{
5657 struct http_txn *txn = l7;
5658 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005659
Willy Tarreaub6866442008-07-14 23:54:42 +02005660 if (!txn)
5661 return 0;
5662
Willy Tarreauc11416f2007-06-17 16:58:38 +02005663 if (txn->req.msg_state != HTTP_MSG_BODY)
5664 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005665
Willy Tarreauc11416f2007-06-17 16:58:38 +02005666 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5667 /* ensure the indexes are not affected */
5668 return 0;
5669
Willy Tarreau21d2af32008-02-14 20:25:24 +01005670 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5671 ptr = http_get_path(txn);
5672 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005673 return 0;
5674
5675 /* OK, we got the '/' ! */
5676 test->ptr = ptr;
5677
5678 while (ptr < end && *ptr != '?')
5679 ptr++;
5680
5681 test->len = ptr - test->ptr;
5682
5683 /* we do not need to set READ_ONLY because the data is in a buffer */
5684 test->flags = ACL_TEST_F_VOL_1ST;
5685 return 1;
5686}
5687
5688
Willy Tarreau8797c062007-05-07 00:55:35 +02005689
5690/************************************************************************/
5691/* All supported keywords must be declared here. */
5692/************************************************************************/
5693
5694/* Note: must not be declared <const> as its list will be overwritten */
5695static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005696 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5697 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5698 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5699 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005700
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005701 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5702 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5703 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5704 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5705 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5706 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5707 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5708 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5709 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005710
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005711 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5712 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5713 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5714 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5715 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5716 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5717 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5718 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5719 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5720 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005721
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005722 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5723 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5724 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5725 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5726 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5727 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5728 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5729 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5730 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005731
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005732 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5733 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5734 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5735 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5736 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5737 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5738 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005739
Willy Tarreauf3d25982007-05-08 22:45:09 +02005740 { NULL, NULL, NULL, NULL },
5741
5742#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005743 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5744 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5745 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5746 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5747 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5748 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5749 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5750
Willy Tarreau8797c062007-05-07 00:55:35 +02005751 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5752 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5753 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5754 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5755 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5756 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5757 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5758 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5759
5760 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5761 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5762 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5763 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5764 { NULL, NULL, NULL, NULL },
5765#endif
5766}};
5767
5768
5769__attribute__((constructor))
5770static void __http_protocol_init(void)
5771{
5772 acl_register_keywords(&acl_kws);
5773}
5774
5775
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005777 * Local variables:
5778 * c-indent-level: 8
5779 * c-basic-offset: 8
5780 * End:
5781 */