blob: 36bf1c5d9a137836258b2826675f30824f67022d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200549 buffer_shutw(t->req);
550 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaudafde432008-08-17 01:00:46 +0200648/* Processes the client, server, request and response jobs of a session task,
649 * then puts it back to the wait queue in a clean state, or cleans up its
650 * resources if it must be deleted. Returns in <next> the date the task wants
651 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
652 * nothing too many times, the request and response buffers flags are monitored
653 * and each function is called only if at least another function has changed at
654 * least one flag. If one of the functions called returns non-zero, then it
655 * will be called once again after all other functions. This permits explicit
656 * external loops which may be useful for complex state machines.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 */
Willy Tarreaudafde432008-08-17 01:00:46 +0200658#define PROCESS_CLI 0x1
659#define PROCESS_SRV 0x2
660#define PROCESS_REQ 0x4
661#define PROCESS_RTR 0x8
662#define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR)
663
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200664void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
666 struct session *s = t->context;
Willy Tarreaudafde432008-08-17 01:00:46 +0200667 unsigned resync = PROCESS_ALL;
668 unsigned int rqf;
669 unsigned int rpf;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
Willy Tarreau507385d2008-08-17 13:04:25 +0200671 /* check timeout expiration only once and adjust buffer flags
672 * accordingly.
673 */
674 if (unlikely(tick_is_expired(t->expire, now_ms))) {
675 if (tick_is_expired(s->req->rex, now_ms))
676 s->req->flags |= BF_READ_TIMEOUT;
677
678 if (tick_is_expired(s->req->wex, now_ms))
679 s->req->flags |= BF_WRITE_TIMEOUT;
680
681 if (tick_is_expired(s->rep->rex, now_ms))
682 s->rep->flags |= BF_READ_TIMEOUT;
683
684 if (tick_is_expired(s->rep->wex, now_ms))
685 s->rep->flags |= BF_WRITE_TIMEOUT;
686 }
687
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 do {
Willy Tarreaudafde432008-08-17 01:00:46 +0200689 if (resync & PROCESS_REQ) {
690 resync &= ~PROCESS_REQ;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200691 if (s->req->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200692 rqf = s->req->flags;
693 rpf = s->rep->flags;
694
695 if (process_request(s))
696 resync |= PROCESS_REQ;
697
Willy Tarreau2df28e82008-08-17 15:20:19 +0200698 if (!s->req->analysers && !(s->req->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200699 s->req->flags |= BF_MAY_FORWARD;
700
701 if (rqf != s->req->flags || rpf != s->rep->flags)
702 resync |= PROCESS_ALL & ~PROCESS_REQ;
703 }
704 }
705
706 if (resync & PROCESS_RTR) {
707 resync &= ~PROCESS_RTR;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200708 if (s->rep->analysers) {
Willy Tarreaudafde432008-08-17 01:00:46 +0200709 rqf = s->req->flags;
710 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200711
Willy Tarreaudafde432008-08-17 01:00:46 +0200712 if (process_response(s))
713 resync |= PROCESS_RTR;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200714
Willy Tarreau2df28e82008-08-17 15:20:19 +0200715 if (!s->rep->analysers && !(s->rep->flags & BF_MAY_FORWARD))
Willy Tarreaudafde432008-08-17 01:00:46 +0200716 s->rep->flags |= BF_MAY_FORWARD;
717
718 if (rqf != s->req->flags || rpf != s->rep->flags)
719 resync |= PROCESS_ALL & ~PROCESS_RTR;
720 }
721 }
722
723 if (resync & PROCESS_CLI) {
724 rqf = s->req->flags;
725 rpf = s->rep->flags;
726
727 resync &= ~PROCESS_CLI;
728 if (process_cli(s))
729 resync |= PROCESS_CLI;
730
731 if (rqf != s->req->flags || rpf != s->rep->flags)
732 resync |= PROCESS_ALL & ~PROCESS_CLI;
733 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200734
Willy Tarreaudafde432008-08-17 01:00:46 +0200735 if (resync & PROCESS_SRV) {
736 rqf = s->req->flags;
737 rpf = s->rep->flags;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200738
Willy Tarreaudafde432008-08-17 01:00:46 +0200739 resync &= ~PROCESS_SRV;
740 if (process_srv(s))
741 resync |= PROCESS_SRV;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200742
Willy Tarreaudafde432008-08-17 01:00:46 +0200743 if (rqf != s->req->flags || rpf != s->rep->flags)
744 resync |= PROCESS_ALL & ~PROCESS_SRV;
745 }
746 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200748 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100749
750 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
751 session_process_counters(s);
752
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200753 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
754 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreau7f875f62008-08-11 17:35:01 +0200756 /* Trick: if a request is being waiting for the server to respond,
757 * and if we know the server can timeout, we don't want the timeout
758 * to expire on the client side first, but we're still interested
759 * in passing data from the client to the server (eg: POST). Thus,
760 * we can cancel the client's request timeout if the server's
761 * request timeout is set and the server has not yet sent a response.
762 */
763
Willy Tarreauba392ce2008-08-16 21:13:23 +0200764 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200765 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200766 s->req->rex = TICK_ETERNITY;
767
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200768 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
769 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreau2df28e82008-08-17 15:20:19 +0200770 if (s->req->analysers) {
771 if (s->req->analysers & AN_REQ_INSPECT)
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200772 t->expire = tick_first(t->expire, s->inspect_exp);
Willy Tarreau2df28e82008-08-17 15:20:19 +0200773 else if (s->req->analysers & AN_REQ_HTTP_HDR)
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200774 t->expire = tick_first(t->expire, s->txn.exp);
775 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776
777 /* restore t to its place in the task list */
778 task_queue(t);
779
Willy Tarreaua7c52762008-08-16 18:40:18 +0200780#ifdef DEBUG_DEV
781 /* this may only happen when no timeout is set or in case of an FSM bug */
782 if (!t->expire)
783 ABORT_NOW();
784#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200785 *next = t->expire;
786 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
788
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100789 s->fe->feconn--;
790 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200791 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 actconn--;
793
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200794 if (unlikely((global.mode & MODE_DEBUG) &&
795 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200797 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 s->uniq_id, s->be->id,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200799 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd,
800 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 write(1, trash, len);
802 }
803
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200804 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100805 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806
807 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200808 if (s->logs.logwait &&
809 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200810 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
811 if (s->fe->to_log & LW_REQ)
812 http_sess_log(s);
813 else
814 tcp_sess_log(s);
815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816
817 /* the task MUST not be in the run queue anymore */
818 task_delete(t);
819 session_free(s);
820 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200821 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822}
823
824
Willy Tarreau42250582007-04-01 01:30:43 +0200825extern const char sess_term_cond[8];
826extern const char sess_fin_state[8];
827extern const char *monthname[12];
828const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
829const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
830 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
831 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200832struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200833struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834
Willy Tarreau42250582007-04-01 01:30:43 +0200835/*
836 * send a log for the session when we have enough info about it.
837 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100838 */
Willy Tarreau42250582007-04-01 01:30:43 +0200839static void http_sess_log(struct session *s)
840{
841 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
842 struct proxy *fe = s->fe;
843 struct proxy *be = s->be;
844 struct proxy *prx_log;
845 struct http_txn *txn = &s->txn;
846 int tolog;
847 char *uri, *h;
848 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200849 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200850 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200851 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200852 int hdr;
853
854 if (fe->logfac1 < 0 && fe->logfac2 < 0)
855 return;
856 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100857
Willy Tarreau42250582007-04-01 01:30:43 +0200858 if (s->cli_addr.ss_family == AF_INET)
859 inet_ntop(AF_INET,
860 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
861 pn, sizeof(pn));
862 else
863 inet_ntop(AF_INET6,
864 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
865 pn, sizeof(pn));
866
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200867 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200868
869 /* FIXME: let's limit ourselves to frontend logging for now. */
870 tolog = fe->to_log;
871
872 h = tmpline;
873 if (fe->to_log & LW_REQHDR &&
874 txn->req.cap &&
875 (h < tmpline + sizeof(tmpline) - 10)) {
876 *(h++) = ' ';
877 *(h++) = '{';
878 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
879 if (hdr)
880 *(h++) = '|';
881 if (txn->req.cap[hdr] != NULL)
882 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
883 '#', hdr_encode_map, txn->req.cap[hdr]);
884 }
885 *(h++) = '}';
886 }
887
888 if (fe->to_log & LW_RSPHDR &&
889 txn->rsp.cap &&
890 (h < tmpline + sizeof(tmpline) - 7)) {
891 *(h++) = ' ';
892 *(h++) = '{';
893 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
894 if (hdr)
895 *(h++) = '|';
896 if (txn->rsp.cap[hdr] != NULL)
897 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
898 '#', hdr_encode_map, txn->rsp.cap[hdr]);
899 }
900 *(h++) = '}';
901 }
902
903 if (h < tmpline + sizeof(tmpline) - 4) {
904 *(h++) = ' ';
905 *(h++) = '"';
906 uri = txn->uri ? txn->uri : "<BADREQ>";
907 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
908 '#', url_encode_map, uri);
909 *(h++) = '"';
910 }
911 *h = '\0';
912
913 svid = (tolog & LW_SVID) ?
914 (s->data_source != DATA_SRC_STATS) ?
915 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
916
Willy Tarreau70089872008-06-13 21:12:51 +0200917 t_request = -1;
918 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
919 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
920
Willy Tarreau42250582007-04-01 01:30:43 +0200921 send_log(prx_log, LOG_INFO,
922 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
923 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100924 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200925 pn,
926 (s->cli_addr.ss_family == AF_INET) ?
927 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
928 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200929 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200930 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200931 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200932 t_request,
933 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200934 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
935 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
936 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
937 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100938 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200939 txn->cli_cookie ? txn->cli_cookie : "-",
940 txn->srv_cookie ? txn->srv_cookie : "-",
941 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
942 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
943 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
944 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
945 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100946 (s->flags & SN_REDISP)?"+":"",
947 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200948 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
949
950 s->logs.logwait = 0;
951}
952
Willy Tarreau117f59e2007-03-04 18:17:17 +0100953
954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001043const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1044 unsigned int state, const char *ptr, const char *end,
1045 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
1047 __label__
1048 http_msg_rpver,
1049 http_msg_rpver_sp,
1050 http_msg_rpcode,
1051 http_msg_rpcode_sp,
1052 http_msg_rpreason,
1053 http_msg_rpline_eol,
1054 http_msg_ood, /* out of data */
1055 http_msg_invalid;
1056
1057 switch (state) {
1058 http_msg_rpver:
1059 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001060 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1062
1063 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001064 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001065 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1066 }
1067 goto http_msg_invalid;
1068
1069 http_msg_rpver_sp:
1070 case HTTP_MSG_RPVER_SP:
1071 if (likely(!HTTP_IS_LWS(*ptr))) {
1072 msg->sl.st.c = ptr - msg_buf;
1073 goto http_msg_rpcode;
1074 }
1075 if (likely(HTTP_IS_SPHT(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1077 /* so it's a CR/LF, this is invalid */
1078 goto http_msg_invalid;
1079
1080 http_msg_rpcode:
1081 case HTTP_MSG_RPCODE:
1082 if (likely(!HTTP_IS_LWS(*ptr)))
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1084
1085 if (likely(HTTP_IS_SPHT(*ptr))) {
1086 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1087 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1088 }
1089
1090 /* so it's a CR/LF, so there is no reason phrase */
1091 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1092 http_msg_rsp_reason:
1093 /* FIXME: should we support HTTP responses without any reason phrase ? */
1094 msg->sl.st.r = ptr - msg_buf;
1095 msg->sl.st.r_l = 0;
1096 goto http_msg_rpline_eol;
1097
1098 http_msg_rpcode_sp:
1099 case HTTP_MSG_RPCODE_SP:
1100 if (likely(!HTTP_IS_LWS(*ptr))) {
1101 msg->sl.st.r = ptr - msg_buf;
1102 goto http_msg_rpreason;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1106 /* so it's a CR/LF, so there is no reason phrase */
1107 goto http_msg_rsp_reason;
1108
1109 http_msg_rpreason:
1110 case HTTP_MSG_RPREASON:
1111 if (likely(!HTTP_IS_CRLF(*ptr)))
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1113 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1114 http_msg_rpline_eol:
1115 /* We have seen the end of line. Note that we do not
1116 * necessarily have the \n yet, but at least we know that we
1117 * have EITHER \r OR \n, otherwise the response would not be
1118 * complete. We can then record the response length and return
1119 * to the caller which will be able to register it.
1120 */
1121 msg->sl.st.l = ptr - msg->sol;
1122 return ptr;
1123
1124#ifdef DEBUG_FULL
1125 default:
1126 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1127 exit(1);
1128#endif
1129 }
1130
1131 http_msg_ood:
1132 /* out of data */
1133 if (ret_state)
1134 *ret_state = state;
1135 if (ret_ptr)
1136 *ret_ptr = (char *)ptr;
1137 return NULL;
1138
1139 http_msg_invalid:
1140 /* invalid message */
1141 if (ret_state)
1142 *ret_state = HTTP_MSG_ERROR;
1143 return NULL;
1144}
1145
1146
1147/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 * This function parses a request line between <ptr> and <end>, starting with
1149 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1150 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1151 * will give undefined results.
1152 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1153 * and that msg->sol points to the beginning of the request.
1154 * If a complete line is found (which implies that at least one CR or LF is
1155 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1156 * returned indicating an incomplete line (which does not mean that parts have
1157 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1158 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1159 * upon next call.
1160 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001161 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1163 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001164 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001166const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1167 unsigned int state, const char *ptr, const char *end,
1168 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 __label__
1171 http_msg_rqmeth,
1172 http_msg_rqmeth_sp,
1173 http_msg_rquri,
1174 http_msg_rquri_sp,
1175 http_msg_rqver,
1176 http_msg_rqline_eol,
1177 http_msg_ood, /* out of data */
1178 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 switch (state) {
1181 http_msg_rqmeth:
1182 case HTTP_MSG_RQMETH:
1183 if (likely(HTTP_IS_TOKEN(*ptr)))
1184 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001187 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1189 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001191 if (likely(HTTP_IS_CRLF(*ptr))) {
1192 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001193 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_req09_uri:
1195 msg->sl.rq.u = ptr - msg_buf;
1196 http_msg_req09_uri_e:
1197 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1198 http_msg_req09_ver:
1199 msg->sl.rq.v = ptr - msg_buf;
1200 msg->sl.rq.v_l = 0;
1201 goto http_msg_rqline_eol;
1202 }
1203 goto http_msg_invalid;
1204
1205 http_msg_rqmeth_sp:
1206 case HTTP_MSG_RQMETH_SP:
1207 if (likely(!HTTP_IS_LWS(*ptr))) {
1208 msg->sl.rq.u = ptr - msg_buf;
1209 goto http_msg_rquri;
1210 }
1211 if (likely(HTTP_IS_SPHT(*ptr)))
1212 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1213 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1214 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001215
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 http_msg_rquri:
1217 case HTTP_MSG_RQURI:
1218 if (likely(!HTTP_IS_LWS(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001220
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221 if (likely(HTTP_IS_SPHT(*ptr))) {
1222 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1227 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 http_msg_rquri_sp:
1230 case HTTP_MSG_RQURI_SP:
1231 if (likely(!HTTP_IS_LWS(*ptr))) {
1232 msg->sl.rq.v = ptr - msg_buf;
1233 goto http_msg_rqver;
1234 }
1235 if (likely(HTTP_IS_SPHT(*ptr)))
1236 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1237 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1238 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 http_msg_rqver:
1241 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001242 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244
1245 if (likely(HTTP_IS_CRLF(*ptr))) {
1246 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1247 http_msg_rqline_eol:
1248 /* We have seen the end of line. Note that we do not
1249 * necessarily have the \n yet, but at least we know that we
1250 * have EITHER \r OR \n, otherwise the request would not be
1251 * complete. We can then record the request length and return
1252 * to the caller which will be able to register it.
1253 */
1254 msg->sl.rq.l = ptr - msg->sol;
1255 return ptr;
1256 }
1257
1258 /* neither an HTTP_VER token nor a CRLF */
1259 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261#ifdef DEBUG_FULL
1262 default:
1263 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1264 exit(1);
1265#endif
1266 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 http_msg_ood:
1269 /* out of data */
1270 if (ret_state)
1271 *ret_state = state;
1272 if (ret_ptr)
1273 *ret_ptr = (char *)ptr;
1274 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 http_msg_invalid:
1277 /* invalid message */
1278 if (ret_state)
1279 *ret_state = HTTP_MSG_ERROR;
1280 return NULL;
1281}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
1283
Willy Tarreau8973c702007-01-21 23:58:29 +01001284/*
1285 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 * when data are missing and recalled at the exact same location with no
1288 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1290 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1293{
1294 __label__
1295 http_msg_rqbefore,
1296 http_msg_rqbefore_cr,
1297 http_msg_rqmeth,
1298 http_msg_rqline_end,
1299 http_msg_hdr_first,
1300 http_msg_hdr_name,
1301 http_msg_hdr_l1_sp,
1302 http_msg_hdr_l1_lf,
1303 http_msg_hdr_l1_lws,
1304 http_msg_hdr_val,
1305 http_msg_hdr_l2_lf,
1306 http_msg_hdr_l2_lws,
1307 http_msg_complete_header,
1308 http_msg_last_lf,
1309 http_msg_ood, /* out of data */
1310 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001311
Willy Tarreaue69eada2008-01-27 00:34:10 +01001312 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001315 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 ptr = buf->lr;
1317 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (unlikely(ptr >= end))
1320 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 /*
1324 * First, states that are specific to the response only.
1325 * We check them first so that request and headers are
1326 * closer to each other (accessed more often).
1327 */
1328 http_msg_rpbefore:
1329 case HTTP_MSG_RPBEFORE:
1330 if (likely(HTTP_IS_TOKEN(*ptr))) {
1331 if (likely(ptr == buf->data)) {
1332 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001333 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 } else {
1335#if PARSE_PRESERVE_EMPTY_LINES
1336 /* only skip empty leading lines, don't remove them */
1337 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001338 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001339#else
1340 /* Remove empty leading lines, as recommended by
1341 * RFC2616. This takes a lot of time because we
1342 * must move all the buffer backwards, but this
1343 * is rarely needed. The method above will be
1344 * cleaner when we'll be able to start sending
1345 * the request from any place in the buffer.
1346 */
1347 buf->lr = ptr;
1348 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001349 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 msg->sol = buf->data;
1351 ptr = buf->data;
1352 end = buf->r;
1353#endif
1354 }
1355 hdr_idx_init(idx);
1356 state = HTTP_MSG_RPVER;
1357 goto http_msg_rpver;
1358 }
1359
1360 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1361 goto http_msg_invalid;
1362
1363 if (unlikely(*ptr == '\n'))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1365 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1366 /* stop here */
1367
1368 http_msg_rpbefore_cr:
1369 case HTTP_MSG_RPBEFORE_CR:
1370 EXPECT_LF_HERE(ptr, http_msg_invalid);
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1372 /* stop here */
1373
1374 http_msg_rpver:
1375 case HTTP_MSG_RPVER:
1376 case HTTP_MSG_RPVER_SP:
1377 case HTTP_MSG_RPCODE:
1378 case HTTP_MSG_RPCODE_SP:
1379 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001380 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001381 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 if (unlikely(!ptr))
1383 return;
1384
1385 /* we have a full response and we know that we have either a CR
1386 * or an LF at <ptr>.
1387 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001388 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1390
1391 msg->sol = ptr;
1392 if (likely(*ptr == '\r'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1394 goto http_msg_rpline_end;
1395
1396 http_msg_rpline_end:
1397 case HTTP_MSG_RPLINE_END:
1398 /* msg->sol must point to the first of CR or LF. */
1399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1401 /* stop here */
1402
1403 /*
1404 * Second, states that are specific to the request only
1405 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 http_msg_rqbefore:
1407 case HTTP_MSG_RQBEFORE:
1408 if (likely(HTTP_IS_TOKEN(*ptr))) {
1409 if (likely(ptr == buf->data)) {
1410 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 } else {
1413#if PARSE_PRESERVE_EMPTY_LINES
1414 /* only skip empty leading lines, don't remove them */
1415 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001416 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 /* Remove empty leading lines, as recommended by
1419 * RFC2616. This takes a lot of time because we
1420 * must move all the buffer backwards, but this
1421 * is rarely needed. The method above will be
1422 * cleaner when we'll be able to start sending
1423 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 buf->lr = ptr;
1426 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001427 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 msg->sol = buf->data;
1429 ptr = buf->data;
1430 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001433 /* we will need this when keep-alive will be supported
1434 hdr_idx_init(idx);
1435 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 state = HTTP_MSG_RQMETH;
1437 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1441 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (unlikely(*ptr == '\n'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1445 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 http_msg_rqbefore_cr:
1449 case HTTP_MSG_RQBEFORE_CR:
1450 EXPECT_LF_HERE(ptr, http_msg_invalid);
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 http_msg_rqmeth:
1455 case HTTP_MSG_RQMETH:
1456 case HTTP_MSG_RQMETH_SP:
1457 case HTTP_MSG_RQURI:
1458 case HTTP_MSG_RQURI_SP:
1459 case HTTP_MSG_RQVER:
1460 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001461 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(!ptr))
1463 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* we have a full request and we know that we have either a CR
1466 * or an LF at <ptr>.
1467 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 msg->sol = ptr;
1472 if (likely(*ptr == '\r'))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 http_msg_rqline_end:
1477 case HTTP_MSG_RQLINE_END:
1478 /* check for HTTP/0.9 request : no version information available.
1479 * msg->sol must point to the first of CR or LF.
1480 */
1481 if (unlikely(msg->sl.rq.v_l == 0))
1482 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 /*
1489 * Common states below
1490 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_hdr_first:
1492 case HTTP_MSG_HDR_FIRST:
1493 msg->sol = ptr;
1494 if (likely(!HTTP_IS_CRLF(*ptr))) {
1495 goto http_msg_hdr_name;
1496 }
1497
1498 if (likely(*ptr == '\r'))
1499 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1500 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 http_msg_hdr_name:
1503 case HTTP_MSG_HDR_NAME:
1504 /* assumes msg->sol points to the first char */
1505 if (likely(HTTP_IS_TOKEN(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(*ptr == ':')) {
1509 msg->col = ptr - buf->data;
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 http_msg_hdr_l1_sp:
1516 case HTTP_MSG_HDR_L1_SP:
1517 /* assumes msg->sol points to the first char and msg->col to the colon */
1518 if (likely(HTTP_IS_SPHT(*ptr)))
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 /* header value can be basically anything except CR/LF */
1522 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (likely(!HTTP_IS_CRLF(*ptr))) {
1525 goto http_msg_hdr_val;
1526 }
1527
1528 if (likely(*ptr == '\r'))
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1530 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 http_msg_hdr_l1_lf:
1533 case HTTP_MSG_HDR_L1_LF:
1534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_hdr_l1_lws:
1538 case HTTP_MSG_HDR_L1_LWS:
1539 if (likely(HTTP_IS_SPHT(*ptr))) {
1540 /* replace HT,CR,LF with spaces */
1541 for (; buf->data+msg->sov < ptr; msg->sov++)
1542 buf->data[msg->sov] = ' ';
1543 goto http_msg_hdr_l1_sp;
1544 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001545 /* we had a header consisting only in spaces ! */
1546 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 goto http_msg_complete_header;
1548
1549 http_msg_hdr_val:
1550 case HTTP_MSG_HDR_VAL:
1551 /* assumes msg->sol points to the first char, msg->col to the
1552 * colon, and msg->sov points to the first character of the
1553 * value.
1554 */
1555 if (likely(!HTTP_IS_CRLF(*ptr)))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 msg->eol = ptr;
1559 /* Note: we could also copy eol into ->eoh so that we have the
1560 * real header end in case it ends with lots of LWS, but is this
1561 * really needed ?
1562 */
1563 if (likely(*ptr == '\r'))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1565 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 http_msg_hdr_l2_lf:
1568 case HTTP_MSG_HDR_L2_LF:
1569 EXPECT_LF_HERE(ptr, http_msg_invalid);
1570 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 http_msg_hdr_l2_lws:
1573 case HTTP_MSG_HDR_L2_LWS:
1574 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1575 /* LWS: replace HT,CR,LF with spaces */
1576 for (; msg->eol < ptr; msg->eol++)
1577 *msg->eol = ' ';
1578 goto http_msg_hdr_val;
1579 }
1580 http_msg_complete_header:
1581 /*
1582 * It was a new header, so the last one is finished.
1583 * Assumes msg->sol points to the first char, msg->col to the
1584 * colon, msg->sov points to the first character of the value
1585 * and msg->eol to the first CR or LF so we know how the line
1586 * ends. We insert last header into the index.
1587 */
1588 /*
1589 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1590 write(2, msg->sol, msg->eol-msg->sol);
1591 fprintf(stderr,"\n");
1592 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1595 idx, idx->tail) < 0))
1596 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 msg->sol = ptr;
1599 if (likely(!HTTP_IS_CRLF(*ptr))) {
1600 goto http_msg_hdr_name;
1601 }
1602
1603 if (likely(*ptr == '\r'))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1605 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 http_msg_last_lf:
1608 case HTTP_MSG_LAST_LF:
1609 /* Assumes msg->sol points to the first of either CR or LF */
1610 EXPECT_LF_HERE(ptr, http_msg_invalid);
1611 ptr++;
1612 buf->lr = ptr;
1613 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001614 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 return;
1616#ifdef DEBUG_FULL
1617 default:
1618 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1619 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001620#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 }
1622 http_msg_ood:
1623 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 buf->lr = ptr;
1626 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 http_msg_invalid:
1629 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001630 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 return;
1632}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001633
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001634/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001635 * It normally returns zero, but may return 1 if it absolutely needs to be
1636 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001637 * t->req->analysers. It might make sense to explode it into several other
1638 * functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001640int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 struct buffer *req = t->req;
1643 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001644
Willy Tarreau2df28e82008-08-17 15:20:19 +02001645 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02001646 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001647 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02001648 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
1649 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02001650 req->rex, rep->wex, req->flags, rep->flags, req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreaudafde432008-08-17 01:00:46 +02001652 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02001653 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001654 struct tcp_rule *rule;
1655 int partial;
1656
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001657 /* We will abort if we encounter a read error. In theory, we
1658 * should not abort if we get a close, it might be valid,
1659 * although very unlikely. FIXME: we'll abort for now, this
1660 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001661 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001662 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001663 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001664 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001665 t->fe->failed_req++;
1666 if (!(t->flags & SN_ERR_MASK))
1667 t->flags |= SN_ERR_CLICL;
1668 if (!(t->flags & SN_FINST_MASK))
1669 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001670 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001671 }
1672
1673 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001674 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001675 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001676 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001677 t->fe->failed_req++;
1678 if (!(t->flags & SN_ERR_MASK))
1679 t->flags |= SN_ERR_CLITO;
1680 if (!(t->flags & SN_FINST_MASK))
1681 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001682 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001683 }
1684
1685 /* We don't know whether we have enough data, so must proceed
1686 * this way :
1687 * - iterate through all rules in their declaration order
1688 * - if one rule returns MISS, it means the inspect delay is
1689 * not over yet, then return immediately, otherwise consider
1690 * it as a non-match.
1691 * - if one rule returns OK, then return OK
1692 * - if one rule returns KO, then return KO
1693 */
1694
Willy Tarreauba392ce2008-08-16 21:13:23 +02001695 if (req->flags & (BF_READ_NULL | BF_SHUTR) ||
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001696 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001697 partial = 0;
1698 else
1699 partial = ACL_PARTIAL;
1700
1701 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1702 int ret = ACL_PAT_PASS;
1703
1704 if (rule->cond) {
1705 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1706 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001707 /* just set the request timeout once at the beginning of the request */
1708 if (!tick_isset(t->inspect_exp))
1709 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001710 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001711 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001712
Willy Tarreaub6866442008-07-14 23:54:42 +02001713 ret = acl_pass(ret);
1714 if (rule->cond->pol == ACL_COND_UNLESS)
1715 ret = !ret;
1716 }
1717
1718 if (ret) {
1719 /* we have a matching rule. */
1720 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02001721 buffer_shutr(req);
1722 buffer_shutw(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001723 fd_delete(t->cli_fd);
1724 t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001725 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001726 t->fe->failed_req++;
1727 if (!(t->flags & SN_ERR_MASK))
1728 t->flags |= SN_ERR_PRXCOND;
1729 if (!(t->flags & SN_FINST_MASK))
1730 t->flags |= SN_FINST_R;
1731 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02001732 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001733 }
1734 /* otherwise accept */
1735 break;
1736 }
1737 }
1738
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001739 /* if we get there, it means we have no rule which matches, or
1740 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001741 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001742 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001743 t->inspect_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001744 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001745
Willy Tarreau2df28e82008-08-17 15:20:19 +02001746 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 /*
1748 * Now parse the partial (or complete) lines.
1749 * We will check the request syntax, and also join multi-line
1750 * headers. An index of all the lines will be elaborated while
1751 * parsing.
1752 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001753 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 * req->data + req->eoh = end of processed headers / start of current one
1758 * req->data + req->eol = end of current header or line (LF or CRLF)
1759 * req->lr = first non-visited byte
1760 * req->r = end of data
1761 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001764 struct http_txn *txn = &t->txn;
1765 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001769 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 /* 1: we might have to print this header in debug mode */
1772 if (unlikely((global.mode & MODE_DEBUG) &&
1773 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001774 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001776
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001777 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 eol = sol + msg->sl.rq.l;
1779 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001780
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001781 sol += hdr_idx_first_pos(&txn->hdr_idx);
1782 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001785 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001787 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1788 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 }
1790 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
1793 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001795 * If not so, we check the FD and buffer states before leaving.
1796 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1798 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001799 *
1800 */
1801
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001802 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 /*
1804 * First, let's catch bad requests.
1805 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001806 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001808
1809 /* 1: Since we are in header mode, if there's no space
1810 * left for headers, we won't be able to free more
1811 * later, so the session will never terminate. We
1812 * must terminate it now.
1813 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001814 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 /* FIXME: check if URI is set and return Status
1816 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817 */
Willy Tarreau06619262006-12-17 08:37:22 +01001818 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819 }
1820
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001821 /* 2: have we encountered a close ? */
1822 else if (req->flags & BF_READ_NULL) {
1823 txn->status = 400;
1824 client_retnclose(t, error_message(t, HTTP_ERR_400));
1825 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001826 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001827 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001828
Willy Tarreau58f10d72006-12-04 02:26:12 +01001829 if (!(t->flags & SN_ERR_MASK))
1830 t->flags |= SN_ERR_CLICL;
1831 if (!(t->flags & SN_FINST_MASK))
1832 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001833 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001834 }
1835
1836 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001837 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001838 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001839 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001840 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001841 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001842 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001843 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001844 if (!(t->flags & SN_ERR_MASK))
1845 t->flags |= SN_ERR_CLITO;
1846 if (!(t->flags & SN_FINST_MASK))
1847 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001848 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001849 }
1850
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001851 /* 4: have we encountered a read error or did we have to shutdown ? */
Willy Tarreauba392ce2008-08-16 21:13:23 +02001852 else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001853 /* we cannot return any message on error */
1854 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001855 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001856 t->fe->failed_req++;
1857 if (!(t->flags & SN_ERR_MASK))
1858 t->flags |= SN_ERR_CLICL;
1859 if (!(t->flags & SN_FINST_MASK))
1860 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001861 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001862 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001863
1864 /* just set the request timeout once at the beginning of the request */
1865 if (!tick_isset(t->txn.exp))
1866 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1867
1868 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001869 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001870 }
1871
1872
1873 /****************************************************************
1874 * More interesting part now : we know that we have a complete *
1875 * request which at least looks like HTTP. We have an indicator *
1876 * of each header's length, so we can parse them quickly. *
1877 ****************************************************************/
1878
Willy Tarreau2df28e82008-08-17 15:20:19 +02001879 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001880
Willy Tarreau9cdde232007-05-02 20:58:19 +02001881 /* ensure we keep this pointer to the beginning of the message */
1882 msg->sol = req->data + msg->som;
1883
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 /*
1885 * 1: identify the method
1886 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001887 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001888
1889 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001891 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 if (unlikely((t->fe->monitor_uri_len != 0) &&
1895 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1896 !memcmp(&req->data[msg->sl.rq.u],
1897 t->fe->monitor_uri,
1898 t->fe->monitor_uri_len))) {
1899 /*
1900 * We have found the monitor URI
1901 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001902 struct acl_cond *cond;
1903 cur_proxy = t->fe;
1904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001906
1907 /* Check if we want to fail this monitor request or not */
1908 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1909 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001910
1911 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001912 if (cond->pol == ACL_COND_UNLESS)
1913 ret = !ret;
1914
1915 if (ret) {
1916 /* we fail this request, let's return 503 service unavail */
1917 txn->status = 503;
1918 client_retnclose(t, error_message(t, HTTP_ERR_503));
1919 goto return_prx_cond;
1920 }
1921 }
1922
1923 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001924 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 client_retnclose(t, &http_200_chunk);
1926 goto return_prx_cond;
1927 }
1928
1929 /*
1930 * 3: Maybe we have to copy the original REQURI for the logs ?
1931 * Note: we cannot log anymore if the request has been
1932 * classified as invalid.
1933 */
1934 if (unlikely(t->logs.logwait & LW_REQ)) {
1935 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001936 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 int urilen = msg->sl.rq.l;
1938
1939 if (urilen >= REQURI_LEN)
1940 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001941 memcpy(txn->uri, &req->data[msg->som], urilen);
1942 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943
1944 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001945 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 } else {
1947 Alert("HTTP logging : out of memory.\n");
1948 }
1949 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001950
Willy Tarreau06619262006-12-17 08:37:22 +01001951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001952 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1953 if (unlikely(msg->sl.rq.v_l == 0)) {
1954 int delta;
1955 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001956 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 cur_end = msg->sol + msg->sl.rq.l;
1958 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 if (msg->sl.rq.u_l == 0) {
1961 /* if no URI was set, add "/" */
1962 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1963 cur_end += delta;
1964 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001965 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 /* add HTTP version */
1967 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1968 msg->eoh += delta;
1969 cur_end += delta;
1970 cur_end = (char *)http_parse_reqline(msg, req->data,
1971 HTTP_MSG_RQMETH,
1972 msg->sol, cur_end + 1,
1973 NULL, NULL);
1974 if (unlikely(!cur_end))
1975 goto return_bad_req;
1976
1977 /* we have a full HTTP/1.0 request now and we know that
1978 * we have either a CR or an LF at <ptr>.
1979 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001980 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981 }
1982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983
1984 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001985 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001986 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001987 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001988
1989 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001991 * As opposed to version 1.2, now they will be evaluated in the
1992 * filters order and not in the header order. This means that
1993 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001994 *
1995 * We can now check whether we want to switch to another
1996 * backend, in which case we will re-check the backend's
1997 * filters and various options. In order to support 3-level
1998 * switching, here's how we should proceed :
1999 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002000 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002001 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002002 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002003 * There cannot be any switch from there, so ->be cannot be
2004 * changed anymore.
2005 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002006 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002007 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002008 * The response path will be able to apply either ->be, or
2009 * ->be then ->fe filters in order to match the reverse of
2010 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002011 */
2012
Willy Tarreau06619262006-12-17 08:37:22 +01002013 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002014 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002015 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002016 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002017 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002018
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002019 /* first check whether we have some ACLs set to redirect this request */
2020 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2021 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002022
2023 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002024 if (rule->cond->pol == ACL_COND_UNLESS)
2025 ret = !ret;
2026
2027 if (ret) {
2028 struct chunk rdr = { trash, 0 };
2029 const char *msg_fmt;
2030
2031 /* build redirect message */
2032 switch(rule->code) {
2033 case 303:
2034 rdr.len = strlen(HTTP_303);
2035 msg_fmt = HTTP_303;
2036 break;
2037 case 301:
2038 rdr.len = strlen(HTTP_301);
2039 msg_fmt = HTTP_301;
2040 break;
2041 case 302:
2042 default:
2043 rdr.len = strlen(HTTP_302);
2044 msg_fmt = HTTP_302;
2045 break;
2046 }
2047
2048 if (unlikely(rdr.len > sizeof(trash)))
2049 goto return_bad_req;
2050 memcpy(rdr.str, msg_fmt, rdr.len);
2051
2052 switch(rule->type) {
2053 case REDIRECT_TYPE_PREFIX: {
2054 const char *path;
2055 int pathlen;
2056
2057 path = http_get_path(txn);
2058 /* build message using path */
2059 if (path) {
2060 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2061 } else {
2062 path = "/";
2063 pathlen = 1;
2064 }
2065
2066 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2067 goto return_bad_req;
2068
2069 /* add prefix */
2070 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2071 rdr.len += rule->rdr_len;
2072
2073 /* add path */
2074 memcpy(rdr.str + rdr.len, path, pathlen);
2075 rdr.len += pathlen;
2076 break;
2077 }
2078 case REDIRECT_TYPE_LOCATION:
2079 default:
2080 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2081 goto return_bad_req;
2082
2083 /* add location */
2084 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2085 rdr.len += rule->rdr_len;
2086 break;
2087 }
2088
2089 /* add end of headers */
2090 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2091 rdr.len += 4;
2092
2093 txn->status = rule->code;
2094 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002095 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002096 client_retnclose(t, &rdr);
2097 goto return_prx_cond;
2098 }
2099 }
2100
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002101 /* first check whether we have some ACLs set to block this request */
2102 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002103 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002104
2105 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002106 if (cond->pol == ACL_COND_UNLESS)
2107 ret = !ret;
2108
2109 if (ret) {
2110 txn->status = 403;
2111 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002112 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002113 client_retnclose(t, error_message(t, HTTP_ERR_403));
2114 goto return_prx_cond;
2115 }
2116 }
2117
Willy Tarreau06619262006-12-17 08:37:22 +01002118 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002119 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002120 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2121 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002122 }
2123
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002124 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2125 /* to ensure correct connection accounting on
2126 * the backend, we count the connection for the
2127 * one managing the queue.
2128 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002129 t->be->beconn++;
2130 if (t->be->beconn > t->be->beconn_max)
2131 t->be->beconn_max = t->be->beconn;
2132 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002133 t->flags |= SN_BE_ASSIGNED;
2134 }
2135
Willy Tarreau06619262006-12-17 08:37:22 +01002136 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002137 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002138 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002139 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002140 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002141 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002142 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002143 goto return_prx_cond;
2144 }
2145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002146 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002147 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002148 !(t->flags & SN_CONN_CLOSED)) {
2149 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002150 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002151 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002152
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002153 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002154 old_idx = 0;
2155
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002156 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2157 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002158 cur_ptr = cur_next;
2159 cur_end = cur_ptr + cur_hdr->len;
2160 cur_next = cur_end + cur_hdr->cr + 1;
2161
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002162 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2163 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002164 /* 3 possibilities :
2165 * - we have already set Connection: close,
2166 * so we remove this line.
2167 * - we have not yet set Connection: close,
2168 * but this line indicates close. We leave
2169 * it untouched and set the flag.
2170 * - we have not yet set Connection: close,
2171 * and this line indicates non-close. We
2172 * replace it.
2173 */
2174 if (t->flags & SN_CONN_CLOSED) {
2175 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002176 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002177 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002178 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2179 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002180 cur_hdr->len = 0;
2181 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002182 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2183 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2184 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002185 cur_next += delta;
2186 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002187 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002188 }
2189 t->flags |= SN_CONN_CLOSED;
2190 }
2191 }
2192 old_idx = cur_idx;
2193 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002194 }
2195 /* add request headers from the rule sets in the same order */
2196 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2197 if (unlikely(http_header_add_tail(req,
2198 &txn->req,
2199 &txn->hdr_idx,
2200 rule_set->req_add[cur_idx])) < 0)
2201 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002202 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002203
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002204 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002205 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002206 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002207 /* we have to check the URI and auth for this request.
2208 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002209 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002210 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002211 if (stats_check_uri_auth(t, rule_set))
2212 return 1;
2213 }
2214
Willy Tarreau55ea7572007-06-17 19:56:27 +02002215 /* now check whether we have some switching rules for this request */
2216 if (!(t->flags & SN_BE_ASSIGNED)) {
2217 struct switching_rule *rule;
2218
2219 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2220 int ret;
2221
2222 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002223
2224 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002225 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002226 ret = !ret;
2227
2228 if (ret) {
2229 t->be = rule->be.backend;
2230 t->be->beconn++;
2231 if (t->be->beconn > t->be->beconn_max)
2232 t->be->beconn_max = t->be->beconn;
2233 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002234
2235 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002236 t->rep->rto = t->req->wto = t->be->timeout.server;
2237 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002238 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002239 t->flags |= SN_BE_ASSIGNED;
2240 break;
2241 }
2242 }
2243 }
2244
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002245 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2246 /* No backend was set, but there was a default
2247 * backend set in the frontend, so we use it and
2248 * loop again.
2249 */
2250 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002251 t->be->beconn++;
2252 if (t->be->beconn > t->be->beconn_max)
2253 t->be->beconn_max = t->be->beconn;
2254 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002255
2256 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002257 t->rep->rto = t->req->wto = t->be->timeout.server;
2258 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002259 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002260 t->flags |= SN_BE_ASSIGNED;
2261 }
2262 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002263
Willy Tarreau58f10d72006-12-04 02:26:12 +01002264
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002265 if (!(t->flags & SN_BE_ASSIGNED)) {
2266 /* To ensure correct connection accounting on
2267 * the backend, we count the connection for the
2268 * one managing the queue.
2269 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002270 t->be->beconn++;
2271 if (t->be->beconn > t->be->beconn_max)
2272 t->be->beconn_max = t->be->beconn;
2273 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002274 t->flags |= SN_BE_ASSIGNED;
2275 }
2276
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002277 /*
2278 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002279 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002280 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002281 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002282 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002284 /*
2285 * If HTTP PROXY is set we simply get remote server address
2286 * parsing incoming request.
2287 */
2288 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2289 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002291
Willy Tarreau2a324282006-12-05 00:05:46 +01002292 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002293 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002294 * so let's do the same now.
2295 */
2296
2297 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002298 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002299 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002300 }
2301
2302
2303 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002304 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002305 * Note that doing so might move headers in the request, but
2306 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002307 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002309 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2310 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002311 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312
Willy Tarreau58f10d72006-12-04 02:26:12 +01002313
Willy Tarreau2a324282006-12-05 00:05:46 +01002314 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002315 * 9: add X-Forwarded-For if either the frontend or the backend
2316 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002317 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002318 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002319 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002320 /* Add an X-Forwarded-For header unless the source IP is
2321 * in the 'except' network range.
2322 */
2323 if ((!t->fe->except_mask.s_addr ||
2324 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2325 != t->fe->except_net.s_addr) &&
2326 (!t->be->except_mask.s_addr ||
2327 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2328 != t->be->except_net.s_addr)) {
2329 int len;
2330 unsigned char *pn;
2331 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002332
Ross Westaf72a1d2008-08-03 10:51:45 +02002333 /* Note: we rely on the backend to get the header name to be used for
2334 * x-forwarded-for, because the header is really meant for the backends.
2335 * However, if the backend did not specify any option, we have to rely
2336 * on the frontend's header name.
2337 */
2338 if (t->be->fwdfor_hdr_len) {
2339 len = t->be->fwdfor_hdr_len;
2340 memcpy(trash, t->be->fwdfor_hdr_name, len);
2341 } else {
2342 len = t->fe->fwdfor_hdr_len;
2343 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2344 }
2345 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002346
Ross Westaf72a1d2008-08-03 10:51:45 +02002347 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002348 &txn->hdr_idx, trash, len)) < 0)
2349 goto return_bad_req;
2350 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002351 }
2352 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002353 /* FIXME: for the sake of completeness, we should also support
2354 * 'except' here, although it is mostly useless in this case.
2355 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002356 int len;
2357 char pn[INET6_ADDRSTRLEN];
2358 inet_ntop(AF_INET6,
2359 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2360 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002361
2362 /* Note: we rely on the backend to get the header name to be used for
2363 * x-forwarded-for, because the header is really meant for the backends.
2364 * However, if the backend did not specify any option, we have to rely
2365 * on the frontend's header name.
2366 */
2367 if (t->be->fwdfor_hdr_len) {
2368 len = t->be->fwdfor_hdr_len;
2369 memcpy(trash, t->be->fwdfor_hdr_name, len);
2370 } else {
2371 len = t->fe->fwdfor_hdr_len;
2372 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2373 }
2374 len += sprintf(trash + len, ": %s", pn);
2375
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002376 if (unlikely(http_header_add_tail2(req, &txn->req,
2377 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002378 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002379 }
2380 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381
Willy Tarreau2a324282006-12-05 00:05:46 +01002382 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002383 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002384 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002385 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002386 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002387 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002388 if ((unlikely(msg->sl.rq.v_l != 8) ||
2389 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2390 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002391 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002392 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002393 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002394 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002395 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2396 * If not assigned, perhaps we are balancing on url_param, but this is a
2397 * POST; and the parameters are in the body, maybe scan there to find our server.
2398 * (unless headers overflowed the buffer?)
2399 */
2400 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2401 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002402 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002403 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2404 /* are there enough bytes here? total == l || r || rlim ?
2405 * len is unsigned, but eoh is int,
2406 * how many bytes of body have we received?
2407 * eoh is the first empty line of the header
2408 */
2409 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002410 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002411
2412 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2413 * We can't assume responsibility for the server's decision,
2414 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2415 * We also can't change our mind later, about which server to choose, so round robin.
2416 */
2417 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2418 struct hdr_ctx ctx;
2419 ctx.idx = 0;
2420 /* Expect is allowed in 1.1, look for it */
2421 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2422 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002423 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002424 /* We can't reliablly stall and wait for data, because of
2425 * .NET clients that don't conform to rfc2616; so, no need for
2426 * the next block to check length expectations.
2427 * We could send 100 status back to the client, but then we need to
2428 * re-write headers, and send the message. And this isn't the right
2429 * place for that action.
2430 * TODO: support Expect elsewhere and delete this block.
2431 */
2432 goto end_check_maybe_wait_for_body;
2433 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002434
2435 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002436 /* nothing to do, we got enough */
2437 } else {
2438 /* limit implies we are supposed to need this many bytes
2439 * to find the parameter. Let's see how many bytes we can wait for.
2440 */
2441 long long hint = len;
2442 struct hdr_ctx ctx;
2443 ctx.idx = 0;
2444 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002445 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002446 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002447 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002448 ctx.idx = 0;
2449 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2450 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002451 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002452 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002453 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002454 hint = 0; /* parse failure, untrusted client */
2455 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002456 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002457 msg->hdr_content_len = hint;
2458 else
2459 hint = 0; /* bad client, sent negative length */
2460 }
2461 }
2462 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002463 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002464 hint = t->be->url_param_post_limit;
2465 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002466 if (len < hint)
Willy Tarreau2df28e82008-08-17 15:20:19 +02002467 req->analysers |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002468 /* else... There are no body bytes to wait for */
2469 }
2470 }
2471 }
2472 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002473
Willy Tarreau2a324282006-12-05 00:05:46 +01002474 /*************************************************************
2475 * OK, that's finished for the headers. We have done what we *
2476 * could. Let's switch to the DATA state. *
2477 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002478
Willy Tarreaue393fe22008-08-16 22:18:07 +02002479 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002480 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002481
Willy Tarreau1fa31262007-12-03 00:36:16 +01002482 /* When a connection is tarpitted, we use the tarpit timeout,
2483 * which may be the same as the connect timeout if unspecified.
2484 * If unset, then set it to zero because we really want it to
2485 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002486 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002487 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002488 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002489 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002490 /* flush the request so that we can drop the connection early
2491 * if the client closes first.
2492 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02002493 req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2494 if (!req->wex)
2495 req->wex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002496 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002498 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002499 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002500
2501 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002502 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002503 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002504 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002505 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002506 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002507 return_prx_cond:
2508 if (!(t->flags & SN_ERR_MASK))
2509 t->flags |= SN_ERR_PRXCOND;
2510 if (!(t->flags & SN_FINST_MASK))
2511 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002512 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002513 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002514 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002515 }
2516
Willy Tarreau2df28e82008-08-17 15:20:19 +02002517 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002518 /* We have to parse the HTTP request body to find any required data.
2519 * "balance url_param check_post" should have been the only way to get
2520 * into this. We were brought here after HTTP header analysis, so all
2521 * related structures are ready.
2522 */
2523 struct http_msg *msg = &t->txn.req;
2524 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2525 long long limit = t->be->url_param_post_limit;
2526 struct hdr_ctx ctx;
2527
2528 ctx.idx = 0;
2529
2530 /* now if we have a length, we'll take the hint */
2531 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2532 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2533 unsigned int chunk = 0;
2534 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2535 char c = msg->sol[body];
2536 if (ishex(c)) {
2537 unsigned int hex = toupper(c) - '0';
2538 if (hex > 9)
2539 hex -= 'A' - '9' - 1;
2540 chunk = (chunk << 4) | hex;
2541 } else
2542 break;
2543 body++;
2544 }
2545 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002546 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002547
2548 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002549 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002550
2551 body += 2; // skip CRLF
2552
2553 /* if we support more then one chunk here, we have to do it again when assigning server
2554 * 1. how much entity data do we have? new var
2555 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2556 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2557 */
2558
2559 if (chunk < limit)
2560 limit = chunk; /* only reading one chunk */
2561 } else {
2562 if (msg->hdr_content_len < limit)
2563 limit = msg->hdr_content_len;
2564 }
2565
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002566 http_body_end:
2567 /* we leave once we know we have nothing left to do. This means that we have
2568 * enough bytes, or that we know we'll not get any more (buffer full, read
2569 * buffer closed).
2570 */
2571 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002572 req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002573 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002574 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002575 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002576 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002577 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002578
Willy Tarreau2df28e82008-08-17 15:20:19 +02002579 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2580 * probably reduce one day's debugging session.
2581 */
2582#ifdef DEBUG_DEV
2583 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY)) {
2584 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2585 __FILE__, __LINE__, req->analysers);
2586 ABORT_NOW();
2587 }
2588#endif
2589 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_BODY;
2590
Willy Tarreaudafde432008-08-17 01:00:46 +02002591 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002592 if (unlikely(req->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02002593 goto update_state;
2594 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002595}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002596
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002597/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002598 * It normally returns zero, but may return 1 if it absolutely needs to be
2599 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002600 * t->rep->analysers. It might make sense to explode it into several other
2601 * functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002602 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002604{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002605 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002606 struct buffer *req = t->req;
2607 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002608
Willy Tarreau2df28e82008-08-17 15:20:19 +02002609 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002610 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002611 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02002612 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
2613 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0,
Willy Tarreau2df28e82008-08-17 15:20:19 +02002614 req->rex, rep->wex, req->flags, rep->flags, rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002615
Willy Tarreaudafde432008-08-17 01:00:46 +02002616 update_state:
Willy Tarreau2df28e82008-08-17 15:20:19 +02002617 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002618 /*
2619 * Now parse the partial (or complete) lines.
2620 * We will check the response syntax, and also join multi-line
2621 * headers. An index of all the lines will be elaborated while
2622 * parsing.
2623 *
2624 * For the parsing, we use a 28 states FSM.
2625 *
2626 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002627 * rep->data + rep->som = beginning of response
2628 * rep->data + rep->eoh = end of processed headers / start of current one
2629 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002630 * rep->lr = first non-visited byte
2631 * rep->r = end of data
2632 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002633
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002634 int cur_idx;
2635 struct http_msg *msg = &txn->rsp;
2636 struct proxy *cur_proxy;
2637
2638 if (likely(rep->lr < rep->r))
2639 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2640
2641 /* 1: we might have to print this header in debug mode */
2642 if (unlikely((global.mode & MODE_DEBUG) &&
2643 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2644 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2645 char *eol, *sol;
2646
2647 sol = rep->data + msg->som;
2648 eol = sol + msg->sl.rq.l;
2649 debug_hdr("srvrep", t, sol, eol);
2650
2651 sol += hdr_idx_first_pos(&txn->hdr_idx);
2652 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2653
2654 while (cur_idx) {
2655 eol = sol + txn->hdr_idx.v[cur_idx].len;
2656 debug_hdr("srvhdr", t, sol, eol);
2657 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2658 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002659 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002662 /*
2663 * Now we quickly check if we have found a full valid response.
2664 * If not so, we check the FD and buffer states before leaving.
2665 * A full response is indicated by the fact that we have seen
2666 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2667 * responses are checked first.
2668 *
2669 * Depending on whether the client is still there or not, we
2670 * may send an error response back or not. Note that normally
2671 * we should only check for HTTP status there, and check I/O
2672 * errors somewhere else.
2673 */
2674
2675 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002676 /* Invalid response */
2677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2678 hdr_response_bad:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002679 buffer_shutr(rep);
2680 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002681 fd_delete(t->srv_fd);
2682 if (t->srv) {
2683 t->srv->cur_sess--;
2684 t->srv->failed_resp++;
2685 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002687 t->be->failed_resp++;
2688 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002689 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002690 txn->status = 502;
2691 client_return(t, error_message(t, HTTP_ERR_502));
2692 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002693 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002694 if (!(t->flags & SN_FINST_MASK))
2695 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002697 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2698 process_srv_queue(t->srv);
2699
Willy Tarreaudafde432008-08-17 01:00:46 +02002700 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002701 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002702 /* write error to client, read error or close from server */
2703 if (req->flags & BF_WRITE_ERROR ||
Willy Tarreauba392ce2008-08-16 21:13:23 +02002704 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) {
2705 buffer_shutr(rep);
2706 buffer_shutw(req);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002707 fd_delete(t->srv_fd);
2708 if (t->srv) {
2709 t->srv->cur_sess--;
2710 t->srv->failed_resp++;
2711 sess_change_server(t, NULL);
2712 }
2713 t->be->failed_resp++;
2714 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002715 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002716 txn->status = 502;
2717 client_return(t, error_message(t, HTTP_ERR_502));
2718 if (!(t->flags & SN_ERR_MASK))
2719 t->flags |= SN_ERR_SRVCL;
2720 if (!(t->flags & SN_FINST_MASK))
2721 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002722
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002723 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2724 process_srv_queue(t->srv);
2725
Willy Tarreaudafde432008-08-17 01:00:46 +02002726 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002728 /* too large response does not fit in buffer. */
Willy Tarreaue393fe22008-08-16 22:18:07 +02002729 else if (rep->flags & BF_FULL) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002730 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002731 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002732 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002733 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02002734 buffer_shutr(rep);
2735 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 fd_delete(t->srv_fd);
2737 if (t->srv) {
2738 t->srv->cur_sess--;
2739 t->srv->failed_resp++;
2740 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002741 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002742 t->be->failed_resp++;
2743 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002744 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002745 txn->status = 504;
2746 client_return(t, error_message(t, HTTP_ERR_504));
2747 if (!(t->flags & SN_ERR_MASK))
2748 t->flags |= SN_ERR_SRVTO;
2749 if (!(t->flags & SN_FINST_MASK))
2750 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002751
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002752 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2753 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002754 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002756 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002757 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758
Willy Tarreau21d2af32008-02-14 20:25:24 +01002759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002760 /*****************************************************************
2761 * More interesting part now : we know that we have a complete *
2762 * response which at least looks like HTTP. We have an indicator *
2763 * of each header's length, so we can parse them quickly. *
2764 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002765
Willy Tarreau2df28e82008-08-17 15:20:19 +02002766 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002767
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002768 /* ensure we keep this pointer to the beginning of the message */
2769 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002770
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002771 /*
2772 * 1: get the status code and check for cacheability.
2773 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002775 t->logs.logwait &= ~LW_RESP;
2776 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002777
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002778 switch (txn->status) {
2779 case 200:
2780 case 203:
2781 case 206:
2782 case 300:
2783 case 301:
2784 case 410:
2785 /* RFC2616 @13.4:
2786 * "A response received with a status code of
2787 * 200, 203, 206, 300, 301 or 410 MAY be stored
2788 * by a cache (...) unless a cache-control
2789 * directive prohibits caching."
2790 *
2791 * RFC2616 @9.5: POST method :
2792 * "Responses to this method are not cacheable,
2793 * unless the response includes appropriate
2794 * Cache-Control or Expires header fields."
2795 */
2796 if (likely(txn->meth != HTTP_METH_POST) &&
2797 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2798 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2799 break;
2800 default:
2801 break;
2802 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002803
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002804 /*
2805 * 2: we may need to capture headers
2806 */
2807 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2808 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2809 txn->rsp.cap, t->fe->rsp_cap);
2810
2811 /*
2812 * 3: we will have to evaluate the filters.
2813 * As opposed to version 1.2, now they will be evaluated in the
2814 * filters order and not in the header order. This means that
2815 * each filter has to be validated among all headers.
2816 *
2817 * Filters are tried with ->be first, then with ->fe if it is
2818 * different from ->be.
2819 */
2820
2821 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2822
2823 cur_proxy = t->be;
2824 while (1) {
2825 struct proxy *rule_set = cur_proxy;
2826
2827 /* try headers filters */
2828 if (rule_set->rsp_exp != NULL) {
2829 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2830 return_bad_resp:
2831 if (t->srv) {
2832 t->srv->cur_sess--;
2833 t->srv->failed_resp++;
2834 sess_change_server(t, NULL);
2835 }
2836 cur_proxy->failed_resp++;
2837 return_srv_prx_502:
Willy Tarreauba392ce2008-08-16 21:13:23 +02002838 buffer_shutr(rep);
2839 buffer_shutw(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002840 fd_delete(t->srv_fd);
2841 t->srv_state = SV_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002842 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002843 txn->status = 502;
2844 client_return(t, error_message(t, HTTP_ERR_502));
2845 if (!(t->flags & SN_ERR_MASK))
2846 t->flags |= SN_ERR_PRXCOND;
2847 if (!(t->flags & SN_FINST_MASK))
2848 t->flags |= SN_FINST_H;
2849 /* We used to have a free connection slot. Since we'll never use it,
2850 * we have to inform the server that it may be used by another session.
2851 */
2852 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2853 process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002854 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002855 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002857
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002858 /* has the response been denied ? */
2859 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002860 if (t->srv) {
2861 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002863 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002864 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 cur_proxy->denied_resp++;
2866 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002867 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002868
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002869 /* We might have to check for "Connection:" */
2870 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2871 !(t->flags & SN_CONN_CLOSED)) {
2872 char *cur_ptr, *cur_end, *cur_next;
2873 int cur_idx, old_idx, delta, val;
2874 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002876 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2877 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002879 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2880 cur_hdr = &txn->hdr_idx.v[cur_idx];
2881 cur_ptr = cur_next;
2882 cur_end = cur_ptr + cur_hdr->len;
2883 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2886 if (val) {
2887 /* 3 possibilities :
2888 * - we have already set Connection: close,
2889 * so we remove this line.
2890 * - we have not yet set Connection: close,
2891 * but this line indicates close. We leave
2892 * it untouched and set the flag.
2893 * - we have not yet set Connection: close,
2894 * and this line indicates non-close. We
2895 * replace it.
2896 */
2897 if (t->flags & SN_CONN_CLOSED) {
2898 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2899 txn->rsp.eoh += delta;
2900 cur_next += delta;
2901 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2902 txn->hdr_idx.used--;
2903 cur_hdr->len = 0;
2904 } else {
2905 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2906 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2907 "close", 5);
2908 cur_next += delta;
2909 cur_hdr->len += delta;
2910 txn->rsp.eoh += delta;
2911 }
2912 t->flags |= SN_CONN_CLOSED;
2913 }
2914 }
2915 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002916 }
2917 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 /* add response headers from the rule sets in the same order */
2920 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2921 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2922 rule_set->rsp_add[cur_idx])) < 0)
2923 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002924 }
2925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 /* check whether we're already working on the frontend */
2927 if (cur_proxy == t->fe)
2928 break;
2929 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002931
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002932 /*
2933 * 4: check for server cookie.
2934 */
2935 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2936 || (t->be->options & PR_O_CHK_CACHE))
2937 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002938
Willy Tarreaubaaee002006-06-26 02:48:02 +02002939
Willy Tarreaua15645d2007-03-18 16:22:39 +01002940 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002941 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002943 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2944 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002946 /*
2947 * 6: add server cookie in the response if needed
2948 */
2949 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2950 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2951 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 /* the server is known, it's not the one the client requested, we have to
2954 * insert a set-cookie here, except if we want to insert only on POST
2955 * requests and this one isn't. Note that servers which don't have cookies
2956 * (eg: some backup servers) will return a full cookie removal request.
2957 */
2958 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2959 t->be->cookie_name,
2960 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002962 if (t->be->cookie_domain)
2963 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002965 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2966 trash, len)) < 0)
2967 goto return_bad_resp;
2968 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002970 /* Here, we will tell an eventual cache on the client side that we don't
2971 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2972 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2973 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2974 */
2975 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2978
2979 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2980 "Cache-control: private", 22)) < 0)
2981 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002982 }
2983 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002986 /*
2987 * 7: check if result will be cacheable with a cookie.
2988 * We'll block the response if security checks have caught
2989 * nasty things such as a cacheable cookie.
2990 */
2991 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2992 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2993 (t->be->options & PR_O_CHK_CACHE)) {
2994
2995 /* we're in presence of a cacheable response containing
2996 * a set-cookie header. We'll block it as requested by
2997 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002998 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002999 if (t->srv) {
3000 t->srv->cur_sess--;
3001 t->srv->failed_secu++;
3002 sess_change_server(t, NULL);
3003 }
3004 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003005
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003006 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3007 t->be->id, t->srv?t->srv->id:"<dispatch>");
3008 send_log(t->be, LOG_ALERT,
3009 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3010 t->be->id, t->srv?t->srv->id:"<dispatch>");
3011 goto return_srv_prx_502;
3012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013
3014 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003015 * 8: add "Connection: close" if needed and not yet set.
3016 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003017 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003018 if (!(t->flags & SN_CONN_CLOSED) &&
3019 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3020 if ((unlikely(msg->sl.st.v_l != 8) ||
3021 unlikely(req->data[msg->som + 7] != '0')) &&
3022 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3023 "Connection: close", 17)) < 0)
3024 goto return_bad_resp;
3025 t->flags |= SN_CONN_CLOSED;
3026 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003027
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003028 /*************************************************************
3029 * OK, that's finished for the headers. We have done what we *
3030 * could. Let's switch to the DATA state. *
3031 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032
Willy Tarreaue393fe22008-08-16 22:18:07 +02003033 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003036#ifdef CONFIG_HAP_TCPSPLICE
3037 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3038 /* TCP splicing supported by both FE and BE */
3039 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3040 }
3041#endif
3042 /* if the user wants to log as soon as possible, without counting
3043 * bytes from the server, then this is the right moment. We have
3044 * to temporarily assign bytes_out to log what we currently have.
3045 */
3046 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3047 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3048 t->logs.bytes_out = txn->rsp.eoh;
3049 if (t->fe->to_log & LW_REQ)
3050 http_sess_log(t);
3051 else
3052 tcp_sess_log(t);
3053 t->logs.bytes_out = 0;
3054 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003056 /* Note: we must not try to cheat by jumping directly to DATA,
3057 * otherwise we would not let the client side wake up.
3058 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003059
Willy Tarreaudafde432008-08-17 01:00:46 +02003060 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003061 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003062
Willy Tarreau2df28e82008-08-17 15:20:19 +02003063 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3064 * probably reduce one day's debugging session.
3065 */
3066#ifdef DEBUG_DEV
3067 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3068 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3069 __FILE__, __LINE__, rep->analysers);
3070 ABORT_NOW();
3071 }
3072#endif
3073 rep->analysers &= AN_RTR_HTTP_HDR;
3074
Willy Tarreaudafde432008-08-17 01:00:46 +02003075 /* we want to leave with that clean */
Willy Tarreau2df28e82008-08-17 15:20:19 +02003076 if (unlikely(rep->analysers != 0))
Willy Tarreaudafde432008-08-17 01:00:46 +02003077 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003078 return 0;
3079}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003081/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003082 * Manages the client FSM and its socket. It normally returns zero, but may
3083 * return 1 if it absolutely wants to be called again.
3084 *
3085 * Note: process_cli is the ONLY function allowed to set cli_state to anything
Willy Tarreaua7c52762008-08-16 18:40:18 +02003086 * but CL_STCLOSE.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 */
3088int process_cli(struct session *t)
3089{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003090 struct buffer *req = t->req;
3091 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003092
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003093 DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003094 now_ms,
3095 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003096 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3097 t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003098 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003099 req->flags, rep->flags,
3100 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101
Willy Tarreaudafde432008-08-17 01:00:46 +02003102 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003103 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3104 * still set this state (and will do until unix sockets are converted).
3105 */
3106 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 /* read or write error */
3108 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003109 buffer_shutr(req);
3110 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003111 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_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003114 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003115 if (!(t->flags & SN_ERR_MASK))
3116 t->flags |= SN_ERR_CLICL;
3117 if (!(t->flags & SN_FINST_MASK)) {
3118 if (t->pend_pos)
3119 t->flags |= SN_FINST_Q;
3120 else if (t->srv_state == SV_STCONN)
3121 t->flags |= SN_FINST_C;
3122 else
3123 t->flags |= SN_FINST_D;
3124 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003125 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003126 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003127 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003128 /* last read, or end of server write */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003129 else if (!(req->flags & BF_SHUTR) && /* not already done */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003130 req->flags & (BF_READ_NULL | BF_SHUTW)) {
3131 buffer_shutr(req);
3132 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003133 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003134 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 } else {
3136 /* output was already closed */
3137 fd_delete(t->cli_fd);
3138 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003139 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003140 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003141 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003142 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003143 /* last server read and buffer empty : we only check them when we're
3144 * allowed to forward the data.
3145 */
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003146 else if (!(rep->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003147 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003148 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
3149 buffer_shutw(rep);
3150 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003151 EV_FD_CLR(t->cli_fd, DIR_WR);
3152 shutdown(t->cli_fd, SHUT_WR);
3153 /* We must ensure that the read part is still alive when switching to shutw */
3154 /* FIXME: is this still true ? */
3155 EV_FD_SET(t->cli_fd, DIR_RD);
3156 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003157 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003158 } else {
3159 fd_delete(t->cli_fd);
3160 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003161 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003162 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003163 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003164 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003166 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003167 buffer_shutr(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003168 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003169 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003170 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003171 } else {
3172 /* output was already closed */
3173 fd_delete(t->cli_fd);
3174 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003175 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003176 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003177 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003178 if (!(t->flags & SN_ERR_MASK))
3179 t->flags |= SN_ERR_CLITO;
3180 if (!(t->flags & SN_FINST_MASK)) {
3181 if (t->pend_pos)
3182 t->flags |= SN_FINST_Q;
3183 else if (t->srv_state == SV_STCONN)
3184 t->flags |= SN_FINST_C;
3185 else
3186 t->flags |= SN_FINST_D;
3187 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003188 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003189 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003190 }
3191 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003192 else if (rep->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003193 buffer_shutw(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003194 if (!(req->flags & BF_SHUTR)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195 EV_FD_CLR(t->cli_fd, DIR_WR);
3196 shutdown(t->cli_fd, SHUT_WR);
3197 /* We must ensure that the read part is still alive when switching to shutw */
3198 /* FIXME: is this still true ? */
3199 EV_FD_SET(t->cli_fd, DIR_RD);
3200 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003201 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003202 } else {
3203 fd_delete(t->cli_fd);
3204 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003205 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003206 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003207 if (!req->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003208 if (!(t->flags & SN_ERR_MASK))
3209 t->flags |= SN_ERR_CLITO;
3210 if (!(t->flags & SN_FINST_MASK)) {
3211 if (t->pend_pos)
3212 t->flags |= SN_FINST_Q;
3213 else if (t->srv_state == SV_STCONN)
3214 t->flags |= SN_FINST_C;
3215 else
3216 t->flags |= SN_FINST_D;
3217 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003218 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003219 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003220 }
3221
3222 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003223 if (!(req->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003224 if (req->flags & BF_FULL) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003225 /* no room to read more data */
3226 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3227 /* stop reading until we get some space */
3228 req->rex = TICK_ETERNITY;
3229 }
3230 } else {
3231 EV_FD_COND_S(t->cli_fd, DIR_RD);
3232 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3233 }
3234 }
3235
3236 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003237 if (!(rep->flags & BF_SHUTW)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003238 /* first, we may have to produce data (eg: stats).
3239 * right now, this is limited to the SHUTR state.
3240 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003241 if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003242 produce_content(t);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003243 if (rep->flags & BF_EMPTY) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003244 buffer_shutw(rep);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003245 fd_delete(t->cli_fd);
3246 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003247 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaudafde432008-08-17 01:00:46 +02003248 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003249 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003250 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003251
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003252 /* 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 +02003253 if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003254 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3255 /* stop writing */
3256 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003257 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003258 } else {
3259 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003260 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003261 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003262 /* restart writing */
3263 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003264 if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003265 /* FIXME: to prevent the client from expiring read timeouts during writes,
3266 * we refresh it, except if it was already infinite. */
3267 req->rex = rep->wex;
3268 }
3269 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003270 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003271 }
3272 return 0; /* other cases change nothing */
3273 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003274 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003275 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3276 int len;
3277 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);
3278 write(1, trash, len);
3279 }
3280 return 0;
3281 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003282#ifdef DEBUG_DEV
3283 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3284 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003285#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003286 return 0;
3287}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003288
Willy Tarreaubaaee002006-06-26 02:48:02 +02003289
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003290/*
Willy Tarreaudafde432008-08-17 01:00:46 +02003291 * Manages the server FSM and its socket. It normally returns zero, but may
3292 * return 1 if it absolutely wants to be called again.
3293 *
Willy Tarreaua7c52762008-08-16 18:40:18 +02003294 * Note: process_srv is the ONLY function allowed to set srv_state to anything
3295 * but SV_STCLOSE. The only exception is for functions called from this
3296 * one (eg: those in backend.c).
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003297 */
3298int process_srv(struct session *t)
3299{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003300 struct http_txn *txn = &t->txn;
3301 struct buffer *req = t->req;
3302 struct buffer *rep = t->rep;
3303 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003305 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 +02003306 now_ms,
3307 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreaua7c52762008-08-16 18:40:18 +02003308 t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0,
3309 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 +02003310 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003311 req->flags, rep->flags,
3312 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreaudafde432008-08-17 01:00:46 +02003314 update_state:
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003315 if (t->srv_state == SV_STIDLE) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003316 if ((rep->flags & BF_SHUTW) ||
3317 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003318 (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003319 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003320 if (t->pend_pos)
3321 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3322 /* note that this must not return any error because it would be able to
3323 * overwrite the client_retnclose() output.
3324 */
3325 if (txn->flags & TX_CLTARPIT)
3326 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3327 else
3328 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3329
Willy Tarreauf8533202008-08-16 14:55:08 +02003330 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003331 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003332 }
Willy Tarreaud9f48362008-08-16 16:39:26 +02003333 else if (req->flags & BF_MAY_FORWARD) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003334 /* the client allows the server to connect */
3335 if (txn->flags & TX_CLTARPIT) {
3336 /* This connection is being tarpitted. The CLIENT side has
3337 * already set the connect expiration date to the right
3338 * timeout. We just have to check that it has not expired.
3339 */
Willy Tarreau507385d2008-08-17 13:04:25 +02003340 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003341 return 0;
3342
3343 /* We will set the queue timer to the time spent, just for
3344 * logging purposes. We fake a 500 server error, so that the
3345 * attacker will not suspect his connection has been tarpitted.
3346 * It will not cause trouble to the logs because we can exclude
3347 * the tarpitted connections by filtering on the 'PT' status flags.
3348 */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003349 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003350 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3351 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3352 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003353 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreaudafde432008-08-17 01:00:46 +02003354 goto update_state;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003355 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003356
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003357 /* Right now, we will need to create a connection to the server.
3358 * We might already have tried, and got a connection pending, in
3359 * which case we will not do anything till it's pending. It's up
3360 * to any other session to release it and wake us up again.
3361 */
3362 if (t->pend_pos) {
Willy Tarreau507385d2008-08-17 13:04:25 +02003363 if (!(req->flags & BF_WRITE_TIMEOUT)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003364 return 0;
3365 } else {
3366 /* we've been waiting too long here */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003367 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003368 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3369 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3370 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003371 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003372 if (t->srv)
3373 t->srv->failed_conns++;
3374 t->be->failed_conns++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003375 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003376 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003377 }
3378
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003379 do {
3380 /* first, get a connection */
3381 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3382 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003383
Willy Tarreaudafde432008-08-17 01:00:46 +02003384 if (srv_redispatch_connect(t)) {
3385 if (t->srv_state == SV_STIDLE)
3386 return 0;
3387 goto update_state;
3388 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003389
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003390 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3391 /* Server supporting redirection and it is possible.
3392 * Invalid requests are reported as such. It concerns all
3393 * the largest ones.
3394 */
3395 struct chunk rdr;
3396 char *path;
3397 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003398
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003399 /* 1: create the response header */
3400 rdr.len = strlen(HTTP_302);
3401 rdr.str = trash;
3402 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003403
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003404 /* 2: add the server's prefix */
3405 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3406 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003407
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003408 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3409 rdr.len += t->srv->rdr_len;
3410
3411 /* 3: add the request URI */
3412 path = http_get_path(txn);
3413 if (!path)
3414 goto cancel_redir;
3415 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3416 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3417 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003418
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003419 memcpy(rdr.str + rdr.len, path, len);
3420 rdr.len += len;
3421 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3422 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003423
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003424 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003425 trace_term(t, TT_HTTP_SRV_3);
3426
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003427 /* FIXME: we should increase a counter of redirects per server and per backend. */
3428 if (t->srv)
3429 t->srv->cum_sess++;
Willy Tarreaudafde432008-08-17 01:00:46 +02003430 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003431 cancel_redir:
3432 txn->status = 400;
3433 t->fe->failed_req++;
3434 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3435 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003436 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreaudafde432008-08-17 01:00:46 +02003437 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003438 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003440 /* try to (re-)connect to the server, and fail if we expire the
3441 * number of retries.
3442 */
3443 if (srv_retryable_connect(t)) {
3444 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003445 if (t->srv_state == SV_STIDLE)
3446 return 0;
3447 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003448 }
3449 } while (1);
Willy Tarreaudafde432008-08-17 01:00:46 +02003450 } /* end if may_forward */
3451 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003452 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003453 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003454 if ((rep->flags & BF_SHUTW) ||
3455 ((req->flags & BF_SHUTR) &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02003456 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003458 req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003459 if (!(t->flags & SN_CONN_TAR)) {
3460 /* if we are in turn-around, we have already closed the FD */
3461 fd_delete(t->srv_fd);
3462 if (t->srv) {
3463 t->srv->cur_sess--;
3464 sess_change_server(t, NULL);
3465 }
3466 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003467
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003468 /* note that this must not return any error because it would be able to
3469 * overwrite the client_retnclose() output.
3470 */
3471 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003472 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreaudafde432008-08-17 01:00:46 +02003473 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003474 }
Willy Tarreau507385d2008-08-17 13:04:25 +02003475 if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003476 return 0; /* nothing changed */
3477 }
3478 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3479 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003480 if (t->flags & SN_CONN_TAR) {
3481 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau507385d2008-08-17 13:04:25 +02003482 if (!(req->flags & BF_WRITE_TIMEOUT))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003483 return 0;
3484 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003485 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003486 else {
3487 fd_delete(t->srv_fd);
3488 if (t->srv) {
3489 t->srv->cur_sess--;
3490 sess_change_server(t, NULL);
3491 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003492
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003493 if (!(req->flags & BF_WRITE_STATUS))
3494 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3495 else
3496 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003497
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003498 /* ensure that we have enough retries left */
Willy Tarreaudafde432008-08-17 01:00:46 +02003499 if (srv_count_retry_down(t, conn_err)) {
3500 goto update_state;
3501 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003502
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003503 if (req->flags & BF_WRITE_ERROR) {
3504 /* we encountered an immediate connection error, and we
3505 * will have to retry connecting to the same server, most
3506 * likely leading to the same result. To avoid this, we
3507 * fake a connection timeout to retry after a turn-around
3508 * time of 1 second. We will wait in the previous if block.
3509 */
3510 t->flags |= SN_CONN_TAR;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003511 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003512 return 0;
3513 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003514 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003515
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003516 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3517 /* We're on our last chance, and the REDISP option was specified.
3518 * We will ignore cookie and force to balance or use the dispatcher.
3519 */
3520 /* let's try to offer this slot to anybody */
3521 if (may_dequeue_tasks(t->srv, t->be))
3522 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003523
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003524 /* it's left to the dispatcher to choose a server */
3525 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3526 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003527
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003528 /* first, get a connection */
Willy Tarreaudafde432008-08-17 01:00:46 +02003529 if (srv_redispatch_connect(t)) {
3530 if (t->srv_state == SV_STCONN)
3531 return 0;
3532 goto update_state;
3533 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003534 } else {
3535 if (t->srv)
3536 t->srv->retries++;
3537 t->be->retries++;
3538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003539
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003540 do {
3541 /* Now we will try to either reconnect to the same server or
3542 * connect to another server. If the connection gets queued
3543 * because all servers are saturated, then we will go back to
3544 * the SV_STIDLE state.
3545 */
3546 if (srv_retryable_connect(t)) {
3547 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaudafde432008-08-17 01:00:46 +02003548 if (t->srv_state == SV_STCONN)
3549 return 0;
3550 goto update_state;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003551 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003552
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003553 /* we need to redispatch the connection to another server */
Willy Tarreaudafde432008-08-17 01:00:46 +02003554 if (srv_redispatch_connect(t)) {
3555 if (t->srv_state == SV_STCONN)
3556 return 0;
3557 goto update_state;
3558 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003559 } while (1);
3560 }
3561 else { /* no error or write 0 */
3562 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003563
Willy Tarreaue393fe22008-08-16 22:18:07 +02003564 if (req->flags & BF_EMPTY) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003565 EV_FD_CLR(t->srv_fd, DIR_WR);
3566 req->wex = TICK_ETERNITY;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003567 } else {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003568 EV_FD_SET(t->srv_fd, DIR_WR);
3569 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3570 if (tick_isset(req->wex)) {
3571 /* FIXME: to prevent the server from expiring read timeouts during writes,
3572 * we refresh it. */
3573 rep->rex = req->wex;
3574 }
3575 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003576
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003577 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3578 EV_FD_SET(t->srv_fd, DIR_RD);
3579 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaue393fe22008-08-16 22:18:07 +02003580 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003581
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003582 /* if the user wants to log as soon as possible, without counting
3583 bytes from the server, then this is the right moment. */
3584 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3585 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3586 tcp_sess_log(t);
3587 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003588#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003589 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3590 /* TCP splicing supported by both FE and BE */
3591 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3592 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003593#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003594 }
3595 else {
Willy Tarreau2df28e82008-08-17 15:20:19 +02003596 rep->analysers |= AN_RTR_HTTP_HDR;
Willy Tarreaue393fe22008-08-16 22:18:07 +02003597 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003598 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3599 /* reset hdr_idx which was already initialized by the request.
3600 * right now, the http parser does it.
3601 * hdr_idx_init(&t->txn.hdr_idx);
3602 */
3603 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003604
3605 t->srv_state = SV_STDATA;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003606 if (!rep->analysers)
Willy Tarreaudafde432008-08-17 01:00:46 +02003607 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau26ed74d2008-08-17 12:11:14 +02003608 req->wex = TICK_ETERNITY;
Willy Tarreaudafde432008-08-17 01:00:46 +02003609 goto update_state;
3610 } /* else no error or write 0 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003611 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003612 else if (t->srv_state == SV_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003613 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003614 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003615 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003616 buffer_shutr(rep);
3617 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003618 fd_delete(t->srv_fd);
3619 if (t->srv) {
3620 t->srv->cur_sess--;
3621 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003622 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003623 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003624 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003625 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003626 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003627 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003628 if (!(t->flags & SN_ERR_MASK))
3629 t->flags |= SN_ERR_SRVCL;
3630 if (!(t->flags & SN_FINST_MASK))
3631 t->flags |= SN_FINST_D;
3632 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003633 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003634 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003635
Willy Tarreaudafde432008-08-17 01:00:46 +02003636 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003637 }
3638 /* last read, or end of client write */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003639 else if (!(rep->flags & BF_SHUTR) && /* not already done */
3640 rep->flags & (BF_READ_NULL | BF_SHUTW)) {
3641 buffer_shutr(rep);
3642 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003643 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003644 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003645 } else {
3646 /* output was already closed */
3647 fd_delete(t->srv_fd);
3648 if (t->srv) {
3649 t->srv->cur_sess--;
3650 sess_change_server(t, NULL);
3651 }
3652 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003653 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003654
3655 if (may_dequeue_tasks(t->srv, t->be))
3656 process_srv_queue(t->srv);
3657 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003658 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003659 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003660 /* end of client read and no more data to send. We can forward
3661 * the close when we're allowed to forward data (anytime right
3662 * now). If we're using option forceclose, then we may also
3663 * shutdown the outgoing write channel once the response starts
3664 * coming from the server.
3665 */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003666 else if (!(req->flags & BF_SHUTW) && /* not already done */
Willy Tarreaue393fe22008-08-16 22:18:07 +02003667 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
Willy Tarreauba392ce2008-08-16 21:13:23 +02003668 (req->flags & BF_SHUTR ||
Willy Tarreau461f6622008-08-15 23:43:19 +02003669 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003670 buffer_shutw(req);
3671 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003672 EV_FD_CLR(t->srv_fd, DIR_WR);
3673 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003674 trace_term(t, TT_HTTP_SRV_9);
Willy Tarreau461f6622008-08-15 23:43:19 +02003675 /* We must ensure that the read part is still alive when switching to shutw */
3676 /* FIXME: is this still true ? */
3677 EV_FD_SET(t->srv_fd, DIR_RD);
3678 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003679 } else {
3680 fd_delete(t->srv_fd);
3681 if (t->srv) {
3682 t->srv->cur_sess--;
3683 sess_change_server(t, NULL);
3684 }
3685 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003686 trace_term(t, TT_HTTP_SRV_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003687
Willy Tarreau461f6622008-08-15 23:43:19 +02003688 if (may_dequeue_tasks(t->srv, t->be))
3689 process_srv_queue(t->srv);
3690 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003691 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003692 }
3693 /* read timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003694 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003695 buffer_shutr(rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003696 if (!(req->flags & BF_SHUTW)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003697 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003698 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003699 } else {
3700 fd_delete(t->srv_fd);
3701 if (t->srv) {
3702 t->srv->cur_sess--;
3703 sess_change_server(t, NULL);
3704 }
3705 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003706 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003707
3708 if (may_dequeue_tasks(t->srv, t->be))
3709 process_srv_queue(t->srv);
3710 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003711 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003712 if (!(t->flags & SN_ERR_MASK))
3713 t->flags |= SN_ERR_SRVTO;
3714 if (!(t->flags & SN_FINST_MASK))
3715 t->flags |= SN_FINST_D;
3716 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003717
3718 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003719 }
3720 /* write timeout */
Willy Tarreau507385d2008-08-17 13:04:25 +02003721 else if (req->flags & BF_WRITE_TIMEOUT) {
Willy Tarreauba392ce2008-08-16 21:13:23 +02003722 buffer_shutw(req);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003723 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003724 EV_FD_CLR(t->srv_fd, DIR_WR);
3725 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003726 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003727 /* We must ensure that the read part is still alive when switching to shutw */
3728 /* FIXME: is this still needed ? */
3729 EV_FD_SET(t->srv_fd, DIR_RD);
3730 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003731 } else {
3732 fd_delete(t->srv_fd);
3733 if (t->srv) {
3734 t->srv->cur_sess--;
3735 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003737 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003738 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003739
Willy Tarreau461f6622008-08-15 23:43:19 +02003740 if (may_dequeue_tasks(t->srv, t->be))
3741 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003742 }
Willy Tarreau2df28e82008-08-17 15:20:19 +02003743 if (!rep->analysers) {
Willy Tarreauf495ddf2008-08-17 14:38:41 +02003744 if (!(t->flags & SN_ERR_MASK))
3745 t->flags |= SN_ERR_SRVTO;
3746 if (!(t->flags & SN_FINST_MASK))
3747 t->flags |= SN_FINST_D;
3748 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003749 goto update_state;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003750 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003751
Willy Tarreau461f6622008-08-15 23:43:19 +02003752 /* manage read timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003753 if (!(rep->flags & BF_SHUTR)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003754 if (rep->flags & BF_FULL) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003755 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3756 rep->rex = TICK_ETERNITY;
3757 } else {
3758 EV_FD_COND_S(t->srv_fd, DIR_RD);
3759 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003760 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003762
Willy Tarreau461f6622008-08-15 23:43:19 +02003763 /* manage write timeout */
Willy Tarreauba392ce2008-08-16 21:13:23 +02003764 if (!(req->flags & BF_SHUTW)) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02003765 if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003766 /* stop writing */
3767 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3768 req->wex = TICK_ETERNITY;
3769 } else {
3770 /* buffer not empty, there are still data to be transferred */
3771 EV_FD_COND_S(t->srv_fd, DIR_WR);
3772 if (!tick_isset(req->wex)) {
3773 /* restart writing */
3774 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauba392ce2008-08-16 21:13:23 +02003775 if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003776 /* FIXME: to prevent the server from expiring read timeouts during writes,
3777 * we refresh it, except if it was already infinite.
3778 */
3779 rep->rex = req->wex;
3780 }
3781 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003782 }
3783 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003784 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003786 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003787 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3788 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003789 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003790 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003791 write(1, trash, len);
3792 }
3793 return 0;
3794 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003795#ifdef DEBUG_DEV
3796 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3797 ABORT_NOW();
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003798#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003799 return 0;
3800}
3801
3802
3803/*
3804 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003805 * called with client socket shut down on input. Right now, only statistics can
3806 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003807 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003808 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003809 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003810 */
3811int produce_content(struct session *s)
3812{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003813 if (s->data_source == DATA_SRC_NONE) {
3814 s->flags &= ~SN_SELF_GEN;
3815 return 1;
3816 }
3817 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003818 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003819 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003820 if (ret >= 0)
3821 return ret;
3822 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003823 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003824
Willy Tarreau91861262007-10-17 17:06:05 +02003825 /* unknown data source or internal error */
3826 s->txn.status = 500;
3827 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003828 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003829 if (!(s->flags & SN_ERR_MASK))
3830 s->flags |= SN_ERR_PRXCOND;
3831 if (!(s->flags & SN_FINST_MASK))
3832 s->flags |= SN_FINST_R;
3833 s->flags &= ~SN_SELF_GEN;
3834 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003835}
3836
3837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003838/* Iterate the same filter through all request headers.
3839 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003840 * Since it can manage the switch to another backend, it updates the per-proxy
3841 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003842 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003843int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003844{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845 char term;
3846 char *cur_ptr, *cur_end, *cur_next;
3847 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003848 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 struct hdr_idx_elem *cur_hdr;
3850 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003851
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003852 last_hdr = 0;
3853
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003854 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003855 old_idx = 0;
3856
3857 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003858 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003859 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003860 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003861 (exp->action == ACT_ALLOW ||
3862 exp->action == ACT_DENY ||
3863 exp->action == ACT_TARPIT))
3864 return 0;
3865
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003866 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003867 if (!cur_idx)
3868 break;
3869
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003870 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003871 cur_ptr = cur_next;
3872 cur_end = cur_ptr + cur_hdr->len;
3873 cur_next = cur_end + cur_hdr->cr + 1;
3874
3875 /* Now we have one header between cur_ptr and cur_end,
3876 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003877 */
3878
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003879 /* The annoying part is that pattern matching needs
3880 * that we modify the contents to null-terminate all
3881 * strings before testing them.
3882 */
3883
3884 term = *cur_end;
3885 *cur_end = '\0';
3886
3887 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3888 switch (exp->action) {
3889 case ACT_SETBE:
3890 /* It is not possible to jump a second time.
3891 * FIXME: should we return an HTTP/500 here so that
3892 * the admin knows there's a problem ?
3893 */
3894 if (t->be != t->fe)
3895 break;
3896
3897 /* Swithing Proxy */
3898 t->be = (struct proxy *) exp->replace;
3899
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003900 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003901 * because we have associated req_cap and rsp_cap to the
3902 * frontend, and the beconn will be updated later.
3903 */
3904
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003905 t->rep->rto = t->req->wto = t->be->timeout.server;
3906 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003907 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003908 last_hdr = 1;
3909 break;
3910
3911 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003912 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003913 last_hdr = 1;
3914 break;
3915
3916 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003917 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003918 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003919 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003920 break;
3921
3922 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003923 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003924 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003925 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003926 break;
3927
3928 case ACT_REPLACE:
3929 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3930 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3931 /* FIXME: if the user adds a newline in the replacement, the
3932 * index will not be recalculated for now, and the new line
3933 * will not be counted as a new header.
3934 */
3935
3936 cur_end += delta;
3937 cur_next += delta;
3938 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003939 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003940 break;
3941
3942 case ACT_REMOVE:
3943 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3944 cur_next += delta;
3945
3946 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003947 txn->req.eoh += delta;
3948 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3949 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003950 cur_hdr->len = 0;
3951 cur_end = NULL; /* null-term has been rewritten */
3952 break;
3953
3954 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003955 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003956 if (cur_end)
3957 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003959 /* keep the link from this header to next one in case of later
3960 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003962 old_idx = cur_idx;
3963 }
3964 return 0;
3965}
3966
3967
3968/* Apply the filter to the request line.
3969 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3970 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003971 * Since it can manage the switch to another backend, it updates the per-proxy
3972 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003973 */
3974int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3975{
3976 char term;
3977 char *cur_ptr, *cur_end;
3978 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003979 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003981
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982
Willy Tarreau3d300592007-03-18 18:34:41 +01003983 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003984 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003985 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 (exp->action == ACT_ALLOW ||
3987 exp->action == ACT_DENY ||
3988 exp->action == ACT_TARPIT))
3989 return 0;
3990 else if (exp->action == ACT_REMOVE)
3991 return 0;
3992
3993 done = 0;
3994
Willy Tarreau9cdde232007-05-02 20:58:19 +02003995 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003996 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003997
3998 /* Now we have the request line between cur_ptr and cur_end */
3999
4000 /* The annoying part is that pattern matching needs
4001 * that we modify the contents to null-terminate all
4002 * strings before testing them.
4003 */
4004
4005 term = *cur_end;
4006 *cur_end = '\0';
4007
4008 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4009 switch (exp->action) {
4010 case ACT_SETBE:
4011 /* It is not possible to jump a second time.
4012 * FIXME: should we return an HTTP/500 here so that
4013 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004014 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004015 if (t->be != t->fe)
4016 break;
4017
4018 /* Swithing Proxy */
4019 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004021 /* right now, the backend switch is not too much complicated
4022 * because we have associated req_cap and rsp_cap to the
4023 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004024 */
4025
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004026 t->rep->rto = t->req->wto = t->be->timeout.server;
4027 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004028 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004029 done = 1;
4030 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004032 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004033 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004034 done = 1;
4035 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004037 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004038 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004039 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004040 done = 1;
4041 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004043 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004044 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004045 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 done = 1;
4047 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004049 case ACT_REPLACE:
4050 *cur_end = term; /* restore the string terminator */
4051 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4052 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4053 /* FIXME: if the user adds a newline in the replacement, the
4054 * index will not be recalculated for now, and the new line
4055 * will not be counted as a new header.
4056 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004057
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004058 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004059 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004060
Willy Tarreau9cdde232007-05-02 20:58:19 +02004061 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004062 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 HTTP_MSG_RQMETH,
4064 cur_ptr, cur_end + 1,
4065 NULL, NULL);
4066 if (unlikely(!cur_end))
4067 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004069 /* we have a full request and we know that we have either a CR
4070 * or an LF at <ptr>.
4071 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004072 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4073 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004074 /* there is no point trying this regex on headers */
4075 return 1;
4076 }
4077 }
4078 *cur_end = term; /* restore the string terminator */
4079 return done;
4080}
Willy Tarreau97de6242006-12-27 17:18:38 +01004081
Willy Tarreau58f10d72006-12-04 02:26:12 +01004082
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004084/*
4085 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4086 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004087 * unparsable request. Since it can manage the switch to another backend, it
4088 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004089 */
4090int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4091{
Willy Tarreau3d300592007-03-18 18:34:41 +01004092 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004094 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004095 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004097 /*
4098 * The interleaving of transformations and verdicts
4099 * makes it difficult to decide to continue or stop
4100 * the evaluation.
4101 */
4102
Willy Tarreau3d300592007-03-18 18:34:41 +01004103 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004104 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4105 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4106 exp = exp->next;
4107 continue;
4108 }
4109
4110 /* Apply the filter to the request line. */
4111 ret = apply_filter_to_req_line(t, req, exp);
4112 if (unlikely(ret < 0))
4113 return -1;
4114
4115 if (likely(ret == 0)) {
4116 /* The filter did not match the request, it can be
4117 * iterated through all headers.
4118 */
4119 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120 }
4121 exp = exp->next;
4122 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004123 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124}
4125
4126
Willy Tarreaua15645d2007-03-18 16:22:39 +01004127
Willy Tarreau58f10d72006-12-04 02:26:12 +01004128/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004129 * Manage client-side cookie. It can impact performance by about 2% so it is
4130 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131 */
4132void manage_client_side_cookies(struct session *t, struct buffer *req)
4133{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004134 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004135 char *p1, *p2, *p3, *p4;
4136 char *del_colon, *del_cookie, *colon;
4137 int app_cookies;
4138
4139 appsess *asession_temp = NULL;
4140 appsess local_asession;
4141
4142 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004143 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004144
Willy Tarreau2a324282006-12-05 00:05:46 +01004145 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004146 * we start with the start line.
4147 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004148 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004149 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004150
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004151 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004152 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004153 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004154
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004155 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156 cur_ptr = cur_next;
4157 cur_end = cur_ptr + cur_hdr->len;
4158 cur_next = cur_end + cur_hdr->cr + 1;
4159
4160 /* We have one full header between cur_ptr and cur_end, and the
4161 * next header starts at cur_next. We're only interested in
4162 * "Cookie:" headers.
4163 */
4164
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004165 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4166 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 old_idx = cur_idx;
4168 continue;
4169 }
4170
4171 /* Now look for cookies. Conforming to RFC2109, we have to support
4172 * attributes whose name begin with a '$', and associate them with
4173 * the right cookie, if we want to delete this cookie.
4174 * So there are 3 cases for each cookie read :
4175 * 1) it's a special attribute, beginning with a '$' : ignore it.
4176 * 2) it's a server id cookie that we *MAY* want to delete : save
4177 * some pointers on it (last semi-colon, beginning of cookie...)
4178 * 3) it's an application cookie : we *MAY* have to delete a previous
4179 * "special" cookie.
4180 * At the end of loop, if a "special" cookie remains, we may have to
4181 * remove it. If no application cookie persists in the header, we
4182 * *MUST* delete it
4183 */
4184
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004185 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004186
Willy Tarreau58f10d72006-12-04 02:26:12 +01004187 /* del_cookie == NULL => nothing to be deleted */
4188 del_colon = del_cookie = NULL;
4189 app_cookies = 0;
4190
4191 while (p1 < cur_end) {
4192 /* skip spaces and colons, but keep an eye on these ones */
4193 while (p1 < cur_end) {
4194 if (*p1 == ';' || *p1 == ',')
4195 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004196 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004197 break;
4198 p1++;
4199 }
4200
4201 if (p1 == cur_end)
4202 break;
4203
4204 /* p1 is at the beginning of the cookie name */
4205 p2 = p1;
4206 while (p2 < cur_end && *p2 != '=')
4207 p2++;
4208
4209 if (p2 == cur_end)
4210 break;
4211
4212 p3 = p2 + 1; /* skips the '=' sign */
4213 if (p3 == cur_end)
4214 break;
4215
4216 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004217 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218 p4++;
4219
4220 /* here, we have the cookie name between p1 and p2,
4221 * and its value between p3 and p4.
4222 * we can process it :
4223 *
4224 * Cookie: NAME=VALUE;
4225 * | || || |
4226 * | || || +--> p4
4227 * | || |+-------> p3
4228 * | || +--------> p2
4229 * | |+------------> p1
4230 * | +-------------> colon
4231 * +--------------------> cur_ptr
4232 */
4233
4234 if (*p1 == '$') {
4235 /* skip this one */
4236 }
4237 else {
4238 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004239 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004240 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004241 (p4 - p1 >= t->fe->capture_namelen) &&
4242 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004243 int log_len = p4 - p1;
4244
Willy Tarreau086b3b42007-05-13 21:45:51 +02004245 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 Alert("HTTP logging : out of memory.\n");
4247 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004248 if (log_len > t->fe->capture_len)
4249 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004250 memcpy(txn->cli_cookie, p1, log_len);
4251 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 }
4253 }
4254
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004255 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4256 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004258 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004259 char *delim;
4260
4261 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4262 * have the server ID betweek p3 and delim, and the original cookie between
4263 * delim+1 and p4. Otherwise, delim==p4 :
4264 *
4265 * Cookie: NAME=SRV~VALUE;
4266 * | || || | |
4267 * | || || | +--> p4
4268 * | || || +--------> delim
4269 * | || |+-----------> p3
4270 * | || +------------> p2
4271 * | |+----------------> p1
4272 * | +-----------------> colon
4273 * +------------------------> cur_ptr
4274 */
4275
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004276 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 for (delim = p3; delim < p4; delim++)
4278 if (*delim == COOKIE_DELIM)
4279 break;
4280 }
4281 else
4282 delim = p4;
4283
4284
4285 /* Here, we'll look for the first running server which supports the cookie.
4286 * This allows to share a same cookie between several servers, for example
4287 * to dedicate backup servers to specific servers only.
4288 * However, to prevent clients from sticking to cookie-less backup server
4289 * when they have incidentely learned an empty cookie, we simply ignore
4290 * empty cookies and mark them as invalid.
4291 */
4292 if (delim == p3)
4293 srv = NULL;
4294
4295 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004296 if (srv->cookie && (srv->cklen == delim - p3) &&
4297 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004298 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004299 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004300 txn->flags &= ~TX_CK_MASK;
4301 txn->flags |= TX_CK_VALID;
4302 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303 t->srv = srv;
4304 break;
4305 } else {
4306 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004307 txn->flags &= ~TX_CK_MASK;
4308 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004309 }
4310 }
4311 srv = srv->next;
4312 }
4313
Willy Tarreau3d300592007-03-18 18:34:41 +01004314 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004315 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004316 txn->flags &= ~TX_CK_MASK;
4317 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004318 }
4319
4320 /* depending on the cookie mode, we may have to either :
4321 * - delete the complete cookie if we're in insert+indirect mode, so that
4322 * the server never sees it ;
4323 * - remove the server id from the cookie value, and tag the cookie as an
4324 * application cookie so that it does not get accidentely removed later,
4325 * if we're in cookie prefix mode
4326 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004327 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004328 int delta; /* negative */
4329
4330 delta = buffer_replace2(req, p3, delim + 1, 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
4337 del_cookie = del_colon = NULL;
4338 app_cookies++; /* protect the header from deletion */
4339 }
4340 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004341 (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 +01004342 del_cookie = p1;
4343 del_colon = colon;
4344 }
4345 } else {
4346 /* now we know that we must keep this cookie since it's
4347 * not ours. But if we wanted to delete our cookie
4348 * earlier, we cannot remove the complete header, but we
4349 * can remove the previous block itself.
4350 */
4351 app_cookies++;
4352
4353 if (del_cookie != NULL) {
4354 int delta; /* negative */
4355
4356 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4357 p4 += delta;
4358 cur_end += delta;
4359 cur_next += delta;
4360 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004361 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004362 del_cookie = del_colon = NULL;
4363 }
4364 }
4365
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004366 if ((t->be->appsession_name != NULL) &&
4367 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004368 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004369
Willy Tarreau58f10d72006-12-04 02:26:12 +01004370 /* Cool... it's the right one */
4371
4372 asession_temp = &local_asession;
4373
Willy Tarreau63963c62007-05-13 21:29:55 +02004374 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4376 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4377 return;
4378 }
4379
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004380 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4381 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004382 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004383
Willy Tarreau58f10d72006-12-04 02:26:12 +01004384 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004385 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4386 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004387 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004388 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004389 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004390 Alert("Not enough memory process_cli():asession:calloc().\n");
4391 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4392 return;
4393 }
4394
4395 asession_temp->sessid = local_asession.sessid;
4396 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004397 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004398 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 } else {
4400 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004401 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004404 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 Alert("Found Application Session without matching server.\n");
4406 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004407 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408 while (srv) {
4409 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004410 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004412 txn->flags &= ~TX_CK_MASK;
4413 txn->flags |= TX_CK_VALID;
4414 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004415 t->srv = srv;
4416 break;
4417 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004418 txn->flags &= ~TX_CK_MASK;
4419 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 }
4421 }
4422 srv = srv->next;
4423 }/* end while(srv) */
4424 }/* end else if server == NULL */
4425
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004426 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004427 asession_temp->request_count++;
4428#if defined(DEBUG_HASH)
4429 Alert("manage_client_side_cookies\n");
4430 appsession_hash_dump(&(t->be->htbl_proxy));
4431#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004432 }/* end if ((t->proxy->appsession_name != NULL) ... */
4433 }
4434
4435 /* we'll have to look for another cookie ... */
4436 p1 = p4;
4437 } /* while (p1 < cur_end) */
4438
4439 /* There's no more cookie on this line.
4440 * We may have marked the last one(s) for deletion.
4441 * We must do this now in two ways :
4442 * - if there is no app cookie, we simply delete the header ;
4443 * - if there are app cookies, we must delete the end of the
4444 * string properly, including the colon/semi-colon before
4445 * the cookie name.
4446 */
4447 if (del_cookie != NULL) {
4448 int delta;
4449 if (app_cookies) {
4450 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4451 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004452 cur_hdr->len += delta;
4453 } else {
4454 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004455
4456 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004457 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4458 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459 cur_hdr->len = 0;
4460 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004461 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004462 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004463 }
4464
4465 /* keep the link from this header to next one */
4466 old_idx = cur_idx;
4467 } /* end of cookie processing on this header */
4468}
4469
4470
Willy Tarreaua15645d2007-03-18 16:22:39 +01004471/* Iterate the same filter through all response headers contained in <rtr>.
4472 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4473 */
4474int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4475{
4476 char term;
4477 char *cur_ptr, *cur_end, *cur_next;
4478 int cur_idx, old_idx, last_hdr;
4479 struct http_txn *txn = &t->txn;
4480 struct hdr_idx_elem *cur_hdr;
4481 int len, delta;
4482
4483 last_hdr = 0;
4484
4485 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4486 old_idx = 0;
4487
4488 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004489 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004490 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004491 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492 (exp->action == ACT_ALLOW ||
4493 exp->action == ACT_DENY))
4494 return 0;
4495
4496 cur_idx = txn->hdr_idx.v[old_idx].next;
4497 if (!cur_idx)
4498 break;
4499
4500 cur_hdr = &txn->hdr_idx.v[cur_idx];
4501 cur_ptr = cur_next;
4502 cur_end = cur_ptr + cur_hdr->len;
4503 cur_next = cur_end + cur_hdr->cr + 1;
4504
4505 /* Now we have one header between cur_ptr and cur_end,
4506 * and the next header starts at cur_next.
4507 */
4508
4509 /* The annoying part is that pattern matching needs
4510 * that we modify the contents to null-terminate all
4511 * strings before testing them.
4512 */
4513
4514 term = *cur_end;
4515 *cur_end = '\0';
4516
4517 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4518 switch (exp->action) {
4519 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004520 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004521 last_hdr = 1;
4522 break;
4523
4524 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004525 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004526 last_hdr = 1;
4527 break;
4528
4529 case ACT_REPLACE:
4530 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4531 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4532 /* FIXME: if the user adds a newline in the replacement, the
4533 * index will not be recalculated for now, and the new line
4534 * will not be counted as a new header.
4535 */
4536
4537 cur_end += delta;
4538 cur_next += delta;
4539 cur_hdr->len += delta;
4540 txn->rsp.eoh += delta;
4541 break;
4542
4543 case ACT_REMOVE:
4544 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4545 cur_next += delta;
4546
4547 /* FIXME: this should be a separate function */
4548 txn->rsp.eoh += delta;
4549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4550 txn->hdr_idx.used--;
4551 cur_hdr->len = 0;
4552 cur_end = NULL; /* null-term has been rewritten */
4553 break;
4554
4555 }
4556 }
4557 if (cur_end)
4558 *cur_end = term; /* restore the string terminator */
4559
4560 /* keep the link from this header to next one in case of later
4561 * removal of next header.
4562 */
4563 old_idx = cur_idx;
4564 }
4565 return 0;
4566}
4567
4568
4569/* Apply the filter to the status line in the response buffer <rtr>.
4570 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4571 * or -1 if a replacement resulted in an invalid status line.
4572 */
4573int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4574{
4575 char term;
4576 char *cur_ptr, *cur_end;
4577 int done;
4578 struct http_txn *txn = &t->txn;
4579 int len, delta;
4580
4581
Willy Tarreau3d300592007-03-18 18:34:41 +01004582 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004583 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004584 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004585 (exp->action == ACT_ALLOW ||
4586 exp->action == ACT_DENY))
4587 return 0;
4588 else if (exp->action == ACT_REMOVE)
4589 return 0;
4590
4591 done = 0;
4592
Willy Tarreau9cdde232007-05-02 20:58:19 +02004593 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004594 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4595
4596 /* Now we have the status line between cur_ptr and cur_end */
4597
4598 /* The annoying part is that pattern matching needs
4599 * that we modify the contents to null-terminate all
4600 * strings before testing them.
4601 */
4602
4603 term = *cur_end;
4604 *cur_end = '\0';
4605
4606 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4607 switch (exp->action) {
4608 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004609 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004610 done = 1;
4611 break;
4612
4613 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004614 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004615 done = 1;
4616 break;
4617
4618 case ACT_REPLACE:
4619 *cur_end = term; /* restore the string terminator */
4620 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4621 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4622 /* FIXME: if the user adds a newline in the replacement, the
4623 * index will not be recalculated for now, and the new line
4624 * will not be counted as a new header.
4625 */
4626
4627 txn->rsp.eoh += delta;
4628 cur_end += delta;
4629
Willy Tarreau9cdde232007-05-02 20:58:19 +02004630 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004631 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004632 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004633 cur_ptr, cur_end + 1,
4634 NULL, NULL);
4635 if (unlikely(!cur_end))
4636 return -1;
4637
4638 /* we have a full respnse and we know that we have either a CR
4639 * or an LF at <ptr>.
4640 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004641 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004642 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4643 /* there is no point trying this regex on headers */
4644 return 1;
4645 }
4646 }
4647 *cur_end = term; /* restore the string terminator */
4648 return done;
4649}
4650
4651
4652
4653/*
4654 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4655 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4656 * unparsable response.
4657 */
4658int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4659{
Willy Tarreau3d300592007-03-18 18:34:41 +01004660 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004661 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004662 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663 int ret;
4664
4665 /*
4666 * The interleaving of transformations and verdicts
4667 * makes it difficult to decide to continue or stop
4668 * the evaluation.
4669 */
4670
Willy Tarreau3d300592007-03-18 18:34:41 +01004671 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004672 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4673 exp->action == ACT_PASS)) {
4674 exp = exp->next;
4675 continue;
4676 }
4677
4678 /* Apply the filter to the status line. */
4679 ret = apply_filter_to_sts_line(t, rtr, exp);
4680 if (unlikely(ret < 0))
4681 return -1;
4682
4683 if (likely(ret == 0)) {
4684 /* The filter did not match the response, it can be
4685 * iterated through all headers.
4686 */
4687 apply_filter_to_resp_headers(t, rtr, exp);
4688 }
4689 exp = exp->next;
4690 }
4691 return 0;
4692}
4693
4694
4695
4696/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004697 * Manage server-side cookies. It can impact performance by about 2% so it is
4698 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004699 */
4700void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4701{
4702 struct http_txn *txn = &t->txn;
4703 char *p1, *p2, *p3, *p4;
4704
4705 appsess *asession_temp = NULL;
4706 appsess local_asession;
4707
4708 char *cur_ptr, *cur_end, *cur_next;
4709 int cur_idx, old_idx, delta;
4710
Willy Tarreaua15645d2007-03-18 16:22:39 +01004711 /* Iterate through the headers.
4712 * we start with the start line.
4713 */
4714 old_idx = 0;
4715 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4716
4717 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4718 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004719 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720
4721 cur_hdr = &txn->hdr_idx.v[cur_idx];
4722 cur_ptr = cur_next;
4723 cur_end = cur_ptr + cur_hdr->len;
4724 cur_next = cur_end + cur_hdr->cr + 1;
4725
4726 /* We have one full header between cur_ptr and cur_end, and the
4727 * next header starts at cur_next. We're only interested in
4728 * "Cookie:" headers.
4729 */
4730
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004731 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4732 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004733 old_idx = cur_idx;
4734 continue;
4735 }
4736
4737 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004738 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739
4740
4741 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004742 if (t->be->cookie_name == NULL &&
4743 t->be->appsession_name == NULL &&
4744 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004745 return;
4746
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004747 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004748
4749 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004750 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4751 break;
4752
4753 /* p1 is at the beginning of the cookie name */
4754 p2 = p1;
4755
4756 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4757 p2++;
4758
4759 if (p2 == cur_end || *p2 == ';') /* next cookie */
4760 break;
4761
4762 p3 = p2 + 1; /* skip the '=' sign */
4763 if (p3 == cur_end)
4764 break;
4765
4766 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004767 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004768 p4++;
4769
4770 /* here, we have the cookie name between p1 and p2,
4771 * and its value between p3 and p4.
4772 * we can process it.
4773 */
4774
4775 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004776 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004777 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004778 (p4 - p1 >= t->be->capture_namelen) &&
4779 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004780 int log_len = p4 - p1;
4781
Willy Tarreau086b3b42007-05-13 21:45:51 +02004782 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004783 Alert("HTTP logging : out of memory.\n");
4784 }
4785
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004786 if (log_len > t->be->capture_len)
4787 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004788 memcpy(txn->srv_cookie, p1, log_len);
4789 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004790 }
4791
4792 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004793 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4794 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004795 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004796 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004797
4798 /* If the cookie is in insert mode on a known server, we'll delete
4799 * this occurrence because we'll insert another one later.
4800 * We'll delete it too if the "indirect" option is set and we're in
4801 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004802 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4803 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004804 /* this header must be deleted */
4805 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4806 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4807 txn->hdr_idx.used--;
4808 cur_hdr->len = 0;
4809 cur_next += delta;
4810 txn->rsp.eoh += delta;
4811
Willy Tarreau3d300592007-03-18 18:34:41 +01004812 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004813 }
4814 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004815 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816 /* replace bytes p3->p4 with the cookie name associated
4817 * with this server since we know it.
4818 */
4819 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4820 cur_hdr->len += delta;
4821 cur_next += delta;
4822 txn->rsp.eoh += delta;
4823
Willy Tarreau3d300592007-03-18 18:34:41 +01004824 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004825 }
4826 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004827 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004828 /* insert the cookie name associated with this server
4829 * before existing cookie, and insert a delimitor between them..
4830 */
4831 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4832 cur_hdr->len += delta;
4833 cur_next += delta;
4834 txn->rsp.eoh += delta;
4835
4836 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004837 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004838 }
4839 }
4840 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004841 else if ((t->be->appsession_name != NULL) &&
4842 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004843
4844 /* Cool... it's the right one */
4845
4846 size_t server_id_len = strlen(t->srv->id) + 1;
4847 asession_temp = &local_asession;
4848
Willy Tarreau63963c62007-05-13 21:29:55 +02004849 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4851 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4852 return;
4853 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004854 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4855 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856 asession_temp->serverid = NULL;
4857
4858 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004859 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4860 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004861 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004862 Alert("Not enough Memory process_srv():asession:calloc().\n");
4863 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4864 return;
4865 }
4866 asession_temp->sessid = local_asession.sessid;
4867 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004868 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004869 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4870 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004871 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004872 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004873 }
4874
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004876 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004877 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4878 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4879 return;
4880 }
4881 asession_temp->serverid[0] = '\0';
4882 }
4883
4884 if (asession_temp->serverid[0] == '\0')
4885 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4886
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004887 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004888 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004889#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004890 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004891 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004892#endif
4893 }/* end if ((t->proxy->appsession_name != NULL) ... */
4894 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4895 } /* we're now at the end of the cookie value */
4896
4897 /* keep the link from this header to next one */
4898 old_idx = cur_idx;
4899 } /* end of cookie processing on this header */
4900}
4901
4902
4903
4904/*
4905 * Check if response is cacheable or not. Updates t->flags.
4906 */
4907void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4908{
4909 struct http_txn *txn = &t->txn;
4910 char *p1, *p2;
4911
4912 char *cur_ptr, *cur_end, *cur_next;
4913 int cur_idx;
4914
Willy Tarreau5df51872007-11-25 16:20:08 +01004915 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004916 return;
4917
4918 /* Iterate through the headers.
4919 * we start with the start line.
4920 */
4921 cur_idx = 0;
4922 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4923
4924 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4925 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004926 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927
4928 cur_hdr = &txn->hdr_idx.v[cur_idx];
4929 cur_ptr = cur_next;
4930 cur_end = cur_ptr + cur_hdr->len;
4931 cur_next = cur_end + cur_hdr->cr + 1;
4932
4933 /* We have one full header between cur_ptr and cur_end, and the
4934 * next header starts at cur_next. We're only interested in
4935 * "Cookie:" headers.
4936 */
4937
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004938 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4939 if (val) {
4940 if ((cur_end - (cur_ptr + val) >= 8) &&
4941 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4942 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4943 return;
4944 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945 }
4946
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004947 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4948 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004949 continue;
4950
4951 /* OK, right now we know we have a cache-control header at cur_ptr */
4952
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004953 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004954
4955 if (p1 >= cur_end) /* no more info */
4956 continue;
4957
4958 /* p1 is at the beginning of the value */
4959 p2 = p1;
4960
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004961 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004962 p2++;
4963
4964 /* we have a complete value between p1 and p2 */
4965 if (p2 < cur_end && *p2 == '=') {
4966 /* we have something of the form no-cache="set-cookie" */
4967 if ((cur_end - p1 >= 21) &&
4968 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4969 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004970 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004971 continue;
4972 }
4973
4974 /* OK, so we know that either p2 points to the end of string or to a comma */
4975 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4976 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4977 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4978 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004979 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004980 return;
4981 }
4982
4983 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004984 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004985 continue;
4986 }
4987 }
4988}
4989
4990
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991/*
4992 * Try to retrieve a known appsession in the URI, then the associated server.
4993 * If the server is found, it's assigned to the session.
4994 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004995void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004996{
Willy Tarreau3d300592007-03-18 18:34:41 +01004997 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004998 appsess *asession_temp = NULL;
4999 appsess local_asession;
5000 char *request_line;
5001
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005002 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005003 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005004 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005005 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005006 return;
5007
5008 /* skip ';' */
5009 request_line++;
5010
5011 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005012 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005013 return;
5014
5015 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005016 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005017
5018 /* First try if we already have an appsession */
5019 asession_temp = &local_asession;
5020
Willy Tarreau63963c62007-05-13 21:29:55 +02005021 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005022 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5023 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5024 return;
5025 }
5026
5027 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005028 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5029 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005030 asession_temp->serverid = NULL;
5031
5032 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005033 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5034 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005035 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005036 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005037 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005038 Alert("Not enough memory process_cli():asession:calloc().\n");
5039 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5040 return;
5041 }
5042 asession_temp->sessid = local_asession.sessid;
5043 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005044 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005045 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005046 }
5047 else {
5048 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005049 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005050 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005051
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005052 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005053 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005054
Willy Tarreau58f10d72006-12-04 02:26:12 +01005055#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005056 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005057 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005058#endif
5059 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005060 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005061 Alert("Found Application Session without matching server.\n");
5062 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005063 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005064 while (srv) {
5065 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005066 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005067 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005068 txn->flags &= ~TX_CK_MASK;
5069 txn->flags |= TX_CK_VALID;
5070 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005071 t->srv = srv;
5072 break;
5073 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005074 txn->flags &= ~TX_CK_MASK;
5075 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005076 }
5077 }
5078 srv = srv->next;
5079 }
5080 }
5081}
5082
5083
Willy Tarreaub2513902006-12-17 14:52:38 +01005084/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005085 * In a GET or HEAD request, check if the requested URI matches the stats uri
5086 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005087 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005088 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005089 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005090 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005091 *
5092 * Returns 1 if the session's state changes, otherwise 0.
5093 */
5094int stats_check_uri_auth(struct session *t, struct proxy *backend)
5095{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005096 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005097 struct uri_auth *uri_auth = backend->uri_auth;
5098 struct user_auth *user;
5099 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005100 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005101
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005102 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005104 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005105 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005106 return 0;
5107
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005108 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005109
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005110 /* the URI is in h */
5111 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005112 return 0;
5113
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005114 h += uri_auth->uri_len;
5115 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5116 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005117 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005118 break;
5119 }
5120 h++;
5121 }
5122
5123 if (uri_auth->refresh) {
5124 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5125 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5126 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005127 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005128 break;
5129 }
5130 h++;
5131 }
5132 }
5133
Willy Tarreau55bb8452007-10-17 18:44:57 +02005134 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5135 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5136 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005137 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005138 break;
5139 }
5140 h++;
5141 }
5142
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005143 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5144
Willy Tarreaub2513902006-12-17 14:52:38 +01005145 /* we are in front of a interceptable URI. Let's check
5146 * if there's an authentication and if it's valid.
5147 */
5148 user = uri_auth->users;
5149 if (!user) {
5150 /* no user auth required, it's OK */
5151 authenticated = 1;
5152 } else {
5153 authenticated = 0;
5154
5155 /* a user list is defined, we have to check.
5156 * skip 21 chars for "Authorization: Basic ".
5157 */
5158
5159 /* FIXME: this should move to an earlier place */
5160 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005161 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5162 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5163 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005164 if (len > 14 &&
5165 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005166 txn->auth_hdr.str = h;
5167 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005168 break;
5169 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005170 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005171 }
5172
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005173 if (txn->auth_hdr.len < 21 ||
5174 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005175 user = NULL;
5176
5177 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005178 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5179 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005180 user->user_pwd, user->user_len)) {
5181 authenticated = 1;
5182 break;
5183 }
5184 user = user->next;
5185 }
5186 }
5187
5188 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005189 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005190
5191 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005192 msg.str = trash;
5193 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005194 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005195 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005196 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005197 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005198 if (!(t->flags & SN_ERR_MASK))
5199 t->flags |= SN_ERR_PRXCOND;
5200 if (!(t->flags & SN_FINST_MASK))
5201 t->flags |= SN_FINST_R;
5202 return 1;
5203 }
5204
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005205 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005206 * data.
5207 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005208 EV_FD_CLR(t->cli_fd, DIR_RD);
5209 buffer_shutr(t->req);
5210 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005211 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005212 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005213 t->data_source = DATA_SRC_STATS;
5214 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005215 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005216 produce_content(t);
5217 return 1;
5218}
5219
5220
Willy Tarreaubaaee002006-06-26 02:48:02 +02005221/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005222 * Print a debug line with a header
5223 */
5224void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5225{
5226 int len, max;
5227 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5228 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5229 max = end - start;
5230 UBOUND(max, sizeof(trash) - len - 1);
5231 len += strlcpy2(trash + len, start, max + 1);
5232 trash[len++] = '\n';
5233 write(1, trash, len);
5234}
5235
5236
Willy Tarreau8797c062007-05-07 00:55:35 +02005237/************************************************************************/
5238/* The code below is dedicated to ACL parsing and matching */
5239/************************************************************************/
5240
5241
5242
5243
5244/* 1. Check on METHOD
5245 * We use the pre-parsed method if it is known, and store its number as an
5246 * integer. If it is unknown, we use the pointer and the length.
5247 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005248static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005249{
5250 int len, meth;
5251
Willy Tarreauae8b7962007-06-09 23:10:04 +02005252 len = strlen(*text);
5253 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005254
5255 pattern->val.i = meth;
5256 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005257 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005258 if (!pattern->ptr.str)
5259 return 0;
5260 pattern->len = len;
5261 }
5262 return 1;
5263}
5264
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005265static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005266acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5267 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005268{
5269 int meth;
5270 struct http_txn *txn = l7;
5271
Willy Tarreaub6866442008-07-14 23:54:42 +02005272 if (!txn)
5273 return 0;
5274
Willy Tarreauc11416f2007-06-17 16:58:38 +02005275 if (txn->req.msg_state != HTTP_MSG_BODY)
5276 return 0;
5277
Willy Tarreau8797c062007-05-07 00:55:35 +02005278 meth = txn->meth;
5279 test->i = meth;
5280 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005281 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5282 /* ensure the indexes are not affected */
5283 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005284 test->len = txn->req.sl.rq.m_l;
5285 test->ptr = txn->req.sol;
5286 }
5287 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5288 return 1;
5289}
5290
5291static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5292{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005293 int icase;
5294
Willy Tarreau8797c062007-05-07 00:55:35 +02005295 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005296 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005297
5298 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005299 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005300
5301 /* Other method, we must compare the strings */
5302 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005303 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005304
5305 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5306 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5307 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005308 return ACL_PAT_FAIL;
5309 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005310}
5311
5312/* 2. Check on Request/Status Version
5313 * We simply compare strings here.
5314 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005315static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005316{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005317 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005318 if (!pattern->ptr.str)
5319 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005320 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005321 return 1;
5322}
5323
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005324static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005325acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5326 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005327{
5328 struct http_txn *txn = l7;
5329 char *ptr;
5330 int len;
5331
Willy Tarreaub6866442008-07-14 23:54:42 +02005332 if (!txn)
5333 return 0;
5334
Willy Tarreauc11416f2007-06-17 16:58:38 +02005335 if (txn->req.msg_state != HTTP_MSG_BODY)
5336 return 0;
5337
Willy Tarreau8797c062007-05-07 00:55:35 +02005338 len = txn->req.sl.rq.v_l;
5339 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5340
5341 while ((len-- > 0) && (*ptr++ != '/'));
5342 if (len <= 0)
5343 return 0;
5344
5345 test->ptr = ptr;
5346 test->len = len;
5347
5348 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5349 return 1;
5350}
5351
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005352static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005353acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5354 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005355{
5356 struct http_txn *txn = l7;
5357 char *ptr;
5358 int len;
5359
Willy Tarreaub6866442008-07-14 23:54:42 +02005360 if (!txn)
5361 return 0;
5362
Willy Tarreauc11416f2007-06-17 16:58:38 +02005363 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5364 return 0;
5365
Willy Tarreau8797c062007-05-07 00:55:35 +02005366 len = txn->rsp.sl.st.v_l;
5367 ptr = txn->rsp.sol;
5368
5369 while ((len-- > 0) && (*ptr++ != '/'));
5370 if (len <= 0)
5371 return 0;
5372
5373 test->ptr = ptr;
5374 test->len = len;
5375
5376 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5377 return 1;
5378}
5379
5380/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005381static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005382acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5383 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005384{
5385 struct http_txn *txn = l7;
5386 char *ptr;
5387 int len;
5388
Willy Tarreaub6866442008-07-14 23:54:42 +02005389 if (!txn)
5390 return 0;
5391
Willy Tarreauc11416f2007-06-17 16:58:38 +02005392 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5393 return 0;
5394
Willy Tarreau8797c062007-05-07 00:55:35 +02005395 len = txn->rsp.sl.st.c_l;
5396 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5397
5398 test->i = __strl2ui(ptr, len);
5399 test->flags = ACL_TEST_F_VOL_1ST;
5400 return 1;
5401}
5402
5403/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005404static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005405acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5406 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005407{
5408 struct http_txn *txn = l7;
5409
Willy Tarreaub6866442008-07-14 23:54:42 +02005410 if (!txn)
5411 return 0;
5412
Willy Tarreauc11416f2007-06-17 16:58:38 +02005413 if (txn->req.msg_state != HTTP_MSG_BODY)
5414 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005415
Willy Tarreauc11416f2007-06-17 16:58:38 +02005416 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5417 /* ensure the indexes are not affected */
5418 return 0;
5419
Willy Tarreau8797c062007-05-07 00:55:35 +02005420 test->len = txn->req.sl.rq.u_l;
5421 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5422
Willy Tarreauf3d25982007-05-08 22:45:09 +02005423 /* we do not need to set READ_ONLY because the data is in a buffer */
5424 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005425 return 1;
5426}
5427
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005428static int
5429acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5430 struct acl_expr *expr, struct acl_test *test)
5431{
5432 struct http_txn *txn = l7;
5433
Willy Tarreaub6866442008-07-14 23:54:42 +02005434 if (!txn)
5435 return 0;
5436
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005437 if (txn->req.msg_state != HTTP_MSG_BODY)
5438 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005439
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005440 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5441 /* ensure the indexes are not affected */
5442 return 0;
5443
5444 /* Parse HTTP request */
5445 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5446 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5447 test->i = AF_INET;
5448
5449 /*
5450 * If we are parsing url in frontend space, we prepare backend stage
5451 * to not parse again the same url ! optimization lazyness...
5452 */
5453 if (px->options & PR_O_HTTP_PROXY)
5454 l4->flags |= SN_ADDR_SET;
5455
5456 test->flags = ACL_TEST_F_READ_ONLY;
5457 return 1;
5458}
5459
5460static int
5461acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5462 struct acl_expr *expr, struct acl_test *test)
5463{
5464 struct http_txn *txn = l7;
5465
Willy Tarreaub6866442008-07-14 23:54:42 +02005466 if (!txn)
5467 return 0;
5468
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005469 if (txn->req.msg_state != HTTP_MSG_BODY)
5470 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005471
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005472 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5473 /* ensure the indexes are not affected */
5474 return 0;
5475
5476 /* Same optimization as url_ip */
5477 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5478 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5479
5480 if (px->options & PR_O_HTTP_PROXY)
5481 l4->flags |= SN_ADDR_SET;
5482
5483 test->flags = ACL_TEST_F_READ_ONLY;
5484 return 1;
5485}
5486
Willy Tarreauc11416f2007-06-17 16:58:38 +02005487/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5488 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5489 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005490static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005491acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005492 struct acl_expr *expr, struct acl_test *test)
5493{
5494 struct http_txn *txn = l7;
5495 struct hdr_idx *idx = &txn->hdr_idx;
5496 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005497
Willy Tarreaub6866442008-07-14 23:54:42 +02005498 if (!txn)
5499 return 0;
5500
Willy Tarreau33a7e692007-06-10 19:45:56 +02005501 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5502 /* search for header from the beginning */
5503 ctx->idx = 0;
5504
Willy Tarreau33a7e692007-06-10 19:45:56 +02005505 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5506 test->flags |= ACL_TEST_F_FETCH_MORE;
5507 test->flags |= ACL_TEST_F_VOL_HDR;
5508 test->len = ctx->vlen;
5509 test->ptr = (char *)ctx->line + ctx->val;
5510 return 1;
5511 }
5512
5513 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5514 test->flags |= ACL_TEST_F_VOL_HDR;
5515 return 0;
5516}
5517
Willy Tarreau33a7e692007-06-10 19:45:56 +02005518static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005519acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5520 struct acl_expr *expr, struct acl_test *test)
5521{
5522 struct http_txn *txn = l7;
5523
Willy Tarreaub6866442008-07-14 23:54:42 +02005524 if (!txn)
5525 return 0;
5526
Willy Tarreauc11416f2007-06-17 16:58:38 +02005527 if (txn->req.msg_state != HTTP_MSG_BODY)
5528 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005529
Willy Tarreauc11416f2007-06-17 16:58:38 +02005530 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5531 /* ensure the indexes are not affected */
5532 return 0;
5533
5534 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5535}
5536
5537static int
5538acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5539 struct acl_expr *expr, struct acl_test *test)
5540{
5541 struct http_txn *txn = l7;
5542
Willy Tarreaub6866442008-07-14 23:54:42 +02005543 if (!txn)
5544 return 0;
5545
Willy Tarreauc11416f2007-06-17 16:58:38 +02005546 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5547 return 0;
5548
5549 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5550}
5551
5552/* 6. Check on HTTP header count. The number of occurrences is returned.
5553 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5554 */
5555static int
5556acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005557 struct acl_expr *expr, struct acl_test *test)
5558{
5559 struct http_txn *txn = l7;
5560 struct hdr_idx *idx = &txn->hdr_idx;
5561 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005562 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005563
Willy Tarreaub6866442008-07-14 23:54:42 +02005564 if (!txn)
5565 return 0;
5566
Willy Tarreau33a7e692007-06-10 19:45:56 +02005567 ctx.idx = 0;
5568 cnt = 0;
5569 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5570 cnt++;
5571
5572 test->i = cnt;
5573 test->flags = ACL_TEST_F_VOL_HDR;
5574 return 1;
5575}
5576
Willy Tarreauc11416f2007-06-17 16:58:38 +02005577static int
5578acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5579 struct acl_expr *expr, struct acl_test *test)
5580{
5581 struct http_txn *txn = l7;
5582
Willy Tarreaub6866442008-07-14 23:54:42 +02005583 if (!txn)
5584 return 0;
5585
Willy Tarreauc11416f2007-06-17 16:58:38 +02005586 if (txn->req.msg_state != HTTP_MSG_BODY)
5587 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005588
Willy Tarreauc11416f2007-06-17 16:58:38 +02005589 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5590 /* ensure the indexes are not affected */
5591 return 0;
5592
5593 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5594}
5595
5596static int
5597acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5598 struct acl_expr *expr, struct acl_test *test)
5599{
5600 struct http_txn *txn = l7;
5601
Willy Tarreaub6866442008-07-14 23:54:42 +02005602 if (!txn)
5603 return 0;
5604
Willy Tarreauc11416f2007-06-17 16:58:38 +02005605 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5606 return 0;
5607
5608 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5609}
5610
Willy Tarreau33a7e692007-06-10 19:45:56 +02005611/* 7. Check on HTTP header's integer value. The integer value is returned.
5612 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005613 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005614 */
5615static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005616acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005617 struct acl_expr *expr, struct acl_test *test)
5618{
5619 struct http_txn *txn = l7;
5620 struct hdr_idx *idx = &txn->hdr_idx;
5621 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005622
Willy Tarreaub6866442008-07-14 23:54:42 +02005623 if (!txn)
5624 return 0;
5625
Willy Tarreau33a7e692007-06-10 19:45:56 +02005626 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5627 /* search for header from the beginning */
5628 ctx->idx = 0;
5629
Willy Tarreau33a7e692007-06-10 19:45:56 +02005630 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5631 test->flags |= ACL_TEST_F_FETCH_MORE;
5632 test->flags |= ACL_TEST_F_VOL_HDR;
5633 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5634 return 1;
5635 }
5636
5637 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5638 test->flags |= ACL_TEST_F_VOL_HDR;
5639 return 0;
5640}
5641
Willy Tarreauc11416f2007-06-17 16:58:38 +02005642static int
5643acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5644 struct acl_expr *expr, struct acl_test *test)
5645{
5646 struct http_txn *txn = l7;
5647
Willy Tarreaub6866442008-07-14 23:54:42 +02005648 if (!txn)
5649 return 0;
5650
Willy Tarreauc11416f2007-06-17 16:58:38 +02005651 if (txn->req.msg_state != HTTP_MSG_BODY)
5652 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005653
Willy Tarreauc11416f2007-06-17 16:58:38 +02005654 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5655 /* ensure the indexes are not affected */
5656 return 0;
5657
5658 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5659}
5660
5661static int
5662acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5663 struct acl_expr *expr, struct acl_test *test)
5664{
5665 struct http_txn *txn = l7;
5666
Willy Tarreaub6866442008-07-14 23:54:42 +02005667 if (!txn)
5668 return 0;
5669
Willy Tarreauc11416f2007-06-17 16:58:38 +02005670 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5671 return 0;
5672
5673 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5674}
5675
Willy Tarreau737b0c12007-06-10 21:28:46 +02005676/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5677 * the first '/' after the possible hostname, and ends before the possible '?'.
5678 */
5679static int
5680acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5681 struct acl_expr *expr, struct acl_test *test)
5682{
5683 struct http_txn *txn = l7;
5684 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005685
Willy Tarreaub6866442008-07-14 23:54:42 +02005686 if (!txn)
5687 return 0;
5688
Willy Tarreauc11416f2007-06-17 16:58:38 +02005689 if (txn->req.msg_state != HTTP_MSG_BODY)
5690 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005691
Willy Tarreauc11416f2007-06-17 16:58:38 +02005692 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5693 /* ensure the indexes are not affected */
5694 return 0;
5695
Willy Tarreau21d2af32008-02-14 20:25:24 +01005696 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5697 ptr = http_get_path(txn);
5698 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005699 return 0;
5700
5701 /* OK, we got the '/' ! */
5702 test->ptr = ptr;
5703
5704 while (ptr < end && *ptr != '?')
5705 ptr++;
5706
5707 test->len = ptr - test->ptr;
5708
5709 /* we do not need to set READ_ONLY because the data is in a buffer */
5710 test->flags = ACL_TEST_F_VOL_1ST;
5711 return 1;
5712}
5713
5714
Willy Tarreau8797c062007-05-07 00:55:35 +02005715
5716/************************************************************************/
5717/* All supported keywords must be declared here. */
5718/************************************************************************/
5719
5720/* Note: must not be declared <const> as its list will be overwritten */
5721static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005722 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5723 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5724 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5725 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005726
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005727 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5728 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5729 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5730 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5731 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5732 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5733 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5734 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5735 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005736
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005737 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5738 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5739 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5740 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5741 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5742 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5743 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5744 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5745 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5746 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005747
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005748 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5749 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5750 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5751 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5752 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5753 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5754 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5755 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5756 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005757
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005758 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5759 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5760 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5761 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5762 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5763 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5764 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005765
Willy Tarreauf3d25982007-05-08 22:45:09 +02005766 { NULL, NULL, NULL, NULL },
5767
5768#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005769 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5770 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5771 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5772 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5773 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5774 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5775 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5776
Willy Tarreau8797c062007-05-07 00:55:35 +02005777 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5778 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5779 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5780 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5781 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5782 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5783 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5784 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5785
5786 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5787 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5788 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5789 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5790 { NULL, NULL, NULL, NULL },
5791#endif
5792}};
5793
5794
5795__attribute__((constructor))
5796static void __http_protocol_init(void)
5797{
5798 acl_register_keywords(&acl_kws);
5799}
5800
5801
Willy Tarreau58f10d72006-12-04 02:26:12 +01005802/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005803 * Local variables:
5804 * c-indent-level: 8
5805 * c-basic-offset: 8
5806 * End:
5807 */