blob: f4ffae7936c4036ab64f62c455156c913bd24e0c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaua15645d2007-03-18 16:22:39 +01004 * Copyright 2000-2007 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>
33#include <common/time.h>
34#include <common/uri_auth.h>
35#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau8797c062007-05-07 00:55:35 +020037#include <types/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <types/capture.h>
39#include <types/client.h>
40#include <types/global.h>
41#include <types/httperr.h>
42#include <types/polling.h>
43#include <types/proxy.h>
44#include <types/server.h>
45
Willy Tarreau8797c062007-05-07 00:55:35 +020046#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
51#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010052#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
54#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020055#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/session.h>
57#include <proto/task.h>
58
Willy Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#define DEBUG_PARSE_NO_SPEEDUP
64#undef DEBUG_PARSE_NO_SPEEDUP
65
Willy Tarreau976f1ee2006-12-17 10:06:03 +010066/* This is used to perform a quick jump as an alternative to a break/continue
67 * instruction. The first argument is the label for normal operation, and the
68 * second one is the break/continue instruction in the no_speedup mode.
69 */
70
71#ifdef DEBUG_PARSE_NO_SPEEDUP
72#define QUICK_JUMP(x,y) y
73#else
74#define QUICK_JUMP(x,y) goto x
75#endif
76
Willy Tarreau1c47f852006-07-09 08:22:27 +020077/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020079 "HTTP/1.0 200 OK\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Connection: close\r\n"
82 "Content-Type: text/html\r\n"
83 "\r\n"
84 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const struct chunk http_200_chunk = {
87 .str = (char *)&HTTP_200,
88 .len = sizeof(HTTP_200)-1
89};
90
91const char *HTTP_302 =
92 "HTTP/1.0 302 Found\r\n"
93 "Cache-Control: no-cache\r\n"
94 "Connection: close\r\n"
95 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
99 "HTTP/1.0 303 See Other\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Connection: close\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100127 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
133
134 [HTTP_ERR_403] =
135 "HTTP/1.0 403 Forbidden\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
141
142 [HTTP_ERR_408] =
143 "HTTP/1.0 408 Request Time-out\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
149
150 [HTTP_ERR_500] =
151 "HTTP/1.0 500 Server Error\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
157
158 [HTTP_ERR_502] =
159 "HTTP/1.0 502 Bad Gateway\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
165
166 [HTTP_ERR_503] =
167 "HTTP/1.0 503 Service Unavailable\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
173
174 [HTTP_ERR_504] =
175 "HTTP/1.0 504 Gateway Time-out\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
181
182};
183
Willy Tarreau80587432006-12-24 17:47:20 +0100184/* We must put the messages here since GCC cannot initialize consts depending
185 * on strlen().
186 */
187struct chunk http_err_chunks[HTTP_ERR_SIZE];
188
Willy Tarreau42250582007-04-01 01:30:43 +0200189#define FD_SETS_ARE_BITFIELDS
190#ifdef FD_SETS_ARE_BITFIELDS
191/*
192 * This map is used with all the FD_* macros to check whether a particular bit
193 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
194 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
195 * byte should be encoded. Be careful to always pass bytes from 0 to 255
196 * exclusively to the macros.
197 */
198fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
199fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
200
201#else
202#error "Check if your OS uses bitfields for fd_sets"
203#endif
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205void init_proto_http()
206{
Willy Tarreau42250582007-04-01 01:30:43 +0200207 int i;
208 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100209 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200210
Willy Tarreau80587432006-12-24 17:47:20 +0100211 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
212 if (!http_err_msgs[msg]) {
213 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
214 abort();
215 }
216
217 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
218 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
219 }
Willy Tarreau42250582007-04-01 01:30:43 +0200220
221 /* initialize the log header encoding map : '{|}"#' should be encoded with
222 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
223 * URL encoding only requires '"', '#' to be encoded as well as non-
224 * printable characters above.
225 */
226 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
227 memset(url_encode_map, 0, sizeof(url_encode_map));
228 for (i = 0; i < 32; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232 for (i = 127; i < 256; i++) {
233 FD_SET(i, hdr_encode_map);
234 FD_SET(i, url_encode_map);
235 }
236
237 tmp = "\"#{|}";
238 while (*tmp) {
239 FD_SET(*tmp, hdr_encode_map);
240 tmp++;
241 }
242
243 tmp = "\"#";
244 while (*tmp) {
245 FD_SET(*tmp, url_encode_map);
246 tmp++;
247 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200248
249 /* memory allocations */
250 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200251 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100252}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
Willy Tarreau53b6c742006-12-17 13:37:46 +0100254/*
255 * We have 26 list of methods (1 per first letter), each of which can have
256 * up to 3 entries (2 valid, 1 null).
257 */
258struct http_method_desc {
259 http_meth_t meth;
260 int len;
261 const char text[8];
262};
263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100264const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100265 ['C' - 'A'] = {
266 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
267 },
268 ['D' - 'A'] = {
269 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
270 },
271 ['G' - 'A'] = {
272 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
273 },
274 ['H' - 'A'] = {
275 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
276 },
277 ['P' - 'A'] = {
278 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
279 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
280 },
281 ['T' - 'A'] = {
282 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
283 },
284 /* rest is empty like this :
285 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
286 */
287};
288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289/* It is about twice as fast on recent architectures to lookup a byte in a
290 * table than two perform a boolean AND or OR between two tests. Refer to
291 * RFC2616 for those chars.
292 */
293
294const char http_is_spht[256] = {
295 [' '] = 1, ['\t'] = 1,
296};
297
298const char http_is_crlf[256] = {
299 ['\r'] = 1, ['\n'] = 1,
300};
301
302const char http_is_lws[256] = {
303 [' '] = 1, ['\t'] = 1,
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_sep[256] = {
308 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
309 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
310 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
311 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
312 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
313};
314
315const char http_is_ctl[256] = {
316 [0 ... 31] = 1,
317 [127] = 1,
318};
319
320/*
321 * A token is any ASCII char that is neither a separator nor a CTL char.
322 * Do not overwrite values in assignment since gcc-2.95 will not handle
323 * them correctly. Instead, define every non-CTL char's status.
324 */
325const char http_is_token[256] = {
326 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
327 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
328 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
329 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
330 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
331 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
332 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
333 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
334 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
335 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
336 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
337 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
338 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
339 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
340 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
341 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
342 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
343 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
344 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
345 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
346 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
347 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
348 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
349 ['|'] = 1, ['}'] = 0, ['~'] = 1,
350};
351
352
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100353/*
354 * An http ver_token is any ASCII which can be found in an HTTP version,
355 * which includes 'H', 'T', 'P', '/', '.' and any digit.
356 */
357const char http_is_ver_token[256] = {
358 ['.'] = 1, ['/'] = 1,
359 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
360 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
361 ['H'] = 1, ['P'] = 1, ['T'] = 1,
362};
363
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365#ifdef DEBUG_FULL
366static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
367static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
368#endif
369
Willy Tarreau42250582007-04-01 01:30:43 +0200370static void http_sess_log(struct session *s);
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
468 __label__ return_hdr, next_hdr;
469 const char *eol, *sov;
470 int cur_idx;
471
472 if (ctx->idx) {
473 /* We have previously returned a value, let's search
474 * another one on the same line.
475 */
476 cur_idx = ctx->idx;
477 sol = ctx->line;
478 sov = sol + ctx->val + ctx->vlen;
479 eol = sol + idx->v[cur_idx].len;
480
481 if (sov >= eol)
482 /* no more values in this header */
483 goto next_hdr;
484
485 /* values remaining for this header, skip the comma */
486 sov++;
487 while (sov < eol && http_is_lws[(unsigned char)*sov])
488 sov++;
489
490 goto return_hdr;
491 }
492
493 /* first request for this header */
494 sol += hdr_idx_first_pos(idx);
495 cur_idx = hdr_idx_first_idx(idx);
496
497 while (cur_idx) {
498 eol = sol + idx->v[cur_idx].len;
499
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200500 if (len == 0) {
501 /* No argument was passed, we want any header.
502 * To achieve this, we simply build a fake request. */
503 while (sol + len < eol && sol[len] != ':')
504 len++;
505 name = sol;
506 }
507
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 if ((len < eol - sol) &&
509 (sol[len] == ':') &&
510 (strncasecmp(sol, name, len) == 0)) {
511
512 sov = sol + len + 1;
513 while (sov < eol && http_is_lws[(unsigned char)*sov])
514 sov++;
515 return_hdr:
516 ctx->line = sol;
517 ctx->idx = cur_idx;
518 ctx->val = sov - sol;
519
520 eol = find_hdr_value_end(sov, eol);
521 ctx->vlen = eol - sov;
522 return 1;
523 }
524 next_hdr:
525 sol = eol + idx->v[cur_idx].cr + 1;
526 cur_idx = idx->v[cur_idx].next;
527 }
528 return 0;
529}
530
531int http_find_header(const char *name,
532 const char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 return http_find_header2(name, strlen(name), sol, idx, ctx);
536}
537
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 * indicators accordingly. Note that if <status> is 0, or if the message
540 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 */
542void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
545 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100546 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100547 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100548 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 }
551 if (!(t->flags & SN_ERR_MASK))
552 t->flags |= err;
553 if (!(t->flags & SN_FINST_MASK))
554 t->flags |= finst;
555}
556
Willy Tarreau80587432006-12-24 17:47:20 +0100557/* This function returns the appropriate error location for the given session
558 * and message.
559 */
560
561struct chunk *error_message(struct session *s, int msgnum)
562{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200563 if (s->be->errmsg[msgnum].str)
564 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100565 else if (s->fe->errmsg[msgnum].str)
566 return &s->fe->errmsg[msgnum];
567 else
568 return &http_err_chunks[msgnum];
569}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571/*
572 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
573 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
574 */
575static http_meth_t find_http_meth(const char *str, const int len)
576{
577 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579
580 m = ((unsigned)*str - 'A');
581
582 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 for (h = http_methods[m]; h->len > 0; h++) {
584 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100585 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 };
589 return HTTP_METH_OTHER;
590 }
591 return HTTP_METH_NONE;
592
593}
594
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595/* Processes the client and server jobs of a session task, then
596 * puts it back to the wait queue in a clean state, or
597 * cleans up its resources if it must be deleted. Returns
598 * the time the task accepts to wait, or TIME_ETERNITY for
599 * infinity.
600 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200601void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602{
603 struct session *s = t->context;
604 int fsm_resync = 0;
605
606 do {
607 fsm_resync = 0;
608 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
609 fsm_resync |= process_cli(s);
610 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
611 fsm_resync |= process_srv(s);
612 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
613 } while (fsm_resync);
614
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200615 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200616 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
617 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200619 t->expire = s->req->rex;
620 tv_min(&t->expire, &s->req->rex, &s->req->wex);
621 tv_bound(&t->expire, &s->req->cex);
622 tv_bound(&t->expire, &s->rep->rex);
623 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624
625 /* restore t to its place in the task list */
626 task_queue(t);
627
628#ifdef DEBUG_FULL
629 /* DEBUG code : this should never ever happen, otherwise it indicates
630 * that a task still has something to do and will provoke a quick loop.
631 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200632 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 exit(100);
634#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200635 *next = t->expire;
636 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 }
638
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100639 s->fe->feconn--;
640 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200641 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 actconn--;
643
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200644 if (unlikely((global.mode & MODE_DEBUG) &&
645 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100647 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200648 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100649 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 write(1, trash, len);
651 }
652
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200653 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100654 if (s->req != NULL)
655 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100657 s->logs.bytes_out = s->rep->total;
658
659 s->fe->bytes_in += s->logs.bytes_in;
660 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200661 if (s->be != s->fe) {
662 s->be->bytes_in += s->logs.bytes_in;
663 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100664 }
665 if (s->srv) {
666 s->srv->bytes_in += s->logs.bytes_in;
667 s->srv->bytes_out += s->logs.bytes_out;
668 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
670 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200671 if (s->logs.logwait &&
672 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200673 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
674 if (s->fe->to_log & LW_REQ)
675 http_sess_log(s);
676 else
677 tcp_sess_log(s);
678 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
680 /* the task MUST not be in the run queue anymore */
681 task_delete(t);
682 session_free(s);
683 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200684 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685}
686
687
Willy Tarreau42250582007-04-01 01:30:43 +0200688extern const char sess_term_cond[8];
689extern const char sess_fin_state[8];
690extern const char *monthname[12];
691const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
692const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
693 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
694 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200695struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200696struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697
Willy Tarreau42250582007-04-01 01:30:43 +0200698/*
699 * send a log for the session when we have enough info about it.
700 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100701 */
Willy Tarreau42250582007-04-01 01:30:43 +0200702static void http_sess_log(struct session *s)
703{
704 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
705 struct proxy *fe = s->fe;
706 struct proxy *be = s->be;
707 struct proxy *prx_log;
708 struct http_txn *txn = &s->txn;
709 int tolog;
710 char *uri, *h;
711 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200712 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200713 static char tmpline[MAX_SYSLOG_LEN];
714 int hdr;
715
716 if (fe->logfac1 < 0 && fe->logfac2 < 0)
717 return;
718 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100719
Willy Tarreau42250582007-04-01 01:30:43 +0200720 if (s->cli_addr.ss_family == AF_INET)
721 inet_ntop(AF_INET,
722 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
723 pn, sizeof(pn));
724 else
725 inet_ntop(AF_INET6,
726 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
727 pn, sizeof(pn));
728
Willy Tarreaufe944602007-10-25 10:34:16 +0200729 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200730
731 /* FIXME: let's limit ourselves to frontend logging for now. */
732 tolog = fe->to_log;
733
734 h = tmpline;
735 if (fe->to_log & LW_REQHDR &&
736 txn->req.cap &&
737 (h < tmpline + sizeof(tmpline) - 10)) {
738 *(h++) = ' ';
739 *(h++) = '{';
740 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
741 if (hdr)
742 *(h++) = '|';
743 if (txn->req.cap[hdr] != NULL)
744 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
745 '#', hdr_encode_map, txn->req.cap[hdr]);
746 }
747 *(h++) = '}';
748 }
749
750 if (fe->to_log & LW_RSPHDR &&
751 txn->rsp.cap &&
752 (h < tmpline + sizeof(tmpline) - 7)) {
753 *(h++) = ' ';
754 *(h++) = '{';
755 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
756 if (hdr)
757 *(h++) = '|';
758 if (txn->rsp.cap[hdr] != NULL)
759 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
760 '#', hdr_encode_map, txn->rsp.cap[hdr]);
761 }
762 *(h++) = '}';
763 }
764
765 if (h < tmpline + sizeof(tmpline) - 4) {
766 *(h++) = ' ';
767 *(h++) = '"';
768 uri = txn->uri ? txn->uri : "<BADREQ>";
769 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
770 '#', url_encode_map, uri);
771 *(h++) = '"';
772 }
773 *h = '\0';
774
775 svid = (tolog & LW_SVID) ?
776 (s->data_source != DATA_SRC_STATS) ?
777 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
778
779 send_log(prx_log, LOG_INFO,
780 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
781 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
782 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
783 pn,
784 (s->cli_addr.ss_family == AF_INET) ?
785 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
786 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200787 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
788 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200789 fe->id, be->id, svid,
790 s->logs.t_request,
791 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
792 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
793 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
794 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
795 txn->status,
796 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
797 txn->cli_cookie ? txn->cli_cookie : "-",
798 txn->srv_cookie ? txn->srv_cookie : "-",
799 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
800 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
801 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
802 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
803 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
804 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
805
806 s->logs.logwait = 0;
807}
808
Willy Tarreau117f59e2007-03-04 18:17:17 +0100809
810/*
811 * Capture headers from message starting at <som> according to header list
812 * <cap_hdr>, and fill the <idx> structure appropriately.
813 */
814void capture_headers(char *som, struct hdr_idx *idx,
815 char **cap, struct cap_hdr *cap_hdr)
816{
817 char *eol, *sol, *col, *sov;
818 int cur_idx;
819 struct cap_hdr *h;
820 int len;
821
822 sol = som + hdr_idx_first_pos(idx);
823 cur_idx = hdr_idx_first_idx(idx);
824
825 while (cur_idx) {
826 eol = sol + idx->v[cur_idx].len;
827
828 col = sol;
829 while (col < eol && *col != ':')
830 col++;
831
832 sov = col + 1;
833 while (sov < eol && http_is_lws[(unsigned char)*sov])
834 sov++;
835
836 for (h = cap_hdr; h; h = h->next) {
837 if ((h->namelen == col - sol) &&
838 (strncasecmp(sol, h->name, h->namelen) == 0)) {
839 if (cap[h->index] == NULL)
840 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200841 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100842
843 if (cap[h->index] == NULL) {
844 Alert("HTTP capture : out of memory.\n");
845 continue;
846 }
847
848 len = eol - sov;
849 if (len > h->len)
850 len = h->len;
851
852 memcpy(cap[h->index], sov, len);
853 cap[h->index][len]=0;
854 }
855 }
856 sol = eol + idx->v[cur_idx].cr + 1;
857 cur_idx = idx->v[cur_idx].next;
858 }
859}
860
861
Willy Tarreau42250582007-04-01 01:30:43 +0200862/* either we find an LF at <ptr> or we jump to <bad>.
863 */
864#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
865
866/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
867 * otherwise to <http_msg_ood> with <state> set to <st>.
868 */
869#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
870 ptr++; \
871 if (likely(ptr < end)) \
872 goto good; \
873 else { \
874 state = (st); \
875 goto http_msg_ood; \
876 } \
877 } while (0)
878
879
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100881 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100882 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
883 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
884 * will give undefined results.
885 * Note that it is upon the caller's responsibility to ensure that ptr < end,
886 * and that msg->sol points to the beginning of the response.
887 * If a complete line is found (which implies that at least one CR or LF is
888 * found before <end>, the updated <ptr> is returned, otherwise NULL is
889 * returned indicating an incomplete line (which does not mean that parts have
890 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
891 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
892 * upon next call.
893 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200894 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100895 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
896 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200897 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100898 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100899const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100900 const char *ptr, const char *end,
901 char **ret_ptr, int *ret_state)
902{
903 __label__
904 http_msg_rpver,
905 http_msg_rpver_sp,
906 http_msg_rpcode,
907 http_msg_rpcode_sp,
908 http_msg_rpreason,
909 http_msg_rpline_eol,
910 http_msg_ood, /* out of data */
911 http_msg_invalid;
912
913 switch (state) {
914 http_msg_rpver:
915 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100916 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100917 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
918
919 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100920 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100921 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
922 }
923 goto http_msg_invalid;
924
925 http_msg_rpver_sp:
926 case HTTP_MSG_RPVER_SP:
927 if (likely(!HTTP_IS_LWS(*ptr))) {
928 msg->sl.st.c = ptr - msg_buf;
929 goto http_msg_rpcode;
930 }
931 if (likely(HTTP_IS_SPHT(*ptr)))
932 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
933 /* so it's a CR/LF, this is invalid */
934 goto http_msg_invalid;
935
936 http_msg_rpcode:
937 case HTTP_MSG_RPCODE:
938 if (likely(!HTTP_IS_LWS(*ptr)))
939 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
940
941 if (likely(HTTP_IS_SPHT(*ptr))) {
942 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
943 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
944 }
945
946 /* so it's a CR/LF, so there is no reason phrase */
947 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
948 http_msg_rsp_reason:
949 /* FIXME: should we support HTTP responses without any reason phrase ? */
950 msg->sl.st.r = ptr - msg_buf;
951 msg->sl.st.r_l = 0;
952 goto http_msg_rpline_eol;
953
954 http_msg_rpcode_sp:
955 case HTTP_MSG_RPCODE_SP:
956 if (likely(!HTTP_IS_LWS(*ptr))) {
957 msg->sl.st.r = ptr - msg_buf;
958 goto http_msg_rpreason;
959 }
960 if (likely(HTTP_IS_SPHT(*ptr)))
961 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
962 /* so it's a CR/LF, so there is no reason phrase */
963 goto http_msg_rsp_reason;
964
965 http_msg_rpreason:
966 case HTTP_MSG_RPREASON:
967 if (likely(!HTTP_IS_CRLF(*ptr)))
968 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
969 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
970 http_msg_rpline_eol:
971 /* We have seen the end of line. Note that we do not
972 * necessarily have the \n yet, but at least we know that we
973 * have EITHER \r OR \n, otherwise the response would not be
974 * complete. We can then record the response length and return
975 * to the caller which will be able to register it.
976 */
977 msg->sl.st.l = ptr - msg->sol;
978 return ptr;
979
980#ifdef DEBUG_FULL
981 default:
982 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
983 exit(1);
984#endif
985 }
986
987 http_msg_ood:
988 /* out of data */
989 if (ret_state)
990 *ret_state = state;
991 if (ret_ptr)
992 *ret_ptr = (char *)ptr;
993 return NULL;
994
995 http_msg_invalid:
996 /* invalid message */
997 if (ret_state)
998 *ret_state = HTTP_MSG_ERROR;
999 return NULL;
1000}
1001
1002
1003/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004 * This function parses a request line between <ptr> and <end>, starting with
1005 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1006 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1007 * will give undefined results.
1008 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1009 * and that msg->sol points to the beginning of the request.
1010 * If a complete line is found (which implies that at least one CR or LF is
1011 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1012 * returned indicating an incomplete line (which does not mean that parts have
1013 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1014 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1015 * upon next call.
1016 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001017 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1019 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001020 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001022const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 const char *ptr, const char *end,
1024 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001026 __label__
1027 http_msg_rqmeth,
1028 http_msg_rqmeth_sp,
1029 http_msg_rquri,
1030 http_msg_rquri_sp,
1031 http_msg_rqver,
1032 http_msg_rqline_eol,
1033 http_msg_ood, /* out of data */
1034 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001036 switch (state) {
1037 http_msg_rqmeth:
1038 case HTTP_MSG_RQMETH:
1039 if (likely(HTTP_IS_TOKEN(*ptr)))
1040 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001042 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001043 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1045 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001046
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001047 if (likely(HTTP_IS_CRLF(*ptr))) {
1048 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001049 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001050 http_msg_req09_uri:
1051 msg->sl.rq.u = ptr - msg_buf;
1052 http_msg_req09_uri_e:
1053 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1054 http_msg_req09_ver:
1055 msg->sl.rq.v = ptr - msg_buf;
1056 msg->sl.rq.v_l = 0;
1057 goto http_msg_rqline_eol;
1058 }
1059 goto http_msg_invalid;
1060
1061 http_msg_rqmeth_sp:
1062 case HTTP_MSG_RQMETH_SP:
1063 if (likely(!HTTP_IS_LWS(*ptr))) {
1064 msg->sl.rq.u = ptr - msg_buf;
1065 goto http_msg_rquri;
1066 }
1067 if (likely(HTTP_IS_SPHT(*ptr)))
1068 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1069 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1070 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 http_msg_rquri:
1073 case HTTP_MSG_RQURI:
1074 if (likely(!HTTP_IS_LWS(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 if (likely(HTTP_IS_SPHT(*ptr))) {
1078 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1079 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1083 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001084
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_rquri_sp:
1086 case HTTP_MSG_RQURI_SP:
1087 if (likely(!HTTP_IS_LWS(*ptr))) {
1088 msg->sl.rq.v = ptr - msg_buf;
1089 goto http_msg_rqver;
1090 }
1091 if (likely(HTTP_IS_SPHT(*ptr)))
1092 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1093 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1094 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 http_msg_rqver:
1097 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001098 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001100
1101 if (likely(HTTP_IS_CRLF(*ptr))) {
1102 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1103 http_msg_rqline_eol:
1104 /* We have seen the end of line. Note that we do not
1105 * necessarily have the \n yet, but at least we know that we
1106 * have EITHER \r OR \n, otherwise the request would not be
1107 * complete. We can then record the request length and return
1108 * to the caller which will be able to register it.
1109 */
1110 msg->sl.rq.l = ptr - msg->sol;
1111 return ptr;
1112 }
1113
1114 /* neither an HTTP_VER token nor a CRLF */
1115 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117#ifdef DEBUG_FULL
1118 default:
1119 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1120 exit(1);
1121#endif
1122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 http_msg_ood:
1125 /* out of data */
1126 if (ret_state)
1127 *ret_state = state;
1128 if (ret_ptr)
1129 *ret_ptr = (char *)ptr;
1130 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 http_msg_invalid:
1133 /* invalid message */
1134 if (ret_state)
1135 *ret_state = HTTP_MSG_ERROR;
1136 return NULL;
1137}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001138
1139
Willy Tarreau8973c702007-01-21 23:58:29 +01001140/*
1141 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001142 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001143 * when data are missing and recalled at the exact same location with no
1144 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1146 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001147 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1149{
1150 __label__
1151 http_msg_rqbefore,
1152 http_msg_rqbefore_cr,
1153 http_msg_rqmeth,
1154 http_msg_rqline_end,
1155 http_msg_hdr_first,
1156 http_msg_hdr_name,
1157 http_msg_hdr_l1_sp,
1158 http_msg_hdr_l1_lf,
1159 http_msg_hdr_l1_lws,
1160 http_msg_hdr_val,
1161 http_msg_hdr_l2_lf,
1162 http_msg_hdr_l2_lws,
1163 http_msg_complete_header,
1164 http_msg_last_lf,
1165 http_msg_ood, /* out of data */
1166 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 int state; /* updated only when leaving the FSM */
1169 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001171 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 ptr = buf->lr;
1173 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 if (unlikely(ptr >= end))
1176 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 /*
1180 * First, states that are specific to the response only.
1181 * We check them first so that request and headers are
1182 * closer to each other (accessed more often).
1183 */
1184 http_msg_rpbefore:
1185 case HTTP_MSG_RPBEFORE:
1186 if (likely(HTTP_IS_TOKEN(*ptr))) {
1187 if (likely(ptr == buf->data)) {
1188 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001189 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 } else {
1191#if PARSE_PRESERVE_EMPTY_LINES
1192 /* only skip empty leading lines, don't remove them */
1193 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001194 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195#else
1196 /* Remove empty leading lines, as recommended by
1197 * RFC2616. This takes a lot of time because we
1198 * must move all the buffer backwards, but this
1199 * is rarely needed. The method above will be
1200 * cleaner when we'll be able to start sending
1201 * the request from any place in the buffer.
1202 */
1203 buf->lr = ptr;
1204 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001205 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 msg->sol = buf->data;
1207 ptr = buf->data;
1208 end = buf->r;
1209#endif
1210 }
1211 hdr_idx_init(idx);
1212 state = HTTP_MSG_RPVER;
1213 goto http_msg_rpver;
1214 }
1215
1216 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1217 goto http_msg_invalid;
1218
1219 if (unlikely(*ptr == '\n'))
1220 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1222 /* stop here */
1223
1224 http_msg_rpbefore_cr:
1225 case HTTP_MSG_RPBEFORE_CR:
1226 EXPECT_LF_HERE(ptr, http_msg_invalid);
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1228 /* stop here */
1229
1230 http_msg_rpver:
1231 case HTTP_MSG_RPVER:
1232 case HTTP_MSG_RPVER_SP:
1233 case HTTP_MSG_RPCODE:
1234 case HTTP_MSG_RPCODE_SP:
1235 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001236 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001237 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 if (unlikely(!ptr))
1239 return;
1240
1241 /* we have a full response and we know that we have either a CR
1242 * or an LF at <ptr>.
1243 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001244 //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 +01001245 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1246
1247 msg->sol = ptr;
1248 if (likely(*ptr == '\r'))
1249 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1250 goto http_msg_rpline_end;
1251
1252 http_msg_rpline_end:
1253 case HTTP_MSG_RPLINE_END:
1254 /* msg->sol must point to the first of CR or LF. */
1255 EXPECT_LF_HERE(ptr, http_msg_invalid);
1256 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1257 /* stop here */
1258
1259 /*
1260 * Second, states that are specific to the request only
1261 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 http_msg_rqbefore:
1263 case HTTP_MSG_RQBEFORE:
1264 if (likely(HTTP_IS_TOKEN(*ptr))) {
1265 if (likely(ptr == buf->data)) {
1266 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001267 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 } else {
1269#if PARSE_PRESERVE_EMPTY_LINES
1270 /* only skip empty leading lines, don't remove them */
1271 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001272 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001273#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 /* Remove empty leading lines, as recommended by
1275 * RFC2616. This takes a lot of time because we
1276 * must move all the buffer backwards, but this
1277 * is rarely needed. The method above will be
1278 * cleaner when we'll be able to start sending
1279 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001280 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 buf->lr = ptr;
1282 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001283 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 msg->sol = buf->data;
1285 ptr = buf->data;
1286 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001287#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001289 /* we will need this when keep-alive will be supported
1290 hdr_idx_init(idx);
1291 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 state = HTTP_MSG_RQMETH;
1293 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001294 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1297 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(*ptr == '\n'))
1300 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1301 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 http_msg_rqbefore_cr:
1305 case HTTP_MSG_RQBEFORE_CR:
1306 EXPECT_LF_HERE(ptr, http_msg_invalid);
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 http_msg_rqmeth:
1311 case HTTP_MSG_RQMETH:
1312 case HTTP_MSG_RQMETH_SP:
1313 case HTTP_MSG_RQURI:
1314 case HTTP_MSG_RQURI_SP:
1315 case HTTP_MSG_RQVER:
1316 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001317 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 if (unlikely(!ptr))
1319 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 /* we have a full request and we know that we have either a CR
1322 * or an LF at <ptr>.
1323 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 //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 +01001325 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 msg->sol = ptr;
1328 if (likely(*ptr == '\r'))
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 http_msg_rqline_end:
1333 case HTTP_MSG_RQLINE_END:
1334 /* check for HTTP/0.9 request : no version information available.
1335 * msg->sol must point to the first of CR or LF.
1336 */
1337 if (unlikely(msg->sl.rq.v_l == 0))
1338 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EXPECT_LF_HERE(ptr, http_msg_invalid);
1341 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001343
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 /*
1345 * Common states below
1346 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_hdr_first:
1348 case HTTP_MSG_HDR_FIRST:
1349 msg->sol = ptr;
1350 if (likely(!HTTP_IS_CRLF(*ptr))) {
1351 goto http_msg_hdr_name;
1352 }
1353
1354 if (likely(*ptr == '\r'))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1356 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_hdr_name:
1359 case HTTP_MSG_HDR_NAME:
1360 /* assumes msg->sol points to the first char */
1361 if (likely(HTTP_IS_TOKEN(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (likely(*ptr == ':')) {
1365 msg->col = ptr - buf->data;
1366 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1367 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 http_msg_hdr_l1_sp:
1372 case HTTP_MSG_HDR_L1_SP:
1373 /* assumes msg->sol points to the first char and msg->col to the colon */
1374 if (likely(HTTP_IS_SPHT(*ptr)))
1375 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 /* header value can be basically anything except CR/LF */
1378 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 if (likely(!HTTP_IS_CRLF(*ptr))) {
1381 goto http_msg_hdr_val;
1382 }
1383
1384 if (likely(*ptr == '\r'))
1385 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1386 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_hdr_l1_lf:
1389 case HTTP_MSG_HDR_L1_LF:
1390 EXPECT_LF_HERE(ptr, http_msg_invalid);
1391 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 http_msg_hdr_l1_lws:
1394 case HTTP_MSG_HDR_L1_LWS:
1395 if (likely(HTTP_IS_SPHT(*ptr))) {
1396 /* replace HT,CR,LF with spaces */
1397 for (; buf->data+msg->sov < ptr; msg->sov++)
1398 buf->data[msg->sov] = ' ';
1399 goto http_msg_hdr_l1_sp;
1400 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001401 /* we had a header consisting only in spaces ! */
1402 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 goto http_msg_complete_header;
1404
1405 http_msg_hdr_val:
1406 case HTTP_MSG_HDR_VAL:
1407 /* assumes msg->sol points to the first char, msg->col to the
1408 * colon, and msg->sov points to the first character of the
1409 * value.
1410 */
1411 if (likely(!HTTP_IS_CRLF(*ptr)))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 msg->eol = ptr;
1415 /* Note: we could also copy eol into ->eoh so that we have the
1416 * real header end in case it ends with lots of LWS, but is this
1417 * really needed ?
1418 */
1419 if (likely(*ptr == '\r'))
1420 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1421 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_hdr_l2_lf:
1424 case HTTP_MSG_HDR_L2_LF:
1425 EXPECT_LF_HERE(ptr, http_msg_invalid);
1426 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_hdr_l2_lws:
1429 case HTTP_MSG_HDR_L2_LWS:
1430 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1431 /* LWS: replace HT,CR,LF with spaces */
1432 for (; msg->eol < ptr; msg->eol++)
1433 *msg->eol = ' ';
1434 goto http_msg_hdr_val;
1435 }
1436 http_msg_complete_header:
1437 /*
1438 * It was a new header, so the last one is finished.
1439 * Assumes msg->sol points to the first char, msg->col to the
1440 * colon, msg->sov points to the first character of the value
1441 * and msg->eol to the first CR or LF so we know how the line
1442 * ends. We insert last header into the index.
1443 */
1444 /*
1445 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1446 write(2, msg->sol, msg->eol-msg->sol);
1447 fprintf(stderr,"\n");
1448 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1451 idx, idx->tail) < 0))
1452 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 msg->sol = ptr;
1455 if (likely(!HTTP_IS_CRLF(*ptr))) {
1456 goto http_msg_hdr_name;
1457 }
1458
1459 if (likely(*ptr == '\r'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1461 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 http_msg_last_lf:
1464 case HTTP_MSG_LAST_LF:
1465 /* Assumes msg->sol points to the first of either CR or LF */
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 ptr++;
1468 buf->lr = ptr;
1469 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 return;
1472#ifdef DEBUG_FULL
1473 default:
1474 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1475 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001476#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 }
1478 http_msg_ood:
1479 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 buf->lr = ptr;
1482 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 http_msg_invalid:
1485 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001486 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 return;
1488}
1489
1490/*
1491 * manages the client FSM and its socket. BTW, it also tries to handle the
1492 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1493 * 0 else.
1494 */
1495int process_cli(struct session *t)
1496{
1497 int s = t->srv_state;
1498 int c = t->cli_state;
1499 struct buffer *req = t->req;
1500 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1503 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001504 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 req->rex.tv_sec, req->rex.tv_usec,
1506 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (c == CL_STHEADERS) {
1509 /*
1510 * Now parse the partial (or complete) lines.
1511 * We will check the request syntax, and also join multi-line
1512 * headers. An index of all the lines will be elaborated while
1513 * parsing.
1514 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001518 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 * req->data + req->eoh = end of processed headers / start of current one
1520 * req->data + req->eol = end of current header or line (LF or CRLF)
1521 * req->lr = first non-visited byte
1522 * req->r = end of data
1523 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001526 struct http_txn *txn = &t->txn;
1527 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001531 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 /* 1: we might have to print this header in debug mode */
1534 if (unlikely((global.mode & MODE_DEBUG) &&
1535 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001536 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001539 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 eol = sol + msg->sl.rq.l;
1541 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001542
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001543 sol += hdr_idx_first_pos(&txn->hdr_idx);
1544 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001547 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001549 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1550 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 }
1552 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001553
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554
1555 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001557 * If not so, we check the FD and buffer states before leaving.
1558 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1560 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001561 *
1562 */
1563
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001564 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 /*
1566 * First, let's catch bad requests.
1567 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001568 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
1571 /* 1: Since we are in header mode, if there's no space
1572 * left for headers, we won't be able to free more
1573 * later, so the session will never terminate. We
1574 * must terminate it now.
1575 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 if (unlikely(req->l >= req->rlim - req->data)) {
1577 /* FIXME: check if URI is set and return Status
1578 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579 */
Willy Tarreau06619262006-12-17 08:37:22 +01001580 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581 }
1582
1583 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1585 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001586 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001587 fd_delete(t->cli_fd);
1588 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001589 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590 if (!(t->flags & SN_ERR_MASK))
1591 t->flags |= SN_ERR_CLICL;
1592 if (!(t->flags & SN_FINST_MASK))
1593 t->flags |= SN_FINST_R;
1594 return 1;
1595 }
1596
1597 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001598 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001600 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001601 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001602 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001603 if (!(t->flags & SN_ERR_MASK))
1604 t->flags |= SN_ERR_CLITO;
1605 if (!(t->flags & SN_FINST_MASK))
1606 t->flags |= SN_FINST_R;
1607 return 1;
1608 }
1609
1610 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001611 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001612 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613 * full. We cannot loop here since stream_sock_read will disable it only if
1614 * req->l == rlim-data
1615 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001616 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001617 tv_eternity(&req->rex);
1618 }
1619 return t->cli_state != CL_STHEADERS;
1620 }
1621
1622
1623 /****************************************************************
1624 * More interesting part now : we know that we have a complete *
1625 * request which at least looks like HTTP. We have an indicator *
1626 * of each header's length, so we can parse them quickly. *
1627 ****************************************************************/
1628
Willy Tarreau9cdde232007-05-02 20:58:19 +02001629 /* ensure we keep this pointer to the beginning of the message */
1630 msg->sol = req->data + msg->som;
1631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 /*
1633 * 1: identify the method
1634 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001635 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
1637 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001639 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 if (unlikely((t->fe->monitor_uri_len != 0) &&
1643 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1644 !memcmp(&req->data[msg->sl.rq.u],
1645 t->fe->monitor_uri,
1646 t->fe->monitor_uri_len))) {
1647 /*
1648 * We have found the monitor URI
1649 */
1650 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001651 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 client_retnclose(t, &http_200_chunk);
1653 goto return_prx_cond;
1654 }
1655
1656 /*
1657 * 3: Maybe we have to copy the original REQURI for the logs ?
1658 * Note: we cannot log anymore if the request has been
1659 * classified as invalid.
1660 */
1661 if (unlikely(t->logs.logwait & LW_REQ)) {
1662 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001663 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 int urilen = msg->sl.rq.l;
1665
1666 if (urilen >= REQURI_LEN)
1667 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001668 memcpy(txn->uri, &req->data[msg->som], urilen);
1669 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670
1671 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001672 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 } else {
1674 Alert("HTTP logging : out of memory.\n");
1675 }
1676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Willy Tarreau06619262006-12-17 08:37:22 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1680 if (unlikely(msg->sl.rq.v_l == 0)) {
1681 int delta;
1682 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001683 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 cur_end = msg->sol + msg->sl.rq.l;
1685 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 if (msg->sl.rq.u_l == 0) {
1688 /* if no URI was set, add "/" */
1689 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1690 cur_end += delta;
1691 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001692 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 /* add HTTP version */
1694 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1695 msg->eoh += delta;
1696 cur_end += delta;
1697 cur_end = (char *)http_parse_reqline(msg, req->data,
1698 HTTP_MSG_RQMETH,
1699 msg->sol, cur_end + 1,
1700 NULL, NULL);
1701 if (unlikely(!cur_end))
1702 goto return_bad_req;
1703
1704 /* we have a full HTTP/1.0 request now and we know that
1705 * we have either a CR or an LF at <ptr>.
1706 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001707 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001708 }
1709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710
1711 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001712 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001713 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001714 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001715
1716 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718 * As opposed to version 1.2, now they will be evaluated in the
1719 * filters order and not in the header order. This means that
1720 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001721 *
1722 * We can now check whether we want to switch to another
1723 * backend, in which case we will re-check the backend's
1724 * filters and various options. In order to support 3-level
1725 * switching, here's how we should proceed :
1726 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001727 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001728 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001729 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001730 * There cannot be any switch from there, so ->be cannot be
1731 * changed anymore.
1732 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001733 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001734 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001735 * The response path will be able to apply either ->be, or
1736 * ->be then ->fe filters in order to match the reverse of
1737 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738 */
1739
Willy Tarreau06619262006-12-17 08:37:22 +01001740 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001741 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001742 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001743 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001744
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001745 /* first check whether we have some ACLs set to block this request */
1746 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001747 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001748 if (cond->pol == ACL_COND_UNLESS)
1749 ret = !ret;
1750
1751 if (ret) {
1752 txn->status = 403;
1753 /* let's log the request time */
1754 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1755 client_retnclose(t, error_message(t, HTTP_ERR_403));
1756 goto return_prx_cond;
1757 }
1758 }
1759
Willy Tarreau06619262006-12-17 08:37:22 +01001760 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001761 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1763 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001764 }
1765
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001766 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1767 /* to ensure correct connection accounting on
1768 * the backend, we count the connection for the
1769 * one managing the queue.
1770 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001771 t->be->beconn++;
1772 if (t->be->beconn > t->be->beconn_max)
1773 t->be->beconn_max = t->be->beconn;
1774 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001775 t->flags |= SN_BE_ASSIGNED;
1776 }
1777
Willy Tarreau06619262006-12-17 08:37:22 +01001778 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001779 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001780 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001781 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001782 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001783 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001784 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001785 goto return_prx_cond;
1786 }
1787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001789 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 !(t->flags & SN_CONN_CLOSED)) {
1791 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001792 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001794
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001795 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 old_idx = 0;
1797
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001798 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1799 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 cur_ptr = cur_next;
1801 cur_end = cur_ptr + cur_hdr->len;
1802 cur_next = cur_end + cur_hdr->cr + 1;
1803
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001804 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1805 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 /* 3 possibilities :
1807 * - we have already set Connection: close,
1808 * so we remove this line.
1809 * - we have not yet set Connection: close,
1810 * but this line indicates close. We leave
1811 * it untouched and set the flag.
1812 * - we have not yet set Connection: close,
1813 * and this line indicates non-close. We
1814 * replace it.
1815 */
1816 if (t->flags & SN_CONN_CLOSED) {
1817 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001818 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001820 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1821 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 cur_hdr->len = 0;
1823 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001824 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1825 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1826 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 cur_next += delta;
1828 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001829 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 t->flags |= SN_CONN_CLOSED;
1832 }
1833 }
1834 old_idx = cur_idx;
1835 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001836 }
1837 /* add request headers from the rule sets in the same order */
1838 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1839 if (unlikely(http_header_add_tail(req,
1840 &txn->req,
1841 &txn->hdr_idx,
1842 rule_set->req_add[cur_idx])) < 0)
1843 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001844 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001847 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001848 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001849 /* we have to check the URI and auth for this request */
1850 if (stats_check_uri_auth(t, rule_set))
1851 return 1;
1852 }
1853
Willy Tarreau55ea7572007-06-17 19:56:27 +02001854 /* now check whether we have some switching rules for this request */
1855 if (!(t->flags & SN_BE_ASSIGNED)) {
1856 struct switching_rule *rule;
1857
1858 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1859 int ret;
1860
1861 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1862 if (cond->pol == ACL_COND_UNLESS)
1863 ret = !ret;
1864
1865 if (ret) {
1866 t->be = rule->be.backend;
1867 t->be->beconn++;
1868 if (t->be->beconn > t->be->beconn_max)
1869 t->be->beconn_max = t->be->beconn;
1870 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001871
1872 /* assign new parameters to the session from the new backend */
1873 t->rep->rto = t->req->wto = t->be->srvtimeout;
1874 t->req->cto = t->be->contimeout;
1875 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001876 t->flags |= SN_BE_ASSIGNED;
1877 break;
1878 }
1879 }
1880 }
1881
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001882 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1883 /* No backend was set, but there was a default
1884 * backend set in the frontend, so we use it and
1885 * loop again.
1886 */
1887 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001888 t->be->beconn++;
1889 if (t->be->beconn > t->be->beconn_max)
1890 t->be->beconn_max = t->be->beconn;
1891 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001892
1893 /* assign new parameters to the session from the new backend */
1894 t->rep->rto = t->req->wto = t->be->srvtimeout;
1895 t->req->cto = t->be->contimeout;
1896 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001897 t->flags |= SN_BE_ASSIGNED;
1898 }
1899 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001900
Willy Tarreau58f10d72006-12-04 02:26:12 +01001901
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001902 if (!(t->flags & SN_BE_ASSIGNED)) {
1903 /* To ensure correct connection accounting on
1904 * the backend, we count the connection for the
1905 * one managing the queue.
1906 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001907 t->be->beconn++;
1908 if (t->be->beconn > t->be->beconn_max)
1909 t->be->beconn_max = t->be->beconn;
1910 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001911 t->flags |= SN_BE_ASSIGNED;
1912 }
1913
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001914 /*
1915 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001916 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001917 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001918 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001919 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001920
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923
Willy Tarreau2a324282006-12-05 00:05:46 +01001924 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001926 * so let's do the same now.
1927 */
1928
1929 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001930 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001931 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001932 }
1933
1934
1935 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001937 * Note that doing so might move headers in the request, but
1938 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001939 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001940 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001941 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001942 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001943
Willy Tarreau58f10d72006-12-04 02:26:12 +01001944
Willy Tarreau2a324282006-12-05 00:05:46 +01001945 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001946 * 9: add X-Forwarded-For if either the frontend or the backend
1947 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001948 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001949 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001950 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001951 /* Add an X-Forwarded-For header unless the source IP is
1952 * in the 'except' network range.
1953 */
1954 if ((!t->fe->except_mask.s_addr ||
1955 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1956 != t->fe->except_net.s_addr) &&
1957 (!t->be->except_mask.s_addr ||
1958 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1959 != t->be->except_net.s_addr)) {
1960 int len;
1961 unsigned char *pn;
1962 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001963
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001964 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1965 pn[0], pn[1], pn[2], pn[3]);
1966
1967 if (unlikely(http_header_add_tail2(req, &txn->req,
1968 &txn->hdr_idx, trash, len)) < 0)
1969 goto return_bad_req;
1970 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001971 }
1972 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001973 /* FIXME: for the sake of completeness, we should also support
1974 * 'except' here, although it is mostly useless in this case.
1975 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001976 int len;
1977 char pn[INET6_ADDRSTRLEN];
1978 inet_ntop(AF_INET6,
1979 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1980 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001981 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1982 if (unlikely(http_header_add_tail2(req, &txn->req,
1983 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001984 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001985 }
1986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001987
Willy Tarreau2a324282006-12-05 00:05:46 +01001988 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001990 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001991 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001992 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001993 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001994 if ((unlikely(msg->sl.rq.v_l != 8) ||
1995 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1996 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001997 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001998 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001999 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002000 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001
Willy Tarreau2a324282006-12-05 00:05:46 +01002002 /*************************************************************
2003 * OK, that's finished for the headers. We have done what we *
2004 * could. Let's switch to the DATA state. *
2005 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006
Willy Tarreau2a324282006-12-05 00:05:46 +01002007 t->cli_state = CL_STDATA;
2008 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002009
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002010 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011
Willy Tarreaud825eef2007-05-12 22:35:00 +02002012 if (!tv_isset(&t->fe->clitimeout) ||
2013 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002014 /* If the client has no timeout, or if the server is not ready yet,
2015 * and we know for sure that it can expire, then it's cleaner to
2016 * disable the timeout on the client side so that too low values
2017 * cannot make the sessions abort too early.
2018 *
2019 * FIXME-20050705: the server needs a way to re-enable this time-out
2020 * when it switches its state, otherwise a client can stay connected
2021 * indefinitely. This now seems to be OK.
2022 */
2023 tv_eternity(&req->rex);
2024 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025
Willy Tarreau2a324282006-12-05 00:05:46 +01002026 /* When a connection is tarpitted, we use the queue timeout for the
2027 * tarpit delay, which currently happens to be the server's connect
2028 * timeout. If unset, then set it to zero because we really want it
2029 * to expire at one moment.
2030 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002031 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002032 t->req->l = 0;
2033 /* flush the request so that we can drop the connection early
2034 * if the client closes first.
2035 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002036 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002037 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002038 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002040 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002041 goto process_data;
2042
2043 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002044 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002045 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002046 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002047 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002048 return_prx_cond:
2049 if (!(t->flags & SN_ERR_MASK))
2050 t->flags |= SN_ERR_PRXCOND;
2051 if (!(t->flags & SN_FINST_MASK))
2052 t->flags |= SN_FINST_R;
2053 return 1;
2054
Willy Tarreaubaaee002006-06-26 02:48:02 +02002055 }
2056 else if (c == CL_STDATA) {
2057 process_data:
2058 /* FIXME: this error handling is partly buggy because we always report
2059 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2060 * or HEADER phase. BTW, it's not logical to expire the client while
2061 * we're waiting for the server to connect.
2062 */
2063 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002064 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002065 buffer_shutr(req);
2066 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002067 fd_delete(t->cli_fd);
2068 t->cli_state = CL_STCLOSE;
2069 if (!(t->flags & SN_ERR_MASK))
2070 t->flags |= SN_ERR_CLICL;
2071 if (!(t->flags & SN_FINST_MASK)) {
2072 if (t->pend_pos)
2073 t->flags |= SN_FINST_Q;
2074 else if (s == SV_STCONN)
2075 t->flags |= SN_FINST_C;
2076 else
2077 t->flags |= SN_FINST_D;
2078 }
2079 return 1;
2080 }
2081 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002082 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002083 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002084 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002085 t->cli_state = CL_STSHUTR;
2086 return 1;
2087 }
2088 /* last server read and buffer empty */
2089 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002090 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002091 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002092 shutdown(t->cli_fd, SHUT_WR);
2093 /* We must ensure that the read part is still alive when switching
2094 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002095 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002096 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002097 t->cli_state = CL_STSHUTW;
2098 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2099 return 1;
2100 }
2101 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002102 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002103 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002104 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105 t->cli_state = CL_STSHUTR;
2106 if (!(t->flags & SN_ERR_MASK))
2107 t->flags |= SN_ERR_CLITO;
2108 if (!(t->flags & SN_FINST_MASK)) {
2109 if (t->pend_pos)
2110 t->flags |= SN_FINST_Q;
2111 else if (s == SV_STCONN)
2112 t->flags |= SN_FINST_C;
2113 else
2114 t->flags |= SN_FINST_D;
2115 }
2116 return 1;
2117 }
2118 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002119 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002120 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002121 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 shutdown(t->cli_fd, SHUT_WR);
2123 /* We must ensure that the read part is still alive when switching
2124 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002125 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002126 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002127
2128 t->cli_state = CL_STSHUTW;
2129 if (!(t->flags & SN_ERR_MASK))
2130 t->flags |= SN_ERR_CLITO;
2131 if (!(t->flags & SN_FINST_MASK)) {
2132 if (t->pend_pos)
2133 t->flags |= SN_FINST_Q;
2134 else if (s == SV_STCONN)
2135 t->flags |= SN_FINST_C;
2136 else
2137 t->flags |= SN_FINST_D;
2138 }
2139 return 1;
2140 }
2141
2142 if (req->l >= req->rlim - req->data) {
2143 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002144 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002146 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002147 }
2148 } else {
2149 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002150 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002151 if (!tv_isset(&t->fe->clitimeout) ||
2152 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002153 /* If the client has no timeout, or if the server not ready yet, and we
2154 * know for sure that it can expire, then it's cleaner to disable the
2155 * timeout on the client side so that too low values cannot make the
2156 * sessions abort too early.
2157 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002158 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002160 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161 }
2162 }
2163
2164 if ((rep->l == 0) ||
2165 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002166 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2167 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002168 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002169 }
2170 } else {
2171 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002172 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2173 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002174 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175 /* FIXME: to prevent the client from expiring read timeouts during writes,
2176 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002177 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002178 }
2179 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002180 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181 }
2182 }
2183 return 0; /* other cases change nothing */
2184 }
2185 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002186 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002187 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002188 fd_delete(t->cli_fd);
2189 t->cli_state = CL_STCLOSE;
2190 if (!(t->flags & SN_ERR_MASK))
2191 t->flags |= SN_ERR_CLICL;
2192 if (!(t->flags & SN_FINST_MASK)) {
2193 if (t->pend_pos)
2194 t->flags |= SN_FINST_Q;
2195 else if (s == SV_STCONN)
2196 t->flags |= SN_FINST_C;
2197 else
2198 t->flags |= SN_FINST_D;
2199 }
2200 return 1;
2201 }
2202 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2203 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002204 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205 fd_delete(t->cli_fd);
2206 t->cli_state = CL_STCLOSE;
2207 return 1;
2208 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002209 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002210 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002211 fd_delete(t->cli_fd);
2212 t->cli_state = CL_STCLOSE;
2213 if (!(t->flags & SN_ERR_MASK))
2214 t->flags |= SN_ERR_CLITO;
2215 if (!(t->flags & SN_FINST_MASK)) {
2216 if (t->pend_pos)
2217 t->flags |= SN_FINST_Q;
2218 else if (s == SV_STCONN)
2219 t->flags |= SN_FINST_C;
2220 else
2221 t->flags |= SN_FINST_D;
2222 }
2223 return 1;
2224 }
2225
2226 if (t->flags & SN_SELF_GEN) {
2227 produce_content(t);
2228 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002229 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230 fd_delete(t->cli_fd);
2231 t->cli_state = CL_STCLOSE;
2232 return 1;
2233 }
2234 }
2235
2236 if ((rep->l == 0)
2237 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002238 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2239 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002240 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002241 }
2242 } else {
2243 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002244 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2245 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02002246 if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002247 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 }
2249 }
2250 return 0;
2251 }
2252 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002253 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002254 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255 fd_delete(t->cli_fd);
2256 t->cli_state = CL_STCLOSE;
2257 if (!(t->flags & SN_ERR_MASK))
2258 t->flags |= SN_ERR_CLICL;
2259 if (!(t->flags & SN_FINST_MASK)) {
2260 if (t->pend_pos)
2261 t->flags |= SN_FINST_Q;
2262 else if (s == SV_STCONN)
2263 t->flags |= SN_FINST_C;
2264 else
2265 t->flags |= SN_FINST_D;
2266 }
2267 return 1;
2268 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002269 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002270 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002271 fd_delete(t->cli_fd);
2272 t->cli_state = CL_STCLOSE;
2273 return 1;
2274 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002275 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002276 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002277 fd_delete(t->cli_fd);
2278 t->cli_state = CL_STCLOSE;
2279 if (!(t->flags & SN_ERR_MASK))
2280 t->flags |= SN_ERR_CLITO;
2281 if (!(t->flags & SN_FINST_MASK)) {
2282 if (t->pend_pos)
2283 t->flags |= SN_FINST_Q;
2284 else if (s == SV_STCONN)
2285 t->flags |= SN_FINST_C;
2286 else
2287 t->flags |= SN_FINST_D;
2288 }
2289 return 1;
2290 }
2291 else if (req->l >= req->rlim - req->data) {
2292 /* no room to read more data */
2293
2294 /* FIXME-20050705: is it possible for a client to maintain a session
2295 * after the timeout by sending more data after it receives a close ?
2296 */
2297
Willy Tarreau66319382007-04-08 17:17:37 +02002298 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002300 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002301 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2302 }
2303 } else {
2304 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002305 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002306 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002307 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2309 }
2310 }
2311 return 0;
2312 }
2313 else { /* CL_STCLOSE: nothing to do */
2314 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2315 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002316 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);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002317 write(1, trash, len);
2318 }
2319 return 0;
2320 }
2321 return 0;
2322}
2323
2324
2325/*
2326 * manages the server FSM and its socket. It returns 1 if a state has changed
2327 * (and a resync may be needed), 0 else.
2328 */
2329int process_srv(struct session *t)
2330{
2331 int s = t->srv_state;
2332 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002333 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002334 struct buffer *req = t->req;
2335 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002336 int conn_err;
2337
2338#ifdef DEBUG_FULL
2339 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2340#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002341
2342#if 0
2343 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2344 __FUNCTION__, __LINE__,
2345 t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec,
2346 t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec,
2347 t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec);
2348 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2349 __FUNCTION__, __LINE__,
2350 t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec,
2351 t->be->contimeout.tv_sec, t->be->contimeout.tv_usec,
2352 t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec);
2353
2354 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2355 __FUNCTION__, __LINE__,
2356 req->cto.tv_sec, req->cto.tv_usec,
2357 req->rto.tv_sec, req->rto.tv_usec,
2358 req->wto.tv_sec, req->wto.tv_usec);
2359
2360 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2361 __FUNCTION__, __LINE__,
2362 rep->cto.tv_sec, rep->cto.tv_usec,
2363 rep->rto.tv_sec, rep->rto.tv_usec,
2364 rep->wto.tv_sec, rep->wto.tv_usec);
2365#endif
2366
Willy Tarreaubaaee002006-06-26 02:48:02 +02002367 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreauf161a342007-04-08 16:59:42 +02002368 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2369 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002370 //);
2371 if (s == SV_STIDLE) {
2372 if (c == CL_STHEADERS)
2373 return 0; /* stay in idle, waiting for data to reach the client side */
2374 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2375 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002376 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002377 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002378 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002379 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002380 /* note that this must not return any error because it would be able to
2381 * overwrite the client_retnclose() output.
2382 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002383 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002384 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002385 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002386 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002387
2388 return 1;
2389 }
2390 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002391 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002392 /* This connection is being tarpitted. The CLIENT side has
2393 * already set the connect expiration date to the right
2394 * timeout. We just have to check that it has not expired.
2395 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002396 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002397 return 0;
2398
2399 /* We will set the queue timer to the time spent, just for
2400 * logging purposes. We fake a 500 server error, so that the
2401 * attacker will not suspect his connection has been tarpitted.
2402 * It will not cause trouble to the logs because we can exclude
2403 * the tarpitted connections by filtering on the 'PT' status flags.
2404 */
2405 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002406 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002407 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002408 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002409 return 1;
2410 }
2411
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412 /* Right now, we will need to create a connection to the server.
2413 * We might already have tried, and got a connection pending, in
2414 * which case we will not do anything till it's pending. It's up
2415 * to any other session to release it and wake us up again.
2416 */
2417 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002418 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002419 return 0;
2420 else {
2421 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002422 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002423 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002424 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002425 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002426 if (t->srv)
2427 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002428 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002429 return 1;
2430 }
2431 }
2432
2433 do {
2434 /* first, get a connection */
2435 if (srv_redispatch_connect(t))
2436 return t->srv_state != SV_STIDLE;
2437
2438 /* try to (re-)connect to the server, and fail if we expire the
2439 * number of retries.
2440 */
2441 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002442 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002443 return t->srv_state != SV_STIDLE;
2444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 } while (1);
2446 }
2447 }
2448 else if (s == SV_STCONN) { /* connection in progress */
2449 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2450 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002451 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2452 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002453 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002454 fd_delete(t->srv_fd);
2455 if (t->srv)
2456 t->srv->cur_sess--;
2457
2458 /* note that this must not return any error because it would be able to
2459 * overwrite the client_retnclose() output.
2460 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002461 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002462 return 1;
2463 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002464 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002465 //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466 return 0; /* nothing changed */
2467 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002468 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002469 /* timeout, asynchronous connect error or first write error */
2470 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2471
2472 fd_delete(t->srv_fd);
2473 if (t->srv)
2474 t->srv->cur_sess--;
2475
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002476 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002477 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2478 else
2479 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2480
2481 /* ensure that we have enough retries left */
2482 if (srv_count_retry_down(t, conn_err))
2483 return 1;
2484
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002485 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002486 /* We're on our last chance, and the REDISP option was specified.
2487 * We will ignore cookie and force to balance or use the dispatcher.
2488 */
2489 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002490 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002491 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002492
2493 if (t->srv)
2494 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002495 t->be->redispatches++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002496
2497 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2498 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002499 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002500
2501 /* first, get a connection */
2502 if (srv_redispatch_connect(t))
2503 return t->srv_state != SV_STIDLE;
2504 }
2505
Willy Tarreaubaaee002006-06-26 02:48:02 +02002506 do {
2507 /* Now we will try to either reconnect to the same server or
2508 * connect to another server. If the connection gets queued
2509 * because all servers are saturated, then we will go back to
2510 * the SV_STIDLE state.
2511 */
2512 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002513 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514 return t->srv_state != SV_STCONN;
2515 }
2516
2517 /* we need to redispatch the connection to another server */
2518 if (srv_redispatch_connect(t))
2519 return t->srv_state != SV_STCONN;
2520 } while (1);
2521 }
2522 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002523 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002524
2525 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2526 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002527 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002528 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002530 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002531 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002532 /* FIXME: to prevent the server from expiring read timeouts during writes,
2533 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002534 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002535 }
2536 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002537 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002538 }
2539
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002540 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002541 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002542 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002543 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002544
2545 t->srv_state = SV_STDATA;
2546 if (t->srv)
2547 t->srv->cum_sess++;
2548 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2549
2550 /* if the user wants to log as soon as possible, without counting
2551 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002552 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002553 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002554 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002556#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002557 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002558 /* TCP splicing supported by both FE and BE */
2559 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2560 }
2561#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002562 }
2563 else {
2564 t->srv_state = SV_STHEADERS;
2565 if (t->srv)
2566 t->srv->cum_sess++;
2567 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002568 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2569 /* reset hdr_idx which was already initialized by the request.
2570 * right now, the http parser does it.
2571 * hdr_idx_init(&t->txn.hdr_idx);
2572 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002573 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002574 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002575 return 1;
2576 }
2577 }
2578 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002579 /*
2580 * Now parse the partial (or complete) lines.
2581 * We will check the response syntax, and also join multi-line
2582 * headers. An index of all the lines will be elaborated while
2583 * parsing.
2584 *
2585 * For the parsing, we use a 28 states FSM.
2586 *
2587 * Here is the information we currently have :
2588 * rep->data + req->som = beginning of response
2589 * rep->data + req->eoh = end of processed headers / start of current one
2590 * rep->data + req->eol = end of current header or line (LF or CRLF)
2591 * rep->lr = first non-visited byte
2592 * rep->r = end of data
2593 */
2594
2595 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002596 struct http_msg *msg = &txn->rsp;
2597 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002598
Willy Tarreaua15645d2007-03-18 16:22:39 +01002599 if (likely(rep->lr < rep->r))
2600 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002601
Willy Tarreaua15645d2007-03-18 16:22:39 +01002602 /* 1: we might have to print this header in debug mode */
2603 if (unlikely((global.mode & MODE_DEBUG) &&
2604 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2605 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2606 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002607
Willy Tarreaua15645d2007-03-18 16:22:39 +01002608 sol = rep->data + msg->som;
2609 eol = sol + msg->sl.rq.l;
2610 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002611
Willy Tarreaua15645d2007-03-18 16:22:39 +01002612 sol += hdr_idx_first_pos(&txn->hdr_idx);
2613 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002614
Willy Tarreaua15645d2007-03-18 16:22:39 +01002615 while (cur_idx) {
2616 eol = sol + txn->hdr_idx.v[cur_idx].len;
2617 debug_hdr("srvhdr", t, sol, eol);
2618 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2619 cur_idx = txn->hdr_idx.v[cur_idx].next;
2620 }
2621 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002622
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623
Willy Tarreau66319382007-04-08 17:17:37 +02002624 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002625 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002626 * full. We cannot loop here since stream_sock_read will disable it only if
2627 * rep->l == rlim-data
2628 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002629 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002630 tv_eternity(&rep->rex);
2631 }
2632
2633
2634 /*
2635 * Now we quickly check if we have found a full valid response.
2636 * If not so, we check the FD and buffer states before leaving.
2637 * A full response is indicated by the fact that we have seen
2638 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2639 * responses are checked first.
2640 *
2641 * Depending on whether the client is still there or not, we
2642 * may send an error response back or not. Note that normally
2643 * we should only check for HTTP status there, and check I/O
2644 * errors somewhere else.
2645 */
2646
2647 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2648
2649 /* Invalid response, or read error or write error */
2650 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2651 (req->flags & BF_WRITE_ERROR) ||
2652 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002653 buffer_shutr(rep);
2654 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002655 fd_delete(t->srv_fd);
2656 if (t->srv) {
2657 t->srv->cur_sess--;
2658 t->srv->failed_resp++;
2659 }
2660 t->be->failed_resp++;
2661 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002662 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 client_return(t, error_message(t, HTTP_ERR_502));
2664 if (!(t->flags & SN_ERR_MASK))
2665 t->flags |= SN_ERR_SRVCL;
2666 if (!(t->flags & SN_FINST_MASK))
2667 t->flags |= SN_FINST_H;
2668 /* We used to have a free connection slot. Since we'll never use it,
2669 * we have to inform the server that it may be used by another session.
2670 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002671 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002672 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002673
Willy Tarreaua15645d2007-03-18 16:22:39 +01002674 return 1;
2675 }
2676
2677 /* end of client write or end of server read.
2678 * since we are in header mode, if there's no space left for headers, we
2679 * won't be able to free more later, so the session will never terminate.
2680 */
2681 else if (unlikely(rep->flags & BF_READ_NULL ||
2682 c == CL_STSHUTW || c == CL_STCLOSE ||
2683 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002684 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002685 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002686 t->srv_state = SV_STSHUTR;
2687 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2688 return 1;
2689 }
2690
2691 /* read timeout : return a 504 to the client.
2692 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002693 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002694 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002695 buffer_shutr(rep);
2696 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002697 fd_delete(t->srv_fd);
2698 if (t->srv) {
2699 t->srv->cur_sess--;
2700 t->srv->failed_resp++;
2701 }
2702 t->be->failed_resp++;
2703 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002704 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002705 client_return(t, error_message(t, HTTP_ERR_504));
2706 if (!(t->flags & SN_ERR_MASK))
2707 t->flags |= SN_ERR_SRVTO;
2708 if (!(t->flags & SN_FINST_MASK))
2709 t->flags |= SN_FINST_H;
2710 /* We used to have a free connection slot. Since we'll never use it,
2711 * we have to inform the server that it may be used by another session.
2712 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002713 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002714 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002715 return 1;
2716 }
2717
2718 /* last client read and buffer empty */
2719 /* FIXME!!! here, we don't want to switch to SHUTW if the
2720 * client shuts read too early, because we may still have
2721 * some work to do on the headers.
2722 * The side-effect is that if the client completely closes its
2723 * connection during SV_STHEADER, the connection to the server
2724 * is kept until a response comes back or the timeout is reached.
2725 */
2726 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2727 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002728 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002729 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002730
2731 /* We must ensure that the read part is still
2732 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002733 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002734 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002735
2736 shutdown(t->srv_fd, SHUT_WR);
2737 t->srv_state = SV_STSHUTW;
2738 return 1;
2739 }
2740
2741 /* write timeout */
2742 /* FIXME!!! here, we don't want to switch to SHUTW if the
2743 * client shuts read too early, because we may still have
2744 * some work to do on the headers.
2745 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002746 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002747 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002748 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002749 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002750 shutdown(t->srv_fd, SHUT_WR);
2751 /* We must ensure that the read part is still alive
2752 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002753 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002754 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002755
2756 t->srv_state = SV_STSHUTW;
2757 if (!(t->flags & SN_ERR_MASK))
2758 t->flags |= SN_ERR_SRVTO;
2759 if (!(t->flags & SN_FINST_MASK))
2760 t->flags |= SN_FINST_H;
2761 return 1;
2762 }
2763
2764 /*
2765 * And now the non-error cases.
2766 */
2767
2768 /* Data remaining in the request buffer.
2769 * This happens during the first pass here, and during
2770 * long posts.
2771 */
2772 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002773 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2774 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002775 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002776 /* FIXME: to prevent the server from expiring read timeouts during writes,
2777 * we refresh it. */
2778 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002779 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002780 else
2781 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002782 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002783 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002784
Willy Tarreaua15645d2007-03-18 16:22:39 +01002785 /* nothing left in the request buffer */
2786 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002787 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2788 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002789 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002790 }
2791 }
2792
2793 return t->srv_state != SV_STHEADERS;
2794 }
2795
2796
2797 /*****************************************************************
2798 * More interesting part now : we know that we have a complete *
2799 * response which at least looks like HTTP. We have an indicator *
2800 * of each header's length, so we can parse them quickly. *
2801 ****************************************************************/
2802
Willy Tarreau9cdde232007-05-02 20:58:19 +02002803 /* ensure we keep this pointer to the beginning of the message */
2804 msg->sol = rep->data + msg->som;
2805
Willy Tarreaua15645d2007-03-18 16:22:39 +01002806 /*
2807 * 1: get the status code and check for cacheability.
2808 */
2809
2810 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002811 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002812
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002813 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002814 case 200:
2815 case 203:
2816 case 206:
2817 case 300:
2818 case 301:
2819 case 410:
2820 /* RFC2616 @13.4:
2821 * "A response received with a status code of
2822 * 200, 203, 206, 300, 301 or 410 MAY be stored
2823 * by a cache (...) unless a cache-control
2824 * directive prohibits caching."
2825 *
2826 * RFC2616 @9.5: POST method :
2827 * "Responses to this method are not cacheable,
2828 * unless the response includes appropriate
2829 * Cache-Control or Expires header fields."
2830 */
2831 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002832 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002833 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002834 break;
2835 default:
2836 break;
2837 }
2838
2839 /*
2840 * 2: we may need to capture headers
2841 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002842 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002843 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002844 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002845
2846 /*
2847 * 3: we will have to evaluate the filters.
2848 * As opposed to version 1.2, now they will be evaluated in the
2849 * filters order and not in the header order. This means that
2850 * each filter has to be validated among all headers.
2851 *
2852 * Filters are tried with ->be first, then with ->fe if it is
2853 * different from ->be.
2854 */
2855
2856 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2857
2858 cur_proxy = t->be;
2859 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002860 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002861
2862 /* try headers filters */
2863 if (rule_set->rsp_exp != NULL) {
2864 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2865 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 if (t->srv) {
2867 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002868 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002870 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002871 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002872 buffer_shutr(rep);
2873 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002874 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002876 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002877 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002878 if (!(t->flags & SN_ERR_MASK))
2879 t->flags |= SN_ERR_PRXCOND;
2880 if (!(t->flags & SN_FINST_MASK))
2881 t->flags |= SN_FINST_H;
2882 /* We used to have a free connection slot. Since we'll never use it,
2883 * we have to inform the server that it may be used by another session.
2884 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002885 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002886 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887 return 1;
2888 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002889 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002890
Willy Tarreaua15645d2007-03-18 16:22:39 +01002891 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002892 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002893 if (t->srv) {
2894 t->srv->cur_sess--;
2895 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002897 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002898 goto return_srv_prx_502;
2899 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002900
Willy Tarreaua15645d2007-03-18 16:22:39 +01002901 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002902 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002903 !(t->flags & SN_CONN_CLOSED)) {
2904 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002905 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002906 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2909 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002910
Willy Tarreaua15645d2007-03-18 16:22:39 +01002911 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2912 cur_hdr = &txn->hdr_idx.v[cur_idx];
2913 cur_ptr = cur_next;
2914 cur_end = cur_ptr + cur_hdr->len;
2915 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002917 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2918 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002919 /* 3 possibilities :
2920 * - we have already set Connection: close,
2921 * so we remove this line.
2922 * - we have not yet set Connection: close,
2923 * but this line indicates close. We leave
2924 * it untouched and set the flag.
2925 * - we have not yet set Connection: close,
2926 * and this line indicates non-close. We
2927 * replace it.
2928 */
2929 if (t->flags & SN_CONN_CLOSED) {
2930 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2931 txn->rsp.eoh += delta;
2932 cur_next += delta;
2933 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2934 txn->hdr_idx.used--;
2935 cur_hdr->len = 0;
2936 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002937 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2938 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2939 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002940 cur_next += delta;
2941 cur_hdr->len += delta;
2942 txn->rsp.eoh += delta;
2943 }
2944 t->flags |= SN_CONN_CLOSED;
2945 }
2946 }
2947 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002949 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002950
Willy Tarreaua15645d2007-03-18 16:22:39 +01002951 /* add response headers from the rule sets in the same order */
2952 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002953 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2954 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956 }
2957
Willy Tarreaua15645d2007-03-18 16:22:39 +01002958 /* check whether we're already working on the frontend */
2959 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002960 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002961 cur_proxy = t->fe;
2962 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963
Willy Tarreaua15645d2007-03-18 16:22:39 +01002964 /*
2965 * 4: check for server cookie.
2966 */
2967 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002968
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002969
Willy Tarreaua15645d2007-03-18 16:22:39 +01002970 /*
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002971 * 5: check for cache-control or pragma headers.
2972 */
2973 check_response_for_cacheability(t, rep);
2974
2975
2976 /*
2977 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002978 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002979 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2980 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002981 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982
Willy Tarreaua15645d2007-03-18 16:22:39 +01002983 /* the server is known, it's not the one the client requested, we have to
2984 * insert a set-cookie here, except if we want to insert only on POST
2985 * requests and this one isn't. Note that servers which don't have cookies
2986 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002988 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002989 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002990 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002992 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2993 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002994 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002995 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002996
Willy Tarreaua15645d2007-03-18 16:22:39 +01002997 /* Here, we will tell an eventual cache on the client side that we don't
2998 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2999 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3000 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3001 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003002 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3003
3004 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3005
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003006 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3007 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003008 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003009 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003010 }
3011
3012
3013 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003014 * 7: check if result will be cacheable with a cookie.
3015 * We'll block the response if security checks have caught
3016 * nasty things such as a cacheable cookie.
3017 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003018 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3019 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003020 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021
Willy Tarreaua15645d2007-03-18 16:22:39 +01003022 /* we're in presence of a cacheable response containing
3023 * a set-cookie header. We'll block it as requested by
3024 * the 'checkcache' option, and send an alert.
3025 */
3026 if (t->srv) {
3027 t->srv->cur_sess--;
3028 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003029 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003030 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003031
Willy Tarreaua15645d2007-03-18 16:22:39 +01003032 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003033 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003034 send_log(t->be, LOG_ALERT,
3035 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003036 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003037 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038 }
3039
Willy Tarreaua15645d2007-03-18 16:22:39 +01003040 /*
3041 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003042 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003043 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003044 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003045 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003046 if ((unlikely(msg->sl.st.v_l != 8) ||
3047 unlikely(req->data[msg->som + 7] != '0')) &&
3048 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003049 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003050 goto return_bad_resp;
3051 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003053
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055 /*************************************************************
3056 * OK, that's finished for the headers. We have done what we *
3057 * could. Let's switch to the DATA state. *
3058 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059
Willy Tarreaua15645d2007-03-18 16:22:39 +01003060 t->srv_state = SV_STDATA;
3061 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003062 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003063
3064 /* client connection already closed or option 'forceclose' required :
3065 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003066 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003067 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003068 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003069 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003070 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071
3072 /* We must ensure that the read part is still alive when switching
3073 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003074 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003075 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003076
Willy Tarreaua15645d2007-03-18 16:22:39 +01003077 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003078 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079 }
3080
Willy Tarreaua15645d2007-03-18 16:22:39 +01003081#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003082 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003083 /* TCP splicing supported by both FE and BE */
3084 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003085 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086#endif
3087 /* if the user wants to log as soon as possible, without counting
3088 bytes from the server, then this is the right moment. */
3089 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3090 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003091 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003092 if (t->fe->to_log & LW_REQ)
3093 http_sess_log(t);
3094 else
3095 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003096 }
3097
Willy Tarreaua15645d2007-03-18 16:22:39 +01003098 /* Note: we must not try to cheat by jumping directly to DATA,
3099 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003100 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101
3102 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003103 }
3104 else if (s == SV_STDATA) {
3105 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003106 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003107 buffer_shutr(rep);
3108 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109 fd_delete(t->srv_fd);
3110 if (t->srv) {
3111 t->srv->cur_sess--;
3112 t->srv->failed_resp++;
3113 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003114 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003115 t->srv_state = SV_STCLOSE;
3116 if (!(t->flags & SN_ERR_MASK))
3117 t->flags |= SN_ERR_SRVCL;
3118 if (!(t->flags & SN_FINST_MASK))
3119 t->flags |= SN_FINST_D;
3120 /* We used to have a free connection slot. Since we'll never use it,
3121 * we have to inform the server that it may be used by another session.
3122 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003123 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003124 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125
3126 return 1;
3127 }
3128 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003129 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003130 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003131 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003132 t->srv_state = SV_STSHUTR;
3133 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3134 return 1;
3135 }
3136 /* end of client read and no more data to send */
3137 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003138 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003139 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140 shutdown(t->srv_fd, SHUT_WR);
3141 /* We must ensure that the read part is still alive when switching
3142 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003143 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003144 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003145
3146 t->srv_state = SV_STSHUTW;
3147 return 1;
3148 }
3149 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003150 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003151 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003152 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003153 t->srv_state = SV_STSHUTR;
3154 if (!(t->flags & SN_ERR_MASK))
3155 t->flags |= SN_ERR_SRVTO;
3156 if (!(t->flags & SN_FINST_MASK))
3157 t->flags |= SN_FINST_D;
3158 return 1;
3159 }
3160 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003161 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003162 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003163 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003164 shutdown(t->srv_fd, SHUT_WR);
3165 /* We must ensure that the read part is still alive when switching
3166 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003167 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003168 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003169 t->srv_state = SV_STSHUTW;
3170 if (!(t->flags & SN_ERR_MASK))
3171 t->flags |= SN_ERR_SRVTO;
3172 if (!(t->flags & SN_FINST_MASK))
3173 t->flags |= SN_FINST_D;
3174 return 1;
3175 }
3176
3177 /* recompute request time-outs */
3178 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003179 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3180 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003181 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003182 }
3183 }
3184 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003185 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3186 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003187 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003188 /* FIXME: to prevent the server from expiring read timeouts during writes,
3189 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003190 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 }
3192 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003193 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003194 }
3195 }
3196
3197 /* recompute response time-outs */
3198 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003199 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003200 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201 }
3202 }
3203 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003204 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003205 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003206 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003207 }
3208 }
3209
3210 return 0; /* other cases change nothing */
3211 }
3212 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003213 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003214 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003215 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003216 fd_delete(t->srv_fd);
3217 if (t->srv) {
3218 t->srv->cur_sess--;
3219 t->srv->failed_resp++;
3220 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003221 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003222 //close(t->srv_fd);
3223 t->srv_state = SV_STCLOSE;
3224 if (!(t->flags & SN_ERR_MASK))
3225 t->flags |= SN_ERR_SRVCL;
3226 if (!(t->flags & SN_FINST_MASK))
3227 t->flags |= SN_FINST_D;
3228 /* We used to have a free connection slot. Since we'll never use it,
3229 * we have to inform the server that it may be used by another session.
3230 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003231 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003232 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003233
3234 return 1;
3235 }
3236 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003237 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003238 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003239 fd_delete(t->srv_fd);
3240 if (t->srv)
3241 t->srv->cur_sess--;
3242 //close(t->srv_fd);
3243 t->srv_state = SV_STCLOSE;
3244 /* We used to have a free connection slot. Since we'll never use it,
3245 * we have to inform the server that it may be used by another session.
3246 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003247 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003248 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003249
3250 return 1;
3251 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003252 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003253 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003254 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255 fd_delete(t->srv_fd);
3256 if (t->srv)
3257 t->srv->cur_sess--;
3258 //close(t->srv_fd);
3259 t->srv_state = SV_STCLOSE;
3260 if (!(t->flags & SN_ERR_MASK))
3261 t->flags |= SN_ERR_SRVTO;
3262 if (!(t->flags & SN_FINST_MASK))
3263 t->flags |= SN_FINST_D;
3264 /* We used to have a free connection slot. Since we'll never use it,
3265 * we have to inform the server that it may be used by another session.
3266 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003267 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003268 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003269
3270 return 1;
3271 }
3272 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003273 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3274 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003275 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003276 }
3277 }
3278 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003279 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3280 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02003281 if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003282 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003283 }
3284 }
3285 return 0;
3286 }
3287 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003288 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003289 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003290 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291 fd_delete(t->srv_fd);
3292 if (t->srv) {
3293 t->srv->cur_sess--;
3294 t->srv->failed_resp++;
3295 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003296 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003297 //close(t->srv_fd);
3298 t->srv_state = SV_STCLOSE;
3299 if (!(t->flags & SN_ERR_MASK))
3300 t->flags |= SN_ERR_SRVCL;
3301 if (!(t->flags & SN_FINST_MASK))
3302 t->flags |= SN_FINST_D;
3303 /* We used to have a free connection slot. Since we'll never use it,
3304 * we have to inform the server that it may be used by another session.
3305 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003306 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003307 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003308
3309 return 1;
3310 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003311 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003312 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003313 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003314 fd_delete(t->srv_fd);
3315 if (t->srv)
3316 t->srv->cur_sess--;
3317 //close(t->srv_fd);
3318 t->srv_state = SV_STCLOSE;
3319 /* We used to have a free connection slot. Since we'll never use it,
3320 * we have to inform the server that it may be used by another session.
3321 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003322 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003323 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324
3325 return 1;
3326 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003327 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003328 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003329 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003330 fd_delete(t->srv_fd);
3331 if (t->srv)
3332 t->srv->cur_sess--;
3333 //close(t->srv_fd);
3334 t->srv_state = SV_STCLOSE;
3335 if (!(t->flags & SN_ERR_MASK))
3336 t->flags |= SN_ERR_SRVTO;
3337 if (!(t->flags & SN_FINST_MASK))
3338 t->flags |= SN_FINST_D;
3339 /* We used to have a free connection slot. Since we'll never use it,
3340 * we have to inform the server that it may be used by another session.
3341 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003342 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003343 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344
3345 return 1;
3346 }
3347 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003348 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003349 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003350 }
3351 }
3352 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003353 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003354 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003355 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003356 }
3357 }
3358 return 0;
3359 }
3360 else { /* SV_STCLOSE : nothing to do */
3361 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3362 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003363 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003364 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003365 write(1, trash, len);
3366 }
3367 return 0;
3368 }
3369 return 0;
3370}
3371
3372
3373/*
3374 * Produces data for the session <s> depending on its source. Expects to be
3375 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3376 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3377 * session, which it uses to keep on being called when there is free space in
3378 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3379 * if it changes the session state from CL_STSHUTR, otherwise 0.
3380 */
3381int produce_content(struct session *s)
3382{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003383 if (s->data_source == DATA_SRC_NONE) {
3384 s->flags &= ~SN_SELF_GEN;
3385 return 1;
3386 }
3387 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003388 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003389 int ret = stats_dump_http(s, s->be->uri_auth,
3390 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003391 if (ret >= 0)
3392 return ret;
3393 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003394 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003395
Willy Tarreau91861262007-10-17 17:06:05 +02003396 /* unknown data source or internal error */
3397 s->txn.status = 500;
3398 client_retnclose(s, error_message(s, HTTP_ERR_500));
3399 if (!(s->flags & SN_ERR_MASK))
3400 s->flags |= SN_ERR_PRXCOND;
3401 if (!(s->flags & SN_FINST_MASK))
3402 s->flags |= SN_FINST_R;
3403 s->flags &= ~SN_SELF_GEN;
3404 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003405}
3406
3407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003408/* Iterate the same filter through all request headers.
3409 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003410 * Since it can manage the switch to another backend, it updates the per-proxy
3411 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003412 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003413int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003414{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003415 char term;
3416 char *cur_ptr, *cur_end, *cur_next;
3417 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003418 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003419 struct hdr_idx_elem *cur_hdr;
3420 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003422 last_hdr = 0;
3423
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003424 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003425 old_idx = 0;
3426
3427 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003428 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003429 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003430 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003431 (exp->action == ACT_ALLOW ||
3432 exp->action == ACT_DENY ||
3433 exp->action == ACT_TARPIT))
3434 return 0;
3435
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003436 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003437 if (!cur_idx)
3438 break;
3439
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003440 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003441 cur_ptr = cur_next;
3442 cur_end = cur_ptr + cur_hdr->len;
3443 cur_next = cur_end + cur_hdr->cr + 1;
3444
3445 /* Now we have one header between cur_ptr and cur_end,
3446 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003447 */
3448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003449 /* The annoying part is that pattern matching needs
3450 * that we modify the contents to null-terminate all
3451 * strings before testing them.
3452 */
3453
3454 term = *cur_end;
3455 *cur_end = '\0';
3456
3457 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3458 switch (exp->action) {
3459 case ACT_SETBE:
3460 /* It is not possible to jump a second time.
3461 * FIXME: should we return an HTTP/500 here so that
3462 * the admin knows there's a problem ?
3463 */
3464 if (t->be != t->fe)
3465 break;
3466
3467 /* Swithing Proxy */
3468 t->be = (struct proxy *) exp->replace;
3469
3470 /* right now, the backend switch is not too much complicated
3471 * because we have associated req_cap and rsp_cap to the
3472 * frontend, and the beconn will be updated later.
3473 */
3474
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003475 t->rep->rto = t->req->wto = t->be->srvtimeout;
3476 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003477 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003478 last_hdr = 1;
3479 break;
3480
3481 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003482 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003483 last_hdr = 1;
3484 break;
3485
3486 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003487 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003488 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003489 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003490 break;
3491
3492 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003493 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003494 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003495 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003496 break;
3497
3498 case ACT_REPLACE:
3499 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3500 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3501 /* FIXME: if the user adds a newline in the replacement, the
3502 * index will not be recalculated for now, and the new line
3503 * will not be counted as a new header.
3504 */
3505
3506 cur_end += delta;
3507 cur_next += delta;
3508 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003509 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003510 break;
3511
3512 case ACT_REMOVE:
3513 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3514 cur_next += delta;
3515
3516 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003517 txn->req.eoh += delta;
3518 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3519 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003520 cur_hdr->len = 0;
3521 cur_end = NULL; /* null-term has been rewritten */
3522 break;
3523
3524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003525 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003526 if (cur_end)
3527 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003529 /* keep the link from this header to next one in case of later
3530 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003531 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003532 old_idx = cur_idx;
3533 }
3534 return 0;
3535}
3536
3537
3538/* Apply the filter to the request line.
3539 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3540 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003541 * Since it can manage the switch to another backend, it updates the per-proxy
3542 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003543 */
3544int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3545{
3546 char term;
3547 char *cur_ptr, *cur_end;
3548 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003549 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003550 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003551
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552
Willy Tarreau3d300592007-03-18 18:34:41 +01003553 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003554 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003555 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003556 (exp->action == ACT_ALLOW ||
3557 exp->action == ACT_DENY ||
3558 exp->action == ACT_TARPIT))
3559 return 0;
3560 else if (exp->action == ACT_REMOVE)
3561 return 0;
3562
3563 done = 0;
3564
Willy Tarreau9cdde232007-05-02 20:58:19 +02003565 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003566 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003567
3568 /* Now we have the request line between cur_ptr and cur_end */
3569
3570 /* The annoying part is that pattern matching needs
3571 * that we modify the contents to null-terminate all
3572 * strings before testing them.
3573 */
3574
3575 term = *cur_end;
3576 *cur_end = '\0';
3577
3578 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3579 switch (exp->action) {
3580 case ACT_SETBE:
3581 /* It is not possible to jump a second time.
3582 * FIXME: should we return an HTTP/500 here so that
3583 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003585 if (t->be != t->fe)
3586 break;
3587
3588 /* Swithing Proxy */
3589 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003591 /* right now, the backend switch is not too much complicated
3592 * because we have associated req_cap and rsp_cap to the
3593 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003594 */
3595
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003596 t->rep->rto = t->req->wto = t->be->srvtimeout;
3597 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003598 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003599 done = 1;
3600 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003602 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003603 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003604 done = 1;
3605 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003608 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003609 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 done = 1;
3611 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003614 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003615 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003616 done = 1;
3617 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003619 case ACT_REPLACE:
3620 *cur_end = term; /* restore the string terminator */
3621 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3622 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3623 /* FIXME: if the user adds a newline in the replacement, the
3624 * index will not be recalculated for now, and the new line
3625 * will not be counted as a new header.
3626 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003627
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003628 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003629 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003630
Willy Tarreau9cdde232007-05-02 20:58:19 +02003631 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003632 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003633 HTTP_MSG_RQMETH,
3634 cur_ptr, cur_end + 1,
3635 NULL, NULL);
3636 if (unlikely(!cur_end))
3637 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003639 /* we have a full request and we know that we have either a CR
3640 * or an LF at <ptr>.
3641 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003642 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3643 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003644 /* there is no point trying this regex on headers */
3645 return 1;
3646 }
3647 }
3648 *cur_end = term; /* restore the string terminator */
3649 return done;
3650}
Willy Tarreau97de6242006-12-27 17:18:38 +01003651
Willy Tarreau58f10d72006-12-04 02:26:12 +01003652
Willy Tarreau58f10d72006-12-04 02:26:12 +01003653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003654/*
3655 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3656 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003657 * unparsable request. Since it can manage the switch to another backend, it
3658 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003659 */
3660int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3661{
Willy Tarreau3d300592007-03-18 18:34:41 +01003662 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003663 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003664 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003665 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003667 /*
3668 * The interleaving of transformations and verdicts
3669 * makes it difficult to decide to continue or stop
3670 * the evaluation.
3671 */
3672
Willy Tarreau3d300592007-03-18 18:34:41 +01003673 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003674 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3675 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3676 exp = exp->next;
3677 continue;
3678 }
3679
3680 /* Apply the filter to the request line. */
3681 ret = apply_filter_to_req_line(t, req, exp);
3682 if (unlikely(ret < 0))
3683 return -1;
3684
3685 if (likely(ret == 0)) {
3686 /* The filter did not match the request, it can be
3687 * iterated through all headers.
3688 */
3689 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003690 }
3691 exp = exp->next;
3692 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003693 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003694}
3695
3696
Willy Tarreaua15645d2007-03-18 16:22:39 +01003697
Willy Tarreau58f10d72006-12-04 02:26:12 +01003698/*
3699 * Manager client-side cookie
3700 */
3701void manage_client_side_cookies(struct session *t, struct buffer *req)
3702{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003703 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704 char *p1, *p2, *p3, *p4;
3705 char *del_colon, *del_cookie, *colon;
3706 int app_cookies;
3707
3708 appsess *asession_temp = NULL;
3709 appsess local_asession;
3710
3711 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003712 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003713
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003714 if (t->be->cookie_name == NULL &&
3715 t->be->appsession_name == NULL &&
3716 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003717 return;
3718
Willy Tarreau2a324282006-12-05 00:05:46 +01003719 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003720 * we start with the start line.
3721 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003722 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003723 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003724
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003725 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003726 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003727 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003728
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003729 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003730 cur_ptr = cur_next;
3731 cur_end = cur_ptr + cur_hdr->len;
3732 cur_next = cur_end + cur_hdr->cr + 1;
3733
3734 /* We have one full header between cur_ptr and cur_end, and the
3735 * next header starts at cur_next. We're only interested in
3736 * "Cookie:" headers.
3737 */
3738
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003739 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3740 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003741 old_idx = cur_idx;
3742 continue;
3743 }
3744
3745 /* Now look for cookies. Conforming to RFC2109, we have to support
3746 * attributes whose name begin with a '$', and associate them with
3747 * the right cookie, if we want to delete this cookie.
3748 * So there are 3 cases for each cookie read :
3749 * 1) it's a special attribute, beginning with a '$' : ignore it.
3750 * 2) it's a server id cookie that we *MAY* want to delete : save
3751 * some pointers on it (last semi-colon, beginning of cookie...)
3752 * 3) it's an application cookie : we *MAY* have to delete a previous
3753 * "special" cookie.
3754 * At the end of loop, if a "special" cookie remains, we may have to
3755 * remove it. If no application cookie persists in the header, we
3756 * *MUST* delete it
3757 */
3758
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003759 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003760
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761 /* del_cookie == NULL => nothing to be deleted */
3762 del_colon = del_cookie = NULL;
3763 app_cookies = 0;
3764
3765 while (p1 < cur_end) {
3766 /* skip spaces and colons, but keep an eye on these ones */
3767 while (p1 < cur_end) {
3768 if (*p1 == ';' || *p1 == ',')
3769 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003770 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003771 break;
3772 p1++;
3773 }
3774
3775 if (p1 == cur_end)
3776 break;
3777
3778 /* p1 is at the beginning of the cookie name */
3779 p2 = p1;
3780 while (p2 < cur_end && *p2 != '=')
3781 p2++;
3782
3783 if (p2 == cur_end)
3784 break;
3785
3786 p3 = p2 + 1; /* skips the '=' sign */
3787 if (p3 == cur_end)
3788 break;
3789
3790 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003791 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003792 p4++;
3793
3794 /* here, we have the cookie name between p1 and p2,
3795 * and its value between p3 and p4.
3796 * we can process it :
3797 *
3798 * Cookie: NAME=VALUE;
3799 * | || || |
3800 * | || || +--> p4
3801 * | || |+-------> p3
3802 * | || +--------> p2
3803 * | |+------------> p1
3804 * | +-------------> colon
3805 * +--------------------> cur_ptr
3806 */
3807
3808 if (*p1 == '$') {
3809 /* skip this one */
3810 }
3811 else {
3812 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003813 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003814 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003815 (p4 - p1 >= t->fe->capture_namelen) &&
3816 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003817 int log_len = p4 - p1;
3818
Willy Tarreau086b3b42007-05-13 21:45:51 +02003819 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003820 Alert("HTTP logging : out of memory.\n");
3821 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003822 if (log_len > t->fe->capture_len)
3823 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003824 memcpy(txn->cli_cookie, p1, log_len);
3825 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003826 }
3827 }
3828
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003829 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3830 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003831 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003832 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003833 char *delim;
3834
3835 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3836 * have the server ID betweek p3 and delim, and the original cookie between
3837 * delim+1 and p4. Otherwise, delim==p4 :
3838 *
3839 * Cookie: NAME=SRV~VALUE;
3840 * | || || | |
3841 * | || || | +--> p4
3842 * | || || +--------> delim
3843 * | || |+-----------> p3
3844 * | || +------------> p2
3845 * | |+----------------> p1
3846 * | +-----------------> colon
3847 * +------------------------> cur_ptr
3848 */
3849
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003850 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851 for (delim = p3; delim < p4; delim++)
3852 if (*delim == COOKIE_DELIM)
3853 break;
3854 }
3855 else
3856 delim = p4;
3857
3858
3859 /* Here, we'll look for the first running server which supports the cookie.
3860 * This allows to share a same cookie between several servers, for example
3861 * to dedicate backup servers to specific servers only.
3862 * However, to prevent clients from sticking to cookie-less backup server
3863 * when they have incidentely learned an empty cookie, we simply ignore
3864 * empty cookies and mark them as invalid.
3865 */
3866 if (delim == p3)
3867 srv = NULL;
3868
3869 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003870 if (srv->cookie && (srv->cklen == delim - p3) &&
3871 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003872 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003873 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003874 txn->flags &= ~TX_CK_MASK;
3875 txn->flags |= TX_CK_VALID;
3876 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003877 t->srv = srv;
3878 break;
3879 } else {
3880 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003881 txn->flags &= ~TX_CK_MASK;
3882 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003883 }
3884 }
3885 srv = srv->next;
3886 }
3887
Willy Tarreau3d300592007-03-18 18:34:41 +01003888 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003889 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003890 txn->flags &= ~TX_CK_MASK;
3891 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003892 }
3893
3894 /* depending on the cookie mode, we may have to either :
3895 * - delete the complete cookie if we're in insert+indirect mode, so that
3896 * the server never sees it ;
3897 * - remove the server id from the cookie value, and tag the cookie as an
3898 * application cookie so that it does not get accidentely removed later,
3899 * if we're in cookie prefix mode
3900 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003901 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003902 int delta; /* negative */
3903
3904 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3905 p4 += delta;
3906 cur_end += delta;
3907 cur_next += delta;
3908 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003909 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003910
3911 del_cookie = del_colon = NULL;
3912 app_cookies++; /* protect the header from deletion */
3913 }
3914 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003915 (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 +01003916 del_cookie = p1;
3917 del_colon = colon;
3918 }
3919 } else {
3920 /* now we know that we must keep this cookie since it's
3921 * not ours. But if we wanted to delete our cookie
3922 * earlier, we cannot remove the complete header, but we
3923 * can remove the previous block itself.
3924 */
3925 app_cookies++;
3926
3927 if (del_cookie != NULL) {
3928 int delta; /* negative */
3929
3930 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3931 p4 += delta;
3932 cur_end += delta;
3933 cur_next += delta;
3934 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003935 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003936 del_cookie = del_colon = NULL;
3937 }
3938 }
3939
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003940 if ((t->be->appsession_name != NULL) &&
3941 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003942 /* first, let's see if the cookie is our appcookie*/
3943
3944 /* Cool... it's the right one */
3945
3946 asession_temp = &local_asession;
3947
Willy Tarreau63963c62007-05-13 21:29:55 +02003948 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003949 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3950 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3951 return;
3952 }
3953
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003954 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3955 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003956 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003957
Willy Tarreau58f10d72006-12-04 02:26:12 +01003958 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003959 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3960 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003961 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003962 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003963 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003964 Alert("Not enough memory process_cli():asession:calloc().\n");
3965 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3966 return;
3967 }
3968
3969 asession_temp->sessid = local_asession.sessid;
3970 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003971 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003972 } else {
3973 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003974 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003975 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976 if (asession_temp->serverid == NULL) {
3977 Alert("Found Application Session without matching server.\n");
3978 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003979 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003980 while (srv) {
3981 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003982 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003983 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003984 txn->flags &= ~TX_CK_MASK;
3985 txn->flags |= TX_CK_VALID;
3986 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003987 t->srv = srv;
3988 break;
3989 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003990 txn->flags &= ~TX_CK_MASK;
3991 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003992 }
3993 }
3994 srv = srv->next;
3995 }/* end while(srv) */
3996 }/* end else if server == NULL */
3997
Willy Tarreaud825eef2007-05-12 22:35:00 +02003998 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003999 }/* end if ((t->proxy->appsession_name != NULL) ... */
4000 }
4001
4002 /* we'll have to look for another cookie ... */
4003 p1 = p4;
4004 } /* while (p1 < cur_end) */
4005
4006 /* There's no more cookie on this line.
4007 * We may have marked the last one(s) for deletion.
4008 * We must do this now in two ways :
4009 * - if there is no app cookie, we simply delete the header ;
4010 * - if there are app cookies, we must delete the end of the
4011 * string properly, including the colon/semi-colon before
4012 * the cookie name.
4013 */
4014 if (del_cookie != NULL) {
4015 int delta;
4016 if (app_cookies) {
4017 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4018 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004019 cur_hdr->len += delta;
4020 } else {
4021 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004022
4023 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004024 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4025 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004026 cur_hdr->len = 0;
4027 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004028 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004029 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004030 }
4031
4032 /* keep the link from this header to next one */
4033 old_idx = cur_idx;
4034 } /* end of cookie processing on this header */
4035}
4036
4037
Willy Tarreaua15645d2007-03-18 16:22:39 +01004038/* Iterate the same filter through all response headers contained in <rtr>.
4039 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4040 */
4041int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4042{
4043 char term;
4044 char *cur_ptr, *cur_end, *cur_next;
4045 int cur_idx, old_idx, last_hdr;
4046 struct http_txn *txn = &t->txn;
4047 struct hdr_idx_elem *cur_hdr;
4048 int len, delta;
4049
4050 last_hdr = 0;
4051
4052 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4053 old_idx = 0;
4054
4055 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004056 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004057 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004058 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004059 (exp->action == ACT_ALLOW ||
4060 exp->action == ACT_DENY))
4061 return 0;
4062
4063 cur_idx = txn->hdr_idx.v[old_idx].next;
4064 if (!cur_idx)
4065 break;
4066
4067 cur_hdr = &txn->hdr_idx.v[cur_idx];
4068 cur_ptr = cur_next;
4069 cur_end = cur_ptr + cur_hdr->len;
4070 cur_next = cur_end + cur_hdr->cr + 1;
4071
4072 /* Now we have one header between cur_ptr and cur_end,
4073 * and the next header starts at cur_next.
4074 */
4075
4076 /* The annoying part is that pattern matching needs
4077 * that we modify the contents to null-terminate all
4078 * strings before testing them.
4079 */
4080
4081 term = *cur_end;
4082 *cur_end = '\0';
4083
4084 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4085 switch (exp->action) {
4086 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004087 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004088 last_hdr = 1;
4089 break;
4090
4091 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004092 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004093 last_hdr = 1;
4094 break;
4095
4096 case ACT_REPLACE:
4097 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4098 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4099 /* FIXME: if the user adds a newline in the replacement, the
4100 * index will not be recalculated for now, and the new line
4101 * will not be counted as a new header.
4102 */
4103
4104 cur_end += delta;
4105 cur_next += delta;
4106 cur_hdr->len += delta;
4107 txn->rsp.eoh += delta;
4108 break;
4109
4110 case ACT_REMOVE:
4111 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4112 cur_next += delta;
4113
4114 /* FIXME: this should be a separate function */
4115 txn->rsp.eoh += delta;
4116 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4117 txn->hdr_idx.used--;
4118 cur_hdr->len = 0;
4119 cur_end = NULL; /* null-term has been rewritten */
4120 break;
4121
4122 }
4123 }
4124 if (cur_end)
4125 *cur_end = term; /* restore the string terminator */
4126
4127 /* keep the link from this header to next one in case of later
4128 * removal of next header.
4129 */
4130 old_idx = cur_idx;
4131 }
4132 return 0;
4133}
4134
4135
4136/* Apply the filter to the status line in the response buffer <rtr>.
4137 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4138 * or -1 if a replacement resulted in an invalid status line.
4139 */
4140int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4141{
4142 char term;
4143 char *cur_ptr, *cur_end;
4144 int done;
4145 struct http_txn *txn = &t->txn;
4146 int len, delta;
4147
4148
Willy Tarreau3d300592007-03-18 18:34:41 +01004149 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004150 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004151 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004152 (exp->action == ACT_ALLOW ||
4153 exp->action == ACT_DENY))
4154 return 0;
4155 else if (exp->action == ACT_REMOVE)
4156 return 0;
4157
4158 done = 0;
4159
Willy Tarreau9cdde232007-05-02 20:58:19 +02004160 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004161 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4162
4163 /* Now we have the status line between cur_ptr and cur_end */
4164
4165 /* The annoying part is that pattern matching needs
4166 * that we modify the contents to null-terminate all
4167 * strings before testing them.
4168 */
4169
4170 term = *cur_end;
4171 *cur_end = '\0';
4172
4173 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4174 switch (exp->action) {
4175 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004176 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004177 done = 1;
4178 break;
4179
4180 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004181 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004182 done = 1;
4183 break;
4184
4185 case ACT_REPLACE:
4186 *cur_end = term; /* restore the string terminator */
4187 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4188 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4189 /* FIXME: if the user adds a newline in the replacement, the
4190 * index will not be recalculated for now, and the new line
4191 * will not be counted as a new header.
4192 */
4193
4194 txn->rsp.eoh += delta;
4195 cur_end += delta;
4196
Willy Tarreau9cdde232007-05-02 20:58:19 +02004197 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004198 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004199 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004200 cur_ptr, cur_end + 1,
4201 NULL, NULL);
4202 if (unlikely(!cur_end))
4203 return -1;
4204
4205 /* we have a full respnse and we know that we have either a CR
4206 * or an LF at <ptr>.
4207 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004208 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004209 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4210 /* there is no point trying this regex on headers */
4211 return 1;
4212 }
4213 }
4214 *cur_end = term; /* restore the string terminator */
4215 return done;
4216}
4217
4218
4219
4220/*
4221 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4222 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4223 * unparsable response.
4224 */
4225int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4226{
Willy Tarreau3d300592007-03-18 18:34:41 +01004227 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004228 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004229 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004230 int ret;
4231
4232 /*
4233 * The interleaving of transformations and verdicts
4234 * makes it difficult to decide to continue or stop
4235 * the evaluation.
4236 */
4237
Willy Tarreau3d300592007-03-18 18:34:41 +01004238 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004239 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4240 exp->action == ACT_PASS)) {
4241 exp = exp->next;
4242 continue;
4243 }
4244
4245 /* Apply the filter to the status line. */
4246 ret = apply_filter_to_sts_line(t, rtr, exp);
4247 if (unlikely(ret < 0))
4248 return -1;
4249
4250 if (likely(ret == 0)) {
4251 /* The filter did not match the response, it can be
4252 * iterated through all headers.
4253 */
4254 apply_filter_to_resp_headers(t, rtr, exp);
4255 }
4256 exp = exp->next;
4257 }
4258 return 0;
4259}
4260
4261
4262
4263/*
4264 * Manager server-side cookies
4265 */
4266void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4267{
4268 struct http_txn *txn = &t->txn;
4269 char *p1, *p2, *p3, *p4;
4270
4271 appsess *asession_temp = NULL;
4272 appsess local_asession;
4273
4274 char *cur_ptr, *cur_end, *cur_next;
4275 int cur_idx, old_idx, delta;
4276
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004277 if (t->be->cookie_name == NULL &&
4278 t->be->appsession_name == NULL &&
4279 t->be->capture_name == NULL &&
4280 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004281 return;
4282
4283 /* Iterate through the headers.
4284 * we start with the start line.
4285 */
4286 old_idx = 0;
4287 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4288
4289 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4290 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004291 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004292
4293 cur_hdr = &txn->hdr_idx.v[cur_idx];
4294 cur_ptr = cur_next;
4295 cur_end = cur_ptr + cur_hdr->len;
4296 cur_next = cur_end + cur_hdr->cr + 1;
4297
4298 /* We have one full header between cur_ptr and cur_end, and the
4299 * next header starts at cur_next. We're only interested in
4300 * "Cookie:" headers.
4301 */
4302
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004303 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4304 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305 old_idx = cur_idx;
4306 continue;
4307 }
4308
4309 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004310 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004311
4312
4313 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004314 if (t->be->cookie_name == NULL &&
4315 t->be->appsession_name == NULL &&
4316 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004317 return;
4318
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004319 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004320
4321 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004322 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4323 break;
4324
4325 /* p1 is at the beginning of the cookie name */
4326 p2 = p1;
4327
4328 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4329 p2++;
4330
4331 if (p2 == cur_end || *p2 == ';') /* next cookie */
4332 break;
4333
4334 p3 = p2 + 1; /* skip the '=' sign */
4335 if (p3 == cur_end)
4336 break;
4337
4338 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004339 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004340 p4++;
4341
4342 /* here, we have the cookie name between p1 and p2,
4343 * and its value between p3 and p4.
4344 * we can process it.
4345 */
4346
4347 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004348 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004349 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 (p4 - p1 >= t->be->capture_namelen) &&
4351 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004352 int log_len = p4 - p1;
4353
Willy Tarreau086b3b42007-05-13 21:45:51 +02004354 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004355 Alert("HTTP logging : out of memory.\n");
4356 }
4357
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004358 if (log_len > t->be->capture_len)
4359 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004360 memcpy(txn->srv_cookie, p1, log_len);
4361 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004362 }
4363
4364 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004365 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4366 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004367 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004368 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004369
4370 /* If the cookie is in insert mode on a known server, we'll delete
4371 * this occurrence because we'll insert another one later.
4372 * We'll delete it too if the "indirect" option is set and we're in
4373 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004374 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4375 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004376 /* this header must be deleted */
4377 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4378 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4379 txn->hdr_idx.used--;
4380 cur_hdr->len = 0;
4381 cur_next += delta;
4382 txn->rsp.eoh += delta;
4383
Willy Tarreau3d300592007-03-18 18:34:41 +01004384 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004385 }
4386 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004387 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004388 /* replace bytes p3->p4 with the cookie name associated
4389 * with this server since we know it.
4390 */
4391 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4392 cur_hdr->len += delta;
4393 cur_next += delta;
4394 txn->rsp.eoh += delta;
4395
Willy Tarreau3d300592007-03-18 18:34:41 +01004396 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004397 }
4398 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004399 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004400 /* insert the cookie name associated with this server
4401 * before existing cookie, and insert a delimitor between them..
4402 */
4403 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4404 cur_hdr->len += delta;
4405 cur_next += delta;
4406 txn->rsp.eoh += delta;
4407
4408 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004409 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004410 }
4411 }
4412 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004413 else if ((t->be->appsession_name != NULL) &&
4414 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004415
4416 /* Cool... it's the right one */
4417
4418 size_t server_id_len = strlen(t->srv->id) + 1;
4419 asession_temp = &local_asession;
4420
Willy Tarreau63963c62007-05-13 21:29:55 +02004421 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004422 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4423 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4424 return;
4425 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004426 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4427 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004428 asession_temp->serverid = NULL;
4429
4430 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004431 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004432 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004433 Alert("Not enough Memory process_srv():asession:calloc().\n");
4434 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4435 return;
4436 }
4437 asession_temp->sessid = local_asession.sessid;
4438 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004439 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4440 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004442 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004443 }
4444
Willy Tarreaua15645d2007-03-18 16:22:39 +01004445 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004446 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004447 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4448 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4449 return;
4450 }
4451 asession_temp->serverid[0] = '\0';
4452 }
4453
4454 if (asession_temp->serverid[0] == '\0')
4455 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4456
Willy Tarreaud825eef2007-05-12 22:35:00 +02004457 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004458
4459#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004460 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004461#endif
4462 }/* end if ((t->proxy->appsession_name != NULL) ... */
4463 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4464 } /* we're now at the end of the cookie value */
4465
4466 /* keep the link from this header to next one */
4467 old_idx = cur_idx;
4468 } /* end of cookie processing on this header */
4469}
4470
4471
4472
4473/*
4474 * Check if response is cacheable or not. Updates t->flags.
4475 */
4476void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4477{
4478 struct http_txn *txn = &t->txn;
4479 char *p1, *p2;
4480
4481 char *cur_ptr, *cur_end, *cur_next;
4482 int cur_idx;
4483
Willy Tarreau3d300592007-03-18 18:34:41 +01004484 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004485 return;
4486
4487 /* Iterate through the headers.
4488 * we start with the start line.
4489 */
4490 cur_idx = 0;
4491 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4492
4493 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4494 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004495 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004496
4497 cur_hdr = &txn->hdr_idx.v[cur_idx];
4498 cur_ptr = cur_next;
4499 cur_end = cur_ptr + cur_hdr->len;
4500 cur_next = cur_end + cur_hdr->cr + 1;
4501
4502 /* We have one full header between cur_ptr and cur_end, and the
4503 * next header starts at cur_next. We're only interested in
4504 * "Cookie:" headers.
4505 */
4506
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004507 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4508 if (val) {
4509 if ((cur_end - (cur_ptr + val) >= 8) &&
4510 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4511 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4512 return;
4513 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004514 }
4515
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004516 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4517 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004518 continue;
4519
4520 /* OK, right now we know we have a cache-control header at cur_ptr */
4521
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004522 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004523
4524 if (p1 >= cur_end) /* no more info */
4525 continue;
4526
4527 /* p1 is at the beginning of the value */
4528 p2 = p1;
4529
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004530 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531 p2++;
4532
4533 /* we have a complete value between p1 and p2 */
4534 if (p2 < cur_end && *p2 == '=') {
4535 /* we have something of the form no-cache="set-cookie" */
4536 if ((cur_end - p1 >= 21) &&
4537 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4538 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004539 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004540 continue;
4541 }
4542
4543 /* OK, so we know that either p2 points to the end of string or to a comma */
4544 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4545 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4546 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4547 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004548 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004549 return;
4550 }
4551
4552 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004553 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004554 continue;
4555 }
4556 }
4557}
4558
4559
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560/*
4561 * Try to retrieve a known appsession in the URI, then the associated server.
4562 * If the server is found, it's assigned to the session.
4563 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004564void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004565{
Willy Tarreau3d300592007-03-18 18:34:41 +01004566 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004567 appsess *asession_temp = NULL;
4568 appsess local_asession;
4569 char *request_line;
4570
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004571 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004572 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004573 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004574 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004575 return;
4576
4577 /* skip ';' */
4578 request_line++;
4579
4580 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004581 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004582 return;
4583
4584 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004585 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004586
4587 /* First try if we already have an appsession */
4588 asession_temp = &local_asession;
4589
Willy Tarreau63963c62007-05-13 21:29:55 +02004590 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004591 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4592 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4593 return;
4594 }
4595
4596 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004597 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4598 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004599 asession_temp->serverid = NULL;
4600
4601 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004602 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004603 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004604 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004605 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004606 Alert("Not enough memory process_cli():asession:calloc().\n");
4607 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4608 return;
4609 }
4610 asession_temp->sessid = local_asession.sessid;
4611 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004612 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004613 }
4614 else {
4615 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004616 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004617 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004618
Willy Tarreaud825eef2007-05-12 22:35:00 +02004619 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004620 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004621
Willy Tarreau58f10d72006-12-04 02:26:12 +01004622#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004623 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004624#endif
4625 if (asession_temp->serverid == NULL) {
4626 Alert("Found Application Session without matching server.\n");
4627 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004628 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004629 while (srv) {
4630 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004631 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004632 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004633 txn->flags &= ~TX_CK_MASK;
4634 txn->flags |= TX_CK_VALID;
4635 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004636 t->srv = srv;
4637 break;
4638 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004639 txn->flags &= ~TX_CK_MASK;
4640 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004641 }
4642 }
4643 srv = srv->next;
4644 }
4645 }
4646}
4647
4648
Willy Tarreaub2513902006-12-17 14:52:38 +01004649/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004650 * In a GET or HEAD request, check if the requested URI matches the stats uri
4651 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004652 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004653 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004654 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004655 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004656 *
4657 * Returns 1 if the session's state changes, otherwise 0.
4658 */
4659int stats_check_uri_auth(struct session *t, struct proxy *backend)
4660{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004661 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004662 struct uri_auth *uri_auth = backend->uri_auth;
4663 struct user_auth *user;
4664 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004665 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004667 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004668 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004669 return 0;
4670
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004671 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004672
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004673 /* the URI is in h */
4674 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004675 return 0;
4676
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004677 h += uri_auth->uri_len;
4678 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4679 if (memcmp(h, ";up", 3) == 0) {
4680 t->flags |= SN_STAT_HIDEDWN;
4681 break;
4682 }
4683 h++;
4684 }
4685
4686 if (uri_auth->refresh) {
4687 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4688 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4689 if (memcmp(h, ";norefresh", 10) == 0) {
4690 t->flags |= SN_STAT_NORFRSH;
4691 break;
4692 }
4693 h++;
4694 }
4695 }
4696
Willy Tarreau55bb8452007-10-17 18:44:57 +02004697 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4698 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4699 if (memcmp(h, ";csv", 4) == 0) {
4700 t->flags |= SN_STAT_FMTCSV;
4701 break;
4702 }
4703 h++;
4704 }
4705
Willy Tarreaub2513902006-12-17 14:52:38 +01004706 /* we are in front of a interceptable URI. Let's check
4707 * if there's an authentication and if it's valid.
4708 */
4709 user = uri_auth->users;
4710 if (!user) {
4711 /* no user auth required, it's OK */
4712 authenticated = 1;
4713 } else {
4714 authenticated = 0;
4715
4716 /* a user list is defined, we have to check.
4717 * skip 21 chars for "Authorization: Basic ".
4718 */
4719
4720 /* FIXME: this should move to an earlier place */
4721 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004722 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4723 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4724 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004725 if (len > 14 &&
4726 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004727 txn->auth_hdr.str = h;
4728 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004729 break;
4730 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004731 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004732 }
4733
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004734 if (txn->auth_hdr.len < 21 ||
4735 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004736 user = NULL;
4737
4738 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004739 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4740 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004741 user->user_pwd, user->user_len)) {
4742 authenticated = 1;
4743 break;
4744 }
4745 user = user->next;
4746 }
4747 }
4748
4749 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004750 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004751
4752 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004753 msg.str = trash;
4754 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004755 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004756 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004757 if (!(t->flags & SN_ERR_MASK))
4758 t->flags |= SN_ERR_PRXCOND;
4759 if (!(t->flags & SN_FINST_MASK))
4760 t->flags |= SN_FINST_R;
4761 return 1;
4762 }
4763
4764 /* The request is valid, the user is authenticate. Let's start sending
4765 * data.
4766 */
4767 t->cli_state = CL_STSHUTR;
4768 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004769 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004770 t->data_source = DATA_SRC_STATS;
4771 t->data_state = DATA_ST_INIT;
4772 produce_content(t);
4773 return 1;
4774}
4775
4776
Willy Tarreaubaaee002006-06-26 02:48:02 +02004777/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004778 * Print a debug line with a header
4779 */
4780void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4781{
4782 int len, max;
4783 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4784 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4785 max = end - start;
4786 UBOUND(max, sizeof(trash) - len - 1);
4787 len += strlcpy2(trash + len, start, max + 1);
4788 trash[len++] = '\n';
4789 write(1, trash, len);
4790}
4791
4792
Willy Tarreau8797c062007-05-07 00:55:35 +02004793/************************************************************************/
4794/* The code below is dedicated to ACL parsing and matching */
4795/************************************************************************/
4796
4797
4798
4799
4800/* 1. Check on METHOD
4801 * We use the pre-parsed method if it is known, and store its number as an
4802 * integer. If it is unknown, we use the pointer and the length.
4803 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004804static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004805{
4806 int len, meth;
4807
Willy Tarreauae8b7962007-06-09 23:10:04 +02004808 len = strlen(*text);
4809 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004810
4811 pattern->val.i = meth;
4812 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004813 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004814 if (!pattern->ptr.str)
4815 return 0;
4816 pattern->len = len;
4817 }
4818 return 1;
4819}
4820
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004821static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004822acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4823 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004824{
4825 int meth;
4826 struct http_txn *txn = l7;
4827
Willy Tarreauc11416f2007-06-17 16:58:38 +02004828 if (txn->req.msg_state != HTTP_MSG_BODY)
4829 return 0;
4830
Willy Tarreau8797c062007-05-07 00:55:35 +02004831 meth = txn->meth;
4832 test->i = meth;
4833 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004834 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4835 /* ensure the indexes are not affected */
4836 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004837 test->len = txn->req.sl.rq.m_l;
4838 test->ptr = txn->req.sol;
4839 }
4840 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4841 return 1;
4842}
4843
4844static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4845{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004846 int icase;
4847
Willy Tarreau8797c062007-05-07 00:55:35 +02004848 if (test->i != pattern->val.i)
4849 return 0;
4850
4851 if (test->i != HTTP_METH_OTHER)
4852 return 1;
4853
4854 /* Other method, we must compare the strings */
4855 if (pattern->len != test->len)
4856 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004857
4858 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4859 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4860 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004861 return 0;
4862 return 1;
4863}
4864
4865/* 2. Check on Request/Status Version
4866 * We simply compare strings here.
4867 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004868static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004869{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004870 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004871 if (!pattern->ptr.str)
4872 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004873 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004874 return 1;
4875}
4876
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004877static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004878acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4879 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004880{
4881 struct http_txn *txn = l7;
4882 char *ptr;
4883 int len;
4884
Willy Tarreauc11416f2007-06-17 16:58:38 +02004885 if (txn->req.msg_state != HTTP_MSG_BODY)
4886 return 0;
4887
Willy Tarreau8797c062007-05-07 00:55:35 +02004888 len = txn->req.sl.rq.v_l;
4889 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4890
4891 while ((len-- > 0) && (*ptr++ != '/'));
4892 if (len <= 0)
4893 return 0;
4894
4895 test->ptr = ptr;
4896 test->len = len;
4897
4898 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4899 return 1;
4900}
4901
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004902static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004903acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4904 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004905{
4906 struct http_txn *txn = l7;
4907 char *ptr;
4908 int len;
4909
Willy Tarreauc11416f2007-06-17 16:58:38 +02004910 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4911 return 0;
4912
Willy Tarreau8797c062007-05-07 00:55:35 +02004913 len = txn->rsp.sl.st.v_l;
4914 ptr = txn->rsp.sol;
4915
4916 while ((len-- > 0) && (*ptr++ != '/'));
4917 if (len <= 0)
4918 return 0;
4919
4920 test->ptr = ptr;
4921 test->len = len;
4922
4923 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4924 return 1;
4925}
4926
4927/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004928static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004929acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4930 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004931{
4932 struct http_txn *txn = l7;
4933 char *ptr;
4934 int len;
4935
Willy Tarreauc11416f2007-06-17 16:58:38 +02004936 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4937 return 0;
4938
Willy Tarreau8797c062007-05-07 00:55:35 +02004939 len = txn->rsp.sl.st.c_l;
4940 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4941
4942 test->i = __strl2ui(ptr, len);
4943 test->flags = ACL_TEST_F_VOL_1ST;
4944 return 1;
4945}
4946
4947/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004948static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004949acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4950 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004951{
4952 struct http_txn *txn = l7;
4953
Willy Tarreauc11416f2007-06-17 16:58:38 +02004954 if (txn->req.msg_state != HTTP_MSG_BODY)
4955 return 0;
4956 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4957 /* ensure the indexes are not affected */
4958 return 0;
4959
Willy Tarreau8797c062007-05-07 00:55:35 +02004960 test->len = txn->req.sl.rq.u_l;
4961 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4962
Willy Tarreauf3d25982007-05-08 22:45:09 +02004963 /* we do not need to set READ_ONLY because the data is in a buffer */
4964 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004965 return 1;
4966}
4967
Willy Tarreauc11416f2007-06-17 16:58:38 +02004968/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4969 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4970 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004971static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004972acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004973 struct acl_expr *expr, struct acl_test *test)
4974{
4975 struct http_txn *txn = l7;
4976 struct hdr_idx *idx = &txn->hdr_idx;
4977 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004978
4979 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4980 /* search for header from the beginning */
4981 ctx->idx = 0;
4982
Willy Tarreau33a7e692007-06-10 19:45:56 +02004983 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4984 test->flags |= ACL_TEST_F_FETCH_MORE;
4985 test->flags |= ACL_TEST_F_VOL_HDR;
4986 test->len = ctx->vlen;
4987 test->ptr = (char *)ctx->line + ctx->val;
4988 return 1;
4989 }
4990
4991 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4992 test->flags |= ACL_TEST_F_VOL_HDR;
4993 return 0;
4994}
4995
Willy Tarreau33a7e692007-06-10 19:45:56 +02004996static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004997acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4998 struct acl_expr *expr, struct acl_test *test)
4999{
5000 struct http_txn *txn = l7;
5001
5002 if (txn->req.msg_state != HTTP_MSG_BODY)
5003 return 0;
5004 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5005 /* ensure the indexes are not affected */
5006 return 0;
5007
5008 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5009}
5010
5011static int
5012acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5013 struct acl_expr *expr, struct acl_test *test)
5014{
5015 struct http_txn *txn = l7;
5016
5017 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5018 return 0;
5019
5020 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5021}
5022
5023/* 6. Check on HTTP header count. The number of occurrences is returned.
5024 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5025 */
5026static int
5027acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005028 struct acl_expr *expr, struct acl_test *test)
5029{
5030 struct http_txn *txn = l7;
5031 struct hdr_idx *idx = &txn->hdr_idx;
5032 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005033 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005034
Willy Tarreau33a7e692007-06-10 19:45:56 +02005035 ctx.idx = 0;
5036 cnt = 0;
5037 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5038 cnt++;
5039
5040 test->i = cnt;
5041 test->flags = ACL_TEST_F_VOL_HDR;
5042 return 1;
5043}
5044
Willy Tarreauc11416f2007-06-17 16:58:38 +02005045static int
5046acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5047 struct acl_expr *expr, struct acl_test *test)
5048{
5049 struct http_txn *txn = l7;
5050
5051 if (txn->req.msg_state != HTTP_MSG_BODY)
5052 return 0;
5053 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5054 /* ensure the indexes are not affected */
5055 return 0;
5056
5057 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5058}
5059
5060static int
5061acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5062 struct acl_expr *expr, struct acl_test *test)
5063{
5064 struct http_txn *txn = l7;
5065
5066 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5067 return 0;
5068
5069 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5070}
5071
Willy Tarreau33a7e692007-06-10 19:45:56 +02005072/* 7. Check on HTTP header's integer value. The integer value is returned.
5073 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005074 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005075 */
5076static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005077acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005078 struct acl_expr *expr, struct acl_test *test)
5079{
5080 struct http_txn *txn = l7;
5081 struct hdr_idx *idx = &txn->hdr_idx;
5082 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005083
5084 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5085 /* search for header from the beginning */
5086 ctx->idx = 0;
5087
Willy Tarreau33a7e692007-06-10 19:45:56 +02005088 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5089 test->flags |= ACL_TEST_F_FETCH_MORE;
5090 test->flags |= ACL_TEST_F_VOL_HDR;
5091 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5092 return 1;
5093 }
5094
5095 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5096 test->flags |= ACL_TEST_F_VOL_HDR;
5097 return 0;
5098}
5099
Willy Tarreauc11416f2007-06-17 16:58:38 +02005100static int
5101acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5102 struct acl_expr *expr, struct acl_test *test)
5103{
5104 struct http_txn *txn = l7;
5105
5106 if (txn->req.msg_state != HTTP_MSG_BODY)
5107 return 0;
5108 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5109 /* ensure the indexes are not affected */
5110 return 0;
5111
5112 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5113}
5114
5115static int
5116acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5117 struct acl_expr *expr, struct acl_test *test)
5118{
5119 struct http_txn *txn = l7;
5120
5121 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5122 return 0;
5123
5124 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5125}
5126
Willy Tarreau737b0c12007-06-10 21:28:46 +02005127/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5128 * the first '/' after the possible hostname, and ends before the possible '?'.
5129 */
5130static int
5131acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5132 struct acl_expr *expr, struct acl_test *test)
5133{
5134 struct http_txn *txn = l7;
5135 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005136
Willy Tarreauc11416f2007-06-17 16:58:38 +02005137 if (txn->req.msg_state != HTTP_MSG_BODY)
5138 return 0;
5139 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5140 /* ensure the indexes are not affected */
5141 return 0;
5142
Willy Tarreau737b0c12007-06-10 21:28:46 +02005143 ptr = txn->req.sol + txn->req.sl.rq.u;
5144 end = ptr + txn->req.sl.rq.u_l;
5145
5146 if (ptr >= end)
5147 return 0;
5148
5149 /* RFC2616, par. 5.1.2 :
5150 * Request-URI = "*" | absuri | abspath | authority
5151 */
5152
5153 if (*ptr == '*')
5154 return 0;
5155
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005156 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005157 /* this is a scheme as described by RFC3986, par. 3.1 */
5158 ptr++;
5159 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005160 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005161 ptr++;
5162 /* skip '://' */
5163 if (ptr == end || *ptr++ != ':')
5164 return 0;
5165 if (ptr == end || *ptr++ != '/')
5166 return 0;
5167 if (ptr == end || *ptr++ != '/')
5168 return 0;
5169 }
5170 /* skip [user[:passwd]@]host[:[port]] */
5171
5172 while (ptr < end && *ptr != '/')
5173 ptr++;
5174
5175 if (ptr == end)
5176 return 0;
5177
5178 /* OK, we got the '/' ! */
5179 test->ptr = ptr;
5180
5181 while (ptr < end && *ptr != '?')
5182 ptr++;
5183
5184 test->len = ptr - test->ptr;
5185
5186 /* we do not need to set READ_ONLY because the data is in a buffer */
5187 test->flags = ACL_TEST_F_VOL_1ST;
5188 return 1;
5189}
5190
5191
Willy Tarreau8797c062007-05-07 00:55:35 +02005192
5193/************************************************************************/
5194/* All supported keywords must be declared here. */
5195/************************************************************************/
5196
5197/* Note: must not be declared <const> as its list will be overwritten */
5198static struct acl_kw_list acl_kws = {{ },{
5199 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5200 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5201 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005202 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005203
Willy Tarreauae8b7962007-06-09 23:10:04 +02005204 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5205 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5206 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5207 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5208 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5209 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5210 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
Willy Tarreau8797c062007-05-07 00:55:35 +02005211
Willy Tarreauc11416f2007-06-17 16:58:38 +02005212 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5213 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5214 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5215 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5216 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5217 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5218 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5219 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5220 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5221
5222 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5223 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5224 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5225 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5226 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5227 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5228 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5229 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5230 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005231
5232 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5233 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5234 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5235 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5236 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5237 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5238 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5239
Willy Tarreauf3d25982007-05-08 22:45:09 +02005240 { NULL, NULL, NULL, NULL },
5241
5242#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005243 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5244 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5245 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5246 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5247 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5248 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5249 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5250
Willy Tarreau8797c062007-05-07 00:55:35 +02005251 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5252 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5253 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5254 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5255 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5256 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5257 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5258 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5259
5260 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5261 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5262 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5263 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5264 { NULL, NULL, NULL, NULL },
5265#endif
5266}};
5267
5268
5269__attribute__((constructor))
5270static void __http_protocol_init(void)
5271{
5272 acl_register_keywords(&acl_kws);
5273}
5274
5275
Willy Tarreau58f10d72006-12-04 02:26:12 +01005276/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005277 * Local variables:
5278 * c-indent-level: 8
5279 * c-basic-offset: 8
5280 * End:
5281 */